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); }); },