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.

44 lines
1.3 KiB

  1. <?php
  2. namespace App\Controller;
  3. use App\Model\OrderMain;
  4. use App\Model\Users;
  5. use App\Request\WxminiPayRequest;
  6. use EasyWeChat\Factory;
  7. use Hyperf\Guzzle\CoroutineHandler;
  8. class PaymentController extends BaseController
  9. {
  10. public function wxminiPay(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, 'type' => $data['type'], '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(200, ['order_id' => $data['order_id']],'订单不存在或已失效');
  22. }
  23. $result = $app->order->unify([
  24. 'body' => '懒族生活 - 外卖下单',
  25. 'out_trade_no' => $orderMain->global_order_id,
  26. 'total_fee' => bcmul(floatval($orderMain->money), 100),
  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. }