From 8c80190e0a59af60cf82ee96daf477fdc74f35fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Wed, 9 Oct 2024 22:32:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E9=80=80=E6=AC=BE=E9=80=9A=E7=9F=A5=E5=8F=8A=E5=85=B6=E4=BB=96?= =?UTF-8?q?=E5=B8=B8=E9=A9=BB=E5=86=85=E5=AD=98=E6=A1=86=E6=9E=B6=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/service/contract/PaymentInterface.php | 4 +- .../src/service/payment/AliPayment.php | 10 ++-- .../src/service/payment/BalancePayment.php | 4 +- .../src/service/payment/CouponPayment.php | 4 +- .../src/service/payment/EmptyPayment.php | 4 +- .../src/service/payment/IntegralPayment.php | 4 +- .../src/service/payment/JoinPayment.php | 14 ++--- .../src/service/payment/VoucherPayment.php | 4 +- .../payment/wechat/WechatPaymentV2.php | 56 +++++++++++-------- .../payment/wechat/WechatPaymentV3.php | 49 ++++++++-------- 10 files changed, 84 insertions(+), 69 deletions(-) diff --git a/plugin/think-plugs-payment/src/service/contract/PaymentInterface.php b/plugin/think-plugs-payment/src/service/contract/PaymentInterface.php index 7e27313e3..447259beb 100644 --- a/plugin/think-plugs-payment/src/service/contract/PaymentInterface.php +++ b/plugin/think-plugs-payment/src/service/contract/PaymentInterface.php @@ -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; /** * 发起支付退款 diff --git a/plugin/think-plugs-payment/src/service/payment/AliPayment.php b/plugin/think-plugs-payment/src/service/payment/AliPayment.php index 5e0ae030f..7af2a375f 100644 --- a/plugin/think-plugs-payment/src/service/payment/AliPayment.php +++ b/plugin/think-plugs-payment/src/service/payment/AliPayment.php @@ -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'); diff --git a/plugin/think-plugs-payment/src/service/payment/BalancePayment.php b/plugin/think-plugs-payment/src/service/payment/BalancePayment.php index 2cec3fedb..489db0b1d 100644 --- a/plugin/think-plugs-payment/src/service/payment/BalancePayment.php +++ b/plugin/think-plugs-payment/src/service/payment/BalancePayment.php @@ -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'); } diff --git a/plugin/think-plugs-payment/src/service/payment/CouponPayment.php b/plugin/think-plugs-payment/src/service/payment/CouponPayment.php index 59ac374ac..15c0d592a 100644 --- a/plugin/think-plugs-payment/src/service/payment/CouponPayment.php +++ b/plugin/think-plugs-payment/src/service/payment/CouponPayment.php @@ -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'); } diff --git a/plugin/think-plugs-payment/src/service/payment/EmptyPayment.php b/plugin/think-plugs-payment/src/service/payment/EmptyPayment.php index 250eaa579..00b7cfbb1 100644 --- a/plugin/think-plugs-payment/src/service/payment/EmptyPayment.php +++ b/plugin/think-plugs-payment/src/service/payment/EmptyPayment.php @@ -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'); } diff --git a/plugin/think-plugs-payment/src/service/payment/IntegralPayment.php b/plugin/think-plugs-payment/src/service/payment/IntegralPayment.php index 5a1493678..6c66cfbde 100644 --- a/plugin/think-plugs-payment/src/service/payment/IntegralPayment.php +++ b/plugin/think-plugs-payment/src/service/payment/IntegralPayment.php @@ -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'); } diff --git a/plugin/think-plugs-payment/src/service/payment/JoinPayment.php b/plugin/think-plugs-payment/src/service/payment/JoinPayment.php index 675576d70..a9a8cc0d8 100644 --- a/plugin/think-plugs-payment/src/service/payment/JoinPayment.php +++ b/plugin/think-plugs-payment/src/service/payment/JoinPayment.php @@ -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'); diff --git a/plugin/think-plugs-payment/src/service/payment/VoucherPayment.php b/plugin/think-plugs-payment/src/service/payment/VoucherPayment.php index acaa36e76..aa538096e 100644 --- a/plugin/think-plugs-payment/src/service/payment/VoucherPayment.php +++ b/plugin/think-plugs-payment/src/service/payment/VoucherPayment.php @@ -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'); } diff --git a/plugin/think-plugs-payment/src/service/payment/wechat/WechatPaymentV2.php b/plugin/think-plugs-payment/src/service/payment/wechat/WechatPaymentV2.php index aa1b1905a..17cbab27d 100644 --- a/plugin/think-plugs-payment/src/service/payment/wechat/WechatPaymentV2.php +++ b/plugin/think-plugs-payment/src/service/payment/wechat/WechatPaymentV2.php @@ -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']); } diff --git a/plugin/think-plugs-payment/src/service/payment/wechat/WechatPaymentV3.php b/plugin/think-plugs-payment/src/service/payment/wechat/WechatPaymentV3.php index 4223f96be..73375f306 100644 --- a/plugin/think-plugs-payment/src/service/payment/wechat/WechatPaymentV3.php +++ b/plugin/think-plugs-payment/src/service/payment/wechat/WechatPaymentV3.php @@ -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' ] ];