fix: 更新支付退款通知及其他常驻内存框架处理

This commit is contained in:
邹景立 2024-10-09 22:32:16 +08:00
parent 9af866f8c3
commit 8c80190e0a
10 changed files with 84 additions and 69 deletions

View File

@ -60,10 +60,10 @@ interface PaymentInterface
/** /**
* 支付通知处理 * 支付通知处理
* @param array $data * @param array $data
* @param ?array $notify * @param ?array $body
* @return \think\Response * @return \think\Response
*/ */
public function notify(array $data = [], ?array $notify = null): Response; public function notify(array $data = [], ?array $body = null): Response;
/** /**
* 发起支付退款 * 发起支付退款

View File

@ -47,10 +47,10 @@ class AliPayment implements PaymentInterface
$this->config = [ $this->config = [
// 沙箱模式 // 沙箱模式
'debug' => false, 'debug' => false,
// 签名类型RSA|RSA2
'sign_type' => "RSA2",
// 应用ID // 应用ID
'appid' => $this->cfgParams['alipay_appid'], 'appid' => $this->cfgParams['alipay_appid'],
// 签名类型RSA|RSA2
'sign_type' => "RSA2",
// 支付宝公钥 (1行填写特别注意这里是支付宝公钥不是应用公钥最好从开发者中心的网页上去复制) // 支付宝公钥 (1行填写特别注意这里是支付宝公钥不是应用公钥最好从开发者中心的网页上去复制)
'public_key' => $this->_trimCert($this->cfgParams['alipay_public_key']), 'public_key' => $this->_trimCert($this->cfgParams['alipay_public_key']),
// 支付宝私钥 (1行填写) // 支付宝私钥 (1行填写)
@ -125,13 +125,13 @@ class AliPayment implements PaymentInterface
/** /**
* 支付通知处理 * 支付通知处理
* @param array $data * @param array $data
* @param ?array $notify * @param ?array $body
* @return \think\Response * @return \think\Response
* @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\InvalidResponseException
*/ */
public function notify(array $data = [], ?array $notify = null): Response public function notify(array $data = [], ?array $body = null): Response
{ {
$notify = $data ?: App::instance($this->config)->notify(); $notify = $body ?: App::instance($this->config)->notify();
if (in_array($notify['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) { if (in_array($notify['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
if ($this->updateAction($notify['out_trade_no'], $notify['trade_no'], $notify['total_amount'])) { if ($this->updateAction($notify['out_trade_no'], $notify['trade_no'], $notify['total_amount'])) {
return response('success'); return response('success');

View File

@ -58,10 +58,10 @@ class BalancePayment implements PaymentInterface
/** /**
* 支付通知处理 * 支付通知处理
* @param array $data * @param array $data
* @param ?array $notify * @param ?array $body
* @return \think\Response * @return \think\Response
*/ */
public function notify(array $data = [], ?array $notify = null): Response public function notify(array $data = [], ?array $body = null): Response
{ {
return response('SUCCESS'); return response('SUCCESS');
} }

View File

@ -58,10 +58,10 @@ class CouponPayment implements PaymentInterface
/** /**
* 支付通知处理 * 支付通知处理
* @param array $data * @param array $data
* @param ?array $notify * @param ?array $body
* @return \think\Response * @return \think\Response
*/ */
public function notify(array $data = [], ?array $notify = null): Response public function notify(array $data = [], ?array $body = null): Response
{ {
return response('SUCCESS'); return response('SUCCESS');
} }

View File

@ -86,10 +86,10 @@ class EmptyPayment implements PaymentInterface
/** /**
* 支付通知处理 * 支付通知处理
* @param array $data * @param array $data
* @param ?array $notify * @param ?array $body
* @return \think\Response * @return \think\Response
*/ */
public function notify(array $data = [], ?array $notify = null): Response public function notify(array $data = [], ?array $body = null): Response
{ {
return response('SUCCESS'); return response('SUCCESS');
} }

View File

@ -58,10 +58,10 @@ class IntegralPayment implements PaymentInterface
/** /**
* 支付通知处理 * 支付通知处理
* @param array $data * @param array $data
* @param ?array $notify * @param ?array $body
* @return \think\Response * @return \think\Response
*/ */
public function notify(array $data = [], ?array $notify = null): Response public function notify(array $data = [], ?array $body = null): Response
{ {
return response('SUCCESS'); return response('SUCCESS');
} }

View File

@ -111,18 +111,18 @@ class JoinPayment implements PaymentInterface
/** /**
* 支付结果处理 * 支付结果处理
* @param array|null $data * @param array|null $data
* @param array|null $notify * @param array|null $body
* @return \think\Response * @return \think\Response
*/ */
public function notify(?array $data = null, ?array $notify = null): Response public function notify(?array $data = null, ?array $body = null): Response
{ {
$notify = $data ?: $this->app->request->get(); $body = $data ?: $this->app->request->get();
foreach ($notify as &$item) $item = urldecode($item); foreach ($body as &$item) $item = urldecode($item);
if (empty($notify['hmac']) || $notify['hmac'] !== $this->_doSign($notify)) { if (empty($body['hmac']) || $body['hmac'] !== $this->_doSign($body)) {
return response('error'); return response('error');
} }
if (isset($notify['r6_Status']) && intval($notify['r6_Status']) === 100) { if (isset($body['r6_Status']) && intval($body['r6_Status']) === 100) {
if ($this->updateAction($notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'])) { if ($this->updateAction($body['r2_OrderNo'], $body['r9_BankTrxNo'], $body['r3_Amount'])) {
return response('success'); return response('success');
} else { } else {
return response('error'); return response('error');

View File

@ -57,10 +57,10 @@ class VoucherPayment implements PaymentInterface
/** /**
* 支付通知处理 * 支付通知处理
* @param array $data * @param array $data
* @param ?array $notify * @param ?array $body
* @return \think\Response * @return \think\Response
*/ */
public function notify(array $data = [], ?array $notify = null): Response public function notify(array $data = [], ?array $body = null): Response
{ {
return response('SUCCESS'); return response('SUCCESS');
} }

View File

@ -125,34 +125,46 @@ class WechatPaymentV2 extends WechatPayment
/** /**
* 支付通知处理 * 支付通知处理
* @param array $data * @param array $data
* @param ?array $notify * @param ?array $body
* @return \think\Response * @return \think\Response
* @throws \WeChat\Exceptions\InvalidDecryptException
* @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\InvalidResponseException
* @throws \think\admin\Exception * @throws \think\admin\Exception
*/ */
public function notify(array $data = [], ?array $notify = null): Response public function notify(array $data = [], ?array $body = null): Response
{ {
$notify = $notify ?: $this->payment->getNotify(); if ($data['scen'] === 'order') {
p($notify, false, 'notify_v2'); // 支付通知处理
if ($data['scen'] === 'order' && $notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') { $notify = $this->payment->getNotify($body);
[$pCode, $pTrade] = [$notify['out_trade_no'], $notify['transaction_id']]; p($notify, false, 'notify_payment_v2');
[$pAmount, $pCoupon] = [strval($notify['cash_fee'] / 100), strval(($notify['coupon_fee'] ?? 0) / 100)]; if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') {
if (!$this->updateAction($pCode, $pTrade, $pAmount, null, $pCoupon, $notify)) { [$pCode, $pTrade] = [$notify['out_trade_no'], $notify['transaction_id']];
return xml(['return_code' => 'ERROR', 'return_msg' => '数据更新失败']); [$pAmount, $pCoupon] = [strval($notify['cash_fee'] / 100), strval(($notify['coupon_fee'] ?? 0) / 100)];
if (!$this->updateAction($pCode, $pTrade, $pAmount, null, $pCoupon, $notify)) {
return xml(['return_code' => 'ERROR', 'return_msg' => '数据更新失败']);
}
}
} elseif ($data['scen'] === 'refund') {
// 退款通知信息
$notify = Refund::instance($this->config)->getNotify($body);
p($notify, false, 'notify_refund_v2');
if (!empty($notify['result']) && is_array($notify['result'])) {
$notify = array_merge($notify, $notify['result']);
unset($notify['result'], $notify['req_info']);
}
if (isset($notify['refund_status']) && $notify['refund_status'] == 'SUCCESS') {
$refund = PluginPaymentRefund::mk()->where(['code' => $notify['out_refund_no']])->findOrEmpty();
if ($refund->isEmpty()) return xml(['return_code' => 'ERROR', 'return_msg' => '数据更新失败']);
$refund->save([
'refund_time' => date('Y-m-d H:i:s', strtotime($notify['success_time'])),
'refund_trade' => $notify['transaction_id'],
'refund_scode' => $notify['refund_status'],
'refund_status' => 1,
'refund_notify' => json_encode($notify, 64 | 256),
'refund_account' => $notify['refund_recv_accout'] ?? '',
]);
static::syncRefund($refund->getAttr('record_code'));
} }
} elseif ($data['scen'] === 'refund' && ($notify['refund_status'] ?? '') == 'SUCCESS') {
if ($data['order'] !== $notify['out_refund_no']) return response('error', 500);
$refund = PluginPaymentRefund::mk()->where(['code' => $notify['out_refund_no']])->findOrEmpty();
if ($refund->isEmpty()) return xml(['return_code' => 'ERROR', 'return_msg' => '数据更新失败']);
$refund->save([
'refund_time' => date('Y-m-d H:i:s', strtotime($notify['success_time'])),
'refund_trade' => $notify['refund_id'],
'refund_scode' => $notify['refund_status'],
'refund_status' => 1,
'refund_notify' => json_encode($notify, 64 | 256),
'refund_account' => $notify['refund_recv_accout'] ?? '',
]);
static::syncRefund($refund->getAttr('record_code'));
} }
return xml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']); return xml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
} }

View File

@ -75,12 +75,12 @@ class WechatPaymentV3 extends WechatPayment
[$payCode] = [Payment::withPaymentCode(), $this->withUserUnid($account)]; [$payCode] = [Payment::withPaymentCode(), $this->withUserUnid($account)];
$body = empty($orderRemark) ? $orderTitle : ($orderTitle . '-' . $orderRemark); $body = empty($orderRemark) ? $orderTitle : ($orderTitle . '-' . $orderRemark);
$data = [ $data = [
'appid' => $this->config['appid'], 'appid' => $this->config['appid'],
'mchid' => $this->config['mch_id'], 'mchid' => $this->config['mch_id'],
'payer' => ['openid' => $this->withUserField($account, 'openid')], 'payer' => ['openid' => $this->withUserField($account, 'openid')],
'amount' => ['total' => intval($payAmount * 100), 'currency' => 'CNY'], 'amount' => ['total' => intval($payAmount * 100), 'currency' => 'CNY'],
'notify_url' => $this->withNotifyUrl($payCode), 'notify_url' => $this->withNotifyUrl($payCode),
'description' => $body, 'description' => $body,
'out_trade_no' => $payCode, 'out_trade_no' => $payCode,
]; ];
$tradeType = static::tradeTypes[$this->cfgType] ?? ''; $tradeType = static::tradeTypes[$this->cfgType] ?? '';
@ -131,17 +131,19 @@ class WechatPaymentV3 extends WechatPayment
/** /**
* 支付通知处理 * 支付通知处理
* @param array $data * @param array $data
* @param ?array $notify * @param ?array $body
* @return \think\Response * @return \think\Response
*/ */
public function notify(array $data = [], ?array $notify = null): Response public function notify(array $data = [], ?array $body = null): Response
{ {
try { try {
$notify = $notify ?: $this->payment->notify(); // 接收通知内容
$notify = $this->payment->notify($body);
p($notify, false, 'notify_v3'); p($notify, false, 'notify_v3');
$result = empty($notify['result']) ? [] : json_decode($notify['result'], true); $result = empty($notify['result']) ? [] : json_decode($notify['result'], true);
if (empty($result) || !is_array($result)) return response('error', 500); if (empty($result) || !is_array($result)) return response('error', 500);
if ($data['scen'] === 'order' && ($result['trade_state'] ?? '') == 'SUCCESS') { // 支付通知处理
if ($data['scen'] === 'order' && $result['trade_state'] ?? '' == 'SUCCESS') {
// 不考虑支付平台的优惠券金额 // 不考虑支付平台的优惠券金额
$pAmount = strval($result['amount']['total'] / 100); $pAmount = strval($result['amount']['total'] / 100);
[$pCode, $pTrade] = [$result['out_trade_no'], $result['transaction_id']]; [$pCode, $pTrade] = [$result['out_trade_no'], $result['transaction_id']];
@ -149,15 +151,16 @@ class WechatPaymentV3 extends WechatPayment
if (!$this->updateAction($pCode, $pTrade, $pAmount, null, $pCoupon, $result)) { if (!$this->updateAction($pCode, $pTrade, $pAmount, null, $pCoupon, $result)) {
return response('error', 500); return response('error', 500);
} }
} elseif ($data['scen'] === 'refund' && ($result['refund_status'] ?? '') == 'SUCCESS') { } elseif ($data['scen'] === 'refund' && $result['refund_status'] ?? '' == 'SUCCESS') {
if ($data['order'] !== $result['out_refund_no']) return response('error', 500); // 退款通知信息
$refund = PluginPaymentRefund::mk()->where(['code' => $result['out_refund_no']])->findOrEmpty(); $refund = PluginPaymentRefund::mk()->where(['code' => $result['out_refund_no']])->findOrEmpty();
if ($refund->isEmpty()) return response('error', 500); else $refund->save([ if ($refund->isEmpty()) return response('error', 500);
'refund_time' => date('Y-m-d H:i:s', strtotime($result['success_time'])), $refund->save([
'refund_trade' => $result['refund_id'], 'refund_time' => date('Y-m-d H:i:s', strtotime($result['success_time'])),
'refund_scode' => $result['refund_status'], 'refund_trade' => $result['refund_id'],
'refund_status' => 1, 'refund_scode' => $result['refund_status'],
'refund_notify' => json_encode($result, 64 | 256), 'refund_status' => 1,
'refund_notify' => json_encode($result, 64 | 256),
'refund_account' => $result['user_received_account'] ?? '', 'refund_account' => $result['user_received_account'] ?? '',
]); ]);
static::syncRefund($refund->getAttr('record_code')); static::syncRefund($refund->getAttr('record_code'));
@ -185,12 +188,12 @@ class WechatPaymentV3 extends WechatPayment
$record = static::syncRefund($pcode, $rcode, $amount, $reason); $record = static::syncRefund($pcode, $rcode, $amount, $reason);
// 发起退款申请 // 发起退款申请
$options = [ $options = [
'out_trade_no' => $pcode, 'out_trade_no' => $pcode,
'out_refund_no' => $rcode, 'out_refund_no' => $rcode,
'notify_url' => static::withNotifyUrl($rcode, 'refund'), 'notify_url' => static::withNotifyUrl($rcode, 'refund'),
'amount' => [ 'amount' => [
'total' => intval($record->getAttr('payment_amount') * 100), 'total' => intval($record->getAttr('payment_amount') * 100),
'refund' => intval(floatval($amount) * 100), 'refund' => intval(floatval($amount) * 100),
'currency' => 'CNY' 'currency' => 'CNY'
] ]
]; ];