From fd9d7d631230868abb27ec743126973711bf3129 Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 14 Dec 2020 14:55:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=94=AF=E4=BB=98=E9=80=9A?= =?UTF-8?q?=E9=81=93=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/controller/api/Notify.php | 10 +++--- app/data/controller/api/auth/Config.php | 2 +- app/data/service/PaymentService.php | 36 +++++++++++++------ .../service/payment/JoinPaymentService.php | 20 +++++------ .../service/payment/WechatPaymentService.php | 20 +++++++---- 5 files changed, 55 insertions(+), 33 deletions(-) diff --git a/app/data/controller/api/Notify.php b/app/data/controller/api/Notify.php index fbcc7767c..e9bdd1c1a 100644 --- a/app/data/controller/api/Notify.php +++ b/app/data/controller/api/Notify.php @@ -16,16 +16,17 @@ class Notify extends Controller /** * 微信支付通知 * @param string $scene 支付场景 + * @param string $type 支付通道 * @return string * @throws \WeChat\Exceptions\InvalidResponseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function wxpay(string $scene = 'order'): string + public function wxpay(string $scene = 'order', string $type = ''): string { if (strtolower($scene) === 'order') { - return WechatPaymentService::instance()->notify(); + return WechatPaymentService::instance()->notify($type); } else { return 'success'; } @@ -34,15 +35,16 @@ class Notify extends Controller /** * 汇聚支付通知 * @param string $scene 支付场景 + * @param string $type 支付通道 * @return string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function joinpay(string $scene = 'order'): string + public function joinpay(string $scene = 'order', string $type = ''): string { if (strtolower($scene) === 'order') { - return JoinPaymentService::instance()->notify(); + return JoinPaymentService::instance()->notify($type); } else { return 'success'; } diff --git a/app/data/controller/api/auth/Config.php b/app/data/controller/api/auth/Config.php index d3b0ae198..11c74ff7a 100644 --- a/app/data/controller/api/auth/Config.php +++ b/app/data/controller/api/auth/Config.php @@ -22,7 +22,7 @@ class Config extends Auth { $types = []; foreach (PaymentService::TYPES as $type => $arr) { - if (isset($arr['auth']) && in_array($this->type, $arr['auth'])) { + if (isset($arr['bind']) && in_array($this->type, $arr['bind'])) { $types[] = $type; } } diff --git a/app/data/service/PaymentService.php b/app/data/service/PaymentService.php index cafa1d719..5fc978a9d 100644 --- a/app/data/service/PaymentService.php +++ b/app/data/service/PaymentService.php @@ -19,21 +19,36 @@ abstract class PaymentService extends Service const PAYMENT_JOINPAY_XCX = 'joinpay_xcx'; // 微信支付通道 + const PAYMENT_WECHAT_MWEB = 'wechat_mweb'; const PAYMENT_WECHAT_JSAPI = 'wechat_jsapi'; + const PAYMENT_WECHAT_NATIVE = 'wechat_native'; // 支付通道描述 const TYPES = [ - PaymentService::PAYMENT_JOINPAY_XCX => [ - 'name' => '汇聚小程序支付', - 'auth' => [UserService::APITYPE_WXAPP], + PaymentService::PAYMENT_JOINPAY_XCX => [ + 'type' => 'WEIXIN_XCX', + 'name' => '汇聚小程序JSAPI支付', + 'bind' => [UserService::APITYPE_WXAPP], ], - PaymentService::PAYMENT_JOINPAY_GZH => [ - 'name' => '汇聚服务号支付', - 'auth' => [UserService::APITYPE_WECHAT], + PaymentService::PAYMENT_JOINPAY_GZH => [ + 'type' => 'WEIXIN_GZH', + 'name' => '汇聚服务号JSAPI支付', + 'bind' => [UserService::APITYPE_WECHAT], ], - PaymentService::PAYMENT_WECHAT_JSAPI => [ - 'name' => '微信商户支付', - 'auth' => [UserService::APITYPE_WXAPP, UserService::APITYPE_WECHAT], + PaymentService::PAYMENT_WECHAT_MWEB => [ + 'type' => 'MWEB', + 'name' => '微信商户H5支付', + 'bind' => [UserService::APITYPE_WAP], + ], + PaymentService::PAYMENT_WECHAT_NATIVE => [ + 'type' => 'NATIVE', + 'name' => '微信商户NATIVE支付', + 'bind' => [UserService::APITYPE_WEB], + ], + PaymentService::PAYMENT_WECHAT_JSAPI => [ + 'type' => 'JSAPI', + 'name' => '微信商户JSAPI支付', + 'bind' => [UserService::APITYPE_WXAPP, UserService::APITYPE_WECHAT], ], ]; @@ -123,9 +138,10 @@ abstract class PaymentService extends Service /** * 支付通知处理 + * @param string $type * @return string */ - abstract public function notify(): string; + abstract public function notify(string $type = ''): string; /** * 订单主动查询 diff --git a/app/data/service/payment/JoinPaymentService.php b/app/data/service/payment/JoinPaymentService.php index 3aa50d2ba..4271b51ea 100644 --- a/app/data/service/payment/JoinPaymentService.php +++ b/app/data/service/payment/JoinPaymentService.php @@ -68,12 +68,8 @@ class JoinPaymentService extends PaymentService public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payDescription): array { try { - $types = [ - static::PAYMENT_JOINPAY_GZH => 'WEIXIN_GZH', - static::PAYMENT_JOINPAY_XCX => 'WEIXIN_XCX', - ]; - if (isset($types[static::$type])) { - $type = $types[static::$type]; + if (isset(static::TYPES[static::$type])) { + $type = static::TYPES[static::$type]['type']; } else { throw new \think\Exception('支付类型[' . static::$type . ']未配置定义!'); } @@ -85,7 +81,7 @@ class JoinPaymentService extends PaymentService 'p4_Cur' => '1', 'p5_ProductName' => $payTitle, 'p6_ProductDesc' => $payDescription, - 'p9_NotifyUrl' => sysuri('@data/api.notify/joinpay/scene/order', [], false, true), + 'p9_NotifyUrl' => sysuri('@data/api.notify/joinpay/scene/order/type/' . static::$type, [], false, true), 'q1_FrpCode' => $type ?? '', 'q5_OpenId' => $openid, 'q7_AppId' => $this->appid, @@ -126,13 +122,15 @@ class JoinPaymentService extends PaymentService /** * 支付结果处理 + * @param string $type 支付通道 * @return string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function notify(): string + public function notify(string $type = ''): string { + $type = $type ?: static::$type; $notify = $this->app->request->get(); foreach ($notify as &$item) $item = urldecode($item); if (empty($notify['hmac']) || $notify['hmac'] !== $this->_doSign($notify)) { @@ -142,14 +140,14 @@ class JoinPaymentService extends PaymentService // 更新支付记录 data_save('DataPaymentItem', [ 'order_no' => $notify['r2_OrderNo'], - 'payment_type' => static::$type, + 'payment_type' => $type, 'payment_code' => $notify['r9_BankTrxNo'], 'payment_amount' => $notify['r3_Amount'], 'payment_status' => 1, 'payment_datatime' => date('Y-m-d H:i:s'), - ], 'order_no', ['payment_type' => static::$type, 'payment_status' => 0]); + ], 'order_no', ['payment_type' => $type, 'payment_status' => 0]); // 更新记录状态 - if ($this->updateOrder($notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'], 'joinpay')) { + if ($this->updateOrder($notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'], $type)) { return 'success'; } } diff --git a/app/data/service/payment/WechatPaymentService.php b/app/data/service/payment/WechatPaymentService.php index a78c319bc..8c1948545 100644 --- a/app/data/service/payment/WechatPaymentService.php +++ b/app/data/service/payment/WechatPaymentService.php @@ -3,7 +3,6 @@ namespace app\data\service\payment; use app\data\service\PaymentService; -use http\Exception; use WePay\Order; /** @@ -59,14 +58,19 @@ class WechatPaymentService extends PaymentService public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payDescription): array { try { + if (isset(static::TYPES[static::$type])) { + $type = static::TYPES[static::$type]['type']; + } else { + throw new \think\Exception('支付类型[' . static::$type . ']未配置定义!'); + } $body = empty($payDescription) ? $payTitle : ($payTitle . '-' . $payDescription); $data = [ 'body' => $body, 'openid' => $openid, 'out_trade_no' => $orderNo, 'total_fee' => $payAmount * 100, - 'trade_type' => 'JSAPI', - 'notify_url' => sysuri('@data/api.notify/wxpay/scene/order', [], false, true), + 'trade_type' => $type, + 'notify_url' => sysuri('@data/api.notify/wxpay/scene/order/type/' . static::$type, [], false, true), 'spbill_create_ip' => $this->app->request->ip(), ]; if (empty($data['openid'])) unset($data['openid']); @@ -93,27 +97,29 @@ class WechatPaymentService extends PaymentService /** * 支付结果处理 + * @param string $type 支付通道 * @return string * @throws \WeChat\Exceptions\InvalidResponseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function notify(): string + public function notify(string $type = ''): string { + $type = $type ?: static::$type; $notify = $this->payment->getNotify(); if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') { // 更新支付记录 data_save('DataPaymentItem', [ 'order_no' => $notify['out_trade_no'], - 'payment_type' => static::$type, + 'payment_type' => $type, 'payment_code' => $notify['transaction_id'], 'payment_amount' => $notify['cash_fee'] / 100, 'payment_status' => 1, 'payment_datatime' => date('Y-m-d H:i:s'), - ], 'order_no', ['payment_type' => static::$type, 'payment_status' => 0]); + ], 'order_no', ['payment_type' => $type, 'payment_status' => 0]); // 更新记录状态 - if ($this->updateOrder($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100, 'wechat')) { + if ($this->updateOrder($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100, $type)) { return $this->payment->getNotifySuccessReply(); } } else {