diff --git a/serve/app/Http/Controllers/Api/AuthController.php b/serve/app/Http/Controllers/Api/AuthController.php index bbedc55..05f07fa 100644 --- a/serve/app/Http/Controllers/Api/AuthController.php +++ b/serve/app/Http/Controllers/Api/AuthController.php @@ -127,13 +127,6 @@ class AuthController extends Controller $user = $request->user('api'); $vip = VipPackage::query()->findOrFail($request->input('vip_id')); - if (SystemConfig::get('wechat_pay_app_id') == null || - SystemConfig::get('wechat_pay_mch_id') == null || - SystemConfig::get('wechat_pay_secret_key') == null || - SystemConfig::get('wechat_pay_secret_key') == null || - SystemConfig::get('wechat_pay_certificate') == null) { - return $this->failed('清设置支付信息'); - } try { $res = Payment::pay( channel: WeChatPayNative::class, @@ -151,7 +144,7 @@ class AuthController extends Controller } catch (\Exception $e) { info($e->getMessage()); - return $this->failed('支付失败!'); + return $this->failed('支付失败!'.$e->getMessage()); } } } diff --git a/serve/app/PayChannels/WeChatPayNative.php b/serve/app/PayChannels/WeChatPayNative.php index c419743..44a9418 100644 --- a/serve/app/PayChannels/WeChatPayNative.php +++ b/serve/app/PayChannels/WeChatPayNative.php @@ -16,22 +16,40 @@ class WeChatPayNative private Application $payApp; + private $appid; + private $mchid; + public function __construct() { - $config = [ - 'appid' => SystemConfig::get('wechat_pay_app_id'), - 'mch_id' => SystemConfig::get('wechat_pay_mch_id'), - 'secret_key' => SystemConfig::get('wechat_pay_secret_key'), - // 商户证书 - 'private_key' => SystemConfig::get('wechat_pay_secret_key'), - 'certificate' => SystemConfig::get('wechat_pay_certificate'), - /*'platform_certs' => [ - storage_path('/certs/wechatpay.pem'), - ],*/ - 'http' => [ - 'throw' => false, - ], - ]; + $this->appid = SystemConfig::get('wechat_pay_app_id'); + $this->mchid = SystemConfig::get('wechat_pay_mch_id'); + + if (!empty($this->appid) && !empty(!$this->mchid)) { + $config = [ + 'appid' => $this->appid, + 'mch_id' => $this->mchid, + 'secret_key' => SystemConfig::get('wechat_pay_secret_key'), + // 商户证书 + 'private_key' => SystemConfig::get('wechat_pay_secret_key'), + 'certificate' => SystemConfig::get('wechat_pay_certificate'), + /*'platform_certs' => [ + storage_path('/certs/wechatpay.pem'), + ],*/ + 'http' => [ + 'throw' => false, + ], + ]; + } else { + $config = config('services.wechat_pay'); + unset($config['platform_certs']); + $this->appid = $config['appid']; + $this->mchid = $config['mch_id']; + } + + if (empty($this->appid) || empty($this->mchid)) { + throw new ApiCustomError('请完善支付配置信息!'); + } + $this->payApp = new Application($config); } @@ -39,8 +57,8 @@ class WeChatPayNative public function pay($payment, array $data = []): array { $payInfo = $this->payApp->getClient()->postJson('v3/pay/transactions/native', [ - 'appid' => config('services.wechat_pay.appid'), - 'mchid' => config('services.wechat_pay.mch_id'), + 'appid' => $this->appid, + 'mchid' => $this->mchid, 'description' => data_get($data, 'description', ''), 'out_trade_no' => $payment->no, 'notify_url' => config('app.url').'/api/wechat/payment_notify',