Преглед изворни кода

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

chengwl пре 13 часа
родитељ
комит
12dbd969b2

+ 9 - 3
packages/Longyi/Pay/Afterpay/src/Payment/Afterpay.php

@@ -209,9 +209,15 @@ class Afterpay extends Payment
             return false;
         }
         $cart = Cart::getCart();
-        $billingCountry = $cart->billing_address->country;
-        $allowedCountries = ['AU', 'CA', 'NZ', 'GB', 'US'];
-        return in_array($billingCountry, $allowedCountries);
+        if (! $cart) {
+            return false;
+        }
+        if ($cart->billing_address) {
+            $billingCountry = $cart->billing_address->country;
+            $allowedCountries = ['AU', 'CA', 'NZ', 'GB', 'US'];
+            return in_array($billingCountry, $allowedCountries);
+        }
+        return true;
     }
 
     public function formatCurrencyValue($number): float

+ 9 - 3
packages/Longyi/Pay/Airwallex/src/Payment/Airwallex.php

@@ -237,8 +237,14 @@ class Airwallex extends Payment
             return false;
         }
         $cart = Cart::getCart();
-        $billingCountry = $cart->billing_address->country;
-        $allowedCountries = ['AU', 'CA', 'NZ', 'GB', 'US'];
-        return in_array($billingCountry, $allowedCountries);
+        if (! $cart) {
+            return false;
+        }
+        if ($cart->billing_address) {
+            $billingCountry = $cart->billing_address->country;
+            $allowedCountries = ['AU', 'CA', 'NZ', 'GB', 'US'];
+            return in_array($billingCountry, $allowedCountries);
+        }
+        return true;
     }
 }

+ 4 - 4
packages/Longyi/Pay/AwxAfterpay/src/Http/Controllers/AfterpayController.php

@@ -88,11 +88,11 @@ class AfterpayController extends Controller
         $awxReturnResult = request()->input('awx_return_result');
         $orderId = request()->input('orderId');
         if ($awxReturnResult == 'success') {
-            session()->flash('order_id', $orderId);
-            return redirect()->route('shop.checkout.onepage.success');
+            return redirect()->to('/paymentresult/success?orderId=' . $orderId);
+        } else if ($awxReturnResult == 'cancel') {
+            return redirect()->to('/paymentresult/cancel?orderId=' . $orderId);
         } else {
-            session()->flash('error', 'klarna cancel');
-            return redirect()->route('shop.checkout.cart.index');
+            return redirect()->to('/paymentresult/failure?orderId=' . $orderId);
         }
     }
 

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

@@ -15,6 +15,7 @@ class AwxAfterpay extends Payment
     protected $apikey;
     protected $secret;
     protected $currentOrder;
+    protected $currentInput;
     protected $prefix = 'QQS';
     protected $tokenCacheKey = 'airwallex_klarna_token';
     protected $tokenUrl = 'https://api-demo.airwallex.com/api/v1/authentication/login';
@@ -55,15 +56,26 @@ class AwxAfterpay extends Payment
         $this->currentOrder = $order;
         return $this;
     }
+
     public function getOrder()
     {
         return $this->currentOrder;
     }
+    public function setInput($input = null): self
+    {
+        $this->currentInput = $input;
+        return $this;
+    }
+
+    public function getInput()
+    {
+        return $this->currentInput;
+    }
     public function createGatewayOrder()
     {
-        return $this->createPayment($this->getOrder(), ['merchantOrderId' => $this->getOrder()->id]);
+        return $this->createPayment($this->getOrder(), ['merchantOrderId' => $this->getOrder()->id], $this->currentInput);
     }
-    public function createPayment($order, $override)
+    public function createPayment($order, $override, $input)
     {
         $shop = $order->shipping_address;
         $bill = $order->billing_address;
@@ -78,7 +90,7 @@ class AwxAfterpay extends Payment
             'request_id' => $this->createUuid(),
             'amount' => $grandTotal,
             'descriptor' => $this->prefix,
-            'return_url' => $this->getBaseUlr() . '/klarna/standard/return?orderId=' . $override['merchantOrderId'],
+            'return_url' => $input->paymentSuccessUrl. '?orderId=' . $order->id,
             'merchant_order_id' => $this->prefix . $override['merchantOrderId'],
             'currency' => $order->order_currency_code,
             'customer' => [
@@ -289,8 +301,14 @@ class AwxAfterpay extends Payment
             return false;
         }
         $cart = Cart::getCart();
-        $billingCountry = $cart->billing_address->country;
-        $allowedCountries = ['AU', 'CA', 'NZ', 'GB', 'US'];
-        return in_array($billingCountry, $allowedCountries);
+        if (! $cart) {
+            return false;
+        }
+        if ($cart->billing_address) {
+            $billingCountry = $cart->billing_address->country;
+            $allowedCountries = ['AU', 'CA', 'NZ', 'GB', 'US'];
+            return in_array($billingCountry, $allowedCountries);
+        }
+        return true;
     }
 }

+ 4 - 4
packages/Longyi/Pay/AwxKlarna/src/Http/Controllers/KlarnaController.php

@@ -87,11 +87,11 @@ class KlarnaController extends Controller
         $awxReturnResult = request()->input('awx_return_result');
         $orderId = request()->input('orderId');
         if ($awxReturnResult == 'success') {
-            session()->flash('order_id', $orderId);
-            return redirect()->route('shop.checkout.onepage.success');
+            return redirect()->to('/paymentresult/success?orderId=' . $orderId);
+        } else if ($awxReturnResult == 'cancel') {
+            return redirect()->to('/paymentresult/cancel?orderId=' . $orderId);
         } else {
-            session()->flash('error', 'afterpay cancel');
-            return redirect()->route('shop.checkout.cart.index');
+            return redirect()->to('/paymentresult/failure?orderId=' . $orderId);
         }
     }
 

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

@@ -16,6 +16,7 @@ class AwxKlarna extends Payment
     protected $apikey;
     protected $secret;
     protected $currentOrder;
+    protected $currentInput;
     protected $prefix = 'QQS';
     protected $tokenCacheKey = 'airwallex_klarna_token';
     protected $tokenUrl = 'https://api-demo.airwallex.com/api/v1/authentication/login';
@@ -61,11 +62,21 @@ class AwxKlarna extends Payment
     {
         return $this->currentOrder;
     }
+    public function setInput($input = null): self
+    {
+        $this->currentInput = $input;
+        return $this;
+    }
+
+    public function getInput()
+    {
+        return $this->currentInput;
+    }
     public function createGatewayOrder()
     {
-        return $this->createPayment($this->getOrder(), ['merchantOrderId' => $this->getOrder()->id]);
+        return $this->createPayment($this->getOrder(), ['merchantOrderId' => $this->getOrder()->id], $this->getInput());
     }
-    public function createPayment($order, $override)
+    public function createPayment($order, $override, $input)
     {
         $shop = $order->shipping_address;
         $bill = $order->billing_address;
@@ -80,7 +91,7 @@ class AwxKlarna extends Payment
             'request_id' => $this->createUuid(),
             'amount' => $grandTotal,
             'descriptor' => $this->prefix,
-            'return_url' => $this->getBaseUlr() . '/klarna/standard/return?orderId=' . $override['merchantOrderId'],
+            'return_url' => $input->paymentSuccessUrl . '?orderId=' . $order->id,
             'merchant_order_id' => $this->prefix . $override['merchantOrderId'],
             'currency' => $order->order_currency_code,
             'customer' => [
@@ -297,11 +308,17 @@ class AwxKlarna extends Payment
             return false;
         }
         $cart = Cart::getCart();
-        $billingCountry = $cart->billing_address->country;
-        $allowedCountries = ['AU', 'AT', 'BE', 'CA', 'CZ', 'DK', 'FI', 'FR', 'DE', 'GR', 'IE',
-            'IT', 'NL', 'NO', 'PL', 'PT', 'ES', 'SE', 'CH', 'GB', 'US'
-        ];
-        return in_array($billingCountry, $allowedCountries);
+        if (! $cart) {
+            return false;
+        }
+        if ($cart->billing_address) {
+            $billingCountry = $cart->billing_address->country;
+            $allowedCountries = ['AU', 'AT', 'BE', 'CA', 'CZ', 'DK', 'FI', 'FR', 'DE', 'GR', 'IE',
+                'IT', 'NL', 'NO', 'PL', 'PT', 'ES', 'SE', 'CH', 'GB', 'US'
+            ];
+            return in_array($billingCountry, $allowedCountries);
+        }
+        return true;
     }
 
     public function getDescription()

+ 44 - 3
packages/Webkul/BagistoApi/src/Models/CustomerOrderItem.php

@@ -5,6 +5,10 @@ namespace Webkul\BagistoApi\Models;
 use ApiPlatform\Metadata\ApiProperty;
 use ApiPlatform\Metadata\ApiResource;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\HasOne;
+use Illuminate\Database\Eloquent\Relations\MorphTo;
 
 /**
  * Customer Order Item — nested API resource (no standalone endpoints).
@@ -16,7 +20,7 @@ use Illuminate\Database\Eloquent\Model;
     shortName: 'CustomerOrderItem',
     operations: [],
     graphQlOperations: [],
-    normalizationContext: ['attributes' => ['id','qty_ordered', 'qty_shipped', 'qty_invoiced', 'qty_canceled', 'qty_refunded', 'sku', 'name', 'price', 'base_price', 'total', 'base_total', 'type']],
+    normalizationContext: ['attributes' => ['id','qty_ordered', 'qty_shipped', 'qty_invoiced', 'qty_canceled', 'qty_refunded', 'sku', 'name', 'price', 'base_price', 'total', 'base_total', 'type', 'product', 'children']],
 )]
 class CustomerOrderItem extends Model
 {
@@ -156,20 +160,57 @@ class CustomerOrderItem extends Model
         return $this->base_total;
     }
 
+    /**
+     * Polymorphic product relationship.
+     *
+     * Mirrors OrderItem::product() — uses the order_items table's
+     * product_id and product_type columns. The BagistoApi Product model
+     * overrides getMorphClass() to return the base class name so that
+     * MorphTo resolution works seamlessly.
+     */
+    public function product(): MorphTo
+    {
+        return $this->morphTo();
+    }
+
+    /**
+     * Get the child item record associated with the order item.
+     */
+    public function child(): HasOne
+    {
+        return $this->hasOne(static::class, 'parent_id');
+    }
+
+    /**
+     * Get the parent item record associated with the order item.
+     */
+    public function parent(): BelongsTo
+    {
+        return $this->belongsTo(static::class, 'parent_id');
+    }
+
+    /**
+     * Get the children items (configurable product variants).
+     */
+    public function children(): HasMany
+    {
+        return $this->hasMany(static::class, 'parent_id');
+    }
+
     /**
      * Override toArray to ensure qty fields are always included in serialization
      */
     public function toArray(): array
     {
         $array = parent::toArray();
-        
+
         // Ensure qty fields are always present
         $array['qty_ordered'] = $this->qty_ordered;
         $array['qty_shipped'] = $this->qty_shipped;
         $array['qty_invoiced'] = $this->qty_invoiced;
         $array['qty_canceled'] = $this->qty_canceled;
         $array['qty_refunded'] = $this->qty_refunded;
-        
+
         return $array;
     }
 }

+ 12 - 9
packages/Webkul/BagistoApi/src/Services/PaymentService.php

@@ -80,7 +80,7 @@ class PaymentService
             $this->validateExpressInitiation($cart, $input);
         } else {
             if(!$cart->shipping_method){
-                
+
                 if ($input->shippingMethod) {
 
                     if (! \Webkul\Shipping\Facades\Shipping::isMethodCodeExists($input->shippingMethod)) {
@@ -98,7 +98,7 @@ class PaymentService
             $cart = Cart::getCart();
         }
 
-      
+
 
         if (! $input->paymentMethod && ! $cart->payment?->method) {
             throw new OperationFailedException(__('bagistoapi::app.graphql.checkout.payment-method-required'));
@@ -242,7 +242,7 @@ class PaymentService
                 if (! $paymentMethodConfig || ! isset($paymentMethodConfig['class'])) {
                     throw new OperationFailedException(__('bagistoapi::app.graphql.checkout.invalid-payment-method'));
                 }
-    
+
                 if (! $order->payment) {
                     throw new OperationFailedException(__('bagistoapi::app.graphql.checkout.payment-method-required'));
                 }
@@ -252,10 +252,10 @@ class PaymentService
                 $order->refresh();
 
             }
-           
+
         }
 
-        $gatewayOrderId = $this->createGatewayOrderForOrder($order);
+        $gatewayOrderId = $this->createGatewayOrderForOrder($order, $input);
 
         $this->writeGatewayOrderId($order, $gatewayOrderId);
 
@@ -409,7 +409,7 @@ class PaymentService
     protected function createGatewayOrder($cart, $order, PaymentInitiateInput $input, bool $express): ?string
     {
         $method = $cart->payment?->method ?? $order->payment?->method;
-        $gatewayHandler = $this->resolveGatewayHandler($method, $order);
+        $gatewayHandler = $this->resolveGatewayHandler($method, $order, $input);
         if ($gatewayHandler) {
            return $gatewayHandler->createGatewayOrder();
         } else {
@@ -446,10 +446,10 @@ class PaymentService
      * Variant used by replay() where there's no Cart - amount comes
      * straight from the existing order.
      */
-    protected function createGatewayOrderForOrder($order): ?string
+    protected function createGatewayOrderForOrder($order, $input): ?string
     {
         $method = $order->payment?->method;
-        $gatewayHandler = $this->resolveGatewayHandler($method, $order);
+        $gatewayHandler = $this->resolveGatewayHandler($method, $order, $input);
         if ($gatewayHandler) {
             return $gatewayHandler->createGatewayOrder();
         } else {
@@ -479,7 +479,7 @@ class PaymentService
             return (string) $gatewayOrderId;
         }
     }
-    protected function resolveGatewayHandler(string $method, $order = null): ?object
+    protected function resolveGatewayHandler(string $method, $order = null, PaymentInitiateInput $input = null): ?object
     {
         $config = config("payment_methods.{$method}");
 
@@ -491,6 +491,9 @@ class PaymentService
             if (method_exists($handler, 'setOrder')) {
                 $handler->setOrder($order);
             }
+            if (method_exists($handler, 'setInput')) {
+                $handler->setInput($input);
+            }
             if (! method_exists($handler, 'createGatewayOrder')) {
                 Log::warning('Payment handler does not implement createGatewayOrder', [
                     'method' => $method,

+ 4 - 0
packages/Webkul/BagistoApi/src/State/PaymentMethodsProvider.php

@@ -9,6 +9,7 @@ use Webkul\BagistoApi\Dto\PaymentMethodOutput;
 use Webkul\BagistoApi\Exception\ResourceNotFoundException;
 use Webkul\BagistoApi\Facades\CartTokenFacade;
 use Webkul\BagistoApi\Facades\TokenHeaderFacade;
+use Webkul\Checkout\Facades\Cart;
 use Webkul\Payment\Facades\Payment;
 
 /**
@@ -38,6 +39,9 @@ class PaymentMethodsProvider implements ProviderInterface
             throw new ResourceNotFoundException(__('bagistoapi::app.graphql.cart.invalid-token'));
         }
 
+        // 将 API 上下文中的购物车设置到 Cart 门面,使 isAvailable() 能获取到购物车信息
+        Cart::setCart($cart);
+
         $methods = Payment::getSupportedPaymentMethods();
 
         if ($methods && is_array($methods) && isset($methods['payment_methods'])) {