|
@@ -18,6 +18,7 @@ use Webkul\Checkout\Facades\Cart;
|
|
|
use Webkul\Checkout\Models\CartAddress;
|
|
use Webkul\Checkout\Models\CartAddress;
|
|
|
use Webkul\Checkout\Repositories\CartRepository;
|
|
use Webkul\Checkout\Repositories\CartRepository;
|
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
|
|
|
|
+use Webkul\Paypal\Payment\SmartButton;
|
|
|
use Webkul\Sales\Repositories\OrderRepository;
|
|
use Webkul\Sales\Repositories\OrderRepository;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -319,21 +320,33 @@ class CheckoutProcessor implements ProcessorInterface
|
|
|
Cart::setCart($cart);
|
|
Cart::setCart($cart);
|
|
|
Cart::collectTotals();
|
|
Cart::collectTotals();
|
|
|
|
|
|
|
|
|
|
+ $paymentTransactionId = $this->resolvePaymentTransactionId($cart);
|
|
|
|
|
+
|
|
|
$orderData = $this->buildOrderDataFromCart($cart);
|
|
$orderData = $this->buildOrderDataFromCart($cart);
|
|
|
$order = $this->orderRepository->create($orderData);
|
|
$order = $this->orderRepository->create($orderData);
|
|
|
|
|
|
|
|
- if (! $order || ! $order->id) {
|
|
|
|
|
|
|
+ $createdOrderId = data_get($order, 'id');
|
|
|
|
|
+
|
|
|
|
|
+ if (! $order || ! $createdOrderId) {
|
|
|
throw new \Exception(__('bagistoapi::app.graphql.checkout.order-creation-failed'));
|
|
throw new \Exception(__('bagistoapi::app.graphql.checkout.order-creation-failed'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $orderId = $order->id;
|
|
|
|
|
|
|
+ $orderId = $createdOrderId;
|
|
|
$order = $this->orderRepository->find($orderId);
|
|
$order = $this->orderRepository->find($orderId);
|
|
|
|
|
|
|
|
if (! $order) {
|
|
if (! $order) {
|
|
|
throw new \Exception(__('bagistoapi::app.graphql.checkout.order-retrieval-failed', ['orderId' => $orderId]));
|
|
throw new \Exception(__('bagistoapi::app.graphql.checkout.order-retrieval-failed', ['orderId' => $orderId]));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- Cart::deActivateCart($cart);
|
|
|
|
|
|
|
+ if ($paymentTransactionId && $order->payment) {
|
|
|
|
|
+ $additionalData = $order->payment->additional ?? [];
|
|
|
|
|
+ $additionalData['paypal_order_id'] = $paymentTransactionId;
|
|
|
|
|
+
|
|
|
|
|
+ $order->payment->additional = $additionalData;
|
|
|
|
|
+ $order->payment->save();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Cart::deActivateCart();
|
|
|
|
|
|
|
|
// Dispatch event for order creation (for push notifications)
|
|
// Dispatch event for order creation (for push notifications)
|
|
|
Event::dispatch('order.created.after', $order);
|
|
Event::dispatch('order.created.after', $order);
|
|
@@ -342,6 +355,7 @@ class CheckoutProcessor implements ProcessorInterface
|
|
|
'id' => $cart->id,
|
|
'id' => $cart->id,
|
|
|
'cartToken' => (string) ($cart->guest_cart_token ?? $cart->customer_id),
|
|
'cartToken' => (string) ($cart->guest_cart_token ?? $cart->customer_id),
|
|
|
'orderId' => (string) $order->id,
|
|
'orderId' => (string) $order->id,
|
|
|
|
|
+ 'paymentTransactionId' => $paymentTransactionId,
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
return $response;
|
|
return $response;
|
|
@@ -360,6 +374,53 @@ class CheckoutProcessor implements ProcessorInterface
|
|
|
return $orderResource->jsonSerialize();
|
|
return $orderResource->jsonSerialize();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Resolve payment transaction id for gateway methods.
|
|
|
|
|
+ */
|
|
|
|
|
+ private function resolvePaymentTransactionId($cart): ?string
|
|
|
|
|
+ {
|
|
|
|
|
+ $paymentMethod = $cart->payment?->method;
|
|
|
|
|
+
|
|
|
|
|
+ if ($paymentMethod !== 'paypal_smart_button') {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $smartButton = app(SmartButton::class);
|
|
|
|
|
+
|
|
|
|
|
+ $paypalOrder = $smartButton->createOrder($this->buildPayPalOrderRequestBody($cart, $smartButton));
|
|
|
|
|
+
|
|
|
|
|
+ $paypalOrderId = $paypalOrder->result->id ?? null;
|
|
|
|
|
+
|
|
|
|
|
+ if (! $paypalOrderId) {
|
|
|
|
|
+ throw new \Exception('Failed to create PayPal order transaction.');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return (string) $paypalOrderId;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Build PayPal create-order payload for Smart Button.
|
|
|
|
|
+ */
|
|
|
|
|
+ private function buildPayPalOrderRequestBody($cart, SmartButton $smartButton): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $shippingAmount = (float) ($cart->selected_shipping_rate?->price ?? 0);
|
|
|
|
|
+ $discountAmount = (float) ($cart->discount_amount ?? 0);
|
|
|
|
|
+ $subTotal = (float) ($cart->sub_total ?? 0);
|
|
|
|
|
+ $taxTotal = (float) ($cart->tax_total ?? 0);
|
|
|
|
|
+ $grandTotal = $subTotal + $taxTotal + $shippingAmount - $discountAmount;
|
|
|
|
|
+
|
|
|
|
|
+ return [
|
|
|
|
|
+ 'intent' => 'CAPTURE',
|
|
|
|
|
+ 'purchase_units' => [[
|
|
|
|
|
+ 'reference_id' => (string) $cart->id,
|
|
|
|
|
+ 'amount' => [
|
|
|
|
|
+ 'currency_code' => $cart->cart_currency_code,
|
|
|
|
|
+ 'value' => $smartButton->formatCurrencyValue($grandTotal),
|
|
|
|
|
+ ],
|
|
|
|
|
+ ]],
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Validate order can be created.
|
|
* Validate order can be created.
|
|
|
*/
|
|
*/
|