Skip to content

Commit

Permalink
Adapt Self-Hosted(Magento Storefront) Checkout for RSA. (#35)
Browse files Browse the repository at this point in the history
* Adapt Self-Hosted(Magento Storefront) Checkout for RSA.

* Adapt Self-Hosted(Magento Storefront) Checkout for RSA.

* Adapt FireTm Checkout one-step checkout for RSA.

* Move refresh and taxes requests to Magento storefront.

* Remove requests queues.

* Adapt FireTm Checkout one-step checkout for RSA.

* Adapt FireTm Checkout one-step checkout for RSA.

* Add translation to payment error message.

* Remove redundant variables.

---------

Co-authored-by: MykolaMalovanets <[email protected]>
  • Loading branch information
nmalevanec and MykolaMalovanets authored Dec 15, 2023
1 parent be63888 commit ccc5ee9
Show file tree
Hide file tree
Showing 10 changed files with 525 additions and 514 deletions.
10 changes: 8 additions & 2 deletions Checkout/Api/Platform/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ public static function setAddresses(
return self::getErrorResult($cartId, $response);
}
$payload = json_decode($request->getRawBody());
/** @var Bold_Checkout_Model_Config $config */
$config = Mage::getSingleton(Bold_Checkout_Model_Config::RESOURCE);
try {
self::prepareStore($quote);
if ($payload->billing_address === null) {
$quote->removeAddress($quote->getBillingAddress()->getId());
$quote->removeAddress($quote->getShippingAddress()->getId());
$quote->setDataChanges(true);
$quote->collectTotals();
$quote->save();
if (!$config->isCheckoutTypeSelfHosted((int)$quote->getStore()->getWebsiteId())) {
$quote->save();
}
$quoteData = Bold_Checkout_Service_Extractor_Quote::extract($quote);
return Bold_Checkout_Rest::buildResponse($response, json_encode($quoteData));
}
Expand All @@ -75,7 +79,9 @@ public static function setAddresses(
}
$quote->setDataChanges(true);
$quote->collectTotals();
$quote->save();
if (!$config->isCheckoutTypeSelfHosted((int)$quote->getStore()->getWebsiteId())) {
$quote->save();
}
$quoteData = Bold_Checkout_Service_Extractor_Quote::extract($quote);
} catch (Mage_Core_Model_Store_Exception $e) {
return self::buildErrorResponse($e->getMessage(), $response);
Expand Down
9 changes: 0 additions & 9 deletions Checkout/Api/Platform/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public static function save(
Mage_Core_Controller_Request_Http $request,
Mage_Core_Controller_Response_Http $response
) {
/** @var Bold_Checkout_Model_Config $config */
$config = Mage::getSingleton(Bold_Checkout_Model_Config::RESOURCE);
$requestBody = json_decode($request->getRawBody());
if (!isset($requestBody->customer)) {
return Bold_Checkout_Rest::buildErrorResponse(
Expand All @@ -54,13 +52,6 @@ public static function save(
'server.validation_error'
);
}
if ($config->isCheckoutTypeSelfHosted($requestBody->customer->website_id)) {
return Bold_Checkout_Rest::buildResponse(
$response,
null,
201
);
}
$customerData = $requestBody->customer;
/** @var Mage_Customer_Model_Customer $customer */
$customer = Mage::getModel('customer/customer');
Expand Down
16 changes: 7 additions & 9 deletions Checkout/Block/Form/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
class Bold_Checkout_Block_Form_Payments extends Mage_Payment_Block_Form
{
const URL = 'https://api.boldcommerce.com/checkout/storefront/';
const PATH = '/checkout/storefront/';

/**
* @var array
Expand Down Expand Up @@ -56,7 +56,6 @@ public function customerIsGuest()
* Get allowed countries.
*
* @return string
* @throws Mage_Core_Exception
*/
public function getAllowedCountries()
{
Expand All @@ -81,7 +80,6 @@ public function getAllowedCountries()
* Get storefront Bold client url.
*
* @return string|null
* @throws Mage_Core_Exception
*/
public function getStoreFrontClientUrl()
{
Expand Down Expand Up @@ -124,29 +122,29 @@ public function getIframeUrl()
}
$websiteId = Mage::app()->getWebsite()->getId();
$shopId = Bold_Checkout_Service_ShopIdentifier::getShopIdentifier($websiteId);
$styles = $this->getStyles();
$styles = $this->getIframeStyles();
if ($styles) {
Bold_Checkout_StorefrontClient::call('POST', 'payments/styles', $styles);
}
$orderId = $boldCheckoutData->data->public_order_id;
$jwtToken = $boldCheckoutData->data->jwt_token;
return self::URL . $shopId . '/' . $orderId . '/payments/iframe?token=' . $jwtToken;
/** @var Bold_Checkout_Model_Config $config */
$config = Mage::getModel(Bold_Checkout_Model_Config::RESOURCE);
$apiUrl = $config->getApiUrl($websiteId);
return $apiUrl . self::PATH . $shopId . '/' . $orderId . '/payments/iframe?token=' . $jwtToken;
}

/**
* Get payment iframe styles from file.
*
* @return array|null
*/
private function getStyles()
private function getIframeStyles()
{
$styles = Mage::getModuleDir('data', 'Bold_Checkout') . DS . 'form/payments/styles.json';
// @phpcs:disable MEQP1.Security.DiscouragedFunction.Found
if (file_exists($styles)) {
return json_decode(file_get_contents($styles), true);
}
// @phpcs:enable MEQP1.Security.DiscouragedFunction.Found

return null;
}
}
26 changes: 8 additions & 18 deletions Checkout/Observer/CheckoutObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public function beforeCheckout(Varien_Event_Observer $event)
}

/**
* Place order on Bold side.
* Place order on Bold side before Magento order is placed.
*
* Before Magento order is placed Bold order should be processed.
*
* @param Varien_Event_Observer $event
* @return void
Expand All @@ -88,28 +90,16 @@ public function beforeSaveOrder(Varien_Event_Observer $event)
if (!$boldCheckoutData || $paymentMethod !== Bold_Checkout_Service_PaymentMethod::CODE) {
return;
}
$taxes = Bold_Checkout_StorefrontClient::call('POST', 'taxes');
if (isset($taxes->errors)) {
$this->throwException();
}
if ($order->getDiscountAmount()) {
$discounts = Bold_Checkout_StorefrontClient::call(
'POST',
'discounts',
['code' => 'Discount']
);
if ($discounts->errors) {
$this->throwException();
}
}
$processOrder = Bold_Checkout_StorefrontClient::call('POST', 'process_order');
if (isset($processOrder->errors)) {
$processOrderResult = Bold_Checkout_StorefrontClient::call('POST', 'process_order');
if (isset($processOrderResult->errors)) {
$this->throwException();
}
}

/**
* Save bold order data to database.
* Save Bold order data to database after order has been placed on Magento side.
*
* After Magento order has been placed, we have order id and can save Bold order data(public id) to database.
*
* @param Varien_Event_Observer $event
* @return void
Expand Down
164 changes: 0 additions & 164 deletions Checkout/Observer/OnepageControllerObserver.php

This file was deleted.

37 changes: 36 additions & 1 deletion Checkout/Service/Order/ProcessOrder.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
<?php

/**
* Update Magento order payment information.
*/
class Bold_Checkout_Service_Order_ProcessOrder
{
/**
* Update order payment.
*
* Due to order is placed on Magento side in case of self-hosted checkout with Magento storefront
* only payment information should be updated.
*
* @param stdClass $payload
* @return Mage_Sales_Model_Order
* @throws Mage_Core_Exception
*/
public static function process(stdClass $payload)
{
//todo: implement.
$attempt = 1;
do {
/** @var Bold_Checkout_Model_Order $extOrderData */
$extOrderData = Mage::getModel(Bold_Checkout_Model_Order::RESOURCE);
$extOrderData->load($payload->order->publicId, Bold_Checkout_Model_Resource_Order::PUBLIC_ID);
$orderId = $extOrderData->getOrderId();
if (!$orderId) {
$attempt++;
sleep(1);
}
} while (!$orderId && $attempt < 5);
/** @var Mage_Sales_Model_Order $order */
$order = Mage::getModel('sales/order')->load($extOrderData->getOrderId());
if (!$order->getId()) {
Mage::throwException(Mage::helper('core')->__('Order not found'));
}
Bold_Checkout_Service_Order_Payment::processPayment(
$order,
$payload->order->payment,
$payload->order->transaction
);
Mage::dispatchEvent('bold_order_process_after', ['order' => $order, 'payload' => $payload]);
return current(Bold_Checkout_Service_Extractor_Order::extract([$order]));
}
}
Loading

0 comments on commit ccc5ee9

Please sign in to comment.