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.

80 lines
2.5 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. $result['sign_type'] = 'MD5';
  32. $result['timestamp'] = time();
  33. return $this->success($result);
  34. }
  35. public function wxminiPayOffline(WxminiPayRequest $request){
  36. $data = $request->validated();
  37. $config = config('wxpay');
  38. $app = Factory::payment($config);
  39. $app['guzzle_handler'] = CoroutineHandler::class;
  40. // 待支付的,未超时(15min,900sec)的订单
  41. $orderMain = OrderMain::query()
  42. ->where(['dm_state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']])
  43. ->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
  44. ->first();
  45. if (empty($orderMain)) {
  46. return $this->result(ErrorCode::PAY_FAILURE, $data,'订单不存在或已失效');
  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. 'notify_url' => config('site_host') . '/v1/notify/wxminiOffline',
  53. 'trade_type' => 'JSAPI',
  54. 'openid' => $data['openid'],
  55. ]);
  56. $result['sign_type'] = 'MD5';
  57. $result['timestamp'] = time();
  58. return $this->success($result);
  59. }
  60. }