文件
Henglianshutong/addons/shopro/library/pay/provider/Base.php

227 行
7.7 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace addons\shopro\library\pay\provider;
use think\Log;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Contract\HttpClientInterface;
use addons\shopro\facade\HttpClient;
use app\admin\model\shopro\PayConfig;
class Base
{
/**
* yansongda 支付示例
*
* @var Yansongda\Pay\Pay
*/
protected $pay = null; // yansongda 支付实例
public $config = null; // 支付参数
/**
* 确保 Yansongda Pay V3 类库与命名空间正确加载
*/
public static function ensurePayLoaded()
{
if (class_exists('\Yansongda\Pay\Pay') && method_exists('\Yansongda\Pay\Pay', 'config')) {
return;
}
// 尝试触发 epay 钩子
\think\Hook::listen('epay_config_init');
if (class_exists('\Yansongda\Pay\Pay') && method_exists('\Yansongda\Pay\Pay', 'config')) {
return;
}
// 保障性直接注册 epay 插件中的 Yansongda V3 类库
$libraryDir = (defined('ADDON_PATH') ? ADDON_PATH : (ROOT_PATH . 'addons' . DS)) . 'epay' . DS . 'library' . DS;
if (is_dir($libraryDir)) {
$versionDir = (PHP_VERSION_ID >= 80000 && is_dir($libraryDir . 'v3new')) ? 'v3new' : 'v3';
$yansongdaDir = $libraryDir . $versionDir . DS . 'Yansongda' . DS;
if (is_dir($yansongdaDir)) {
if (is_dir($yansongdaDir . 'Artful')) {
\think\Loader::addNamespace('Yansongda\Artful', $yansongdaDir . 'Artful' . DS);
if (file_exists($yansongdaDir . 'Artful' . DS . 'Functions.php')) {
require_once $yansongdaDir . 'Artful' . DS . 'Functions.php';
}
}
\think\Loader::addNamespace('Yansongda\Pay', $yansongdaDir . 'Pay' . DS);
$checkArr = [
'\Hyperf\Context\Context' => 'context',
'\Hyperf\Contract\Castable' => 'contract',
'\Hyperf\Engine\Constant' => 'engine',
'\Hyperf\Macroable\Macroable' => 'macroable',
'\Hyperf\Pimple\Container' => 'pimple',
'\Hyperf\Utils\Arr' => 'utils',
];
foreach ($checkArr as $index => $item) {
if (!class_exists($index) && is_dir($libraryDir . 'hyperf' . DS . $item . DS . 'src' . DS)) {
\think\Loader::addNamespace(substr($index, 1, strrpos($index, '\\') - 1), $libraryDir . 'hyperf' . DS . $item . DS . 'src' . DS);
}
}
if (!class_exists('\Yansongda\Supports\Logger') && is_dir($yansongdaDir . 'Supports')) {
\think\Loader::addNamespace('Yansongda\Supports', $yansongdaDir . 'Supports' . DS);
}
if (file_exists($yansongdaDir . 'Pay' . DS . 'Functions.php')) {
require_once $yansongdaDir . 'Pay' . DS . 'Functions.php';
}
}
}
}
/**
* yansongda 支付初始化
*
* @param string $payment
* @param array $config
* @return Yansongda\Pay\Pay
*/
public function init($payment, $config = [], $type = 'normal')
{
self::ensurePayLoaded();
$this->config = $this->getConfig($payment, $config, $type);
$this->pay = Pay::config($this->config);
Pay::set(HttpClientInterface::class, HttpClient::instance()); // 使用自定义 client (也继承至 GuzzleHttp\Client)
}
/**
* 获取支付的所有参数
*
* @param string $payment
* @return array
*/
protected function getConfig($payment, $config = [], $type = 'normal')
{
// 获取平台配置
$platformConfig = $this->getPlatformConfig();
extract($platformConfig);
$params = $this->getPayConfig($payment, $paymentConfig);
// 格式化支付参数
$params['mode'] = (int)($params['mode'] ?? 0);
$params = $this->formatConfig($params, ['app_id' => $app_id], $type);
// 合并传入的参数
$params = array_merge($params, $config);
// 合并参数
$config = $this->baseConfig();
$config = array_merge($config, [$payment => ['default' => $params]]);
return $config;
}
/**
* 获取平台配置参数
*
* @return array
*/
protected function getPlatformConfig()
{
$platformConfig = sheep_config('shop.platform.' . $this->platform);
$paymentConfig = $platformConfig['payment'] ?? [];
$app_id = $platformConfig['app_id'] ?? '';
return compact('paymentConfig', 'app_id');
}
/**
* 根据平台以及支付方式 获取支付配置表的配置参数
*
* @param string $payment
* @return array
*/
protected function getPayConfig($payment, $paymentConfig)
{
$methods = $paymentConfig['methods'];
$payment_config = $paymentConfig[$payment] ?? 0;
if (!in_array($payment, $methods)) {
error_stop('当前平台未开启该支付方式');
}
if ($payment_config) {
$payConfig = PayConfig::normal()->where('type', $payment)->find($payment_config);
}
if (!isset($payConfig) || !$payConfig) {
error_stop('支付配置参数不存在');
}
return $payConfig->params;
}
/**
* 获取对应的支付方法名
*
* @param strign $payment
* @return string
*/
protected function getMethod($payment)
{
$method = [
'wechat' => [
'WechatOfficialAccount' => 'mp', //公众号支付 Collection
'WechatMiniProgram' => 'mini', //小程序支付 Collection
'H5' => 'h5', //手机网站支付 Response // php = 8, epay = 3 时,使用的是 epay v3new, H5 =》 h5,之前版本为 H5 =》 wap
'App' => 'app' // APP 支付 JsonResponse
],
'alipay' => [
'WechatOfficialAccount' => 'h5', //手机网站支付 Response
'WechatMiniProgram' => 'h5', //小程序支付
'H5' => 'h5', //手机网站支付 Response // php = 8, epay = 3 时,使用的是 epay v3new, H5 =》 h5,之前版本为 H5 =》 wap
'App' => 'app' //APP 支付 JsonResponse
],
];
return $method[$payment][$this->platform];
}
/**
* yansongda 基础配置
*
* @return void
*/
protected function baseConfig()
{
$log_path = RUNTIME_PATH . 'log/pay/';
if (!is_dir($log_path)) {
@mkdir($log_path, 0755, true);
}
return [
'logger' => [ // optional
'enable' => true,
'file' => $log_path . 'pay.log',
'level' => config('app_debug') ? 'debug' : 'info', // 建议生产环境等级调整为 info,开发环境为 debug
'type' => 'daily', // optional, 可选 daily.
'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
],
'http' => [ // optional
'timeout' => 5.0,
'connect_timeout' => 5.0,
// 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
],
];
}
}