Shoppay.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 支付接口
  7. */
  8. namespace app\ais\controller\api\v1;
  9. use app\ais\controller\api\Base;
  10. use app\ais\model\AisShopOrder;
  11. use app\ais\model\AisBank;
  12. use app\ais\model\AisShop;
  13. use app\ais\model\AisConfig;
  14. use app\ais\model\AisCouponUser;
  15. use app\ais\model\AisVipUser;
  16. use app\common\model\SystemUser;
  17. use app\common\facade\WechatPay;
  18. use app\common\model\SystemUserLevel;
  19. class Shoppay extends Base{
  20. /**
  21. * 初始化当前应用是否登录
  22. * @return void
  23. */
  24. public function initialize() {
  25. parent::initialize();
  26. $this->isUserAuth();
  27. }
  28. /**
  29. * 读取订单并支付
  30. * @param integer $id 商品ID
  31. * @return void
  32. */
  33. public function index(){
  34. $param['id'] = $this->request->param('id/d',0);
  35. $this->apiSign($param);
  36. //查找商品
  37. $item = AisShop::field('id,store_id,coupon_id,name,title,img,points_price,sell_price,vip_price,market_price,end_time')
  38. ->where(['is_sale'=>1,'id' => $param['id'],'member_miniapp_id' => $this->member_miniapp_id])->find();
  39. if(empty($item)){
  40. return enjson(403,'没有内容');
  41. }
  42. return enjson(200,AisShop::userPayPrice($item,$this->user->id));
  43. }
  44. /**
  45. * 下单发起支付或重新在支付
  46. */
  47. public function payment(){
  48. if (request()->isPost()) {
  49. $param['id'] = $this->request->param('id/d');
  50. $param['telphone'] = $this->request->param('telphone/s');
  51. $param['message'] = $this->request->param('message','');
  52. $param['ucode'] = $this->request->param('ucode','');
  53. $this->apiSign($param);
  54. $item = AisShop::where(['is_sale'=>1,'id' => $param['id'],'member_miniapp_id' => $this->member_miniapp_id])->find();
  55. if(empty($item)){
  56. return enjson(403,'商品已经下架');
  57. }
  58. if($item->warehouse_num <= 0){
  59. return enjson(403,'商品已售完');
  60. }
  61. $is_rest_payment = true;
  62. //读取订单
  63. $shop_order = AisShopOrder::where(['member_miniapp_id' => $this->member_miniapp_id,'uid' =>$this->user->id,'shop_id' => $item->id,'is_del' => 0])->find();
  64. $order_no = order_no($this->user->invite_code);
  65. if(empty($shop_order)){
  66. $validate = $this->validate($param,'Dopay.shop');
  67. if (true !== $validate) {
  68. return enjson(403,$validate);
  69. }
  70. $sell_price = $item->sell_price;
  71. }else{
  72. if($shop_order->paid_at == 1 && $shop_order->status == 0){
  73. return enjson(403,'本商品订单未核销,请先去商家核销');
  74. }
  75. if($shop_order->sell_price < $item->sell_price){
  76. $shop_order->is_del = 1; //设置删除
  77. return enjson(403,'订单已失效,请重新下单');
  78. }else{
  79. $shop_order->order_no = $order_no;
  80. }
  81. $shop_order->save();
  82. $sell_price = $shop_order->amount;
  83. $is_rest_payment = false;
  84. }
  85. //价格计算器
  86. $item = AisShop::userPayPrice($item,$this->user->id);
  87. //唤醒微信支付参数
  88. $payparm = [
  89. 'openid' => $this->user->miniapp_uid,
  90. 'miniapp_id' => $this->member_miniapp_id,
  91. 'name' => $item->name,
  92. 'order_no' => $order_no,
  93. 'attach' => 'N',
  94. 'total_fee' => $item->amount <= 0 ? 1 : $item->amount * 100,
  95. 'notify_url' => api(1, 'ais/notify/shop', $this->miniapp_id),
  96. ];
  97. if($this->member_miniapp->is_psp){
  98. if(empty($item->store->mch_id)){
  99. $setting = AisConfig::getConfig($this->miniapp_id);
  100. if (empty($setting->mchid)) {
  101. return enjson(403,'服务商模式下,必须配置默认商户号');
  102. }
  103. $payparm['mchid'] = $setting->mchid;
  104. }else{
  105. $level = SystemUserLevel::where(['user_id' => $this->user->id,'level' => 1])->find();
  106. if(!empty($level) && $level->parent_id != $item->store->manage_uid){
  107. $payparm['profit_sharing'] = 'Y';
  108. $payparm['attach'] = 'Y';
  109. }
  110. if(!empty($param['ucode'])){
  111. $payparm['profit_sharing'] = 'Y';
  112. $payparm['attach'] = 'Y';
  113. }
  114. $payparm['mchid'] = $item->store->mch_id;
  115. }
  116. }
  117. $ispay = WechatPay::orderPay($payparm);
  118. if ($ispay['code'] == 0) {
  119. return enjson(403,$ispay['msg']);
  120. }
  121. //判断是否创建订单
  122. if($is_rest_payment){
  123. $param['uid'] = $this->user->id;
  124. $param['member_miniapp_id'] = $this->member_miniapp_id;
  125. $param['store_id'] = $item->store_id;
  126. $param['sell_price'] = $item->sell_price;
  127. $param['amount'] = $item->amount;
  128. $param['points'] = $item->user_point;
  129. $param['thrifty'] = $item->thrifty;
  130. $param['share_uid'] = SystemUser::isInvite($param['ucode']);
  131. $param['shop_cache'] = $item->toArray();
  132. AisShopOrder::createOrder($param,$order_no);
  133. }
  134. return enjson(200,$ispay['data']);
  135. }
  136. }
  137. }