Lae/app/Providers/AppServiceProvider.php

116 lines
3.6 KiB
PHP
Raw Normal View History

2022-08-12 07:56:56 +00:00
<?php
namespace App\Providers;
2022-09-08 16:12:02 +00:00
use Illuminate\Support\ServiceProvider;
2022-09-01 09:48:29 +00:00
use Alipay\EasySDK\Kernel\Config as AlipayConfig;
use Alipay\EasySDK\Kernel\Factory as AlipayFactory;
2022-08-19 09:51:52 +00:00
use Illuminate\Support\Facades\Http;
2022-09-01 09:48:29 +00:00
use Illuminate\Support\Facades\Storage;
2022-10-28 07:43:31 +00:00
use EasyWeChat\Pay\Application as WePay;
2022-08-12 07:56:56 +00:00
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
2022-09-08 16:12:02 +00:00
require_once app()->basePath('app') . '/Helpers.php';
2022-08-12 07:56:56 +00:00
}
public function boot()
{
//
2022-08-19 09:51:52 +00:00
2022-09-09 04:56:23 +00:00
// header('server: Cluster Ready!');
// header('x-powered-by: LaeCloud');
// header('x-for-you: Code is Poetry.');
2022-09-08 16:12:02 +00:00
2022-08-19 09:51:52 +00:00
Http::macro('remote', function ($api_token, $url) {
2022-08-27 08:28:39 +00:00
// 关闭证书验证
return Http::withoutVerifying()->withHeaders([
2022-08-19 09:51:52 +00:00
'X-Remote-Api-Token' => $api_token,
2022-08-29 09:31:08 +00:00
'Content-Type' => 'application/json'
2022-10-04 10:34:23 +00:00
])->withOptions([
'version' => 2,
2022-08-19 09:51:52 +00:00
])->baseUrl($url);
});
2022-09-01 09:48:29 +00:00
AlipayFactory::setOptions($this->alipayOptions());
2022-10-28 07:43:31 +00:00
$wechat_pay_config = [
'mch_id' => config('payment.wepay.mch_id'),
// 商户证书
'private_key' => __DIR__ . '/certs/apiclient_key.pem',
'certificate' => __DIR__ . '/certs/apiclient_cert.pem',
// v3 API 秘钥
'secret_key' =>
config('payment.wepay.v3_secret_key'),
// v2 API 秘钥
'v2_secret_key' => config('payment.wepay.v2_secret_key'),
// 平台证书:微信支付 APIv3 平台证书,需要使用工具下载
// 下载工具https://github.com/wechatpay-apiv3/CertificateDownloader
'platform_certs' => [
// '/path/to/wechatpay/cert.pem',
],
/**
* 接口请求相关配置,超时时间等,具体可用参数请参考:
* https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php
*/
'http' => [
'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
'timeout' => 5.0,
],
];
$app = new WePay($wechat_pay_config);
// mount app to global
app()->instance('wepay', $app);
2022-09-01 09:48:29 +00:00
}
private function alipayOptions()
{
$options = new AlipayConfig();
$options->protocol = 'https';
2022-09-05 17:05:46 +00:00
// if local
if (app()->environment() == 'local') {
$options->gatewayHost = 'openapi.alipaydev.com';
} else {
$options->gatewayHost = 'openapi.alipay.com';
}
2022-09-01 09:48:29 +00:00
$options->signType = 'RSA2';
2022-10-28 07:43:31 +00:00
$options->appId = config('payment.alipay.app_id');
2022-09-01 09:48:29 +00:00
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
$options->merchantPrivateKey = trim(Storage::get('alipayAppPriv.key'));
$options->alipayCertPath = storage_path('app/alipayCertPublicKey_RSA2.crt');
$options->alipayRootCertPath = storage_path('app/alipayRootCert.crt');
$options->merchantCertPath = storage_path('app/appCertPublicKey.crt');
//注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
// $options->alipayPublicKey = Storage::get('alipayCertPublicKey_RSA2.crt');
//可设置异步通知接收服务地址(可选)
2022-09-08 18:35:00 +00:00
$options->notifyUrl = route('balances.notify');
2022-09-01 09:48:29 +00:00
return $options;
2022-08-12 07:56:56 +00:00
}
}