Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merchant reference set for PayPal Express #1080

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions jest/sfccCartridgeMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ jest.mock('*/cartridge/adyen/utils/adyenHelper', () => ({
})),
isAdyenGivingAvailable: jest.fn(() => true),
isApplePay: jest.fn(() => true),
isPayPalExpress: jest.fn(() => false),
getAdyenGivingConfig: jest.fn(() => true),
getApplicationInfo: jest.fn(() => ({
externalPlatform: { version: 'SFRA' },
Expand Down
9 changes: 9 additions & 0 deletions metadata/site_import/meta/system-objecttype-extensions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
<externally-managed-flag>false</externally-managed-flag>
<min-length>0</min-length>
</attribute-definition>
<attribute-definition attribute-id="Adyen_paypalExpressResponse">
<display-name xml:lang="x-default">Adyen PayPal Express Response</display-name>
<description xml:lang="x-default">PayPal Express response used to render the confirmation form</description>
<type>text</type>
<mandatory-flag>false</mandatory-flag>
<externally-managed-flag>false</externally-managed-flag>
<min-length>0</min-length>
</attribute-definition>
</custom-attribute-definitions>
<group-definitions>
<attribute-group group-id="AdyenPayments">
Expand All @@ -53,6 +61,7 @@
<attribute attribute-id="Adyen_paymentMethod"/>
<attribute attribute-id="Adyen_value"/>
<attribute attribute-id="Adyen_donationAmount"/>
<attribute attribute-id="Adyen_paypalExpressResponse"/>
</attribute-group>
</group-definitions>
</type-extension>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ function callPaymentFromComponent(data, component) {
data: JSON.stringify(data),
},
success(response) {
helpers.createShowConfirmationForm(window.showConfirmationAction);
helpers.setOrderFormData(response);
handlePaypalResponse(response, component);
},
});
Expand All @@ -43,6 +41,10 @@ function makeExpressPaymentDetailsCall(data) {
data: JSON.stringify({ data }),
contentType: 'application/json; charset=utf-8',
async: false,
success(response) {
helpers.createShowConfirmationForm(window.showConfirmationAction);
helpers.setOrderFormData(response);
},
});
}

Expand Down Expand Up @@ -79,6 +81,9 @@ async function mountPaypalComponent() {
},
onAdditionalDetails: (state) => {
makeExpressPaymentDetailsCall(state.data);
document.querySelector('#additionalDetailsHidden').value =
JSON.stringify(state.data);
document.querySelector('#showConfirmationForm').submit();
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const URLUtils = require('dw/web/URLUtils');
const OrderMgr = require('dw/order/OrderMgr');
const Transaction = require('dw/system/Transaction');
const BasketMgr = require('dw/order/BasketMgr');
const AdyenHelper = require('*/cartridge/adyen/utils/adyenHelper');
const adyenCheckout = require('*/cartridge/adyen/scripts/payments/adyenCheckout');
const AdyenLogs = require('*/cartridge/adyen/logs/adyenCustomLogs');
const COHelpers = require('*/cartridge/scripts/checkout/checkoutHelpers');
Expand Down Expand Up @@ -60,25 +59,26 @@ function makeExpressPaymentDetailsCall(req, res, next) {
const request = JSON.parse(req.body);
const currentBasket = BasketMgr.getCurrentBasket();

const paymentsDetailsResponse = adyenCheckout.doPaymentsDetailsCall(
request.data,
);
const response = adyenCheckout.doPaymentsDetailsCall(request.data);

setBillingAndShippingAddress(currentBasket);
const order = OrderMgr.createOrder(currentBasket);
const order = OrderMgr.createOrder(
currentBasket,
session.privacy.paypalExpressOrderNo,
);
const fraudDetectionStatus = { status: 'success' };
const placeOrderResult = COHelpers.placeOrder(order, fraudDetectionStatus);
if (placeOrderResult.error) {
AdyenLogs.error_log('Failed to place the PayPal express order');
}

const response = AdyenHelper.createAdyenCheckoutResponse(
paymentsDetailsResponse,
);

response.orderNo = order.orderNo;
response.orderToken = order.orderToken;
res.json(response);
// Storing the paypal express response to make use of show confirmation logic
Transaction.wrap(() => {
order.custom.Adyen_paypalExpressResponse = JSON.stringify(response);
});
res.json({ orderNo: response.orderNo, orderToken: response.orderToken });
return next();
} catch (e) {
AdyenLogs.error_log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ function createPaymentRequest(args) {

paymentRequest = AdyenHelper.add3DS2Data(paymentRequest);
const paymentMethodType = paymentRequest.paymentMethod.type;
const isPayPalExpress =
paymentRequest.paymentMethod.type === 'paypal' &&
paymentRequest.paymentMethod.subtype === 'express';
const isPayPalExpress = AdyenHelper.isPayPalExpress(
paymentRequest.paymentMethod,
);

// Add Risk data
if (AdyenConfigs.getAdyenBasketFieldsEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ function handleAuthorisedPayment(
) {
// custom fraudDetection
const fraudDetectionStatus = { status: 'success' };
const isPayPalExpress = order.custom.Adyen_paypalExpressResponse;

// Places the order
const placeOrderResult = COHelpers.placeOrder(order, fraudDetectionStatus);
if (placeOrderResult.error) {
return handlePaymentError(order, adyenPaymentInstrument, { res, next });
// Places the order, for PayPal express the order is placed from makeExpressPaymentDetailsCall.js
if (!isPayPalExpress) {
const placeOrderResult = COHelpers.placeOrder(order, fraudDetectionStatus);
if (placeOrderResult.error) {
return handlePaymentError(order, adyenPaymentInstrument, { res, next });
}
}

Transaction.wrap(() => {
Expand Down Expand Up @@ -101,6 +104,7 @@ function handlePaymentResult(result, order, adyenPaymentInstrument, options) {
Transaction.wrap(() => {
order.custom.Adyen_pspReference = result.pspReference;
order.custom.Adyen_eventCode = result.resultCode;
order.custom.Adyen_paypalExpressResponse = null;
});
return handlePaymentError(order, adyenPaymentInstrument, options);
}
Expand Down Expand Up @@ -135,15 +139,25 @@ function handlePayment(stateData, order, options) {
return handlePaymentError(order, adyenPaymentInstrument, options);
}
}

const detailsCall = hasStateData
? handlePaymentsDetailsCall(stateData, adyenPaymentInstrument)
: null;
const paymentData = JSON.parse(
adyenPaymentInstrument.custom.adyenPaymentData,
);
const isPayPalExpress = AdyenHelper.isPayPalExpress(
paymentData.paymentMethod,
);
const detailsCall =
hasStateData && !isPayPalExpress
? handlePaymentsDetailsCall(stateData, adyenPaymentInstrument)
: null;
if (isPayPalExpress) {
finalResult = JSON.parse(order.custom.Adyen_paypalExpressResponse);
} else {
finalResult = finalResult || detailsCall?.result;
}

Transaction.wrap(() => {
adyenPaymentInstrument.custom.adyenPaymentData = null;
});
finalResult = finalResult || detailsCall?.result;

return handlePaymentResult(
finalResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const collections = require('*/cartridge/scripts/util/collections');
const ShippingMgr = require('dw/order/ShippingMgr');
const ShippingMethodModel = require('*/cartridge/models/shipping/shippingMethod');
const PaymentInstrument = require('dw/order/PaymentInstrument');
const OrderMgr = require('dw/order/OrderMgr');
const StringUtils = require('dw/util/StringUtils');
//script includes
const AdyenLogs = require('*/cartridge/adyen/logs/adyenCustomLogs');
Expand Down Expand Up @@ -339,6 +340,13 @@ let adyenHelperObj = {
return false;
},

isPayPalExpress(paymentMethod){
if (paymentMethod.type === 'paypal' && paymentMethod.subtype === 'express'){
return true;
}
return false;
},

// Get stored card token of customer saved card based on matched cardUUID
getCardToken(cardUUID, customer) {
let token = '';
Expand Down Expand Up @@ -504,6 +512,13 @@ let adyenHelperObj = {
orderToken = order.getOrderToken();
}

// creates order number to be utilized for PayPal express
if (adyenHelperObj.isPayPalExpress(stateData.paymentMethod)){
const paypalExpressOrderNo = OrderMgr.createOrderNo();
session.privacy.paypalExpressOrderNo = paypalExpressOrderNo;
reference = paypalExpressOrderNo;
}

let signature = '';
//Create signature to verify returnUrl if there is an order
if (order && order.getUUID()) {
Expand Down