76 lines
2.0 KiB
PHP
76 lines
2.0 KiB
PHP
<?php
|
|
|
|
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
|
|
{
|
|
/**
|
|
* 支付
|
|
*
|
|
* @param array $params
|
|
* @return array
|
|
*/
|
|
public function pay(array $params)
|
|
{
|
|
$result = $this->app()->pay(
|
|
$params['subject'],
|
|
$params['out_trade_no'],
|
|
$params['total_amount'],
|
|
);
|
|
|
|
return [
|
|
'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;
|
|
}
|
|
}
|