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.
 
 

75 lines
2.4 KiB

<?php
namespace App\Controller;
use App\Constants\ErrorCode;
use App\Model\OrderMain;
use App\Request\WxminiPayRequest;
use EasyWeChat\Factory;
use Hyperf\Guzzle\CoroutineHandler;
class PaymentController extends BaseController
{
public function wxminiPayOnline(WxminiPayRequest $request){
$data = $request->validated();
$config = config('wxpay');
$app = Factory::payment($config);
$app['guzzle_handler'] = CoroutineHandler::class;
// 待支付的,未超时(15min,900sec)的订单
$orderMain = OrderMain::query()
->where(['state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']])
->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
->first();
if (empty($orderMain)) {
return $this->result(ErrorCode::PAY_FAILURE, ['order_id' => $data['order_id']],'订单不存在或已失效');
}
$result = $app->order->unify([
'body' => '懒族生活 - 外卖下单',
'out_trade_no' => $orderMain->global_order_id,
'total_fee' => bcmul(floatval($orderMain->money), 100),
'notify_url' => config('site_host') . '/v1/notify/wxminiOnline',
'trade_type' => 'JSAPI',
'openid' => $data['openid'],
]);
return $this->success($result);
}
public function wxminiPayOffline(WxminiPayRequest $request){
$data = $request->validated();
$config = config('wxpay');
$app = Factory::payment($config);
$app['guzzle_handler'] = CoroutineHandler::class;
// 待支付的,未超时(15min,900sec)的订单
$orderMain = OrderMain::query()
->where(['dm_state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']])
->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
->first();
if (empty($orderMain)) {
return $this->result(ErrorCode::PAY_FAILURE, ['order_id' => $data['order_id']],'订单不存在或已失效');
}
$result = $app->order->unify([
'body' => '懒族生活 - 当面支付',
'out_trade_no' => $orderMain->global_order_id,
'total_fee' => bcmul(floatval($orderMain->money), 100),
'notify_url' => config('site_host') . '/v1/notify/wxminiOffline',
'trade_type' => 'JSAPI',
'openid' => $data['openid'],
]);
return $this->success($result);
}
}