Base.php
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace addons\groupon\library\payv3\provider;
use think\Log;
use Yansongda\Pay\Pay;
use addons\groupon\exception\Exception;
class Base
{
protected $defaultConfig = [];
/**
* yansongda 支付示例
*
* @var Yansongda\Pay\Pay
*/
protected $pay = null; // yansongda 支付实例
public $config = null; // 支付参数
/**
* yansongda 支付初始化
*
* @param string $payment
* @param array $config
* @return Yansongda\Pay\Pay
*/
public function init($payment, $config = [], $type = 'normal')
{
$this->defaultConfig = [
'logger' => [ // optional
'enable' => true,
'file' => ROOT_PATH . 'runtime/log/pay/pay.log',
'level' => config('app.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)
],
];
$this->config = $this->getConfig($payment, $config, $type);
// print_r($this->config);exit;
$this->pay = Pay::config($this->config);
}
/**
* 获取支付的所有参数
*
* @param string $payment
* @return array
*/
protected function getConfig($payment, $config = [], $type = 'normal')
{
$pay_name = $payment == 'wechat' ? 'wechatv3' : $payment;
$v3Config = \addons\groupon\model\Config::get(['name' => $pay_name]);
if (!$v3Config && $payment == 'wechat') {
throw new Exception('请配置微信支付V3参数');
}
$params = json_decode($v3Config->value, true);
$platformConfig = json_decode(\addons\groupon\model\Config::get(['name' => $this->platform])->value, true);
$app_id = $platformConfig['app_id'] ?? '';
// 格式化支付参数
$params = $this->formatConfig($params, ['app_id' => $app_id], $type);
// 合并传入的参数
$params = array_merge($params, $config);
// 合并参数
$config = array_merge($this->defaultConfig, [$payment => ['default' => $params]]);
return $config;
}
/**
* 获取对应的支付方法名
*
* @param strign $payment
* @return string
*/
protected function getMethod($payment)
{
$method = [
'wechat' => [
'wxOfficialAccount' => 'mp', //公众号支付 Collection
'wxMiniProgram' => 'mini', //小程序支付 Collection
'H5' => 'wap', //手机网站支付 Response
'App' => 'app' // APP 支付 JsonResponse
],
'alipay' => [
'wxOfficialAccount' => 'wap', //手机网站支付 Response
'wxMiniProgram' => 'wap', //小程序支付
'H5' => 'wap', //手机网站支付 Response
'App' => 'app' //APP 支付 JsonResponse
],
];
return $method[$payment][$this->platform];
}
}