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.

74 lines
2.4 KiB

5 years ago
5 years ago
5 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. return $this->success($result);
  32. }
  33. public function wxminiPayOffline(WxminiPayRequest $request){
  34. $data = $request->validated();
  35. $config = config('wxpay');
  36. $app = Factory::payment($config);
  37. $app['guzzle_handler'] = CoroutineHandler::class;
  38. // 待支付的,未超时(15min,900sec)的订单
  39. $orderMain = OrderMain::query()
  40. ->where(['dm_state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']])
  41. ->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
  42. ->first();
  43. if (empty($orderMain)) {
  44. return $this->result(ErrorCode::PAY_FAILURE, ['order_id' => $data['order_id']],'订单不存在或已失效');
  45. }
  46. $result = $app->order->unify([
  47. 'body' => '懒族生活 - 当面支付',
  48. 'out_trade_no' => $orderMain->global_order_id,
  49. 'total_fee' => bcmul(floatval($orderMain->money), 100),
  50. 'notify_url' => config('site_host') . '/v1/notify/wxminiOffline',
  51. 'trade_type' => 'JSAPI',
  52. 'openid' => $data['openid'],
  53. ]);
  54. return $this->success($result);
  55. }
  56. }