diff --git a/app/data/service/PaymentService.php b/app/data/service/PaymentService.php index 3796ba6eb..7d6a0aee8 100644 --- a/app/data/service/PaymentService.php +++ b/app/data/service/PaymentService.php @@ -104,7 +104,7 @@ abstract class PaymentService extends Service * 当前支付通道 * @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 * @throws \think\Exception */ - public static function build(string $paycode): PaymentService + public static function build(string $code): PaymentService { - static::$code = $paycode; - if (isset(static::$driver[$paycode])) { - return static::$driver[$paycode]; + [static::$code, static::$type, static::$params] = self::config($code); + if (isset(static::$driver[$code])) return static::$driver[$code]; + // 实例化具体支付通道类型 + 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(); if (empty($payment)) { - throw new \think\Exception("支付通道[#{$paycode}]已关闭"); + throw new \think\Exception("支付通道[#{$code}]禁用关闭"); } - static::$config = @json_decode($payment['content'], true); - if (empty(static::$config)) { - throw new \think\Exception("支付通道[#{$paycode}]配置无效"); + $params = @json_decode($payment['content'], true); + if (empty($params)) { + throw new \think\Exception("支付通道[#{$code}]配置无效"); } - // 支付通道类型验证 if (empty(static::TYPES[$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']}]未定义"); + throw new \think\Exception("支付通道[@{$payment['type']}]匹配失败"); } + return [$payment['code'], $payment['type'], $params]; } /** diff --git a/app/data/service/payment/AlipayPaymentService.php b/app/data/service/payment/AlipayPaymentService.php index 1556bf654..c36a43878 100644 --- a/app/data/service/payment/AlipayPaymentService.php +++ b/app/data/service/payment/AlipayPaymentService.php @@ -30,11 +30,11 @@ class AlipayPaymentService extends PaymentService // 签名类型(RSA|RSA2) 'sign_type' => "RSA2", // 应用ID - 'appid' => static::$config['alipay_appid'], + 'appid' => static::$params['alipay_appid'], // 支付宝公钥 (1行填写,特别注意,这里是支付宝公钥,不是应用公钥,最好从开发者中心的网页上去复制) - 'public_key' => $this->_trimCertHeader(static::$config['alipay_public_key']), + 'public_key' => $this->_trimCertHeader(static::$params['alipay_public_key']), // 支付宝私钥 (1行填写) - 'private_key' => $this->_trimCertHeader(static::$config['alipay_private_key']), + 'private_key' => $this->_trimCertHeader(static::$params['alipay_private_key']), // 应用公钥证书(新版资金类接口转 app_cert_sn) # 'app_cert' => '', // 支付宝根证书(新版资金类接口转 alipay_root_cert_sn) diff --git a/app/data/service/payment/JoinPaymentService.php b/app/data/service/payment/JoinPaymentService.php index 772d87e8d..36e893ffe 100644 --- a/app/data/service/payment/JoinPaymentService.php +++ b/app/data/service/payment/JoinPaymentService.php @@ -48,10 +48,10 @@ class JoinPaymentService extends PaymentService */ protected function initialize(): JoinPaymentService { - $this->appid = static::$config['joinpay_appid']; - $this->trade = static::$config['joinpay_trade'];; - $this->mchid = static::$config['joinpay_mch_id']; - $this->mchkey = static::$config['joinpay_mch_key']; + $this->appid = static::$params['joinpay_appid']; + $this->trade = static::$params['joinpay_trade'];; + $this->mchid = static::$params['joinpay_mch_id']; + $this->mchkey = static::$params['joinpay_mch_key']; return $this; } diff --git a/app/data/service/payment/WechatPaymentService.php b/app/data/service/payment/WechatPaymentService.php index a7de045ee..22924e2f6 100644 --- a/app/data/service/payment/WechatPaymentService.php +++ b/app/data/service/payment/WechatPaymentService.php @@ -25,9 +25,9 @@ class WechatPaymentService extends PaymentService protected function initialize(): WechatPaymentService { $this->payment = Order::instance([ - 'appid' => static::$config['wechat_appid'], - 'mch_id' => static::$config['wechat_mch_id'], - 'mch_key' => static::$config['wechat_mch_key'], + 'appid' => static::$params['wechat_appid'], + 'mch_id' => static::$params['wechat_mch_id'], + 'mch_key' => static::$params['wechat_mch_key'], 'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat', ]); return $this;