You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\Controller;
  3. use App\Constants\ErrorCode;
  4. use App\Model\Order;
  5. use App\Model\OrderGoods;
  6. use App\Model\OrderMain;
  7. use App\Request\WxminiPayRequest;
  8. use Hyperf\Di\Annotation\Inject;
  9. use App\Service\PurchaseLimitServiceInterface;
  10. use EasyWeChat\Factory;
  11. use Hyperf\DbConnection\Db;
  12. use Hyperf\Guzzle\CoroutineHandler;
  13. class PaymentController extends BaseController
  14. {
  15. /**
  16. * @Inject
  17. * @var PurchaseLimitServiceInterface
  18. */
  19. protected $purchaseLimitService;
  20. public function wxminiPayOnline(WxminiPayRequest $request){
  21. $data = $request->validated();
  22. $config = config('wxpay');
  23. $app = Factory::payment($config);
  24. $app['guzzle_handler'] = CoroutineHandler::class;
  25. // 待支付的,未超时(15min,900sec)的订单
  26. $orderMain = OrderMain::query()
  27. ->where(['state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']])
  28. ->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
  29. ->first();
  30. if (empty($orderMain)) {
  31. return $this->result(ErrorCode::PAY_FAILURE, $data,'订单不存在或已失效');
  32. }
  33. //查询订单商品信息
  34. $order = Order::query()
  35. ->where('order_main_id',$data['order_id'])
  36. ->select('id')
  37. ->get()
  38. ->toArray();
  39. $orderGoods = OrderGoods::query()
  40. ->whereIn('order_id',$order)
  41. ->get();
  42. //判断是否有购买多个特价商品
  43. $result = $this->purchaseLimitService->PurchaseLimit($orderGoods);
  44. if(!$result){
  45. Db::rollBack();
  46. return '同一个订单不能购买多个特价商品';
  47. }
  48. $result = $app->order->unify([
  49. 'body' => '懒族生活 - 外卖下单',
  50. 'out_trade_no' => $orderMain->global_order_id,
  51. // 'total_fee' => bcmul(floatval($orderMain->money), 100, 0),
  52. 'total_fee' => 1,
  53. 'notify_url' => config('site_host') . '/wechat/notify/wxminionline',
  54. 'trade_type' => 'JSAPI',
  55. 'openid' => $data['openid'],
  56. ]);
  57. // 返回支付参数给前端
  58. $parameters = [
  59. 'appId' => $result['appid'],
  60. 'timeStamp' => '' . time() . '',
  61. 'nonceStr' => uniqid(),
  62. 'package' => 'prepay_id=' . $result['prepay_id'],
  63. 'signType' => 'MD5'
  64. ];
  65. $parameters['paySign'] = $this->signture($parameters, $config['key']);
  66. return $this->success($parameters);
  67. }
  68. public function wxminiPayOffline(WxminiPayRequest $request){
  69. $data = $request->validated();
  70. $config = config('wxpay');
  71. $app = Factory::payment($config);
  72. $app['guzzle_handler'] = CoroutineHandler::class;
  73. // 待支付的,未超时(15min,900sec)的订单
  74. $orderMain = OrderMain::query()
  75. ->where(['dm_state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']])
  76. ->first();
  77. if (empty($orderMain)) {
  78. return $this->result(ErrorCode::PAY_FAILURE, $data,'订单不存在或已失效');
  79. }
  80. $result = $app->order->unify([
  81. 'body' => '懒族生活 - 当面支付',
  82. 'out_trade_no' => $orderMain->global_order_id,
  83. 'total_fee' => bcmul(floatval($orderMain->money), 100, 0),
  84. 'notify_url' => config('site_host') . '/wechat/notify/wxminioffline',
  85. 'trade_type' => 'JSAPI',
  86. 'openid' => $data['openid'],
  87. ]);
  88. // 返回支付参数给前端
  89. $parameters = [
  90. 'appId' => $result['appid'],
  91. 'timeStamp' => '' . time() . '',
  92. 'nonceStr' => uniqid(),
  93. 'package' => 'prepay_id=' . $result['prepay_id'],
  94. 'signType' => 'MD5'
  95. ];
  96. $parameters['paySign'] = $this->signture($parameters, $config['key']);
  97. return $this->success($parameters);
  98. }
  99. /**
  100. * 支付参数加签
  101. * @param $parameters
  102. * @param $key
  103. * @return string
  104. */
  105. private function signture($parameters, $key)
  106. {
  107. // 按字典序排序参数
  108. ksort($parameters);
  109. // http_query
  110. $queryParams = $this->http_query($parameters);
  111. // 加入KEY
  112. $queryParams = $queryParams . "&key=" . $key;
  113. // MD5加密
  114. $queryParams = md5($queryParams);
  115. // 字符转为大写
  116. return strtoupper($queryParams);
  117. }
  118. /**
  119. * 参数转为http query字串
  120. * @param $parameters
  121. * @return string
  122. */
  123. private function http_query($parameters) {
  124. $http_query = [];
  125. foreach ($parameters as $key => $value) {
  126. $http_query[] = $key.'='.$value;
  127. }
  128. return implode('&', $http_query);
  129. }
  130. }