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.

162 lines
5.0 KiB

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