Storepay.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * 买单付款
  4. */
  5. namespace app\smartbc\controller\api\v1;
  6. use app\smartbc\controller\api\Base;
  7. use app\smartbc\model\SmartbcOrder;
  8. use app\smartbc\model\SmartbcStore;
  9. use app\smartbc\model\SmartbcCoupon;
  10. use app\smartbc\model\SmartbcCouponUser;
  11. use app\smartbc\model\SmartbcConfig;
  12. use app\common\facade\WechatPay;
  13. class Storepay extends Base{
  14. protected $config;
  15. /**
  16. * 初始化当前应用管理员是不是联盟城市账户
  17. * @return void
  18. */
  19. public function initialize() {
  20. parent::initialize();
  21. $this->isUserAuth();
  22. $this->config = SmartbcConfig::getConfig($this->member_miniapp_id);
  23. }
  24. /**
  25. * 读取买单商家信息和优惠券
  26. * @return void
  27. */
  28. public function index(){
  29. $param['store_id'] = $this->request->param('store_id/d');
  30. $param['chain_id'] = $this->request->param('chain_id/d');
  31. $rel = $this->apiSign($param);
  32. if ($rel['code'] != 200) {
  33. return enjson($rel['code'],'签名验证失败');
  34. }
  35. if(empty($param['store_id'])){
  36. return enjson(404,'为找到付款好店');
  37. }
  38. $store = SmartbcStore::where(['is_lock' => 0,'id' => $param['store_id']])->field('id,name')->find();
  39. if(empty($store)){
  40. return enjson(303,'未找到付款好店',['url' =>'/pages/index']);
  41. }
  42. if(!empty($param['chain_id'])){
  43. $store = $store->chain()->where('id',$param['chain_id'])->field('id,title,store_id')->find();
  44. if(empty($store)){
  45. return enjson(303,'未找到付款分店',['url' =>'/pages/index']);
  46. }
  47. $store->name = $store->title;
  48. $store->chain_id = $store->id;
  49. $store->id = $store->store_id;
  50. }
  51. $is_get_coupon = 0;
  52. if(empty($this->config->is_pay_open_coupon)){
  53. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  54. $condition[] = ['uid','=',$this->user->id];
  55. $condition[] = ['is_end','=',0];
  56. $condition[] = ['money','=',0];
  57. $condition[] = ['store_id','=',$param['store_id']];
  58. $coupon = SmartbcCouponUser::where($condition)->field('id,name,tips,howmuch,store_id,price,types,coupon_id,discount,create_time')->order('create_time desc')->select();
  59. foreach ($coupon as $key => $value){
  60. $coupon[$key]->is_get_coupon = 0;
  61. $coupon[$key]->end_time = $this->config->end_time * 60 * 60;
  62. }
  63. }else{
  64. $coupon = SmartbcCoupon::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $param['store_id'],'is_lock' => 0,'is_end' => 0])->order('is_top desc,sort desc,create_time desc')->select();
  65. foreach ($coupon as $key => $info){
  66. $coupon[$key]->end_time = $info->create_time + $this->config->end_time * 60 * 60;
  67. $coupon[$key]->is_get_coupon = 0;
  68. }
  69. $is_get_coupon = 1;
  70. }
  71. return enjson(200,['coupon' => $coupon ,'store' => $store,'is_get_coupon' => $is_get_coupon]);
  72. }
  73. /**
  74. * 微信给商家支付
  75. * @param string $no
  76. * @return void
  77. */
  78. public function wechat(){
  79. if (request()->isPost()) {
  80. $rule = [
  81. 'member_miniapp_id' => $this->miniapp_id,
  82. 'store_id' => $this->request->param('store_id/d'),
  83. 'chain_id' => $this->request->param('chain_id/d',0),
  84. 'user_couponr_id' => $this->request->param('user_couponr_id/d',0),
  85. 'money' => $this->request->param('money/f'),
  86. 'amount' => $this->request->param('amount/f'),
  87. 'uid' => $this->user->id,
  88. ];
  89. $validate = $this->validate($rule,'Dopay.gopay');
  90. if (true !== $validate) {
  91. return enjson(204,$validate);
  92. }
  93. //判断好店
  94. $store = SmartbcStore::where(['member_miniapp_id' => $this->miniapp_id,'id' => $rule['store_id'],'is_lock' => 0])->find();
  95. if(empty($store)){
  96. return enjson(204,'未找到商户');
  97. }
  98. $store->chain_id = 0;
  99. if(!empty($rule['chain_id'])){
  100. $chain = $store->chain()->where('id',$rule['chain_id'])->field('id,title,store_id')->find();
  101. if(empty($chain)){
  102. return enjson(204,'未找到付款分店');
  103. }
  104. $store->title = $chain->title;
  105. $store->chain_id = $chain->id;
  106. }
  107. if(empty($store->manage_uid)){
  108. return enjson(204,'未设置老板');
  109. }
  110. if(empty($store->mch_id)){
  111. return enjson(204,'未找到商家微信商户号');
  112. }
  113. //计算付款金额
  114. $amount = SmartbcOrder::countPrice($rule);
  115. if(!$amount){
  116. return enjson(204,'优惠券不满足使用条件');
  117. }
  118. //创建订单
  119. $order_no = $this->user->invite_code.order_no();
  120. //唤醒微信支付参数
  121. $payparm = [
  122. 'openid' => $this->user->miniapp_uid,
  123. 'miniapp_id' => $this->miniapp_id,
  124. 'name' => $store->name,
  125. 'order_no' => $order_no,
  126. 'total_fee' => $amount['price'] * 100,
  127. 'notify_url' => api(1,'smartbc/notify/storepay',$this->miniapp_id),
  128. ];
  129. $payparm['mchid'] = $store->mch_id;
  130. if(!empty($amount['coupon'])){
  131. if($amount['coupon']->parent_store_id > 0){
  132. $rule['parent_store_id'] = $amount['coupon']->parent_store_id; //引荐的店铺
  133. $payparm['profit_sharing'] = 'Y';
  134. }
  135. $rule['coupon_cache'] = $amount['coupon']; //优惠券
  136. }
  137. $ispay = WechatPay::orderPay($payparm);
  138. if($ispay['code'] == 0){
  139. return enjson(204,$ispay['msg']);
  140. }
  141. //添加订单记录
  142. $rule['order_no'] = $order_no;
  143. $rule['price'] = $amount['price']; //实际金额
  144. $rule['store_chain_id'] = $store->chain_id; //分店ID
  145. $rel = SmartbcOrder::addOrder($rule);
  146. if(!$rel){
  147. return enjson(204,'订单创建失败');
  148. }
  149. return enjson(200,'成功',$ispay['data']);
  150. }
  151. }
  152. }