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.

112 lines
3.6 KiB

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\OrderMain;
  5. use App\Request\WxminiPayRequest;
  6. use EasyWeChat\Factory;
  7. use Hyperf\Guzzle\CoroutineHandler;
  8. class PaymentController extends BaseController
  9. {
  10. public function wxminiPayOnline(WxminiPayRequest $request){
  11. $data = $request->validated();
  12. $config = config('wxpay');
  13. $app = Factory::payment($config);
  14. $app['guzzle_handler'] = CoroutineHandler::class;
  15. // 待支付的,未超时(15min,900sec)的订单
  16. $orderMain = OrderMain::query()
  17. ->where(['state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']])
  18. ->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
  19. ->first();
  20. if (empty($orderMain)) {
  21. return $this->result(ErrorCode::PAY_FAILURE, $data,'订单不存在或已失效');
  22. }
  23. $result = $app->order->unify([
  24. 'body' => '懒族生活 - 外卖下单',
  25. 'out_trade_no' => $orderMain->global_order_id,
  26. 'total_fee' => bcmul(floatval($orderMain->money), 100, 0),
  27. 'notify_url' => config('site_host') . '/v1/notify/wxminiOnline',
  28. 'trade_type' => 'JSAPI',
  29. 'openid' => $data['openid'],
  30. ]);
  31. $parameters = [
  32. 'appId' => $result['appid'],
  33. 'timeStamp' => '' . time() . '',
  34. 'nonceStr' => uniqid(),
  35. 'package' => 'prepay_id=' . $result['prepay_id'],
  36. 'signType' => 'MD5'
  37. ];
  38. //签名步骤一:按字典序排序参数
  39. ksort($parameters);
  40. $string = $this->formatBizQueryParaMap($parameters, false);
  41. //签名步骤二:在string后加入KEY
  42. $string = $string . "&key=" . $config['key'];
  43. //签名步骤三:MD5加密
  44. $string = md5($string);
  45. //签名步骤四:所有字符转为大写
  46. $parameters['paySign'] = strtoupper($string);
  47. return $this->success($parameters);
  48. }
  49. ///作用:格式化参数,签名过程需要使用
  50. private function formatBizQueryParaMap($paraMap, $urlencode) {
  51. $buff = "";
  52. ksort($paraMap);
  53. foreach ($paraMap as $k => $v) {
  54. if ($urlencode) {
  55. $v = urlencode($v);
  56. }
  57. $buff .= $k . "=" . $v . "&";
  58. }
  59. $reqPar = null;
  60. if (strlen($buff) > 0) {
  61. $reqPar = substr($buff, 0, strlen($buff) - 1);
  62. }
  63. return $reqPar;
  64. }
  65. public function wxminiPayOffline(WxminiPayRequest $request){
  66. $data = $request->validated();
  67. $config = config('wxpay');
  68. $app = Factory::payment($config);
  69. $app['guzzle_handler'] = CoroutineHandler::class;
  70. // 待支付的,未超时(15min,900sec)的订单
  71. $orderMain = OrderMain::query()
  72. ->where(['dm_state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']])
  73. ->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
  74. ->first();
  75. if (empty($orderMain)) {
  76. return $this->result(ErrorCode::PAY_FAILURE, $data,'订单不存在或已失效');
  77. }
  78. $result = $app->order->unify([
  79. 'body' => '懒族生活 - 当面支付',
  80. 'out_trade_no' => $orderMain->global_order_id,
  81. 'total_fee' => bcmul(floatval($orderMain->money), 100, 0),
  82. 'notify_url' => config('site_host') . '/v1/notify/wxminiOffline',
  83. 'trade_type' => 'JSAPI',
  84. 'openid' => $data['openid'],
  85. ]);
  86. $result['sign_type'] = 'MD5';
  87. $result['timestamp'] = time();
  88. return $this->success($result);
  89. }
  90. }