Explorar o código

修改支付方式

llp hai 1 día
pai
achega
0a7d676842
Modificáronse 1 ficheiros con 84 adicións e 41 borrados
  1. 84 41
      packages/Webkul/BagistoApi/src/Services/PaymentService.php

+ 84 - 41
packages/Webkul/BagistoApi/src/Services/PaymentService.php

@@ -372,36 +372,39 @@ class PaymentService
     protected function createGatewayOrder($cart, $order, PaymentInitiateInput $input, bool $express): ?string
     {
         $method = $cart->payment?->method ?? $order->payment?->method;
+        $gatewayHandler = $this->resolveGatewayHandler($method, $order);
+        if ($gatewayHandler) {
+           return $gatewayHandler->createGatewayOrder();
+        } else {
+            if ($method !== 'paypal_smart_button') {
+                return null;
+            }
 
-        if ($method !== 'paypal_smart_button') {
-            return null;
-        }
+            $smartButton = app(SmartButton::class);
+            $amount = $express
+                ? max(0, (float) ($cart->sub_total ?? 0) - (float) ($cart->discount_amount ?? 0))
+                : (float) ($order->grand_total ?? $cart->sub_total ?? 0);
+
+            $response = $smartButton->createOrder([
+                'intent'         => 'CAPTURE',
+                'purchase_units' => [[
+                    'reference_id' => (string) $cart->id,
+                    'amount'       => [
+                        'currency_code' => $cart->cart_currency_code,
+                        'value'         => $smartButton->formatCurrencyValue($amount),
+                    ],
+                ]],
+            ]);
 
-        $smartButton = app(SmartButton::class);
-        $amount = $express
-            ? max(0, (float) ($cart->sub_total ?? 0) - (float) ($cart->discount_amount ?? 0))
-            : (float) ($order->grand_total ?? $cart->sub_total ?? 0);
-
-        $response = $smartButton->createOrder([
-            'intent'         => 'CAPTURE',
-            'purchase_units' => [[
-                'reference_id' => (string) $cart->id,
-                'amount'       => [
-                    'currency_code' => $cart->cart_currency_code,
-                    'value'         => $smartButton->formatCurrencyValue($amount),
-                ],
-            ]],
-        ]);
+            $gatewayOrderId = $response->result->id ?? null;
 
-        $gatewayOrderId = $response->result->id ?? null;
+            if (! $gatewayOrderId) {
+                throw new OperationFailedException(__('bagistoapi::app.graphql.payment.gateway-create-failed'));
+            }
 
-        if (! $gatewayOrderId) {
-            throw new OperationFailedException(__('bagistoapi::app.graphql.payment.gateway-create-failed'));
+            return (string) $gatewayOrderId;
         }
-
-        return (string) $gatewayOrderId;
     }
-
     /**
      * Variant used by replay() where there's no Cart - amount comes
      * straight from the existing order.
@@ -409,31 +412,71 @@ class PaymentService
     protected function createGatewayOrderForOrder($order): ?string
     {
         $method = $order->payment?->method;
+        $gatewayHandler = $this->resolveGatewayHandler($method, $order);
+        if ($gatewayHandler) {
+            return $gatewayHandler->createGatewayOrder();
+        } else {
+            if ($method !== 'paypal_smart_button') {
+                return null;
+            }
+
+            $smartButton = app(SmartButton::class);
+
+            $response = $smartButton->createOrder([
+                'intent'         => 'CAPTURE',
+                'purchase_units' => [[
+                    'reference_id' => (string) $order->id,
+                    'amount'       => [
+                        'currency_code' => $order->cart_currency_code ?? $order->order_currency_code,
+                        'value'         => $smartButton->formatCurrencyValue((float) $order->grand_total),
+                    ],
+                ]],
+            ]);
+
+            $gatewayOrderId = $response->result->id ?? null;
+
+            if (! $gatewayOrderId) {
+                throw new OperationFailedException(__('bagistoapi::app.graphql.payment.gateway-create-failed'));
+            }
 
-        if ($method !== 'paypal_smart_button') {
+            return (string) $gatewayOrderId;
+        }
+    }
+    protected function resolveGatewayHandler(string $method, $order = null): ?object
+    {
+        $config = config("payment_methods.{$method}");
+
+        if (! $config || ! isset($config['class'])) {
             return null;
         }
 
-        $smartButton = app(SmartButton::class);
+        try {
+            $handler = app($config['class']);
 
-        $response = $smartButton->createOrder([
-            'intent'         => 'CAPTURE',
-            'purchase_units' => [[
-                'reference_id' => (string) $order->id,
-                'amount'       => [
-                    'currency_code' => $order->cart_currency_code ?? $order->order_currency_code,
-                    'value'         => $smartButton->formatCurrencyValue((float) $order->grand_total),
-                ],
-            ]],
-        ]);
+            if (method_exists($handler, 'setOrder')) {
+                $handler->setOrder($order);
+            }
 
-        $gatewayOrderId = $response->result->id ?? null;
+            if (! method_exists($handler, 'createGatewayOrder')) {
+                Log::warning('Payment handler does not implement createGatewayOrder', [
+                    'method' => $method,
+                    'class'  => $config['class'],
+                ]);
 
-        if (! $gatewayOrderId) {
-            throw new OperationFailedException(__('bagistoapi::app.graphql.payment.gateway-create-failed'));
-        }
+                return $handler;
+            }
+
+
+            return null;
+        } catch (\Throwable $e) {
+            Log::error('Failed to instantiate payment handler', [
+                'method' => $method,
+                'class'  => $config['class'] ?? null,
+                'error'  => $e->getMessage(),
+            ]);
 
-        return (string) $gatewayOrderId;
+            return null;
+        }
     }
 
     /**