From bb752083f335edc6aa96ad903100d37d1372f8e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Tue, 4 Jan 2022 17:37:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=AE=9D-app=E6=94=AF?= =?UTF-8?q?=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Providers/AppServiceProvider.php | 28 -------------- app/Services/AlipayService.php | 56 ++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 9ba85efe..28280dd0 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,8 +2,6 @@ namespace App\Providers; -use Alipay\EasySDK\Kernel\Config as AlipayConfig; -use Alipay\EasySDK\Kernel\EasySDKKernel as AlipayKernel; use App\Services\SettingService; use EasyWeChat\Factory as EasyWeChatFactory; use EasyWeChat\Payment\Application as EasyWeChatPaymentApplication; @@ -26,7 +24,6 @@ class AppServiceProvider extends ServiceProvider $this->registerEasyWeChat(); $this->registerRequestRealIp(); $this->registerSettings(); - $this->registerAlipay(); } /** @@ -98,29 +95,4 @@ class AppServiceProvider extends ServiceProvider return new SettingService($app['cache.store']); }); } - - /** - * 注册应用支付宝服务 - * - * @return void - */ - protected function registerAlipay() - { - $this->app->singleton(AlipayKernel::class, function ($app) { - $alipayConfigs = $app['config']->get('alipay'); - - $config = new AlipayConfig(); - $config->protocol = 'https'; - $config->gatewayHost = 'openapi.alipay.com'; - $config->signType = 'RSA2'; - $config->appId = $alipayConfigs['app_id']; - $config->merchantPrivateKey = $alipayConfigs['app_private_key']; - $config->merchantCertPath = $alipayConfigs['app_public_cert_path']; - $config->alipayCertPath = $alipayConfigs['public_cert_path']; - $config->alipayRootCertPath = $alipayConfigs['root_cert_path']; - $config->notifyUrl = $alipayConfigs['notify_url'] ?? url(route('alipay.paid_notify', [], false), [], true); - - return new AlipayKernel($config); - }); - } } diff --git a/app/Services/AlipayService.php b/app/Services/AlipayService.php index 4e1d5a0c..badd6b16 100644 --- a/app/Services/AlipayService.php +++ b/app/Services/AlipayService.php @@ -2,14 +2,17 @@ namespace App\Services; +use AlibabaCloud\Client\Config\Config; +use Alipay\EasySDK\Kernel\CertEnvironment; use Alipay\EasySDK\Kernel\EasySDKKernel; use Alipay\EasySDK\Payment\App\Client as AppClient; class AlipayService { - public function __construct( - protected EasySDKKernel $kernel, - ) { + protected $kernel; + + public function __construct() + { } /** @@ -20,7 +23,7 @@ class AlipayService */ public function pay(array $params) { - $result = (new AppClient($this->kernel))->pay( + $result = $this->app()->pay( $params['subject'], $params['out_trade_no'], $params['total_amount'], @@ -30,4 +33,49 @@ class AlipayService 'body' => $result->body, ]; } + + /** + * App 支付客户端 + * + * @return AppClient + */ + protected function app(): AppClient + { + $kernel = new EasySDKKernel($this->getConfig()); + + return new AppClient($kernel); + } + + /** + * 获取支付宝配置信息 + * + * @return \AlibabaCloud\Client\Config\Config + */ + protected function getConfig(): Config + { + $configs = config('alipay'); + + $cfg = new Config(); + $cfg->protocol = 'https'; + $cfg->gatewayHost = 'openapi.alipay.com'; + $cfg->signType = 'RSA2'; + $cfg->appId = $configs['app_id']; + $cfg->merchantPrivateKey = $configs['app_private_key']; + $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.paid_notify', [], false), [], true); + + $certEnvironment = new CertEnvironment(); + $certEnvironment->certEnvironment( + $cfg->merchantCertPath, + $cfg->alipayCertPath, + $cfg->alipayRootCertPath + ); + $cfg->merchantCertSN = $certEnvironment->getMerchantCertSN(); + $cfg->alipayRootCertSN = $certEnvironment->getRootCertSN(); + $cfg->alipayPublicKey = $certEnvironment->getCachedAlipayPublicKey(); + + return $cfg; + } }