Skip to content

Commit

Permalink
Added mocking to order creation (#73)
Browse files Browse the repository at this point in the history
* Added mocking to order creation
  • Loading branch information
andrii-onufriichuk authored Jan 30, 2024
1 parent 1a64d2c commit 7f11bae
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Plugin/Customer/DisableOrderCreation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Rvvup\Payments\Plugin\Customer;

use Magento\Checkout\Api\PaymentInformationManagementInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\PaymentInterface;
use Rvvup\Payments\Gateway\Method;

class DisableOrderCreation
{
/** @var CartRepositoryInterface */
private $cartRepository;

/**
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
CartRepositoryInterface $cartRepository
) {
$this->cartRepository = $cartRepository;
}

/**
* @param PaymentInformationManagementInterface $subject
* @param callable $proceed
* @param int $cartId
* @param PaymentInterface $paymentMethod
* @param AddressInterface|null $billingAddress
* @return int
* @throws NoSuchEntityException
*/
public function aroundSavePaymentInformationAndPlaceOrder(
PaymentInformationManagementInterface $subject,
callable $proceed,
$cartId,
PaymentInterface $paymentMethod,
AddressInterface $billingAddress = null
): int {
// Return reserved order id if Rvvup Payment.
if (strpos($paymentMethod->getMethod(), Method::PAYMENT_TITLE_PREFIX) === 0) {
$cart = $this->cartRepository->get($cartId);
$id = $cart->getReservedOrderId();
if (!$id) {
$cart->reserveOrderId();
$this->cartRepository->save($cart);
$id = $cart->getReservedOrderId();
}
return $id;
}
return $proceed($cartId, $paymentMethod, $billingAddress);
}
}
56 changes: 56 additions & 0 deletions Plugin/Guest/DisableOrderCreation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Rvvup\Payments\Plugin\Guest;

use Magento\Checkout\Api\GuestPaymentInformationManagementInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\PaymentInterface;
use Rvvup\Payments\Gateway\Method;

class DisableOrderCreation
{
/** @var CartRepositoryInterface */
private $cartRepository;

/**
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(CartRepositoryInterface $cartRepository)
{
$this->cartRepository = $cartRepository;
}

/**
* @param GuestPaymentInformationManagementInterface $subject
* @param callable $proceed
* @param string $cartId
* @param string $email
* @param PaymentInterface $paymentMethod
* @param AddressInterface|null $billingAddress
* @return int
* @throws NoSuchEntityException
*/
public function aroundSavePaymentInformationAndPlaceOrder(
GuestPaymentInformationManagementInterface $subject,
callable $proceed,
$cartId,
$email,
PaymentInterface $paymentMethod,
AddressInterface $billingAddress = null
): int {
// Return reserved order id if Rvvup Payment.
if (strpos($paymentMethod->getMethod(), Method::PAYMENT_TITLE_PREFIX) === 0) {
$cart = $this->cartRepository->get($cartId);
$id = $cart->getReservedOrderId();
if (!$id) {
$cart->reserveOrderId();
$id = $cart->getReservedOrderId();
$this->cartRepository->save($cart);
}
return $id;
}
return $proceed($cartId, $email, $paymentMethod, $billingAddress);
}
}
10 changes: 10 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,14 @@
sortOrder="1"/>
</type>

<type name="Magento\Checkout\Api\GuestPaymentInformationManagementInterface">
<plugin name="disable_order_creation"
type="Rvvup\Payments\Plugin\Guest\DisableOrderCreation"
sortOrder="1"/>
</type>
<type name="Magento\Checkout\Api\PaymentInformationManagementInterface">
<plugin name="disable_order_creation"
type="Rvvup\Payments\Plugin\Customer\DisableOrderCreation"
sortOrder="1"/>
</type>
</config>

0 comments on commit 7f11bae

Please sign in to comment.