-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create temporary basket for express pdp (#1183)
* feat(SFI-876): applepay express pdp * chore(SFI-876): unit tests * chore(SFI-876): unit tests * feat(SFI-876): create temporary basket for express pdp * fix(SFI-876): handle temporary basket creation failure * fix: adding csrf validation to applePayExpressCommon.js and linting * chore: linting --------- Co-authored-by: Zenit Shkreli <[email protected]>
- Loading branch information
1 parent
c49aafa
commit 38adebf
Showing
19 changed files
with
316 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
src/cartridges/app_adyen_SFRA/cartridge/client/default/js/product/detail.js
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
src/cartridges/app_adyen_SFRA/cartridge/client/default/js/product/expressPayments.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const applePayExpressModule = require('../applePayExpressCommon'); | ||
const { APPLE_PAY } = require('../constants'); | ||
|
||
function getProductForm(product) { | ||
const $productInputEl = document.createElement('input'); | ||
$productInputEl.setAttribute('id', 'selected-express-product'); | ||
$productInputEl.setAttribute('name', 'selected-express-product'); | ||
$productInputEl.setAttribute('type', 'hidden'); | ||
$productInputEl.setAttribute('data-pid', `${product.id}`); | ||
$productInputEl.setAttribute('data-basketId', ''); | ||
$productInputEl.value = JSON.stringify(product); | ||
const $productForm = document.createElement('form'); | ||
$productForm.setAttribute('id', 'express-product-form'); | ||
$productForm.setAttribute('name', 'express-product-form'); | ||
$productForm.append($productInputEl); | ||
return $productForm; | ||
} | ||
|
||
function getValueForCurrency(amount, currency) { | ||
const value = Math.round(amount * 10 ** window.fractionDigits); | ||
return { value, currency }; | ||
} | ||
|
||
function getExpressPaymentButtons(product) { | ||
const expressMethodsConfig = { | ||
[APPLE_PAY]: window.isApplePayExpressOnPdpEnabled === 'true', | ||
}; | ||
const enabledExpressPaymentButtons = []; | ||
Object.keys(expressMethodsConfig).forEach((key) => { | ||
if (expressMethodsConfig[key]) { | ||
const $container = document.createElement('div'); | ||
$container.setAttribute('id', `${key}-pdp`); | ||
$container.setAttribute('class', `expressComponent ${key}`); | ||
$container.setAttribute('data-method', `${key}`); | ||
$container.setAttribute('data-pid', `${product.id}`); | ||
enabledExpressPaymentButtons.push($container); | ||
} | ||
}); | ||
return enabledExpressPaymentButtons; | ||
} | ||
|
||
function renderApplePayButton() { | ||
applePayExpressModule.init(); | ||
} | ||
|
||
function renderExpressPaymentButtons() { | ||
$('body').on('product:renderExpressPaymentButtons', (e, response) => { | ||
const { product = {} } = response; | ||
const $expressPaymentButtonsContainer = document.getElementById( | ||
'express-payment-buttons', | ||
); | ||
if (product.readyToOrder && product.available) { | ||
const { price, selectedQuantity } = product; | ||
const { value, currency } = price.sales; | ||
const amount = getValueForCurrency(value * selectedQuantity, currency); | ||
window.basketAmount = JSON.stringify(amount); | ||
const expressPaymentButtons = getExpressPaymentButtons(product); | ||
const $productForm = getProductForm(product); | ||
$expressPaymentButtonsContainer.replaceChildren( | ||
...expressPaymentButtons, | ||
$productForm, | ||
); | ||
renderApplePayButton(); | ||
} else { | ||
$expressPaymentButtonsContainer.replaceChildren(); | ||
} | ||
}); | ||
} | ||
|
||
function init() { | ||
$('body').on('product:updateAddToCart', (e, response) => { | ||
$('body').trigger('product:renderExpressPaymentButtons', { | ||
product: response.product, | ||
}); | ||
}); | ||
$(document).ready(async () => { | ||
$.spinner().start(); | ||
const dataUrl = $('.quantity-select').find('option:selected').data('url'); | ||
const productVariation = await fetch(dataUrl); | ||
if (productVariation.ok) { | ||
const { product } = await productVariation.json(); | ||
$('body').trigger('product:renderExpressPaymentButtons', { | ||
product, | ||
}); | ||
} | ||
$.spinner().stop(); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
init, | ||
renderExpressPaymentButtons, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.