Skip to content

Commit

Permalink
Remove BoltCheckout.configure from check callback in Admin and Firech…
Browse files Browse the repository at this point in the history
…eckout (#703)

* initial refactor without unit test

* removed unused code

* update unit test

* Minor formatting

* removed user notification

Co-authored-by: Leon McCottry <[email protected]>
Co-authored-by: Aden Dong <[email protected]>
  • Loading branch information
3 people authored Mar 13, 2020
1 parent 9ff3da5 commit 03e3c61
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 65 deletions.
24 changes: 7 additions & 17 deletions app/code/community/Bolt/Boltpay/Block/Checkout/Boltpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,17 @@ public function buildBoltCheckoutJavascript($checkoutType, $quote, $hintData, $c
if ($shouldCloneImmediately) break;
case self::CHECKOUT_TYPE_ADMIN:
case self::CHECKOUT_TYPE_FIRECHECKOUT:
// We postpone calling configure until Bolt button clicked and form is ready
// This also allows us to save in cost of unnecessary quote creation
$doChecks = 'var do_checks = 0;';
$boltConfigureCall = "
BoltCheckout.configure(
new Promise(
function (resolve, reject) {
// Store state must be validated prior to open
(resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
}
),
json_hints,
{
check: function() {
$checkCustom
$onCheckCallback
$boltConfigureCall
return true;
}
}
);
$callbacks
);
";
break;
case self::CHECKOUT_TYPE_PRODUCT_PAGE:
Expand All @@ -231,16 +222,15 @@ function (resolve, reject) {
break;
}

if (!isset($doChecks)) $doChecks = 'var do_checks = 1;';

$boltCheckoutJavascript = "
var \$hints_transform = $hintsTransformFunction;
var resolvePromise;
var rejectPromise;
var get_json_cart = function() { return $jsonCart };
var json_hints = \$hints_transform($jsonHints);
var quote_id = '{$quote->getId()}';
var order_completed = false;
$doChecks
window.BoltModal = $boltConfigureCall
";
Expand Down
34 changes: 29 additions & 5 deletions app/code/community/Bolt/Boltpay/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ public function getBoltCallbacks($checkoutType, $isVirtualQuote = false)

return "{
check: function() {
if (do_checks++) {
$checkCustom
$onCheckCallback
}
$checkCustom
$onCheckCallback
return true;
},
onCheckoutStart: function() {
Expand Down Expand Up @@ -98,13 +96,37 @@ public function getBoltCallbacks($checkoutType, $isVirtualQuote = false)
}

/**
* @param $checkoutType
* @param string $checkoutType
* @param bool $isVirtualQuote
*
* @return string
*
* @throws Mage_Core_Model_Store_Exception
*/
public function buildOnCheckCallback($checkoutType, $isVirtualQuote = false)
{
if ( $checkoutType === Bolt_Boltpay_Block_Checkout_Boltpay::CHECKOUT_TYPE_ADMIN ) {
$checkoutTokenUrl = $this->getMagentoUrl("adminhtml/sales_order_create/create/checkoutType/$checkoutType", array(), true);
} else {
$checkoutTokenUrl = $this->getMagentoUrl("boltpay/order/create/checkoutType/$checkoutType");
}

$ajaxCall = "
new Ajax.Request('$checkoutTokenUrl', {
method:'post',
parameters: '',
onSuccess: function(response) {
if(response.responseJSON.error) {
// TODO: Consider informing the user of the error. This could be handled Bolt-server-side
rejectPromise(response.responseJSON.error_messages);
location.reload();
} else {
resolvePromise(response.responseJSON.cart_data);
}
},
onFailure: function(error) { rejectPromise(error); }
});
";
switch ($checkoutType) {
case Bolt_Boltpay_Block_Checkout_Boltpay::CHECKOUT_TYPE_ADMIN:
return
Expand All @@ -126,12 +148,14 @@ public function buildOnCheckCallback($checkoutType, $isVirtualQuote = false)
} "). "
bolt_hidden.classList.add('required-entry');
$ajaxCall
}
";
case Bolt_Boltpay_Block_Checkout_Boltpay::CHECKOUT_TYPE_FIRECHECKOUT:
return
"
if (!checkout.validate()) return false;
$ajaxCall
";
case Bolt_Boltpay_Block_Checkout_Boltpay::CHECKOUT_TYPE_PRODUCT_PAGE:
return /** @lang JavaScript */ 'if (!boltConfigPDP.validate()) return false;';
Expand Down
61 changes: 26 additions & 35 deletions tests/unit/testsuite/Bolt/Boltpay/Block/Checkout/BoltpayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1656,11 +1656,10 @@ private function buildBoltCheckoutJavascript_withVariousConfigsSetUp($checkoutTy
* @param string $checkoutType currently in use
* @param bool $isShoppingCartPage whether current page is checkout/cart
* @param bool $shouldCloneImmediately inverse of extra-config flag for cloneOnClick
* @param bool $expectPostponedConfiguration is postponed or normal configuration expected
*
* @throws Mage_Core_Exception from test setup if unable to stub helper
*/
public function buildBoltCheckoutJavascript_withVariousConfigs_returnsBoltJs($checkoutType, $isShoppingCartPage, $shouldCloneImmediately, $expectPostponedConfiguration)
public function buildBoltCheckoutJavascript_withVariousConfigs_returnsBoltJs($checkoutType, $isShoppingCartPage, $shouldCloneImmediately)
{
list($hintData, $cartData, $currentMock, $quoteMock) = $this->buildBoltCheckoutJavascript_withVariousConfigsSetUp(
$checkoutType,
Expand All @@ -1671,7 +1670,7 @@ public function buildBoltCheckoutJavascript_withVariousConfigs_returnsBoltJs($ch
->with(
'bolt_boltpay_filter_bolt_checkout_javascript',
$this->callback(
function ($js) use ($cartData, $hintData, $expectPostponedConfiguration) {
function ($js) use ($cartData, $hintData, $shouldCloneImmediately) {
$this->assertContains(
sprintf('var $hints_transform = %s;', self::HINTS_TRANSFORM_FUNCTION),
$js
Expand All @@ -1692,25 +1691,25 @@ function ($js) use ($cartData, $hintData, $expectPostponedConfiguration) {
);
$this->assertContains(sprintf("var quote_id = '%s';", self::QUOTE_ID), $js);
$this->assertContains('var order_completed = false;', $js);
$this->assertContains(sprintf('var do_checks = %d;', !$expectPostponedConfiguration), $js);
$windowBoltModal = explode('window.BoltModal = ', $js)[1];
$this->assertContains(
'BoltCheckout.configure(get_json_cart(),json_hints,/*BOLT-CALLBACKS*/);',
preg_replace('/\s*/', '', $windowBoltModal)
);

if ($expectPostponedConfiguration) {
$this->assertRegExp(
/** @lang PhpRegExp */ '/BoltCheckout\.configure\(\s*new Promise/',
$windowBoltModal
if ($shouldCloneImmediately) {
$this->assertContains(
'BoltCheckout.configure(get_json_cart(),json_hints,/*BOLT-CALLBACKS*/);',
preg_replace('/\s*/', '', $windowBoltModal)
);
$this->assertNotContains(
'BoltCheckout.configure(newPromise((resolve,reject)=>{resolvePromise=resolve;rejectPromise=reject;}),json_hints,/*BOLT-CALLBACKS*/);',
preg_replace('/\s*/', '', $windowBoltModal)
);
} else {
$this->assertContains(
'BoltCheckout.configure(newPromise((resolve,reject)=>{resolvePromise=resolve;rejectPromise=reject;}),json_hints,/*BOLT-CALLBACKS*/);',
preg_replace('/\s*/', '', $windowBoltModal)
);
$this->assertRegExp(
sprintf(
/** @lang PhpRegExp */ '/\{\s*check: function\(\) \{\s*%s\s*%s/',
preg_quote(self::CHECK_FUNCTION),
preg_quote(self::ON_CHECK_CALLBACK)
),
$windowBoltModal
$this->assertNotContains(
'BoltCheckout.configure(get_json_cart(),json_hints,/*BOLT-CALLBACKS*/);',
preg_replace('/\s*/', '', $windowBoltModal)
);
}

Expand All @@ -1731,52 +1730,45 @@ function ($js) use ($cartData, $hintData, $expectPostponedConfiguration) {
/**
* Data provider for {@see buildBoltCheckoutJavascript_withVariousConfigs_returnsBoltJs}
*
* @return array containing checkout type, is current page cart flag, should clone immediately config and flag whether to expect postponed configuration
* @return array containing checkout type, is current page cart flag, and should clone immediately config
*/
public function buildBoltCheckoutJavascript_withVariousConfigsProvider()
{
return array(
array(
'checkoutType' => BoltpayCheckoutBlock::CHECKOUT_TYPE_MULTI_PAGE,
'isShoppingCartPage' => true,
'shouldCloneImmediately' => true,
'expectPostponedConfiguration' => false
'shouldCloneImmediately' => true
),
array(
'checkoutType' => BoltpayCheckoutBlock::CHECKOUT_TYPE_MULTI_PAGE,
'isShoppingCartPage' => true,
'shouldCloneImmediately' => false,
'expectPostponedConfiguration' => true
'shouldCloneImmediately' => false
),
array(
'checkoutType' => BoltpayCheckoutBlock::CHECKOUT_TYPE_MULTI_PAGE,
'isShoppingCartPage' => false,
'shouldCloneImmediately' => false,
'expectPostponedConfiguration' => true
'shouldCloneImmediately' => false
),
array(
'checkoutType' => BoltpayCheckoutBlock::CHECKOUT_TYPE_ONE_PAGE,
'isShoppingCartPage' => true,
'shouldCloneImmediately' => true,
'expectPostponedConfiguration' => false
'shouldCloneImmediately' => true
),
array(
'checkoutType' => BoltpayCheckoutBlock::CHECKOUT_TYPE_ONE_PAGE,
'isShoppingCartPage' => true,
'shouldCloneImmediately' => false,
'expectPostponedConfiguration' => true
'shouldCloneImmediately' => false
),
array(
'checkoutType' => BoltpayCheckoutBlock::CHECKOUT_TYPE_ADMIN,
'isShoppingCartPage' => false,
'shouldCloneImmediately' => false,
'expectPostponedConfiguration' => true
'shouldCloneImmediately' => false
),
array(
'checkoutType' => BoltpayCheckoutBlock::CHECKOUT_TYPE_FIRECHECKOUT,
'isShoppingCartPage' => false,
'shouldCloneImmediately' => false,
'expectPostponedConfiguration' => true
'shouldCloneImmediately' => false
),
);
}
Expand Down Expand Up @@ -1820,7 +1812,6 @@ function ($js) use ($cartData, $hintData) {
);
$this->assertContains(sprintf("var quote_id = '%s';", self::QUOTE_ID), $js);
$this->assertContains('var order_completed = false;', $js);
$this->assertContains('var do_checks = 1;', $js);
$windowBoltModal = explode('window.BoltModal = ', $js)[1];
$this->assertContains(
"BoltCheckout.configureProductCheckout(get_json_cart(),json_hints,/*BOLT-CALLBACKS*/,{checkoutButtonClassName:'bolt-product-checkout-button'});",
Expand Down
14 changes: 6 additions & 8 deletions tests/unit/testsuite/Bolt/Boltpay/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public function buildOnCheckCallback_whenCheckoutTypeIsAdmin_returnsCorrectJs($i
} ") . "
bolt_hidden.classList.add('required-entry');
}
new Ajax.Request('
";
$result = $this->currentMock->buildOnCheckCallback($checkoutType, $isVirtualQuote);
$this->assertEquals(
$this->assertContains(
preg_replace('/\s/', '', $checkCallback),
preg_replace('/\s/', '', $result)
);
Expand All @@ -95,7 +95,7 @@ public function buildOnCheckCallback_whenCheckoutTypeIsAdmin_returnsCorrectJsPro
*/
public function buildOnCheckCallback_whenCheckoutTypeIsFireCheckout_returnsCorrectJs()
{
$expected = 'if (!checkout.validate()) return false;';
$expected = 'if (!checkout.validate()) return false; new Ajax.Request(\'';
$trueResult = $this->currentMock->buildOnCheckCallback(
Bolt_Boltpay_Block_Checkout_Boltpay::CHECKOUT_TYPE_FIRECHECKOUT,
true
Expand All @@ -104,11 +104,11 @@ public function buildOnCheckCallback_whenCheckoutTypeIsFireCheckout_returnsCorre
Bolt_Boltpay_Block_Checkout_Boltpay::CHECKOUT_TYPE_FIRECHECKOUT,
false
);
$this->assertEquals(
$this->assertContains(
preg_replace('/\s+/', '', $expected),
preg_replace('/\s+/', '', $trueResult)
);
$this->assertEquals(
$this->assertContains(
preg_replace('/\s+/', '', $expected),
preg_replace('/\s+/', '', $falseResult)
);
Expand Down Expand Up @@ -351,9 +351,7 @@ public function getBoltCallbacks_returnsCorrectJs()

$expected = "{
check: function() {
if (do_checks++) {
if (!boltConfigPDP.validate()) return false;
}
if (!boltConfigPDP.validate()) return false;
return true;
},
onCheckoutStart: function() {
Expand Down

0 comments on commit 03e3c61

Please sign in to comment.