From 80b3d7c8e5f3b6f15e9524c189239c8fb1612446 Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 14 Dec 2020 15:35:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=94=AF=E4=BB=98=E9=80=9A?= =?UTF-8?q?=E9=81=93=EF=BC=8C=E5=A2=9E=E5=8A=A0=E9=80=9A=E9=81=93=E7=BC=96?= =?UTF-8?q?=E5=8F=B7=E4=BC=A0=E9=80=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin_v6.sql | 9 ++++-- app/data/controller/api/Notify.php | 12 ++++---- app/data/service/PaymentService.php | 11 +++++-- .../service/payment/JoinPaymentService.php | 29 ++++++++++++------- .../service/payment/WechatPaymentService.php | 29 ++++++++++++------- 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/admin_v6.sql b/admin_v6.sql index 9c4c32cf3..1f8eb49c1 100644 --- a/admin_v6.sql +++ b/admin_v6.sql @@ -11,7 +11,7 @@ Target Server Version : 50562 File Encoding : 65001 - Date: 14/12/2020 14:20:05 + Date: 14/12/2020 15:34:25 */ SET NAMES utf8mb4; @@ -139,6 +139,7 @@ CREATE TABLE `data_payment_item` ( `order_no` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '订单单号', `order_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '订单描述', `order_amount` decimal(20, 2) NULL DEFAULT 0.00 COMMENT '订单金额', + `payment_id` bigint(20) NULL DEFAULT 0 COMMENT '支付编号', `payment_type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '支付通道', `payment_code` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '支付单号', `payment_status` tinyint(1) NULL DEFAULT 0 COMMENT '支付状态', @@ -147,6 +148,7 @@ CREATE TABLE `data_payment_item` ( `create_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) USING BTREE, INDEX `idx_data_payment_item_order_no`(`order_no`) USING BTREE, + INDEX `idx_data_payment_item_pament_id`(`payment_id`) USING BTREE, INDEX `idx_data_payment_item_payment_type`(`payment_type`) USING BTREE, INDEX `idx_data_payment_item_payment_status`(`payment_status`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '数据-支付-记录' ROW_FORMAT = COMPACT; @@ -4569,7 +4571,7 @@ CREATE TABLE `system_oplog` ( `username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '操作人用户名', `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '系统-日志' ROW_FORMAT = COMPACT; +) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '系统-日志' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of system_oplog @@ -4577,6 +4579,7 @@ CREATE TABLE `system_oplog` ( INSERT INTO `system_oplog` VALUES (1, 'admin/login/index', '127.0.0.1', '系统用户登录', '登录系统后台成功', 'admin', '2020-12-14 06:00:24'); INSERT INTO `system_oplog` VALUES (2, 'admin/menu/edit', '127.0.0.1', '系统菜单管理', '修改系统菜单[87]成功', 'admin', '2020-12-14 06:00:53'); INSERT INTO `system_oplog` VALUES (3, 'admin/menu/edit', '127.0.0.1', '系统菜单管理', '修改系统菜单[87]成功', 'admin', '2020-12-14 06:01:06'); +INSERT INTO `system_oplog` VALUES (4, 'admin/login/index', '127.0.0.1', '系统用户登录', '登录系统后台成功', 'admin', '2020-12-14 06:34:19'); -- ---------------------------- -- Table structure for system_queue @@ -4642,7 +4645,7 @@ CREATE TABLE `system_user` ( -- ---------------------------- -- Records of system_user -- ---------------------------- -INSERT INTO `system_user` VALUES (10000, 'admin', '21232f297a57a5a743894a0e4a801fc3', '系统管理员', 'https://v6.thinkadmin.top/upload/ec/f571134493e54fe06855c88557052c.png', '', '', '', '', '127.0.0.1', '2020-12-14 06:00:24', 23, '', 1, 0, 0, '2015-11-13 15:14:22'); +INSERT INTO `system_user` VALUES (10000, 'admin', '21232f297a57a5a743894a0e4a801fc3', '系统管理员', 'https://v6.thinkadmin.top/upload/ec/f571134493e54fe06855c88557052c.png', '', '', '', '', '127.0.0.1', '2020-12-14 06:34:19', 24, '', 1, 0, 0, '2015-11-13 15:14:22'); -- ---------------------------- -- Table structure for wechat_fans diff --git a/app/data/controller/api/Notify.php b/app/data/controller/api/Notify.php index e9bdd1c1a..7c040c66f 100644 --- a/app/data/controller/api/Notify.php +++ b/app/data/controller/api/Notify.php @@ -16,17 +16,17 @@ class Notify extends Controller /** * 微信支付通知 * @param string $scene 支付场景 - * @param string $type 支付通道 + * @param string $param 支付通道 * @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 $type = ''): string + public function wxpay(string $scene = 'order', string $param = ''): string { if (strtolower($scene) === 'order') { - return WechatPaymentService::instance()->notify($type); + return WechatPaymentService::instance()->notify($param); } else { return 'success'; } @@ -35,16 +35,16 @@ class Notify extends Controller /** * 汇聚支付通知 * @param string $scene 支付场景 - * @param string $type 支付通道 + * @param string $param 支付通道 * @return string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function joinpay(string $scene = 'order', string $type = ''): string + public function joinpay(string $scene = 'order', string $param = ''): string { if (strtolower($scene) === 'order') { - return JoinPaymentService::instance()->notify($type); + return JoinPaymentService::instance()->notify($param); } else { return 'success'; } diff --git a/app/data/service/PaymentService.php b/app/data/service/PaymentService.php index 6376ec9ce..faf08395f 100644 --- a/app/data/service/PaymentService.php +++ b/app/data/service/PaymentService.php @@ -52,6 +52,12 @@ abstract class PaymentService extends Service ], ]; + /** + * 支付通道编号 + * @var integer + */ + protected static $id; + /** * 默认支付类型 * @var string @@ -79,6 +85,7 @@ abstract class PaymentService extends Service */ public static function build(string $payid): PaymentService { + static::$id = $payid; if (isset(static::$driver[$payid])) { return static::$driver[$payid]; } @@ -156,8 +163,8 @@ abstract class PaymentService extends Service * @param string $orderNo 交易订单单号 * @param string $payAmount 交易订单金额(元) * @param string $payTitle 交易订单名称 - * @param string $payDescription 订单订单描述 + * @param string $payRemark 订单订单描述 * @return array */ - abstract public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payDescription): array; + abstract public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payRemark): array; } \ No newline at end of file diff --git a/app/data/service/payment/JoinPaymentService.php b/app/data/service/payment/JoinPaymentService.php index 4271b51ea..1dfc0ff94 100644 --- a/app/data/service/payment/JoinPaymentService.php +++ b/app/data/service/payment/JoinPaymentService.php @@ -61,15 +61,16 @@ class JoinPaymentService extends PaymentService * @param string $orderNo 交易订单单号 * @param string $payAmount 交易订单金额(元) * @param string $payTitle 交易订单名称 - * @param string $payDescription 订单订单描述 + * @param string $payRemark 订单订单描述 * @return array * @throws \think\Exception */ - public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payDescription): array + public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payRemark): array { try { if (isset(static::TYPES[static::$type])) { - $type = static::TYPES[static::$type]['type']; + $tradeType = static::TYPES[static::$type]['type']; + $tradeParam = static::$type . '_' . static::$id; } else { throw new \think\Exception('支付类型[' . static::$type . ']未配置定义!'); } @@ -80,9 +81,9 @@ class JoinPaymentService extends PaymentService 'p3_Amount' => $payAmount * 100, 'p4_Cur' => '1', 'p5_ProductName' => $payTitle, - 'p6_ProductDesc' => $payDescription, - 'p9_NotifyUrl' => sysuri('@data/api.notify/joinpay/scene/order/type/' . static::$type, [], false, true), - 'q1_FrpCode' => $type ?? '', + 'p6_ProductDesc' => $payRemark, + 'p9_NotifyUrl' => sysuri("@data/api.notify/joinpay/scene/order/param/{$tradeParam}", [], false, true), + 'q1_FrpCode' => $tradeType ?? '', 'q5_OpenId' => $openid, 'q7_AppId' => $this->appid, 'qa_TradeMerchantNo' => $this->trade, @@ -93,7 +94,8 @@ class JoinPaymentService extends PaymentService if (is_array($result) && isset($result['ra_Code']) && intval($result['ra_Code']) === 100) { // 创建支付记录 $this->app->db->name('DataPaymentItem')->insert([ - 'order_no' => $orderNo, 'order_name' => $payTitle, 'order_amount' => $payAmount, 'payment_type' => static::$type, + 'order_no' => $orderNo, 'order_name' => $payTitle, 'order_amount' => $payAmount, + 'payment_id' => static::$id, 'payment_type' => static::$type, ]); // 返回支付参数 return json_decode($result['rc_Result'], true); @@ -130,7 +132,11 @@ class JoinPaymentService extends PaymentService */ public function notify(string $type = ''): string { - $type = $type ?: static::$type; + if (is_numeric(stripos($type, '_'))) { + [$payType, $payId] = explode('_', $type); + } else { + [$payType, $payId] = [$type ?: static::$type, static::$id]; + } $notify = $this->app->request->get(); foreach ($notify as &$item) $item = urldecode($item); if (empty($notify['hmac']) || $notify['hmac'] !== $this->_doSign($notify)) { @@ -140,14 +146,15 @@ class JoinPaymentService extends PaymentService // 更新支付记录 data_save('DataPaymentItem', [ 'order_no' => $notify['r2_OrderNo'], - 'payment_type' => $type, + 'payment_id' => $payId, + 'payment_type' => $payType, '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' => $type, 'payment_status' => 0]); + ], 'order_no', ['payment_type' => $payType, 'payment_status' => 0]); // 更新记录状态 - if ($this->updateOrder($notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'], $type)) { + if ($this->updateOrder($notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'], $payType)) { return 'success'; } } diff --git a/app/data/service/payment/WechatPaymentService.php b/app/data/service/payment/WechatPaymentService.php index 8c1948545..a9d4f2177 100644 --- a/app/data/service/payment/WechatPaymentService.php +++ b/app/data/service/payment/WechatPaymentService.php @@ -51,26 +51,27 @@ class WechatPaymentService extends PaymentService * @param string $orderNo 交易订单单号 * @param string $payAmount 交易订单金额(元) * @param string $payTitle 交易订单名称 - * @param string $payDescription 订单订单描述 + * @param string $payRemark 订单订单描述 * @return array * @throws \think\Exception */ - public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payDescription): array + public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payRemark): array { try { if (isset(static::TYPES[static::$type])) { - $type = static::TYPES[static::$type]['type']; + $tradeType = static::TYPES[static::$type]['type']; + $tradeParam = static::$type . '_' . static::$id; } else { throw new \think\Exception('支付类型[' . static::$type . ']未配置定义!'); } - $body = empty($payDescription) ? $payTitle : ($payTitle . '-' . $payDescription); + $body = empty($payRemark) ? $payTitle : ($payTitle . '-' . $payRemark); $data = [ 'body' => $body, 'openid' => $openid, 'out_trade_no' => $orderNo, 'total_fee' => $payAmount * 100, - 'trade_type' => $type, - 'notify_url' => sysuri('@data/api.notify/wxpay/scene/order/type/' . static::$type, [], false, true), + 'trade_type' => $tradeType ?: '', + 'notify_url' => sysuri("@data/api.notify/wxpay/scene/order/param/{$tradeParam}", [], false, true), 'spbill_create_ip' => $this->app->request->ip(), ]; if (empty($data['openid'])) unset($data['openid']); @@ -78,7 +79,8 @@ class WechatPaymentService extends PaymentService if ($info['return_code'] === 'SUCCESS' && $info['result_code'] === 'SUCCESS') { // 创建支付记录 $this->app->db->name('DataPaymentItem')->insert([ - 'order_no' => $orderNo, 'order_name' => $payTitle, 'order_amount' => $payAmount, 'payment_type' => static::$type, + 'order_no' => $orderNo, 'order_name' => $payTitle, 'order_amount' => $payAmount, + 'payment_id' => static::$id, 'payment_type' => static::$type, ]); // 返回支付参数 return $this->payment->jsapiParams($info['prepay_id']); @@ -106,20 +108,25 @@ class WechatPaymentService extends PaymentService */ public function notify(string $type = ''): string { - $type = $type ?: static::$type; + if (is_numeric(stripos($type, '_'))) { + [$payType, $payId] = explode('_', $type); + } else { + [$payType, $payId] = [$type ?: static::$type, static::$id]; + } $notify = $this->payment->getNotify(); if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') { // 更新支付记录 data_save('DataPaymentItem', [ 'order_no' => $notify['out_trade_no'], - 'payment_type' => $type, + 'payment_id' => $payId, + 'payment_type' => $payType, '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' => $type, 'payment_status' => 0]); + ], 'order_no', ['payment_type' => $payType, 'payment_status' => 0]); // 更新记录状态 - if ($this->updateOrder($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100, $type)) { + if ($this->updateOrder($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100, $payType)) { return $this->payment->getNotifySuccessReply(); } } else {