From a45f89622c9a1f21708916ca0f35254063b2394b Mon Sep 17 00:00:00 2001 From: Anyon Date: Wed, 16 Dec 2020 10:57:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=94=AF=E4=BB=98=E5=8A=A8?= =?UTF-8?q?=E4=BD=9C=E8=AE=B0=E5=BD=95=EF=BC=8C=E5=8F=8A=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/service/PaymentService.php | 71 ++++++++++++++++--- .../service/payment/AlipayPaymentService.php | 28 ++------ .../service/payment/JoinPaymentService.php | 28 ++------ .../service/payment/WechatPaymentService.php | 59 +++++++-------- 4 files changed, 101 insertions(+), 85 deletions(-) diff --git a/app/data/service/PaymentService.php b/app/data/service/PaymentService.php index 7c9a13983..b048be8c2 100644 --- a/app/data/service/PaymentService.php +++ b/app/data/service/PaymentService.php @@ -147,20 +147,20 @@ abstract class PaymentService extends Service * @param string $code 订单单号 * @param string $payno 交易单号 * @param string $amount 支付金额 - * @param string $paytype 支付类型 + * @param null|string $paytype 支付类型 * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function updateOrder(string $code, string $payno, string $amount, string $paytype): bool + public function updateOrder(string $code, string $payno, string $amount, ?string $paytype = null): bool { // 检查订单支付状态 $map = ['order_no' => $code, 'payment_status' => 0, 'status' => 2]; $order = $this->app->db->name('StoreOrder')->where($map)->find(); if (empty($order)) return false; // 更新订单支付状态 - $this->app->db->name('StoreOrder')->where($map)->update([ + $data = [ 'status' => 3, 'payment_code' => $payno, 'payment_type' => $paytype, @@ -168,17 +168,65 @@ abstract class PaymentService extends Service 'payment_amount' => $amount, 'payment_remark' => '微信在线支付', 'payment_datetime' => date('Y-m-d H:i:s'), - ]); + ]; + if (empty($data['payment_type'])) unset($data['payment_type']); + $this->app->db->name('StoreOrder')->where($map)->update($data); // 调用用户升级机制 return OrderService::instance()->syncAmount($order['order_no']); } /** - * 支付通知处理 - * @param string $param 支付通道-支付编号 - * @return string + * 创建支付行为 + * @param string $param 通道-编号 + * @param string $orderNo 商户订单单号 + * @param string $payTitle 商户订单标题 + * @param string $payAmount */ - abstract public function notify(string $param = ''): string; + protected function createPaymentAction(string $param, string $orderNo, string $payTitle, string $payAmount) + { + if (is_numeric(stripos($param, '-'))) { + [$paymentType, $paymentId] = explode('-', $param); + } else { + [$paymentType, $paymentId] = [$param ?: static::$type, static::$id]; + } + // 创建支付记录 + $this->app->db->name('DataPaymentItem')->insert([ + 'order_no' => $orderNo, 'order_name' => $payTitle, 'order_amount' => $payAmount, + 'payment_id' => $paymentId, 'payment_type' => $paymentType, + ]); + } + + /** + * 更新支付记录并更新订单 + * @param string $param 通道-编号 + * @param string $orderNo 商户订单单号 + * @param string $paymentCode 平台交易单号 + * @param string $paymentAmount 实际到账金额 + * @return boolean + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + protected function updatePaymentAction(string $param, string $orderNo, string $paymentCode, string $paymentAmount): bool + { + if (is_numeric(stripos($param, '-'))) { + [$paymentType, $paymentId] = explode('-', $param); + } else { + [$paymentType, $paymentId] = [$param ?: static::$type, static::$id]; + } + // 更新支付记录 + data_save('DataPaymentItem', [ + 'order_no' => $orderNo, + 'payment_id' => $paymentId, + 'payment_type' => $paymentType, + 'payment_code' => $paymentCode, + 'payment_amount' => $paymentAmount, + 'payment_status' => 1, + 'payment_datatime' => date('Y-m-d H:i:s'), + ], 'order_no', ['payment_id' => $paymentId, 'payment_type' => $paymentType]); + // 更新记录状态 + return $this->updateOrder($orderNo, $paymentCode, $paymentAmount, $paymentType); + } /** * 订单主动查询 @@ -187,6 +235,13 @@ abstract class PaymentService extends Service */ abstract public function query(string $orderNo): array; + /** + * 支付通知处理 + * @param string $param 支付通道-支付编号 + * @return string + */ + abstract public function notify(string $param = ''): string; + /** * 创建支付订单 * @param string $openid 会员OPENID diff --git a/app/data/service/payment/AlipayPaymentService.php b/app/data/service/payment/AlipayPaymentService.php index da21f92b3..530ee35f5 100644 --- a/app/data/service/payment/AlipayPaymentService.php +++ b/app/data/service/payment/AlipayPaymentService.php @@ -72,29 +72,15 @@ class AlipayPaymentService extends PaymentService */ public function notify(string $param = ''): string { - if (is_numeric(stripos($param, '-'))) { - [$payType, $payId] = explode('-', $param); - } else { - [$payType, $payId] = [$param ?: static::$type, static::$id]; - } $notify = \AliPay\App::instance($this->params)->notify(); if (in_array($notify['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) { - // 更新支付记录 - data_save('DataPaymentItem', [ - 'order_no' => $notify['out_trade_no'], - 'payment_id' => $payId, - 'payment_type' => $payType, - 'payment_code' => $notify['trade_no'], - 'payment_amount' => $notify['total_amount'], - 'payment_status' => 1, - 'payment_datatime' => date('Y-m-d H:i:s'), - ], 'order_no', ['payment_id' => $payId, 'payment_type' => $payType, 'payment_status' => 0]); - // 更新记录状态 - if ($this->updateOrder($notify['out_trade_no'], $notify['trade_no'], $notify['total_amount'], $payType)) { + if ($this->updatePaymentAction($param, $notify['out_trade_no'], $notify['trade_no'], $notify['total_amount'])) { return 'success'; + } else { + return 'error'; } } else { - return 'error'; + return 'success'; } } @@ -151,10 +137,8 @@ class AlipayPaymentService extends PaymentService if (!empty($payRemark)) $data['body'] = $payRemark; $result = $payment->apply($data); // 创建支付记录 - $this->app->db->name('DataPaymentItem')->insert([ - 'order_no' => $orderNo, 'order_name' => $payTitle, 'order_amount' => $payAmount, - 'payment_id' => static::$id, 'payment_type' => static::$type, - ]); + $this->createPaymentAction($tradeParam, $orderNo, $payTitle, $payAmount); + // 返回支付参数 return ['result' => $result]; } catch (\think\Exception $exception) { throw $exception; diff --git a/app/data/service/payment/JoinPaymentService.php b/app/data/service/payment/JoinPaymentService.php index c51f10e2c..cc2958a43 100644 --- a/app/data/service/payment/JoinPaymentService.php +++ b/app/data/service/payment/JoinPaymentService.php @@ -94,10 +94,7 @@ class JoinPaymentService extends PaymentService $result = $this->_doReuest($data); 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_id' => static::$id, 'payment_type' => static::$type, - ]); + $this->createPaymentAction($tradeParam, $orderNo, $payTitle, $payAmount); // 返回支付参数 return json_decode($result['rc_Result'], true); } elseif (is_array($result) && isset($result['rb_CodeMsg'])) { @@ -133,33 +130,20 @@ class JoinPaymentService extends PaymentService */ public function notify(string $param = ''): string { - if (is_numeric(stripos($param, '-'))) { - [$payType, $payId] = explode('-', $param); - } else { - [$payType, $payId] = [$param ?: 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)) { return 'error'; } if (isset($notify['r6_Status']) && intval($notify['r6_Status']) === 100) { - // 更新支付记录 - data_save('DataPaymentItem', [ - 'order_no' => $notify['r2_OrderNo'], - '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_id' => $payId, 'payment_type' => $payType, 'payment_status' => 0]); - // 更新记录状态 - if ($this->updateOrder($notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'], $payType)) { + if ($this->updatePaymentAction($param, $notify['r2_OrderNo'], $notify['r9_BankTrxNo'], $notify['r3_Amount'])) { return 'success'; + } else { + return 'error'; } + } else { + return 'success'; } - return 'error'; } /** diff --git a/app/data/service/payment/WechatPaymentService.php b/app/data/service/payment/WechatPaymentService.php index 4024c8bed..456ad2a6b 100644 --- a/app/data/service/payment/WechatPaymentService.php +++ b/app/data/service/payment/WechatPaymentService.php @@ -33,18 +33,6 @@ class WechatPaymentService extends PaymentService return $this; } - /** - * 查询微信支付订单 - * @param string $orderNo - * @return array - * @throws \WeChat\Exceptions\InvalidResponseException - * @throws \WeChat\Exceptions\LocalCacheException - */ - public function query(string $orderNo): array - { - return $this->payment->query(['out_trade_no' => $orderNo]); - } - /** * 创建微信支付订单 * @param string $openid 会员OPENID @@ -69,6 +57,7 @@ class WechatPaymentService extends PaymentService $data = [ 'body' => $body, 'openid' => $openid, + 'attach' => $tradeParam, 'out_trade_no' => $orderNo, 'total_fee' => $payAmount * 100, 'trade_type' => $tradeType ?: '', @@ -79,10 +68,7 @@ class WechatPaymentService extends PaymentService $info = $this->payment->create($data); 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_id' => static::$id, 'payment_type' => static::$type, - ]); + $this->createPaymentAction($tradeParam, $orderNo, $payTitle, $payAmount); // 返回支付参数 return $this->payment->jsapiParams($info['prepay_id']); } @@ -98,6 +84,27 @@ class WechatPaymentService extends PaymentService } } + /** + * 查询微信支付订单 + * @param string $orderNo 订单单号 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function query(string $orderNo): array + { + $result = $this->payment->query(['out_trade_no' => $orderNo]); + if (isset($result['return_code']) && isset($result['result_code']) && isset($result['attach'])) { + if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { + $this->updatePaymentAction($result['attach'], $result['out_trade_no'], $result['cash_fee'] / 100, $result['transaction_id']); + } + } + return $result; + } + /** * 支付结果处理 * @param string $param 支付通道 @@ -109,26 +116,12 @@ class WechatPaymentService extends PaymentService */ public function notify(string $param = ''): string { - if (is_numeric(stripos($param, '-'))) { - [$payType, $payId] = explode('-', $param); - } else { - [$payType, $payId] = [$param ?: 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_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_id' => $payId, 'payment_type' => $payType, 'payment_status' => 0]); - // 更新记录状态 - if ($this->updateOrder($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100, $payType)) { + if ($this->updatePaymentAction($param, $notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100)) { return $this->payment->getNotifySuccessReply(); + } else { + return 'error'; } } else { return $this->payment->getNotifySuccessReply();