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.

45 lines
1.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\Model\Users;
  6. use App\Request\WxminiPayRequest;
  7. use EasyWeChat\Factory;
  8. use Hyperf\Guzzle\CoroutineHandler;
  9. class PaymentController extends BaseController
  10. {
  11. public function wxminiPay(WxminiPayRequest $request){
  12. $data = $request->validated();
  13. $config = config('wxpay');
  14. $app = Factory::payment($config);
  15. $app['guzzle_handler'] = CoroutineHandler::class;
  16. // 待支付的,类型一致的,未超时(15min,900sec)的订单
  17. $orderMain = OrderMain::query()
  18. ->where(['state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']])
  19. ->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
  20. ->first();
  21. if (empty($orderMain)) {
  22. return $this->result(ErrorCode::PAY_FAILURE, ['order_id' => $data['order_id']],'订单不存在或已失效');
  23. }
  24. $result = $app->order->unify([
  25. 'body' => '懒族生活 - 外卖下单',
  26. 'out_trade_no' => $orderMain->global_order_id,
  27. 'total_fee' => bcmul(floatval($orderMain->money), 100),
  28. 'notify_url' => config('site_host') . '/v1/notify/wxminiOnline',
  29. 'trade_type' => 'JSAPI',
  30. 'openid' => $data['openid'],
  31. ]);
  32. return $this->success($result);
  33. }
  34. }