Skip to content

Commit

Permalink
Add callback function to support V6 order confirmation redirect (#17)
Browse files Browse the repository at this point in the history
* Add callback function to do order confirmation redirect.

* fix lint.

* Add metadata

* Use SFCC base version.

* lint fix
  • Loading branch information
serenayanbolt authored Oct 20, 2022
1 parent 9e8e1ab commit 895a89f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ exports.getSitePreferences = function () {
boltCdnUrl: boltCdnUrl,
boltMultiPublishableKey: boltMultiPublishableKey,
blockedCharactersList: blockedCharactersList,
boltEnableSessionRecording: Site.getCurrent().getCustomPreferenceValue('boltEnableSessionRecording') || false
boltEnableSessionRecording: Site.getCurrent().getCustomPreferenceValue('boltEnableSessionRecording') || false,
sfccBaseVersion: getSFCCBaseVersion()
};
};

Expand Down Expand Up @@ -90,3 +91,22 @@ exports.getSystemPreference = function (preferenceID) {
}
return System.getPreferences().getCustom()[preferenceID];
};

/**
* Returns the first digit configured in SFCC base version. "6.1.2" returns 6
* @returns {number} the first number
*/
function getSFCCBaseVersion() {
var version = 5;
var sfccBaseVersion = Site.getCurrent().getCustomPreferenceValue('sfccBaseVersion');
if (empty(sfccBaseVersion)) {
return baseVersion;
}

var baseVersion = sfccBaseVersion.split('.');
if (!empty(baseVersion)) {
version = parseInt(baseVersion[0], 10);
}

return version;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $(document).ready(function () {

$('[data-tid="instant-bolt-checkout-button"]').children().replaceWith($('[data-tid="instant-bolt-checkout-button"]').children().clone());
var createBoltOrderUrl = $('.create-bolt-order-url').val();
var sfccBaseVersion = $('#sfccBaseVersion').val();

// add an event handler to Bolt button's click
checkoutBoltButton.click(function (e) {
Expand All @@ -29,7 +30,13 @@ $(document).ready(function () {
id: data.basketID
};

var boltButtonApp = BoltCheckout.configure(cart, data.hints, null); // eslint-disable-line no-undef
var boltButtonApp;
if (sfccBaseVersion >= 6) {
boltButtonApp = BoltCheckout.configure(cart, data.hints, callbacks); // eslint-disable-line no-undef
} else {
boltButtonApp = BoltCheckout.configure(cart, data.hints, null); // eslint-disable-line no-undef
}

// don't open bolt modal for apple pay
if ($(e.target).attr('data-tid') !== 'apple-pay-button') {
boltButtonApp.open();
Expand All @@ -40,4 +47,62 @@ $(document).ready(function () {
});
}
}, 100);
var successRedirect = $('#successRedirect').val();
var sfccData;
var callbacks = {
close: function () {
// This function is called when the Bolt checkout modal is closed.
if (sfccData) {
var redirect = $('<form>')
.appendTo(document.body)
.attr({
method: 'POST',
action: successRedirect
});

$('<input>')
.appendTo(redirect)
.attr({
name: 'orderID',
value: sfccData.merchant_order_number
});

$('<input>')
.appendTo(redirect)
.attr({
name: 'orderToken',
value: sfccData.sfcc.sfcc_order_token
});

redirect.submit();
}
},
onCheckoutStart: function () {
// This function is called after the checkout form is presented to the user.
},

// eslint-disable-next-line no-unused-vars
onEmailEnter: function (email) {
// This function is called after the user enters their email address.
},

onShippingDetailsComplete: function () {
// This function is called when the user proceeds to the shipping options page.
// This is applicable only to multi-step checkout.
},

onShippingOptionsComplete: function () {
// This function is called when the user proceeds to the payment details page.
// This is applicable only to multi-step checkout.
},

onPaymentSubmit: function () {
// This function is called after the user clicks the pay button.
},
success: function (transaction, callback) {
// This function is called when the Bolt checkout transaction is successful.
sfccData = transaction;
callback();
}
};
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<isif condition="${pdict.config && pdict.config.boltEnable && pdict.config.boltEnableCartPage && pdict.boltVersion && pdict.boltVersion == 'V2' && (pdict.items.length !== 0 || pdict.giftCertificateItems.length !== 0)}">
<input type="hidden" class="create-bolt-order-url" value="${URLUtils.https('Bolt-GetOrderReference')}" />
<input type="hidden" id="sfccBaseVersion" name="sfccBaseVersion" value="${pdict.config.sfccBaseVersion}" />
<input type="hidden" id="successRedirect" name="successRedirect" value="${URLUtils.url('Order-Confirm')}" />

<isset name="boltCdnUrl" value="${pdict.config.boltCdnUrl}" scope="page" />
<isset name="boltPublishableKey" value="${pdict.config.boltMultiPublishableKey}" scope="page" />
Expand Down
11 changes: 11 additions & 0 deletions metadata/bolt-meta-import/meta/system-objecttype-extensions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,16 @@
<externally-managed-flag>false</externally-managed-flag>
<default-value>false</default-value>
</attribute-definition>
<attribute-definition attribute-id="sfccBaseVersion">
<display-name xml:lang="x-default">SFCC Base Version</display-name>
<description xml:lang="x-default">SFCC base version used in success callback.</description>
<type>string</type>
<mandatory-flag>false</mandatory-flag>
<externally-managed-flag>false</externally-managed-flag>
<min-length>0</min-length>
<regex>^([0-9]+\.)+[0-9]+$</regex>
<default-value>5.0.0</default-value>
</attribute-definition>
</custom-attribute-definitions>

<group-definitions>
Expand All @@ -649,6 +659,7 @@
<attribute attribute-id="boltMerchantDivisionID"/>
<attribute attribute-id="boltProcessorMapping"/>
<attribute attribute-id="boltEnableSessionRecording"/>
<attribute attribute-id="sfccBaseVersion"/>
</attribute-group>
<attribute-group group-id="Bolt Credentials Setting - V1">
<display-name xml:lang="x-default">Bolt Credentials Setting - V1</display-name>
Expand Down

0 comments on commit 895a89f

Please sign in to comment.