Skip to content

Commit

Permalink
Merge pull request #210 from paynl/feature/PLUG-3910
Browse files Browse the repository at this point in the history
PLUG-3910 - Fast checkout patch
  • Loading branch information
woutse authored Oct 26, 2024
2 parents b9b5460 + 13bc42c commit 59c3408
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 21 deletions.
5 changes: 4 additions & 1 deletion Controller/Checkout/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ public function execute()
$order = $this->createFastCheckoutOrder->create($params);
$orderEntityId = $order->getId();
} catch (\Exception $e) {
$this->payHelper->logCritical($e->getMessage(), $params);
$this->payHelper->logCritical('Fast checkout: ' . $e->getMessage(), $params);
if ($e->getCode() == 404) {
return $this->result->setContents('TRUE| Error creating fast checkout order. ' . $e->getMessage());
}
return $this->result->setContents('FALSE| Error creating fast checkout order. ' . $e->getMessage());
}
} elseif (strpos($params['extra3'] ?? '', "fastcheckout") !== false) {
Expand Down
4 changes: 2 additions & 2 deletions Controller/Checkout/FastCheckoutStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ private function getProducts()
$productArr = [];

foreach ($products as $key => $product) {
if ($product->getPrice() > 0) {
if ($product->getPriceInclTax() > 0) {
$productArr[] = [
'id' => $product->getProductId(),
'quantity' => $product->getQty(),
'description' => $product->getName(),
'price' => $product->getPrice() * 100,
'price' => $product->getPriceInclTax() * 100,
'currecny' => $this->storeManager->getStore()->getCurrentCurrencyCode(),
'type' => \Paynl\Transaction::PRODUCT_TYPE_ARTICLE,
'vatPercentage' => ($product->getPriceInclTax() - $product->getBasePrice()) / $product->getBasePrice() * 100,
Expand Down
8 changes: 3 additions & 5 deletions Controller/Checkout/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ public function execute()
$payment = $order->getPayment();

if (empty($payment)) {
$this->_redirect('checkout/cart');
return;
throw new \Exception('No payment found');
}

$methodInstance = $this->paymentHelper->getMethodInstance($payment->getMethod());
if ($methodInstance instanceof \Paynl\Payment\Model\Paymentmethod\Paymentmethod) {
$this->payHelper->logNotice('Start new payment for order ' . $order->getId(), array(), $order->getStore());
$this->payHelper->logNotice('Start new payment for order ' . $order->getId() . '. PayProfileId: ' . $methodInstance->getPaymentOptionId(), array(), $order->getStore());
$redirectUrl = $methodInstance->startTransaction($order);
$this->getResponse()->setNoCacheHeaders();
$this->getResponse()->setRedirect($redirectUrl);
Expand All @@ -105,8 +104,7 @@ public function execute()
$this->_getCheckoutSession()->restoreQuote(); // phpcs:ignore
$this->messageManager->addExceptionMessage($e, __('Something went wrong, please try again later'));
$this->messageManager->addExceptionMessage($e, $e->getMessage());
$this->payHelper->logCritical($e, array(), $order->getStore());

$this->payHelper->logCritical($e->getMessage(), array(), $order->getStore());
$this->_redirect('checkout/cart');
}
}
Expand Down
64 changes: 53 additions & 11 deletions Model/CreateFastCheckoutOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Store\Model\StoreManagerInterface;
use Paynl\Payment\Helper\PayHelper;

class CreateFastCheckoutOrder
{
Expand Down Expand Up @@ -65,6 +66,11 @@ class CreateFastCheckoutOrder
*/
private $searchCriteriaBuilder;

/**
* @var \Paynl\Payment\Helper\PayHelper;
*/
private $payHelper;

/**
* @param StoreManagerInterface $storeManager
* @param QuoteFactory $quote
Expand All @@ -75,6 +81,7 @@ class CreateFastCheckoutOrder
* @param ShippingMethodManagementInterface $shippingMethodManagementInterface
* @param OrderRepositoryInterface $orderRepositoryInterface
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param PayHelper $payHelper
*/
public function __construct(
StoreManagerInterface $storeManager,
Expand All @@ -85,7 +92,8 @@ public function __construct(
OrderFactory $orderFactory,
ShippingMethodManagementInterface $shippingMethodManagementInterface,
OrderRepositoryInterface $orderRepositoryInterface,
SearchCriteriaBuilder $searchCriteriaBuilder
SearchCriteriaBuilder $searchCriteriaBuilder,
PayHelper $payHelper
) {
$this->storeManager = $storeManager;
$this->quote = $quote;
Expand All @@ -96,12 +104,14 @@ public function __construct(
$this->shippingMethodManagementInterface = $shippingMethodManagementInterface;
$this->orderRepositoryInterface = $orderRepositoryInterface;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->payHelper = $payHelper;
}

/**
* @param array $params
* @return Order
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
* @throws \Exception
*/
public function create($params)
{
Expand All @@ -111,6 +121,7 @@ public function create($params)
$shippingAddressData = $checkoutData['shippingAddress'] ?? null;

if (empty($customerData) || empty($billingAddressData) || empty($shippingAddressData)) {
$this->payHelper->logCritical("Fast checkout: Missing data, cannot create order.", ['customerData' => $customerData, 'billingAddressData' => $billingAddressData, 'shippingAddressData' => $shippingAddressData]); // phpcs:ignore
throw new \Exception("Missing data, cannot create order.");
}

Expand All @@ -119,8 +130,25 @@ public function create($params)
$orderId = explode('fastcheckout', $params['orderId']);
$quoteId = $orderId[1] ?? '';

$this->payHelper->logDebug('Start fast checkout order create', ['quoteId' => $quoteId]);

try {
$quote = $this->quote->create()->loadByIdWithoutStore($quoteId);
$this->payHelper->logDebug('Fast checkout: quote->getId()', ['quoteId' => $quoteId, 'magentoQuoteId' => $quote->getId() ?? 'null']);

if (empty($quote['is_active'])) {
$this->payHelper->logDebug('Fast checkout: Quote inactive', ['quoteId' => $quoteId]);

$existingOrder = $this->getExistingOrder($quoteId);
if (empty($existingOrder)) {
$this->payHelper->logDebug('Fast checkout: Reactivating quote', ['quoteId' => $quoteId]);
$quote->setIsActive(true);
$quote->save();
} else {
return $existingOrder;
}
}

$storeId = $quote->getStoreId();

$shippingMethodQuote = $quote->getShippingAddress()->getShippingMethod();
Expand Down Expand Up @@ -148,7 +176,7 @@ public function create($params)
$quote->assignCustomer($customer);
$quote->setSendConfirmation(1);

$billingAddress = $quote->getBillingAddress()->addData(array(
$quote->getBillingAddress()->addData(array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $billingAddressData['firstName'] ?? $customerData['firstName'],
Expand All @@ -170,7 +198,7 @@ public function create($params)
'save_in_address_book' => 1,
));

$shippingAddress = $quote->getShippingAddress()->addData(array(
$quote->getShippingAddress()->addData(array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $shippingAddressData['firstName'] ?? $customerData['firstName'],
Expand All @@ -196,22 +224,26 @@ public function create($params)
$shippingAddress->setCollectShippingRates(true)->collectShippingRates();

$shippingData = $this->shippingMethodManagementInterface->getList($quote->getId());
$shippingMethodsAvaileble = [];
$shippingMethodsAvailable = [];
foreach ($shippingData as $shipping) {
$code = $shipping->getCarrierCode() . '_' . $shipping->getMethodCode();
$shippingMethodsAvaileble[$code] = $code;
$shippingMethodsAvailable[$code] = $code;
}

if (!empty($shippingMethodsAvaileble[$shippingMethodQuote])) {
$this->payHelper->logDebug('Fast checkout: Available shipping methods', $shippingMethodsAvailable);

if (!empty($shippingMethodsAvailable[$shippingMethodQuote])) {
$shippingMethod = $shippingMethodQuote;
} elseif (!empty($shippingMethodsAvaileble[$store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping')])) {
} elseif (!empty($shippingMethodsAvailable[$store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping')])) {
$shippingMethod = $store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping');
} elseif (!empty($shippingMethodsAvaileble[$store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping_backup')])) {
} elseif (!empty($shippingMethodsAvailable[$store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping_backup')])) {
$shippingMethod = $store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping_backup');
}

$this->payHelper->logDebug('Fast checkout: Shipping options', ['quote' => $shippingMethodQuote, 'setting' => $store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping'), 'setting_backup' => $store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping_backup')]); // phpcs:ignore

if (empty($shippingMethod)) {
throw new \Exception("No shipping method availeble");
throw new \Exception("No shipping method available");
}

$shippingAddress->setShippingMethod($shippingMethod);
Expand All @@ -226,23 +258,33 @@ public function create($params)

$service = $this->quoteManagement->submit($quote);
$increment_id = $service->getRealOrderId();
$this->payHelper->logDebug('Fast checkout: Reserved increment_id', [$increment_id]);

$order = $this->orderFactory->create()->loadByIncrementId($increment_id);
$additionalData = $order->getPayment()->getAdditionalInformation();
$additionalData['transactionId'] = $payOrderId;
$order->getPayment()->setAdditionalInformation($additionalData);
$order->save();
$this->payHelper->logDebug('Fast checkout: Created order_id', [$order->getId()]);

$order->addStatusHistoryComment(__('PAY. - Created iDEAL Fast Checkout order'))->save();
} catch (NoSuchEntityException $e) {
$this->payHelper->logDebug('Fast checkout: Quote not found', ['quoteId' => $quoteId, 'error' => $e->getMessage()]);
$order = $this->getExistingOrder($quoteId);
} catch (\Exception $e) {
$this->payHelper->logDebug('Fast checkout: Exception on create', ['quoteId' => $quoteId, 'error' => $e->getMessage()]);
throw new \Exception("Exception on create. " . $e->getMessage());
}
if (empty($order)) {
$this->payHelper->logCritical('Fast checkout: Both order & quote not found', ['quoteId' => $quoteId, 'searchCriteria' => $searchCriteria, 'searchResult' => $searchResult]);
throw new \Exception("Order & Quote can't be found. " . $quoteId, 404);
}
return $order;
}

/**
* @param string $quoteId
* @return Order
* @return Order|boolean
* @throws \Exception
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
*/
Expand All @@ -254,7 +296,7 @@ public function getExistingOrder($quoteId)
$order = array_shift($searchResult);
}
if (empty($order)) {
throw new \Exception("Order can't be found.");
return false;
}
return $order;
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "paynl/magento2-plugin",
"description": "PAY. Payment methods for Magento2",
"type": "magento2-module",
"version": "3.14.3",
"version": "3.14.4",
"require": {
"magento/module-sales": "^102.0.0 || ^103.0.0",
"magento/module-payment": "^100.3.0",
Expand Down
4 changes: 3 additions & 1 deletion view/frontend/web/css/payFastCheckout.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
background-repeat: no-repeat;
background-size: 51px;
background-position: 8px center;
color: white;
}

#top-cart-btn-fastcheckout {
Expand All @@ -36,6 +37,7 @@
background-repeat: no-repeat;
background-size: 51px;
background-position: 8px center;
color: white;
}

#paynl_fast_checkout_fallback button {
Expand All @@ -60,4 +62,4 @@
#paynl_fast_checkout_fallback button {
max-width: 250px;
}
}
}

0 comments on commit 59c3408

Please sign in to comment.