Payment.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\allwin\controller\api\v4;
  9. use app\allwin\controller\api\Base;
  10. use app\allwin\model\AllwinStore;
  11. use app\allwin\model\AllwinStoreChain;
  12. use app\allwin\model\Coupon;
  13. use app\allwin\model\CouponUser;
  14. use app\allwin\model\Order;
  15. use app\allwin\model\MchId;
  16. use app\allwin\model\AllwinConfig;
  17. use app\common\facade\WechatPay;
  18. use app\common\model\SystemMemberForm;
  19. class Payment 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
  31. * @return json
  32. */
  33. public function index(){
  34. $param['store_id'] = $this->request->param('store_id/d',0);
  35. $param['code_id'] = $this->request->param('code_id/s',0);
  36. $param['signkey'] = $this->request->param('signkey');
  37. $param['sign'] = $this->request->param('sign');
  38. $rel = $this->apiSign($param);
  39. if($rel['code'] != 200){
  40. return enjson($rel['code'],'签名验证失败');
  41. }
  42. if($param['code_id']){
  43. $where['code_id'] = $param['code_id'];
  44. }else{
  45. $where['id'] = $param['store_id'];
  46. }
  47. $where['member_miniapp_id'] = $this->miniapp_id;
  48. $where['is_lock'] = 0;
  49. $store = AllwinStore::where($where)->field('id,name,tips,quan_id')->find();
  50. if(empty($store)){
  51. if($param['code_id']){
  52. $chain_where['member_miniapp_id'] = $this->miniapp_id;
  53. $chain_where['code_id'] = $param['code_id'];
  54. $chain = AllwinStoreChain::with(['store' => function($query) {
  55. $query->field('id,name,tips,quan_id');
  56. }])->where($chain_where)->find();
  57. if(empty($chain->store)){
  58. return enjson(301,'未找到付款好店1',['url' =>'/pages/index']);
  59. }
  60. $store = $chain->store;
  61. }else{
  62. return enjson(301,'未找到付款好店',['url' =>'/pages/index']);
  63. }
  64. }
  65. return enjson(200,'成功',$store);
  66. }
  67. /**
  68. * 用户商家已领取的所有有效优惠券
  69. * @param integer $store_id
  70. * @param [type] $price
  71. * @return void
  72. */
  73. public function userCoupon(int $store_id = 0,$price){
  74. $param['store_id'] = $this->request->param('store_id/d',0);
  75. $param['price'] = $this->request->param('price/f',0);
  76. $param['signkey'] = $this->request->param('signkey');
  77. $param['sign'] = $this->request->param('sign');
  78. $rel = $this->apiSign($param);
  79. if($rel['code'] != 200){
  80. return enjson($rel['code'],'签名验证失败');
  81. }
  82. //读取用户优惠券
  83. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  84. $condition[] = ['is_end','=',0]; //结束
  85. $condition[] = ['user_size','>',0]; //大小
  86. $condition[] = ['store_id','=',$store_id]; //好店
  87. $condition[] = ['uid','=',$this->user->id]; //用户
  88. if($param['price'] > 0){
  89. $condition[] = ['howmuch','<=',$param['price']]; //输入金额
  90. }
  91. $coupon = CouponUser::where($condition)->field('id,name,size,user_size,tips,types,price,discount,weekday,create_time,ontypes')->select();
  92. if($coupon->isEmpty()){
  93. return enjson(204);
  94. }
  95. //判断是否可用
  96. foreach ($coupon as $val){
  97. if(strpos($val['weekday'],date('w')) && (($val['ontypes'] == 1 && strtotime(date('Y-m-d',$val['create_time']) . ' 23:59:59') < strtotime("now")) || $val['ontypes'] == 0)){
  98. $val['state'] = 0;
  99. }else{
  100. $val['state'] = 1;
  101. }
  102. }
  103. return enjson(200,'成功',$coupon);
  104. }
  105. /**
  106. * 读取管理商家和买单的优惠券
  107. **/
  108. public function Coupon(){
  109. $param['store_id'] = $this->request->param('store_id/d',0);
  110. $param['quan_id'] = $this->request->param('quan_id/d',0);
  111. $param['price'] = $this->request->param('price/f',0);
  112. $param['page'] = $this->request->param('page/d',1);
  113. $param['signkey'] = $this->request->param('signkey');
  114. $param['sign'] = $this->request->param('sign');
  115. $rel = $this->apiSign($param);
  116. if($rel['code'] != 200){
  117. return enjson($rel['code'],'签名验证失败');
  118. }
  119. $condition = [];
  120. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  121. $condition[] = ['is_lock','=',0];
  122. $condition[] = ['is_end','=',0];
  123. $condition[] = ['is_check','=',1];
  124. $condition[] = ['num','>',0];
  125. $condition[] = ['endtime','>',time()];
  126. if($param['price'] > 0){
  127. $condition[] = ['howmuch','<=',$param['price']]; //输入金额
  128. }
  129. //用户的优惠券
  130. $ids = CouponUser::userCouponIds($this->user->id);
  131. if (!empty($ids)) {
  132. $condition[] = ['id','notin',$ids];
  133. }
  134. //读取自己好店优惠券
  135. $store_coupon = [];
  136. if($param['store_id'] > 0 && $param['page'] == 1){
  137. $storeCoupon = Coupon::where($condition)->where('store_id','=',$param['store_id'])->field('id,name,size,tips,types,price,pay_price,discount,theme')->order('size desc,id desc')->select();
  138. if(!$storeCoupon->isEmpty()) {
  139. $store_coupon = $storeCoupon->toArray();
  140. }
  141. }
  142. $coupon = [];
  143. $condition[] = ['store_id','<>',$param['store_id']];
  144. if($param['quan_id']){
  145. $store = AllwinStore::where(['quan_id' => $param['quan_id']])->column('id');
  146. if(!empty($store)){
  147. $condition[] = ['store_id','in',$store];
  148. }
  149. }
  150. $rel = Coupon::where($condition)->field('id,name,size,tips,types,price,pay_price,discount,theme')->order('is_top desc,sort desc,size desc')->paginate(6,true);
  151. if (!$rel->isEmpty()) {
  152. $coupon = $rel->toArray()['data'];
  153. }
  154. if(!empty($store_coupon)){
  155. $coupon = array_merge_recursive($store_coupon,$coupon);
  156. }
  157. if(empty($coupon)){
  158. return enjson(204);
  159. }
  160. return enjson(200,'成功',$coupon);
  161. }
  162. /**
  163. * 微信给商家支付
  164. * @param string $no
  165. * @return void
  166. */
  167. public function wechat(){
  168. if (request()->isPost()) {
  169. $rule = [
  170. 'member_miniapp_id' => $this->miniapp_id,
  171. 'store_id' => $this->request->param('store_id/d'),
  172. 'user_couponr_id' => $this->request->param('user_couponr_id/d',0),
  173. 'coupon_ids' => $this->request->param('coupon_ids/a'),
  174. 'money' => $this->request->param('money/f'),
  175. 'amount' => $this->request->param('amount/f'),
  176. 'code_id' => $this->request->param('code_id/d'),
  177. 'ucode' => $this->request->param('ucode/s'),
  178. 'uid' => $this->user->id,
  179. ];
  180. $validate = $this->validate($rule, 'Dopay.gopay');
  181. if (true !== $validate) {
  182. return json(['code'=>403,'msg'=>$validate]);
  183. }
  184. //判断好店
  185. $store = AllwinStore::where(['member_miniapp_id' => $this->miniapp_id,'id' => $rule['store_id'],'is_lock' => 0])->field('id,name,state,manage_uid,mch_id')->find();
  186. if(empty($store)){
  187. return json(['code'=>403,'msg'=>'未找到好店']);
  188. }
  189. if(empty($store->manage_uid)){
  190. return json(['code'=>403,'msg'=>'未设置老板']);
  191. }
  192. if($store->state == 0){
  193. return json(['code'=>403,'msg'=>'好店未营业']);
  194. }
  195. //计算付款金额
  196. $amount = Order::countPrice($rule);
  197. if($amount['code'] == 403){
  198. return json($amount);
  199. }
  200. $amount = $amount['data'];
  201. //创建订单
  202. $order_no = $this->user->invite_code.order_no();
  203. //唤醒微信支付参数
  204. $payparm = [
  205. 'openid' => $this->user->miniapp_uid,
  206. 'miniapp_id' => $this->miniapp_id,
  207. 'name' => $store->name,
  208. 'order_no' => $order_no,
  209. 'total_fee' => $amount['order_amount']*100,
  210. 'notify_url' => api(4,'allwin/paymentnotify/index',$this->miniapp_id),
  211. ];
  212. //读取配置
  213. $setting = AllwinConfig::getConfig($this->miniapp_id);
  214. if($setting->is_psp == 1){ //服务商模式
  215. if($store->mch_id){
  216. $default_mchid = MchId::getMch($store->mch_id); //商户的商户号
  217. }else{
  218. $default_mchid = MchId::getMch(0, $this->miniapp_id); //默认收款账户
  219. }
  220. if (empty($default_mchid)) {
  221. return json(['code'=>403,'msg'=>'未找到商户号']);
  222. }
  223. $payparm['mchid'] = $default_mchid->mchid;
  224. //分账参数
  225. switch ($setting->is_fees_types){
  226. case 1:
  227. if($rule['user_couponr_id']){
  228. $payparm['profit_sharing'] = $setting->is_wechat_profitsharing == 1 || $payparm['profit_sharing'] ? 'Y' : 'N';
  229. }else{
  230. $payparm['profit_sharing'] = 'N';
  231. }
  232. break;
  233. case 2:
  234. $payparm['profit_sharing'] = 'N';
  235. break;
  236. default:
  237. $payparm['profit_sharing'] = $setting->is_wechat_profitsharing == 1 ? 'Y' : 'N';
  238. break;
  239. }
  240. }
  241. $ispay = WechatPay::orderPay($payparm);
  242. if($ispay['code'] == 0){
  243. return json(['code'=>403,'msg'=>$ispay['msg']]);
  244. }
  245. //添加订单记录
  246. $rule['order_no'] = $order_no;
  247. $rule['coupon_price'] = $amount['coupon_user_price']; //优惠金额
  248. $rule['store_amount'] = $amount['store_amount']; //好店应收
  249. $rule['platform_amount'] = $amount['platform_amount']; //领优惠券金额
  250. $rule['order_amount'] = $amount['order_amount']; //实支付金额
  251. $rule['user_amount'] = $amount['user_amount']; //输入金额
  252. $rel = Order::addOrder($rule);
  253. if(!$rel){
  254. return json(['code'=>403,'msg'=>'订单创建失败']);
  255. }
  256. //把支付packageID添加到数据库
  257. $package = $ispay['data']['package'];
  258. SystemMemberForm::addForm($this->miniapp_id,$this->user->id,substr($package,10));
  259. $ispay['data']['url'] = '/pages/store/views?store_id='.$rule['store_id'];
  260. return enjson(200,'成功',$ispay['data']);
  261. }
  262. }
  263. }