|
|
@@ -29,11 +29,36 @@ class Afterpay extends Payment
|
|
|
$this->capturePaymentUlr = 'https://global-api.afterpay.com/v2/payments/capture';
|
|
|
$this->configurationUlr = 'https://global-api.afterpay.com/v2/configuration';
|
|
|
}
|
|
|
- $this->clientId = $this->getConfigData('client_id');
|
|
|
- $this->apikey = $this->getConfigData('secret_id');
|
|
|
+ $this->clientId = $this->getConfigData('client_id_us');
|
|
|
+ $this->apikey = $this->getConfigData('secret_id_us');
|
|
|
$this->secret = $this->getConfigData('webhook_id');
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据国家代码动态解析并设置 Afterpay 凭证
|
|
|
+ * 美国用 US 凭证,加拿大用 CA 凭证
|
|
|
+ */
|
|
|
+ protected function resolveCredentials(?string $countryCode = null): void
|
|
|
+ {
|
|
|
+ if ($countryCode === 'CA') {
|
|
|
+ $this->clientId = $this->getConfigData('client_id_ca');
|
|
|
+ $this->apikey = $this->getConfigData('secret_id_ca');
|
|
|
+ } else {
|
|
|
+ // 默认使用美国凭证 (US)
|
|
|
+ $this->clientId = $this->getConfigData('client_id_us');
|
|
|
+ $this->apikey = $this->getConfigData('secret_id_us');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从当前订单或购物车获取国家代码
|
|
|
+ */
|
|
|
+ protected function getCurrentCountryCode(): ?string
|
|
|
+ {
|
|
|
+ $info = $this->getOrder() ?: Cart::getCart();
|
|
|
+ return $info->billing_address->country ?? null;
|
|
|
+ }
|
|
|
+
|
|
|
protected $code = 'afterpay';
|
|
|
|
|
|
public function getRedirectUrl()
|
|
|
@@ -75,6 +100,10 @@ class Afterpay extends Payment
|
|
|
|
|
|
public function createPayment($order, $override, $input = null)
|
|
|
{
|
|
|
+ // 根据订单 billing 国家解析对应凭证
|
|
|
+ $countryCode = $order->billing_address->country ?? null;
|
|
|
+ $this->resolveCredentials($countryCode);
|
|
|
+
|
|
|
$shop = $order->shipping_address;
|
|
|
$bill = $order->billing_address;
|
|
|
$telephone = $shop->phone ?: $bill->phone;
|
|
|
@@ -187,6 +216,10 @@ class Afterpay extends Payment
|
|
|
if ($order->status != Order::STATUS_PENDING) {
|
|
|
return new \Exception('order status is error');
|
|
|
}
|
|
|
+ // 根据订单 billing 国家解析对应凭证
|
|
|
+ $countryCode = $order->billing_address->country ?? null;
|
|
|
+ $this->resolveCredentials($countryCode);
|
|
|
+
|
|
|
$additional = $order->payment->additional;
|
|
|
$token = $additional['gateway_order_id'];
|
|
|
if ($transactionId != $token) {
|
|
|
@@ -221,9 +254,15 @@ class Afterpay extends Payment
|
|
|
|
|
|
|
|
|
|
|
|
- public function fetchConfiguration(string $code = 'afterpay'): ?array
|
|
|
+ public function fetchConfiguration(string $code = 'afterpay', ?string $countryCode = null): ?array
|
|
|
{
|
|
|
try {
|
|
|
+ // 如果未指定国家,尝试从当前订单/购物车获取
|
|
|
+ if ($countryCode === null) {
|
|
|
+ $countryCode = $this->getCurrentCountryCode();
|
|
|
+ }
|
|
|
+ $this->resolveCredentials($countryCode);
|
|
|
+
|
|
|
$client = new Client();
|
|
|
$response = $client->request('GET', $this->configurationUlr, [
|
|
|
'headers' => [
|