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.

49 lines
1.6 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. if (floatval($orderMain->money) != floatval($data['money'])) {
  25. return $this->result(ErrorCode::PAY_FAILURE, ['order_id' => $data['order_id']],'订单金额有误');
  26. }
  27. $result = $app->order->unify([
  28. 'body' => '懒族生活 - 外卖下单',
  29. 'out_trade_no' => $orderMain->global_order_id,
  30. 'total_fee' => bcmul(floatval($orderMain->money), 100),
  31. 'notify_url' => config('site_host') . '/v1/notify/wxminiOnline',
  32. 'trade_type' => 'JSAPI',
  33. 'openid' => $data['openid'],
  34. ]);
  35. return $this->success($result);
  36. }
  37. }