供销押金充值支付和支付回调对接支付系统

This commit is contained in:
chenbo 2023-12-01 09:34:46 +08:00
parent 7783254f1a
commit bda0bae117
3 changed files with 48 additions and 65 deletions

View File

@ -16,8 +16,11 @@ namespace app\api\controller;
use app\api\validate\PayValidate; use app\api\validate\PayValidate;
use app\common\enum\PayEnum;
use app\common\enum\user\UserTerminalEnum; use app\common\enum\user\UserTerminalEnum;
use app\common\logic\PaymentLogic; use app\common\logic\PaymentLogic;
use app\common\logic\PayRequestLogic;
use app\common\model\Company;
use app\common\service\pay\WeChatPayService; use app\common\service\pay\WeChatPayService;
use think\facade\Log; use think\facade\Log;
@ -62,13 +65,23 @@ class PayController extends BaseApiController
if (false === $order) { if (false === $order) {
return $this->fail(PaymentLogic::getError(), $params); return $this->fail(PaymentLogic::getError(), $params);
} }
//支付流程 // 请求支付系统
$redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; $companyInfo = Company::where(['id' => $this->userInfo['company_id']])->find();
$result = PaymentLogic::pay($params['pay_way'], $params['from'], $order, $this->userInfo['terminal'], $redirectUrl); $requestData = [
'street' => $companyInfo['street'],
'order_from' => 12,
'order_type' => 101,
'pay_user_role' => $this->userInfo['group_id'],
'pay_user_info' => $this->userInfo,
'business_order_no' => $order['sn'],
'total_fee' => intval(bcmul($order['order_amount'], 100)),
'business_callback_url' => (string)url('pay/notifyApp', [], false, true)
];
$result = PayRequestLogic::getPrePayId($requestData);
if (false === $result) { if (false === $result) {
return $this->fail(PaymentLogic::getError(), $params); return $this->fail(PaymentLogic::getError(), $params);
} }
return $this->success('', $result); return $this->success('', $result['data']);
} }
@ -122,18 +135,18 @@ class PayController extends BaseApiController
/** /**
* @notes app支付回调 * @notes 支付系统回调
* @return \Psr\Http\Message\ResponseInterface
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \ReflectionException
* @throws \Throwable
* @date 2023/2/28 14:21 * @date 2023/2/28 14:21
*/ */
public function notifyApp() public function notifyApp()
{ {
return (new WeChatPayService(UserTerminalEnum::ANDROID))->notify(); try {
$param = $this->request->param();
Log::info(['支付系统回调', $param]);
(new WeChatPayService(UserTerminalEnum::ANDROID))->notify($param);
return $this->success('ok');
} catch (\Exception $e) {
return $this->fail($e->getMessage());
}
} }
} }

View File

@ -113,7 +113,7 @@ class PayNotifyLogic extends BaseLogic
// 更新充值订单状态 // 更新充值订单状态
$order->transaction_id = $extra['transaction_id']; $order->transaction_id = $extra['transaction_id'];
$order->pay_status = PayEnum::ISPAID; $order->pay_status = $extra['pay_status'];;
$order->pay_time = time(); $order->pay_time = time();
$order->save(); $order->save();
} }

View File

@ -354,45 +354,15 @@ class WeChatPayService extends BasePayService
/** /**
* @notes 支付回调 * @notes 支付回调
* @return \Psr\Http\Message\ResponseInterface
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \ReflectionException
* @throws \Throwable
* @author 段誉
* @date 2023/2/28 14:20
*/ */
public function notify() public function notify($param=[])
{ {
$server = $this->app->getServer(); Log::info(['支付回调', $param]);
// 支付通知 $extra['transaction_id'] = $param['transaction_id'];
$server->handlePaid(function (Message $message) { $extra['pay_status'] = $param['pay_status'];
$data = ['trade_state' => $message['trade_state'] ?? '', 'out_trade_no' => $message['out_trade_no'] ?? '', 'transaction_id' => $message['transaction_id'] ?? '', 'attach' => $message['attach'] ?? '']; PayNotifyLogic::handle('recharge', $param['out_trade_no'], $extra);
Log::info('wechat pay notify: ' . var_export($data, true));
if ($message['trade_state'] === 'SUCCESS') {
$extra['transaction_id'] = $message['transaction_id'];
$attach = $message['attach'];
$message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18);
switch ($attach) {
case 'recharge':
$order = RechargeOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty();
Log::info('wechat pay notify: ' . var_export($order, true));
if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
return true; return true;
} }
PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra);
break;
}
}
return true;
});
// 退款通知
$server->handleRefunded(function (Message $message) {
return true;
});
return $server->serve();
}
public function configForPayment($prepayId, $appId) public function configForPayment($prepayId, $appId)
{ {