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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -125,34 +125,46 @@ class WechatPaymentV2 extends WechatPayment
/**
* 支付通知处理
* @param array $data
* @param ?array $notify
* @param ?array $body
* @return \think\Response
* @throws \WeChat\Exceptions\InvalidDecryptException
* @throws \WeChat\Exceptions\InvalidResponseException
* @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();
p($notify, false, 'notify_v2');
if ($data['scen'] === 'order' && $notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') {
[$pCode, $pTrade] = [$notify['out_trade_no'], $notify['transaction_id']];
[$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' => '数据更新失败']);
if ($data['scen'] === 'order') {
// 支付通知处理
$notify = $this->payment->getNotify($body);
p($notify, false, 'notify_payment_v2');
if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') {
[$pCode, $pTrade] = [$notify['out_trade_no'], $notify['transaction_id']];
[$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']);
}

View File

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