From 9451f5d1a058eb15991c0f506311294a3d23408d Mon Sep 17 00:00:00 2001 From: andrii-onufriichuk <117644505+andrii-onufriichuk@users.noreply.github.com> Date: Mon, 1 Jul 2024 14:21:45 +0300 Subject: [PATCH] Added Rvvup paypal to minicart and cart (#47) * Added Rvvup paypal to minicart and cart --------- Co-authored-by: Andrii Onufriichuk --- src/Block/Paypal.php | 53 +++++ src/Magewire/Product/View/Info/Addtocart.php | 71 +++++-- .../frontend/layout/catalog_product_view.xml | 19 +- .../frontend/layout/checkout_cart_index.xml | 9 - src/view/frontend/layout/default_hyva.xml | 30 +++ .../layout/hyva_checkout_cart_index.xml | 26 +++ src/view/frontend/templates/cart/paypal.phtml | 182 ++++++++++++++++++ .../component/payment/method/paypal.phtml | 3 +- .../product/view/info/addtocart.phtml | 13 +- 9 files changed, 367 insertions(+), 39 deletions(-) create mode 100644 src/Block/Paypal.php delete mode 100644 src/view/frontend/layout/checkout_cart_index.xml create mode 100644 src/view/frontend/layout/default_hyva.xml create mode 100644 src/view/frontend/layout/hyva_checkout_cart_index.xml create mode 100644 src/view/frontend/templates/cart/paypal.phtml diff --git a/src/Block/Paypal.php b/src/Block/Paypal.php new file mode 100644 index 0000000..945427f --- /dev/null +++ b/src/Block/Paypal.php @@ -0,0 +1,53 @@ +checkoutSession = $checkoutSession; + $this->theme = $theme; + parent::__construct($context,$data); + } + + /** + * @return CartInterface + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function getQuote(): CartInterface + { + return $this->checkoutSession->getQuote(); + } + + public function isHyvaThemeUsed(): bool + { + return (bool)$this->theme->isHyva(); + } + +} diff --git a/src/Magewire/Product/View/Info/Addtocart.php b/src/Magewire/Product/View/Info/Addtocart.php index 8ec4aa7..4ae8672 100644 --- a/src/Magewire/Product/View/Info/Addtocart.php +++ b/src/Magewire/Product/View/Info/Addtocart.php @@ -17,6 +17,7 @@ use Magento\Quote\Api\Data\AddressInterfaceFactory; use Magento\Quote\Api\Data\CartInterface; use Magento\Quote\Api\Data\CartInterfaceFactory; +use Magento\Quote\Model\ShippingAddressManagementInterface; use Magewirephp\Magewire\Component; use Rvvup\Payments\Api\ExpressPaymentCreateInterface; use Rvvup\Payments\Exception\PaymentValidationException; @@ -55,12 +56,16 @@ class Addtocart extends Component /** @var PayPal */ private $payPal; + /** @var ShippingAddressManagementInterface */ + private $shippingAddressManagement; + /** * @param Session $checkoutSession * @param CartInterfaceFactory $cartFactory * @param CartRepositoryInterface $cartRepository * @param ProductRepositoryInterface $productRepository * @param BillingAddressManagementInterface $billingAddressManagement + * @param ShippingAddressManagementInterface $shippingAddressManagement * @param AddressInterfaceFactory $addressFactory * @param HyvaCheckoutSession $hyvaCheckoutSession * @param ExpressPaymentCreateInterface $expressPaymentCreate @@ -72,6 +77,7 @@ public function __construct( CartRepositoryInterface $cartRepository, ProductRepositoryInterface $productRepository, BillingAddressManagementInterface $billingAddressManagement, + ShippingAddressManagementInterface $shippingAddressManagement, AddressInterfaceFactory $addressFactory, HyvaCheckoutSession $hyvaCheckoutSession, ExpressPaymentCreateInterface $expressPaymentCreate, @@ -82,6 +88,7 @@ public function __construct( $this->cartRepository = $cartRepository; $this->productRepository = $productRepository; $this->billingAddressManagement = $billingAddressManagement; + $this->shippingAddressManagement = $shippingAddressManagement; $this->addressFactory = $addressFactory; $this->hyvaCheckoutSession = $hyvaCheckoutSession; $this->expressPaymentCreate = $expressPaymentCreate; @@ -90,20 +97,27 @@ public function __construct( /** * @param string $method - * @param string $addToCartRequest + * @param string|null $addToCartRequest + * @param bool $isCart * @return void - * @throws NoSuchEntityException * @throws LocalizedException + * @throws NoSuchEntityException * @throws PaymentValidationException */ - public function createExpressPayment(string $method, string $addToCartRequest): void + public function createExpressPayment( + string $method, + ?string $addToCartRequest = null, + bool $isCart = false + ): void { - $cart = $this->checkoutSession->getQuote()->removeAllItems(); - - $message = $this->addProductToCart($addToCartRequest, $cart); - if ($message) { - $this->dispatchErrorMessage($message); - return; + $cart = $this->checkoutSession->getQuote(); + if (!$isCart) { + $cart = $cart->removeAllItems(); + $message = $this->addProductToCart($addToCartRequest, $cart); + if ($message) { + $this->dispatchErrorMessage($message); + return; + } } $paymentActions = $this->expressPaymentCreate->execute( @@ -122,7 +136,7 @@ public function createExpressPayment(string $method, string $addToCartRequest): * @throws CheckoutException * @throws InputException */ - public function saveAddress(array $billingAddressInput): void + public function saveBillingAddress(array $billingAddressInput): void { $cart = $this->checkoutSession->getQuote(); @@ -133,7 +147,7 @@ public function saveAddress(array $billingAddressInput): void $cart->setCustomerEmail($billingAddress->getEmail()); } - $this->billingAddressManagement->assign($cart->getId(), $billingAddress, true); + $this->billingAddressManagement->assign($cart->getId(), $billingAddress); if ($this->hyvaCheckoutSession->getSteps()) { $this->hyvaCheckoutSession->restart(); @@ -142,15 +156,40 @@ public function saveAddress(array $billingAddressInput): void $this->redirect('checkout'); } + /** + * @param array $shippingAddressInput + * @return void + * @throws LocalizedException + * @throws NoSuchEntityException + * @throws CheckoutException + * @throws InputException + */ + public function saveShippingAddress(array $shippingAddressInput): void + { + $cart = $this->checkoutSession->getQuote(); + + $shippingAddress = $this->addressFactory->create(); + $shippingAddress->setData($shippingAddressInput); + + if (!$cart->getCustomerEmail()) { + $cart->setCustomerEmail($shippingAddress->getEmail()); + } + + if(!$cart->isVirtual()) { + $this->shippingAddressManagement->assign($cart->getId(), $shippingAddress); + } + } + /** Cancel Express Paypal Payment */ - public function cancelExpressPayment(): void + public function cancelExpressPayment(bool $isCart = false): void { $cart = $this->checkoutSession->getQuote(); $payment = $cart->getPayment(); - $cart->removeAllItems(); - $this->cartRepository->save($cart); - $payment->setAdditionalInformation(Method::EXPRESS_PAYMENT_KEY, true); - + if (!$isCart) { + $cart->removeAllItems(); + $this->cartRepository->save($cart); + } + $payment->setAdditionalInformation(Method::EXPRESS_PAYMENT_KEY, false); $this->payPal->cancel($payment); } diff --git a/src/view/frontend/layout/catalog_product_view.xml b/src/view/frontend/layout/catalog_product_view.xml index d951e75..4e92f17 100644 --- a/src/view/frontend/layout/catalog_product_view.xml +++ b/src/view/frontend/layout/catalog_product_view.xml @@ -1,13 +1,14 @@ - - - - - Rvvup\PaymentsHyvaCheckout\Magewire\Product\View\Info\Addtocart - - + + + + + Rvvup\PaymentsHyvaCheckout\Magewire\Product\View\Info\Addtocart + + + diff --git a/src/view/frontend/layout/checkout_cart_index.xml b/src/view/frontend/layout/checkout_cart_index.xml deleted file mode 100644 index ef7f993..0000000 --- a/src/view/frontend/layout/checkout_cart_index.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - Rvvup\PaymentsHyvaCheckout\Magewire\Checkout\Cart\Clearpay - - - diff --git a/src/view/frontend/layout/default_hyva.xml b/src/view/frontend/layout/default_hyva.xml new file mode 100644 index 0000000..c5ce030 --- /dev/null +++ b/src/view/frontend/layout/default_hyva.xml @@ -0,0 +1,30 @@ + + + + + + + Rvvup\Payments\ViewModel\Assets + + + + + + + + Rvvup\PaymentsHyvaCheckout\Magewire\Product\View\Info\Addtocart + + + Rvvup\Payments\ViewModel\PayPal + + + + + + diff --git a/src/view/frontend/layout/hyva_checkout_cart_index.xml b/src/view/frontend/layout/hyva_checkout_cart_index.xml new file mode 100644 index 0000000..a34d4dc --- /dev/null +++ b/src/view/frontend/layout/hyva_checkout_cart_index.xml @@ -0,0 +1,26 @@ + + + + + + Rvvup\PaymentsHyvaCheckout\Magewire\Checkout\Cart\Clearpay + + + + + + + Rvvup\PaymentsHyvaCheckout\Magewire\Product\View\Info\Addtocart + + true + + Rvvup\Payments\ViewModel\PayPal + + + + + + diff --git a/src/view/frontend/templates/cart/paypal.phtml b/src/view/frontend/templates/cart/paypal.phtml new file mode 100644 index 0000000..678f15c --- /dev/null +++ b/src/view/frontend/templates/cart/paypal.phtml @@ -0,0 +1,182 @@ +getData('rvvup_payments_paypal_view_model'); + +if (!$block->isHyvaThemeUsed()) { + return; +} + +if ($paypalViewModel === null || + $block->getQuote() === null || + !$paypalViewModel->isAvailable((string) $block->getQuote()->getGrandTotal())) { + // This is required for magewire + echo '
'; + return; +} +?> + +
+
+
+ diff --git a/src/view/frontend/templates/component/payment/method/paypal.phtml b/src/view/frontend/templates/component/payment/method/paypal.phtml index 8a15581..e377285 100644 --- a/src/view/frontend/templates/component/payment/method/paypal.phtml +++ b/src/view/frontend/templates/component/payment/method/paypal.phtml @@ -24,7 +24,8 @@ here', 'PayPal', - 'href="#" onclick="hyvaCheckout.loader.start()" wire:click.prevent="cancel" class="underline"' + 'href="#" onclick="hyvaCheckout.loader.start();window.location.reload();" + wire:click.prevent="cancel" class="underline"' ); ?> diff --git a/src/view/frontend/templates/product/view/info/addtocart.phtml b/src/view/frontend/templates/product/view/info/addtocart.phtml index 49beaf1..ef615bb 100644 --- a/src/view/frontend/templates/product/view/info/addtocart.phtml +++ b/src/view/frontend/templates/product/view/info/addtocart.phtml @@ -36,18 +36,23 @@ if ($product) { 'use strict'; function rvvupPayPalExpressButton() { var addToCartForm = document.getElementById('product_addtocart_form'); - const component = Magewire.find('escapeJs($block->getNameInLayout()) ?>'); return { init() { const buttonElement = document.querySelector('.rvvup-paypal-express-button-container'); let orderStarted = false; - + Magewire.rescan(); + const component = Magewire.find('rvvup-paypal-minicart-block'); + const isAvailable = rvvup_parameters?.settings?.paypal?.product?.button?.enabled; if (!window.rvvup_paypal) { console.error('PayPal SDK not loaded'); return; } + if (!isAvailable) { + return; + } + rvvup_paypal.Buttons({ style: this.getButtonStyle(), @@ -89,9 +94,9 @@ if ($product) { onApprove: (data, actions) => { return actions.order.get().then((orderData) => { const shippingAddress = this.getShippingAddressFromOrderData(orderData); + component.saveShippingAddress(shippingAddress); const billingAddress = this.getBillingAddressFromOrderData(orderData, shippingAddress); - - return component.saveAddress(billingAddress); + return component.saveBillingAddress(billingAddress); }); },