修改支付通道,增加外部获取配置

This commit is contained in:
Anyon 2021-01-08 14:34:12 +08:00
parent 62c4b6a054
commit c4943b28ce
4 changed files with 44 additions and 35 deletions

View File

@ -104,7 +104,7 @@ abstract class PaymentService extends Service
* 当前支付通道 * 当前支付通道
* @var array * @var array
*/ */
protected static $config; protected static $params;
/** /**
* 支付服务对象 * 支付服务对象
@ -114,41 +114,50 @@ abstract class PaymentService extends Service
/** /**
* 根据配置实例支付服务 * 根据配置实例支付服务
* @param string $paycode 支付通道编号 * @param string $code 支付通道编号
* @return JoinPaymentService|WechatPaymentService|AlipayPaymentService * @return JoinPaymentService|WechatPaymentService|AlipayPaymentService
* @throws \think\Exception * @throws \think\Exception
*/ */
public static function build(string $paycode): PaymentService public static function build(string $code): PaymentService
{ {
static::$code = $paycode; [static::$code, static::$type, static::$params] = self::config($code);
if (isset(static::$driver[$paycode])) { if (isset(static::$driver[$code])) return static::$driver[$code];
return static::$driver[$paycode]; // 实例化具体支付通道类型
if (stripos(static::$type, 'alipay_') === 0) {
return static::$driver[$code] = AlipayPaymentService::instance();
} elseif (stripos(static::$type, 'wechat_') === 0) {
return static::$driver[$code] = WechatPaymentService::instance();
} elseif (stripos(static::$type, 'joinpay_') === 0) {
return static::$driver[$code] = JoinPaymentService::instance();
} else {
throw new \think\Exception(sprintf('支付驱动[%s]未定义', static::$type));
} }
// 支付通道配置验证 }
$map = ['code' => $paycode, 'status' => 1, 'deleted' => 0];
/**
* 根据通道编号获取配置参数
* @param string $code
* @return array [code,type,params]
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function config(string $code): array
{
$map = ['code' => $code, 'status' => 1, 'deleted' => 0];
$payment = app()->db->name('DataPayment')->where($map)->find(); $payment = app()->db->name('DataPayment')->where($map)->find();
if (empty($payment)) { if (empty($payment)) {
throw new \think\Exception("支付通道[#{$paycode}]已关闭"); throw new \think\Exception("支付通道[#{$code}]禁用关闭");
} }
static::$config = @json_decode($payment['content'], true); $params = @json_decode($payment['content'], true);
if (empty(static::$config)) { if (empty($params)) {
throw new \think\Exception("支付通道[#{$paycode}]配置无效"); throw new \think\Exception("支付通道[#{$code}]配置无效");
} }
// 支付通道类型验证
if (empty(static::TYPES[$payment['type']])) { if (empty(static::TYPES[$payment['type']])) {
throw new \think\Exception("支付通道[{$payment['type']}]未定义"); throw new \think\Exception("支付通道[@{$payment['type']}]匹配失败");
}
// 实例化具体支付通道类型
static::$type = $payment['type'];
if (stripos(static::$type, 'alipay_') === 0) {
return static::$driver[$paycode] = AlipayPaymentService::instance();
} elseif (stripos(static::$type, 'wechat_') === 0) {
return static::$driver[$paycode] = WechatPaymentService::instance();
} elseif (stripos(static::$type, 'joinpay_') === 0) {
return static::$driver[$paycode] = JoinPaymentService::instance();
} else {
throw new \think\Exception("支付驱动[{$payment['type']}]未定义");
} }
return [$payment['code'], $payment['type'], $params];
} }
/** /**

View File

@ -30,11 +30,11 @@ class AlipayPaymentService extends PaymentService
// 签名类型RSA|RSA2 // 签名类型RSA|RSA2
'sign_type' => "RSA2", 'sign_type' => "RSA2",
// 应用ID // 应用ID
'appid' => static::$config['alipay_appid'], 'appid' => static::$params['alipay_appid'],
// 支付宝公钥 (1行填写特别注意这里是支付宝公钥不是应用公钥最好从开发者中心的网页上去复制) // 支付宝公钥 (1行填写特别注意这里是支付宝公钥不是应用公钥最好从开发者中心的网页上去复制)
'public_key' => $this->_trimCertHeader(static::$config['alipay_public_key']), 'public_key' => $this->_trimCertHeader(static::$params['alipay_public_key']),
// 支付宝私钥 (1行填写) // 支付宝私钥 (1行填写)
'private_key' => $this->_trimCertHeader(static::$config['alipay_private_key']), 'private_key' => $this->_trimCertHeader(static::$params['alipay_private_key']),
// 应用公钥证书(新版资金类接口转 app_cert_sn // 应用公钥证书(新版资金类接口转 app_cert_sn
# 'app_cert' => '', # 'app_cert' => '',
// 支付宝根证书(新版资金类接口转 alipay_root_cert_sn // 支付宝根证书(新版资金类接口转 alipay_root_cert_sn

View File

@ -48,10 +48,10 @@ class JoinPaymentService extends PaymentService
*/ */
protected function initialize(): JoinPaymentService protected function initialize(): JoinPaymentService
{ {
$this->appid = static::$config['joinpay_appid']; $this->appid = static::$params['joinpay_appid'];
$this->trade = static::$config['joinpay_trade'];; $this->trade = static::$params['joinpay_trade'];;
$this->mchid = static::$config['joinpay_mch_id']; $this->mchid = static::$params['joinpay_mch_id'];
$this->mchkey = static::$config['joinpay_mch_key']; $this->mchkey = static::$params['joinpay_mch_key'];
return $this; return $this;
} }

View File

@ -25,9 +25,9 @@ class WechatPaymentService extends PaymentService
protected function initialize(): WechatPaymentService protected function initialize(): WechatPaymentService
{ {
$this->payment = Order::instance([ $this->payment = Order::instance([
'appid' => static::$config['wechat_appid'], 'appid' => static::$params['wechat_appid'],
'mch_id' => static::$config['wechat_mch_id'], 'mch_id' => static::$params['wechat_mch_id'],
'mch_key' => static::$config['wechat_mch_key'], 'mch_key' => static::$params['wechat_mch_key'],
'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat', 'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat',
]); ]);
return $this; return $this;