Shopdopay.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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\AllwinShopOrder;
  11. use app\allwin\model\Bank;
  12. use app\allwin\model\AllwinShop;
  13. use app\allwin\model\AllwinConfig;
  14. use app\allwin\model\CouponUser;
  15. use app\allwin\model\MchId;
  16. use app\allwin\model\Vip;
  17. use app\common\model\SystemMemberForm;
  18. use app\common\model\SystemUser;
  19. use app\common\facade\WechatPay;
  20. class Shopdopay extends Base{
  21. /**
  22. * 初始化当前应用是否登录
  23. * @return void
  24. */
  25. public function initialize() {
  26. parent::initialize();
  27. $this->isUserAuth();
  28. }
  29. /**
  30. * 购买商品
  31. */
  32. public function index(){
  33. if (request()->isPost()) {
  34. $param['shop_id'] = $this->request->param('shop_id/d');
  35. $param['telphone'] = $this->request->param('telphone/s','');
  36. $param['message'] = $this->request->param('message','');
  37. $param['ucode'] = $this->request->param('ucode','');
  38. $param['signkey'] = $this->request->param('signkey');
  39. $param['sign'] = $this->request->param('sign');
  40. $sign = $this->apiSign($param);
  41. if($sign['code'] != 200){
  42. return enjson($sign['code'],'签名验证失败');
  43. }
  44. //判断是否已下架
  45. $item = AllwinShop::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['shop_id'],'is_sale' => 1,'is_del' => 0])->field('store_id,name,title,warehouse_num,points,vip_price,market_price,sell_price,coupon_id,img,notice')->find();
  46. if(empty($item)){
  47. return enjson(403,'商品已经下架');
  48. }
  49. if($item->warehouse_num <= 0){
  50. return enjson(403,'商品已售完');
  51. }
  52. //读取订单
  53. $shop_order = AllwinShopOrder::where(['member_miniapp_id' => $this->miniapp_id,'user_id' =>$this->user->id,'shop_id' => $param['shop_id'],'is_del' => 0])->field('paid_at,status,order_no,amount,sell_price')->find();
  54. $order_no = $this->user->invite_code.order_no();
  55. if(empty($shop_order)){
  56. $validate = $this->validate($param,'Dopay.goods');
  57. if (true !== $validate) {
  58. return enjson(403,$validate);
  59. }
  60. $sell_price = $item->sell_price;
  61. $is_new_order = true;
  62. }else{
  63. if($shop_order->paid_at == 1 && $shop_order->status == 0){
  64. return enjson(403,'订单未核销,请先去商家核销');
  65. }
  66. if($shop_order->sell_price < $item->sell_price){
  67. $shop_order->is_del = 1;
  68. return enjson(403,'订单已失效,请重新下单');
  69. }else{
  70. $shop_order->order_no = $order_no;
  71. }
  72. $shop_order->save();
  73. $sell_price = $shop_order->amount;
  74. $is_new_order = false;
  75. }
  76. //判断扣除积分
  77. $points = 0; //扣除积分
  78. $points_price = 0; //扣除金额
  79. if($item->points){
  80. $bank = Bank::where(['member_miniapp_id' => $this->miniapp_id,'user_id' => $this->user->id])->field('shop_money')->find();
  81. if(!empty($bank->shop_money)){
  82. if($bank->shop_money >= $item->points){
  83. $points_price = money($item->points/100);
  84. $points = $item->points;
  85. }else{
  86. $points_price = money($bank->shop_money/100);
  87. $points = $bank->shop_money;
  88. }
  89. }
  90. }
  91. //判断是否VIP
  92. $vip_price = 0;
  93. $vip = Vip::where(['user_id' => $this->user->id,'state' => 1,'is_lock' => 0])->count();
  94. if($vip && $item['vip_price']){
  95. $vip_price = $item->vip_price;
  96. }
  97. //优惠券
  98. $coupon_price = 0;
  99. if($item->coupon_id){
  100. $condition[] = ['uid','=',$this->user->id];
  101. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  102. $condition[] = ['is_end','=',0];
  103. $condition[] = ['endtime','>=',time()];
  104. $condition[] = ['coupon_id','=',$item->coupon_id];
  105. $coupon = CouponUser::where($condition)->order('id desc')->find();
  106. if($coupon){
  107. $coupon_price = empty($item->coupon) ? 0 : AllwinShop::calculatePrice($item->sell_price,$coupon);
  108. }
  109. }
  110. $amount = money($sell_price-$vip_price-$points_price-$coupon_price);
  111. $thrifty = money($item->market_price - $amount); //节省多少钱
  112. //唤醒微信支付参数
  113. $payparm = [
  114. 'openid' => $this->user->miniapp_uid,
  115. 'miniapp_id' => $this->miniapp_id,
  116. 'name' => $item->name,
  117. 'order_no' => $order_no,
  118. 'total_fee' => $amount <= 0 ? 1 : $amount*100,
  119. 'notify_url' => api(4,'allwin/shopnotify/index',$this->miniapp_id),
  120. ];
  121. //读取配置
  122. $setting = AllwinConfig::getConfig($this->miniapp_id);
  123. if($setting->is_psp == 1){
  124. if(empty($item->store->mch_id)){
  125. $default_mchid = MchId::getMch(0, $this->miniapp_id); //默认收款账户
  126. }else{
  127. $default_mchid = MchId::getMch($item->store->mch_id); //商户的商户号
  128. $payparm['profit_sharing'] = $setting->is_wechat_profitsharing == 1 ? 'Y' : 'N';
  129. }
  130. if(empty($default_mchid)) {
  131. return enjson(403,'未找到商户号');
  132. }
  133. $payparm['mchid'] = $default_mchid->mchid;
  134. }
  135. $ispay = WechatPay::orderPay($payparm);
  136. if ($ispay['code'] == 0) {
  137. return enjson(403,$ispay['msg']);
  138. }
  139. //判断是否新订单
  140. $rel = true;
  141. if($is_new_order){
  142. $param['user_id'] = $this->user->id;
  143. $param['member_miniapp_id'] = $this->miniapp_id;
  144. $param['store_id'] = $item->store_id;
  145. $param['share_uid'] = SystemUser::isInvite($param['ucode']);
  146. $param['sell_price'] = $item->sell_price;
  147. $param['amount'] = $amount;
  148. $param['thrifty'] = $thrifty;
  149. $param['points'] = $points;
  150. $param['shop_cache'] = $item->toJson();
  151. $rel = AllwinShopOrder::insertOrder($param,$order_no);
  152. }
  153. if(empty($rel)){
  154. return enjson(204,'购买商品失败');
  155. }
  156. return enjson(200,'成功',$ispay['data']);
  157. }
  158. }
  159. /**
  160. * 读取单个商品信息
  161. * @param integer $id 商品ID
  162. * @return void
  163. */
  164. public function cart(){
  165. $param['id'] = $this->request->param('id/d',0);
  166. $param['signkey'] = $this->request->param('signkey');
  167. $param['sign'] = $this->request->param('sign');
  168. $rel = $this->apiSign($param);
  169. if($rel['code'] != 200){
  170. return enjson($rel['code'],'签名验证失败');
  171. }
  172. //查找商品SPU
  173. $item = AllwinShop::where(['is_sale'=>1,'id' => $param['id'],'member_miniapp_id' => $this->miniapp_id])->field('id,store_id,coupon_id,name,title,img,points,sell_price,market_price,end_time,vip_price')->find();
  174. if(empty($item)){
  175. return json(['code'=>403,'msg'=>'没有内容']);
  176. }
  177. $item['market_price'] = money($item['market_price']);
  178. $item['sell_price'] = money($item['sell_price']);
  179. $item['img'] = $item['img'].'?x-oss-process=style/500';
  180. $item['coupon_id'] = $item->coupon_id;
  181. $item['coupon_price'] = 0;
  182. $item['coupon_user_price'] = 0;
  183. if($item->coupon_id){
  184. $item['coupon_price'] = money(empty($item->coupon) ? 0 : AllwinShop::calculatePrice($item->sell_price,$item->coupon));
  185. //用户已领取赠品
  186. $condition[] = ['uid','=',$this->user->id];
  187. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  188. $condition[] = ['is_end','=',0];
  189. $condition[] = ['endtime','>=',time()];
  190. $condition[] = ['coupon_id','=',$item->coupon_id];
  191. $coupon = CouponUser::where($condition)->order('id desc')->find();
  192. if($coupon){
  193. $item['coupon_user_price'] = money(AllwinShop::calculatePrice($item->sell_price,$item->coupon));
  194. }
  195. }
  196. $item['telphone'] = $this->user->phone_uid;
  197. //判断是否会员
  198. $vip = Vip::where(['user_id' => $this->user->id,'state' => 1,'is_lock' => 0])->count();
  199. $item['is_vip'] = $vip ? 1 : 0;
  200. $vip_price = $vip ? $item['vip_price'] : 0; //判断会员是否优惠
  201. //判断积分
  202. $item['points_price'] = money($item->points/100);
  203. $item['bank_points_price'] = 0;
  204. $item['bank_points'] = 0;
  205. if($item->points > 0){
  206. $bank = Bank::where(['user_id' => $this->user->id,'member_miniapp_id' => $this->miniapp_id])->field('shop_money')->find();
  207. if(empty($bank->shop_money)){
  208. $item['shop_money'] = 0;
  209. }else{
  210. if($bank->shop_money >= $item->points){
  211. $item['bank_points_price'] = money($item->points/100);
  212. $item['bank_points'] = $item->points;
  213. }else{
  214. $item['bank_points_price'] = money($bank->shop_money/100);
  215. $item['bank_points'] = $bank->shop_money;
  216. }
  217. $item['shop_money'] = $bank->shop_money;
  218. }
  219. }
  220. $item['amount'] = money($item['sell_price'] - $vip_price - $item['bank_points_price'] - $item['coupon_user_price']);
  221. $thrifty = money($item['market_price'] - $item['amount']);
  222. $item['thrifty'] = $thrifty > 0 ? $thrifty : 0;
  223. if(empty($item)){
  224. return enjson(403,'没有内容');
  225. }else{
  226. return enjson(200,'成功',$item);
  227. }
  228. }
  229. }