Skip to content

Commit 4ea737a

Browse files
authoredJan 23, 2025··
Improve response body (#1239)
* chore: remove basket id from response * chore: clean session attribute in case of error
1 parent d3d6f1e commit 4ea737a

File tree

10 files changed

+57
-35
lines changed

10 files changed

+57
-35
lines changed
 

‎src/cartridges/app_adyen_SFRA/cartridge/client/default/js/__tests__/applePayExpress.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ describe('getShippingMethod', () => {
449449
it('should handle fetch rejection', async () => {
450450
fetch.mockRejectedValue(new Error('Fetch failed'));
451451
try {
452-
await getShippingMethod(null, mockBasketId);
452+
await getShippingMethod(null);
453453
} catch (error) {
454454
expect(error.message).toBe('Fetch failed');
455455
}

‎src/cartridges/app_adyen_SFRA/cartridge/client/default/js/applePayExpress.js

+7-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const { APPLE_PAY } = require('./constants');
99

1010
let checkout;
1111
let shippingMethodsData;
12-
let temporaryBasketId;
1312

1413
function formatCustomerObject(customerData, billingData) {
1514
return {
@@ -110,12 +109,12 @@ function callPaymentFromComponent(data, resolveApplePay, rejectApplePay) {
110109
});
111110
}
112111

113-
async function selectShippingMethod({ shipmentUUID, ID }, basketId, reject) {
112+
async function selectShippingMethod({ shipmentUUID, ID }, reject) {
114113
const requestBody = {
115114
paymentMethodType: APPLE_PAY,
116115
shipmentUUID,
117116
methodID: ID,
118-
basketId,
117+
isExpressPdp: true,
119118
};
120119
return $.ajax({
121120
type: 'POST',
@@ -130,10 +129,10 @@ async function selectShippingMethod({ shipmentUUID, ID }, basketId, reject) {
130129
}).fail(() => reject());
131130
}
132131

133-
function getShippingMethod(shippingContact, basketId, reject) {
132+
function getShippingMethod(shippingContact, reject) {
134133
const requestBody = {
135134
paymentMethodType: APPLE_PAY,
136-
basketId,
135+
isExpressPdp: true,
137136
};
138137
if (shippingContact) {
139138
requestBody.address = {
@@ -204,7 +203,7 @@ async function onAuthorized(resolve, reject, event, amountValue, merchantName) {
204203
};
205204

206205
await callPaymentFromComponent(
207-
{ ...stateData, customer, basketId: temporaryBasketId },
206+
{ ...stateData, customer, isExpressPdp: true },
208207
resolveApplePay,
209208
reject,
210209
);
@@ -229,7 +228,6 @@ async function onShippingMethodSelected(
229228
);
230229
const calculationResponse = await selectShippingMethod(
231230
matchingShippingMethod,
232-
temporaryBasketId,
233231
reject,
234232
);
235233
if (calculationResponse?.grandTotalAmount) {
@@ -252,16 +250,11 @@ async function onShippingMethodSelected(
252250

253251
async function onShippingContactSelected(resolve, reject, event, merchantName) {
254252
const { shippingContact } = event;
255-
shippingMethodsData = await getShippingMethod(
256-
shippingContact,
257-
temporaryBasketId,
258-
reject,
259-
);
253+
shippingMethodsData = await getShippingMethod(shippingContact, reject);
260254
if (shippingMethodsData?.shippingMethods?.length) {
261255
const selectedShippingMethod = shippingMethodsData.shippingMethods[0];
262256
const newCalculation = await selectShippingMethod(
263257
selectedShippingMethod,
264-
temporaryBasketId,
265258
reject,
266259
);
267260
if (newCalculation?.grandTotalAmount) {
@@ -327,8 +320,7 @@ async function init(paymentMethodsResponse) {
327320
onClick: async (resolve, reject) => {
328321
if (window.isExpressPdp) {
329322
const tempBasketResponse = await createTemporaryBasket();
330-
if (tempBasketResponse?.basketId) {
331-
temporaryBasketId = tempBasketResponse.basketId;
323+
if (tempBasketResponse?.temporaryBasketCreated) {
332324
applePayButtonConfig.amount = {
333325
value: tempBasketResponse.amount.value,
334326
currency: tempBasketResponse.amount.currency,

‎src/cartridges/app_adyen_SFRA/cartridge/client/default/js/commons/index.js

+23
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ module.exports.getPaymentMethods = async function getPaymentMethods() {
3939
});
4040
};
4141

42+
/**
43+
* Makes an ajax call to the controller function createTemporaryBasket
44+
*/
45+
module.exports.createTemporaryBasket = async function createTemporaryBasket() {
46+
const productForm = document.getElementById('express-product-form');
47+
const data = new FormData(productForm);
48+
const dataFromEntries = Object.fromEntries(data.entries());
49+
const parsedData = JSON.parse(dataFromEntries['selected-express-product']);
50+
return $.ajax({
51+
url: window.createTemporaryBasketUrl,
52+
type: 'post',
53+
data: {
54+
csrf_token: $('#adyen-token').val(),
55+
data: JSON.stringify({
56+
id: parsedData.id,
57+
bundledProducts: parsedData.bundledProducts,
58+
options: parsedData.options,
59+
selectedQuantity: parsedData.selectedQuantity,
60+
}),
61+
},
62+
});
63+
};
64+
4265
module.exports.checkIfExpressMethodsAreReady =
4366
function checkIfExpressMethodsAreReady() {
4467
const expressMethodsConfig = {

‎src/cartridges/app_adyen_SFRA/cartridge/templates/default/product/components/addToCartButtonExtension.isml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
window.fractionDigits = "${AdyenHelper.getFractionDigits()}"
2121
</script>
2222

23+
<input type="hidden" id="adyen-token" name="${pdict.csrf.tokenName}" value="${pdict.csrf.token}"/>
2324
<div id="express-payment-buttons"></div>
24-
<input type="hidden" id="adyen-token" name="${pdict.csrf.tokenName}" value="${pdict.csrf.token}"/>

‎src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/createTemporaryBasket.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function createTemporaryBasket(req, res, next) {
4444
try {
4545
// Delete any existing open temporary baskets
4646
Transaction.wrap(() => {
47+
session.privacy.temporaryBasketId = null;
4748
BasketMgr.getTemporaryBaskets()
4849
.toArray()
4950
.forEach((basket) => {
@@ -56,11 +57,12 @@ function createTemporaryBasket(req, res, next) {
5657
if (!tempBasket) {
5758
throw new Error('Temporary basket not created');
5859
}
59-
const { id, bundledProducts, options, selectedQuantity } = req.form[
60-
'selected-express-product'
61-
]
62-
? JSON.parse(req.form['selected-express-product'])
63-
: {};
60+
session.privacy.temporaryBasketId = tempBasket.UUID;
61+
62+
const { id, bundledProducts, options, selectedQuantity } = JSON.parse(
63+
req.form.data,
64+
);
65+
6466
addProductToBasket(
6567
tempBasket,
6668
id,
@@ -73,11 +75,12 @@ function createTemporaryBasket(req, res, next) {
7375
currency: tempBasket.getTotalGrossPrice().currencyCode,
7476
};
7577
res.json({
76-
basketId: tempBasket.UUID,
78+
temporaryBasketCreated: true,
7779
amount,
7880
});
7981
} catch (error) {
8082
AdyenLogs.error_log('Failed to create temporary basket', error);
83+
session.privacy.temporaryBasketId = null;
8184
res.setStatusCode(500);
8285
res.json({
8386
errorMessage: Resource.msg(

‎src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/selectShippingMethods.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ const paypalHelper = require('*/cartridge/adyen/utils/paypalHelper');
1717
// eslint-disable-next-line complexity
1818
function callSelectShippingMethod(req, res, next) {
1919
const {
20-
basketId,
20+
isExpressPdp,
2121
shipmentUUID,
2222
methodID,
2323
currentPaymentData,
2424
paymentMethodType,
2525
} = JSON.parse(req.form.data);
26-
const currentBasket = basketId
27-
? BasketMgr.getTemporaryBasket(basketId)
26+
const currentBasket = isExpressPdp
27+
? BasketMgr.getTemporaryBasket(session.privacy.temporaryBasketId)
2828
: BasketMgr.getCurrentBasket();
2929

3030
if (!currentBasket) {

‎src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/shippingMethods.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ function updateShippingAddress(currentBasket, address) {
2626
}
2727
}
2828

29-
function getBasket(basketId) {
30-
return basketId
31-
? BasketMgr.getTemporaryBasket(basketId)
29+
function getBasket(isExpressPdp) {
30+
return isExpressPdp
31+
? BasketMgr.getTemporaryBasket(session.privacy.temporaryBasketId)
3232
: BasketMgr.getCurrentBasket();
3333
}
3434
/**
3535
* Make a request to Adyen to get shipping methods
3636
*/
3737
function callGetShippingMethods(req, res, next) {
3838
try {
39-
const { address, currentPaymentData, paymentMethodType, basketId } =
39+
const { address, currentPaymentData, paymentMethodType, isExpressPdp } =
4040
JSON.parse(req.form.data);
41-
const currentBasket = getBasket(basketId);
41+
const currentBasket = getBasket(isExpressPdp);
4242
if (!currentBasket) {
4343
res.json({
4444
error: true,

‎src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/payments/paymentFromComponent.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ function canSkipSummaryPage(reqDataObj) {
164164
* Make a payment from inside a component, skipping the summary page. (paypal, QRcodes, MBWay)
165165
*/
166166
function paymentFromComponent(req, res, next) {
167-
const { basketId, ...reqDataObj } = JSON.parse(req.form.data);
167+
const { isExpressPdp, ...reqDataObj } = JSON.parse(req.form.data);
168168
if (reqDataObj.cancelTransaction) {
169169
return handleCancellation(res, next, reqDataObj);
170170
}
171-
const currentBasket = basketId
172-
? BasketMgr.getTemporaryBasket(basketId)
171+
const currentBasket = isExpressPdp
172+
? BasketMgr.getTemporaryBasket(session.privacy.temporaryBasketId)
173173
: BasketMgr.getCurrentBasket();
174174
let paymentInstrument;
175175
Transaction.wrap(() => {

‎src/cartridges/int_adyen_SFRA/cartridge/adyen/utils/clearForms.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function clearCustomSessionFields() {
1414
session.privacy.partialPaymentData = null;
1515
session.privacy.amazonExpressShopperDetail = null;
1616
session.privacy.giftCardBalance = null;
17+
session.privacy.temporaryBasketId = null;
1718
}
1819

1920
/**

‎src/cartridges/int_adyen_SFRA/cartridge/adyen/utils/validatePaymentData.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ function validateBasketAmount(currentBasket) {
1212

1313
function validatePaymentDataFromRequest(req, res, next) {
1414
try {
15-
const currentBasket = BasketMgr.getCurrentBasket();
15+
const { isExpressPdp } = req.form?.data ? JSON.parse(req.form.data) : null;
16+
const currentBasket = isExpressPdp
17+
? BasketMgr.getTemporaryBasket(session.privacy.temporaryBasketId)
18+
: BasketMgr.getCurrentBasket();
1619
validateBasketAmount(currentBasket);
1720
} catch (e) {
1821
AdyenLogs.fatal_log(`Error occurred: ${e.message}`);

0 commit comments

Comments
 (0)
Please sign in to comment.