From c002f70ed99c41728bc3edfd3e6e4b6750504b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Tue, 4 Jan 2022 20:20:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=AE=9D=E9=80=80=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/OrderRefundCommand.php | 24 ++++++++++++++ app/Services/AlipayService.php | 35 +++++++++++++++++++-- app/Services/PayService.php | 4 ++- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/OrderRefundCommand.php b/app/Console/Commands/OrderRefundCommand.php index fcd7745e..74cde2cf 100644 --- a/app/Console/Commands/OrderRefundCommand.php +++ b/app/Console/Commands/OrderRefundCommand.php @@ -6,6 +6,7 @@ use App\Exceptions\BizException; use App\Models\BalanceLog; use App\Models\OrderRefundLog; use App\Models\WalletLog; +use App\Services\AlipayService; use App\Services\BalanceService; use App\Services\WalletService; use App\Services\WeChatPayService; @@ -87,6 +88,29 @@ class OrderRefundCommand extends Command ]); } + /** + * 支付宝退款 + * + * @param \App\Models\OrderRefundLog $log + * @return void + */ + protected function refundByAlipay(OrderRefundLog $log) + { + $order = $log->order; + + (new AlipayService())->refund( + $order->pay_sn, + $log->sn, + bcdiv($log->amount, 100, 2), + $log->reason, + ); + + $log->update([ + 'status' => OrderRefundLog::STATUS_SUCCESS, + 'failed_reason' => null, + ]); + } + /** * 可提支付退款 * diff --git a/app/Services/AlipayService.php b/app/Services/AlipayService.php index d0667cd4..339e0f71 100644 --- a/app/Services/AlipayService.php +++ b/app/Services/AlipayService.php @@ -5,8 +5,10 @@ namespace App\Services; use Alipay\EasySDK\Kernel\CertEnvironment; use Alipay\EasySDK\Kernel\Config; use Alipay\EasySDK\Kernel\EasySDKKernel; +use Alipay\EasySDK\Kernel\Util\ResponseChecker; use Alipay\EasySDK\Payment\App\Client as AppClient; use Alipay\EasySDK\Payment\Common\Client as CommonClient; +use App\Exceptions\BizException; class AlipayService { @@ -20,17 +22,44 @@ class AlipayService { $kernel = $this->getKernel(); - $result = (new AppClient($kernel))->pay( + $response = (new AppClient($kernel))->pay( $params['subject'], $params['out_trade_no'], $params['total_amount'], ); return [ - 'body' => $result->body, + 'body' => $response->body, ]; } + /** + * 退款 + * + * @param string $outTradeNo + * @param string $refundSerialNumber + * @param float $refundAmount + * @param string $refundReason + * @return array + */ + public function refund( + string $outTradeNo, + string $refundSerialNumber, + $refundAmount, + ?string $refundReason = null + ) { + $kernel = $this->getKernel(); + + $response = (new CommonClient($kernel)) + ->optional('out_request_no', $refundSerialNumber) + ->optional('refund_reason', $refundReason) + ->refund($outTradeNo, $refundAmount); + + if (! (new ResponseChecker())->success($response)) { + throw new BizException($response->msg.','.$response->subMsg); + } + } + /** * 验证回调通知 * @@ -72,7 +101,7 @@ class AlipayService $cfg->merchantCertPath = $configs['app_public_cert_path']; $cfg->alipayCertPath = $configs['public_cert_path']; $cfg->alipayRootCertPath = $configs['root_cert_path']; - $cfg->notifyUrl = $configs['notify_url'] ?? url(route('alipay.notify', [], false), [], true); + $cfg->notifyUrl = 'https://zichunsheng.cn/callback/alipay' ?? url(route('alipay.notify', [], false), [], true); $certEnvironment = new CertEnvironment(); $certEnvironment->certEnvironment( diff --git a/app/Services/PayService.php b/app/Services/PayService.php index 6689b521..97564489 100644 --- a/app/Services/PayService.php +++ b/app/Services/PayService.php @@ -76,6 +76,9 @@ class PayService } elseif ($payLog->isBalance()) { // 余额支付 $payWay = Order::PAY_WAY_BALANCE; + } elseif ($payLog->isAlipay()) { + // 支付宝 + $payWay = Order::PAY_WAY_ALIPAY; } $payable->update([ @@ -84,7 +87,6 @@ class PayService 'pay_at' => $payLog->pay_at, 'out_trade_no' => $payLog->out_trade_no, 'status' => Order::STATUS_PAID, - 'auto_complete_at' => now()->addDays(app_settings('app.order_auto_complete_days', 7)), ]); // 增加用户的预成长值