Skip to content

Commit

Permalink
Added Rvvup paypal to minicart and cart (#47)
Browse files Browse the repository at this point in the history
* Added Rvvup paypal to minicart and cart


---------

Co-authored-by: Andrii Onufriichuk <[email protected]>
  • Loading branch information
andrii-onufriichuk and Andrii Onufriichuk authored Jul 1, 2024
1 parent 64f1bdd commit 9451f5d
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 39 deletions.
53 changes: 53 additions & 0 deletions src/Block/Paypal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);

namespace Rvvup\PaymentsHyvaCheckout\Block;

use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Quote\Api\Data\CartInterface;
use Hyva\Theme\Service\CurrentTheme;

class Paypal extends Template
{
/** @var Session */
private $checkoutSession;

/** @var CurrentTheme */
private $theme;

/**
* @param Session $checkoutSession
* @param Template\Context $context
* @param CurrentTheme $theme
* @param array $data
*/
public function __construct(
Session $checkoutSession,
Template\Context $context,
CurrentTheme $theme,
array $data = []
) {
$this->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();
}

}
71 changes: 55 additions & 16 deletions src/Magewire/Product/View/Info/Addtocart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -72,6 +77,7 @@ public function __construct(
CartRepositoryInterface $cartRepository,
ProductRepositoryInterface $productRepository,
BillingAddressManagementInterface $billingAddressManagement,
ShippingAddressManagementInterface $shippingAddressManagement,
AddressInterfaceFactory $addressFactory,
HyvaCheckoutSession $hyvaCheckoutSession,
ExpressPaymentCreateInterface $expressPaymentCreate,
Expand All @@ -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;
Expand All @@ -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(
Expand All @@ -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();

Expand All @@ -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();
Expand All @@ -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);
}

Expand Down
19 changes: 10 additions & 9 deletions src/view/frontend/layout/catalog_product_view.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<move element="rvvup_payments.product.catalog_product_view.product.info.addtocart"
destination="addtocart.shortcut.buttons"
/>

<referenceBlock name="rvvup_payments.product.catalog_product_view.product.info.addtocart">
<arguments>
<argument name="magewire" xsi:type="object">Rvvup\PaymentsHyvaCheckout\Magewire\Product\View\Info\Addtocart</argument>
</arguments>
</referenceBlock>
<body>
<move element="rvvup_payments.product.catalog_product_view.product.info.addtocart"
destination="addtocart.shortcut.buttons"
/>
<referenceBlock name="rvvup_payments.product.catalog_product_view.product.info.addtocart">
<arguments>
<argument name="magewire" xsi:type="object">Rvvup\PaymentsHyvaCheckout\Magewire\Product\View\Info\Addtocart</argument>
</arguments>
</referenceBlock>
</body>
</page>
9 changes: 0 additions & 9 deletions src/view/frontend/layout/checkout_cart_index.xml

This file was deleted.

30 changes: 30 additions & 0 deletions src/view/frontend/layout/default_hyva.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<page xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="head.additional">
<block class="Magento\Framework\View\Element\Template"
name="rvvup_payments.checkout_index_index.head.additional.assets"
template="Rvvup_Payments::head/additional/assets.phtml"
ifconfig="payment/rvvup/active">
<arguments>
<argument name="rvvup_payments_assets_view_model"
xsi:type="object">Rvvup\Payments\ViewModel\Assets</argument>
</arguments>
</block>
</referenceBlock>
<referenceBlock name="extra_actions">
<block name="rvvup-paypal-minicart-block"
template="Rvvup_PaymentsHyvaCheckout::cart/paypal.phtml"
class="Rvvup\PaymentsHyvaCheckout\Block\Paypal" ifconfig="payment/rvvup/active">
<arguments>
<argument name="magewire" xsi:type="object">
Rvvup\PaymentsHyvaCheckout\Magewire\Product\View\Info\Addtocart
</argument>
<argument name="rvvup_payments_paypal_view_model" xsi:type="object">
Rvvup\Payments\ViewModel\PayPal
</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
26 changes: 26 additions & 0 deletions src/view/frontend/layout/hyva_checkout_cart_index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="rvvup_payments.checkout_cart_index.methods">
<arguments>
<argument name="magewire" xsi:type="object">Rvvup\PaymentsHyvaCheckout\Magewire\Checkout\Cart\Clearpay</argument>
</arguments>
</referenceBlock>
<referenceContainer name="cart.summary">
<block name="rvvup-paypal-cart-block" after="checkout.cart.methods.bottom"
template="Rvvup_PaymentsHyvaCheckout::cart/paypal.phtml"
class="Rvvup\PaymentsHyvaCheckout\Block\Paypal">
<arguments>
<argument name="magewire" xsi:type="object">
Rvvup\PaymentsHyvaCheckout\Magewire\Product\View\Info\Addtocart
</argument>
<argument name="is_cart_page" xsi:type="boolean">true</argument>
<argument name="rvvup_payments_paypal_view_model" xsi:type="object">
Rvvup\Payments\ViewModel\PayPal
</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
Loading

0 comments on commit 9451f5d

Please sign in to comment.