*/ protected $payments = []; /** * 支付 * * @param array $params * @return array */ public function pay(array $params, string $paymentName = null): array { if (! isset($params['notify_url'])) { $params['notify_url'] = url(route('wxpay.paid_notify', ['payment' => $paymentName], false), [], true); } if (! isset($params['trade_type'])) { $params['trade_type'] = WxpayTradeType::App->value; } $app = $this->payment($paymentName); $result = $app->order->unify($params); $this->validateResult($result, $params); return match ($params['trade_type']) { WxpayTradeType::App->value => $app->jssdk->appConfig($result['prepay_id']), WxpayTradeType::H5->value => Arr::only($result, ['mweb_url']), default => $app->jssdk->bridgeConfig($result['prepay_id'], false), }; } /** * @param string|null $name * @return \EasyWeChat\Payment\Application */ public function payment(?string $name = null): Application { $name = $name ?: 'default'; return $this->payments[$name] = $this->get($name); } /** * @param string $name * @return \EasyWeChat\Payment\Application */ protected function get(string $name): Application { return $this->payments[$name] ?? $this->resolve($name); } /** * @param string $name * @return \EasyWeChat\Payment\Application * * @throws \App\Exceptions\WeChatPayException */ protected function resolve(string $name): Application { $config = config("wechat.payment.{$name}"); if (empty($config)) { throw new WeChatPayException("支付 [{$name}] 配置未找到"); } return Factory::payment($config); } /** * 校验响应结果 * * @param array $result * @param array $raw * @return void * * @throws \App\Exceptions\WeChatPayException */ protected function validateResult(array $result, array $raw = []) { if (Arr::get($result, 'return_code') !== 'SUCCESS') { throw new WeChatPayException(Arr::get($result, 'return_msg', '请求失败'), $raw); } if (Arr::get($result, 'result_code') !== 'SUCCESS') { throw new WeChatPayException(sprintf( '[%s] %s', Arr::get($result, 'err_code', '-1'), Arr::get($result, 'err_code_des', '结果异常') ), $raw); } } }