|
@@ -19,6 +19,7 @@ use Webkul\Shipping\Facades\Shipping;
|
|
|
use Longyi\Gift\Services\GiftCardService;
|
|
use Longyi\Gift\Services\GiftCardService;
|
|
|
use Longyi\Member\Services\MemberDiscountService;
|
|
use Longyi\Member\Services\MemberDiscountService;
|
|
|
use Longyi\ShippingInsurance\Services\ShippingInsuranceService;
|
|
use Longyi\ShippingInsurance\Services\ShippingInsuranceService;
|
|
|
|
|
+use Longyi\RewardPoints\Repositories\RewardPointRepository;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* GraphQL processor for the SaveCheckoutCart mutation.
|
|
* GraphQL processor for the SaveCheckoutCart mutation.
|
|
@@ -39,6 +40,7 @@ class SaveCheckoutCartProcessor implements ProcessorInterface
|
|
|
protected GiftCardService $giftCardService,
|
|
protected GiftCardService $giftCardService,
|
|
|
protected MemberDiscountService $memberDiscountService,
|
|
protected MemberDiscountService $memberDiscountService,
|
|
|
protected ShippingInsuranceService $shippingInsuranceService,
|
|
protected ShippingInsuranceService $shippingInsuranceService,
|
|
|
|
|
+ protected RewardPointRepository $rewardPointRepository,
|
|
|
) {}
|
|
) {}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -88,6 +90,9 @@ class SaveCheckoutCartProcessor implements ProcessorInterface
|
|
|
|
|
|
|
|
if ($this->shouldUpdate($data->useShippingInsurance)) {
|
|
if ($this->shouldUpdate($data->useShippingInsurance)) {
|
|
|
$this->applyShippingInsurance((string) $data->useShippingInsurance);
|
|
$this->applyShippingInsurance((string) $data->useShippingInsurance);
|
|
|
|
|
+
|
|
|
|
|
+ if ($data->rewardPoints !== null && $data->rewardPoints !== -1) {
|
|
|
|
|
+ $this->applyRewardPoints((int) $data->rewardPoints);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Cart::collectTotals();
|
|
Cart::collectTotals();
|
|
@@ -256,5 +261,49 @@ class SaveCheckoutCartProcessor implements ProcessorInterface
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$this->shippingInsuranceService->activate();
|
|
$this->shippingInsuranceService->activate();
|
|
|
|
|
+
|
|
|
|
|
+ * Apply or remove reward points redemption. "0" removes the redemption.
|
|
|
|
|
+ *
|
|
|
|
|
+ * The actual discount amount is computed and deducted from the grand total
|
|
|
|
|
+ * by the RewardPoints listener on the "checkout.cart.collect.totals.after"
|
|
|
|
|
+ * event (same pattern as gift cards), so here we only validate and persist
|
|
|
|
|
+ * how many points the customer wants to redeem.
|
|
|
|
|
+ */
|
|
|
|
|
+ private function applyRewardPoints(int $points): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $cart = Cart::getCart();
|
|
|
|
|
+
|
|
|
|
|
+ if (! $cart) {
|
|
|
|
|
+ throw new OperationFailedException(__('bagistoapi::app.graphql.checkout.invalid-cart'));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $customerId = $cart->customer_id;
|
|
|
|
|
+
|
|
|
|
|
+ if (! $customerId) {
|
|
|
|
|
+ throw new OperationFailedException(__('bagistoapi::app.graphql.checkout.customer-not-found'));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Remove redemption.
|
|
|
|
|
+ if ($points <= 0) {
|
|
|
|
|
+ $cart->reward_points_used = 0;
|
|
|
|
|
+ $cart->reward_points_amount = 0;
|
|
|
|
|
+ $cart->base_reward_points_amount = 0;
|
|
|
|
|
+ $cart->save();
|
|
|
|
|
+
|
|
|
|
|
+ Cart::collectTotals();
|
|
|
|
|
+
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $availablePoints = (int) $this->rewardPointRepository->getCustomerPoints($customerId);
|
|
|
|
|
+
|
|
|
|
|
+ if ($points > $availablePoints) {
|
|
|
|
|
+ throw new OperationFailedException(__('bagistoapi::app.graphql.checkout.insufficient-reward-points'));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $cart->reward_points_used = $points;
|
|
|
|
|
+ $cart->save();
|
|
|
|
|
+
|
|
|
|
|
+ Cart::collectTotals();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|