Pārlūkot izejas kodu

Merge branch 'dev' of http://gogs.hnwmzp.cn/chengwenliang/nshop into dev

chengwl 1 nedēļu atpakaļ
vecāks
revīzija
521aa4aaaa

+ 7 - 25
packages/Longyi/Pay/Afterpay/src/Payment/Afterpay.php

@@ -144,19 +144,15 @@ class Afterpay extends Payment
             'verify' => false
         ]);
         $resultObject = json_decode($response->getBody()->getContents());
-        if ($resultObject->token == 'APPROVED') {
-            return true;
+        if ($resultObject->status == 'APPROVED') {
+            return ['transaction_id' => $resultObject->token, 'amount' => $resultObject->events[0]->amount->amount];
         } else {
-            return false;
+            return [];
         }
     }
 
-    public function paymentsSuccess($orderId, $transactionId)
+    public function captureAndVerify($order, $transactionId)
     {
-        $order = app(OrderRepository::class)->find($orderId);
-        if (!$order->id) {
-            return new \Exception('order is empty');
-        }
         if ($order->status != Order::STATUS_PENDING) {
             return new \Exception('order status is error');
         }
@@ -166,17 +162,7 @@ class Afterpay extends Payment
         if ($transactionId != $token) {
             return new \Exception('order token is error');
         }
-        if (!$this->capture($token)) {
-            return new \Exception('order capture fail');
-        }
-        $order->status = Order::STATUS_PROCESSING;
-        $order->save();
-        $additional['transaction_id'] = $transactionId;
-        if ($order->payment) {
-            $order->payment->additional = $additional;
-            $order->payment->save();
-        }
-        return true;
+        return $this->capture($token);
     }
 
     public function getBaseUlr()
@@ -208,12 +194,8 @@ class Afterpay extends Payment
         if (! parent::isAvailable()) {
             return false;
         }
-        $cart = Cart::getCart();
-        if (! $cart) {
-            return false;
-        }
-        if ($cart->billing_address) {
-            $billingCountry = $cart->billing_address->country;
+        $info = $this->getOrder() ?: Cart::getCart();
+        if ($info && $billingCountry = $info->billing_address->country ?? false) {
             $allowedCountries = ['AU', 'CA', 'NZ', 'GB', 'US'];
             return in_array($billingCountry, $allowedCountries);
         }

+ 10 - 12
packages/Longyi/Pay/Airwallex/src/Payment/Airwallex.php

@@ -164,9 +164,10 @@ class Airwallex extends Payment
         $order->status = Order::STATUS_PROCESSING;
         $order->save();
         if ($order->payment) {
-            $order->payment->update([
-                'transaction_id' => $transactionId,
-            ]);
+            $additional = $order->payment->additional;
+            $additional['transaction_id'] = $transactionId;
+            $order->payment->additional = $additional;
+            $order->payment->save();
         }
     }
     //授权成功修改状态
@@ -183,9 +184,10 @@ class Airwallex extends Payment
         $order->status = Order::STATUS_PROCESSING;
         $order->save();
         if ($order->payment) {
-            $order->payment->update([
-                'transaction_id' => $transactionId,
-            ]);
+            $additional = $order->payment->additional;
+            $additional['transaction_id'] = $transactionId;
+            $order->payment->additional = $additional;
+            $order->payment->save();
         }
     }
 
@@ -236,12 +238,8 @@ class Airwallex extends Payment
         if (! parent::isAvailable()) {
             return false;
         }
-        $cart = Cart::getCart();
-        if (! $cart) {
-            return false;
-        }
-        if ($cart->billing_address) {
-            $billingCountry = $cart->billing_address->country;
+        $info = $this->getOrder() ?: Cart::getCart();
+        if ($info && $billingCountry = $info->billing_address->country ?? false) {
             $allowedCountries = ['AU', 'CA', 'NZ', 'GB', 'US'];
             return in_array($billingCountry, $allowedCountries);
         }

+ 2 - 6
packages/Longyi/Pay/AwxAfterpay/src/Payment/AwxAfterpay.php

@@ -300,12 +300,8 @@ class AwxAfterpay extends Payment
         if (! parent::isAvailable()) {
             return false;
         }
-        $cart = Cart::getCart();
-        if (! $cart) {
-            return false;
-        }
-        if ($cart->billing_address) {
-            $billingCountry = $cart->billing_address->country;
+        $info = $this->getOrder() ?: Cart::getCart();
+        if ($info && $billingCountry = $info->billing_address->country ?? false) {
             $allowedCountries = ['AU', 'CA', 'NZ', 'GB', 'US'];
             return in_array($billingCountry, $allowedCountries);
         }

+ 4 - 8
packages/Longyi/Pay/AwxKlarna/src/Payment/AwxKlarna.php

@@ -307,12 +307,8 @@ class AwxKlarna extends Payment
         if (! parent::isAvailable()) {
             return false;
         }
-        $cart = Cart::getCart();
-        if (! $cart) {
-            return false;
-        }
-        if ($cart->billing_address) {
-            $billingCountry = $cart->billing_address->country;
+        $info = $this->getOrder() ?: Cart::getCart();
+        if ($info && $billingCountry = $info->billing_address->country ?? false) {
             $allowedCountries = ['AU', 'AT', 'BE', 'CA', 'CZ', 'DK', 'FI', 'FR', 'DE', 'GR', 'IE',
                 'IT', 'NL', 'NO', 'PL', 'PT', 'ES', 'SE', 'CH', 'GB', 'US'
             ];
@@ -323,7 +319,7 @@ class AwxKlarna extends Payment
 
     public function getDescription()
     {
-        $cart = Cart::getCart();
-        return round($cart->grand_total / 4, 2);
+        $info = $this->getOrder() ?: Cart::getCart();
+        return round($info->grand_total / 4, 2);
     }
 }

+ 11 - 8
packages/Webkul/BagistoApi/src/Services/PaymentService.php

@@ -479,7 +479,7 @@ class PaymentService
             return (string) $gatewayOrderId;
         }
     }
-    protected function resolveGatewayHandler(string $method, $order = null, PaymentInitiateInput $input = null): ?object
+    protected function resolveGatewayHandler(string $method, $order = null, $input = null): ?object
     {
         $config = config("payment_methods.{$method}");
 
@@ -614,15 +614,18 @@ class PaymentService
                 }
 
                 $capture = null;
-
-                if ($method === 'paypal_smart_button') {
-                    if (! $gatewayOrderId) {
-                        throw new OperationFailedException(__('bagistoapi::app.graphql.payment.gateway-order-id-required'));
+                $gatewayHandler = $this->resolveGatewayHandler($method, $order, $input);
+                if ($gatewayHandler) {
+                    $capture = $gatewayHandler->captureAndVerify($order, $gatewayOrderId);
+                } else {
+                    if ($method === 'paypal_smart_button') {
+                        if (! $gatewayOrderId) {
+                            throw new OperationFailedException(__('bagistoapi::app.graphql.payment.gateway-order-id-required'));
+                        }
+
+                        $capture = $this->captureAndVerify($order, $gatewayOrderId);
                     }
-
-                    $capture = $this->captureAndVerify($order, $gatewayOrderId);
                 }
-
                 if ($isExpress) {
                     $this->fillAddressesFromCallback($order, $input);
                 }

+ 1 - 1
packages/Webkul/BagistoApi/src/State/RepayOrderPaymentMethodsProvider.php

@@ -54,7 +54,7 @@ class RepayOrderPaymentMethodsProvider implements ProviderInterface
             throw new OperationFailedException(__('bagistoapi::app.graphql.payment.not-pending', ['status' => $order->status]));
         }
 
-        $methods = Payment::getSupportedPaymentMethods();
+        $methods = Payment::getSupportedPaymentMethods($order);
 
         if ($methods && is_array($methods) && isset($methods['payment_methods'])) {
             $methodsArray = $methods['payment_methods'];

+ 7 - 4
packages/Webkul/Payment/src/Payment.php

@@ -11,10 +11,10 @@ class Payment
      *
      * @return array
      */
-    public function getSupportedPaymentMethods()
+    public function getSupportedPaymentMethods($order = null)
     {
         return [
-            'payment_methods'  => $this->getPaymentMethods(),
+            'payment_methods'  => $this->getPaymentMethods($order),
         ];
     }
 
@@ -23,13 +23,16 @@ class Payment
      *
      * @return array
      */
-    public function getPaymentMethods()
+    public function getPaymentMethods($order = null)
     {
         $paymentMethods = [];
 
         foreach (Config::get('payment_methods') as $paymentMethodConfig) {
             $paymentMethod = app($paymentMethodConfig['class']);
-
+            // 注入 order,让 isAvailable() 能用 order 信息
+            if ($order && method_exists($paymentMethod, 'setOrder')) {
+                $paymentMethod->setOrder($order);
+            }
             if ($paymentMethod->isAvailable()) {
                 $paymentMethods[] = [
                     'method'       => $paymentMethod->getCode(),