Skip to content

Commit

Permalink
Afterpay v23.2.0-beta.1
Browse files Browse the repository at this point in the history
- New : Cash App Pay as a payment option for US merchants.
- New : European translations.
- Update :  Service definitions to use the Global API.
- Fix :  Defect that sometimes caused a discrepancy in the presentation of discount prices between Afterpay and the SFCC BM.
- Other minor improvements.
  • Loading branch information
ghatamehta-afterpay committed Oct 20, 2022
1 parent cd4c527 commit 8c26ee4
Show file tree
Hide file tree
Showing 154 changed files with 1,907 additions and 510 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
node_modules
cartridges/int_afterpay_sfra/cartridge/static/default/css
cartridges/int_afterpay_sfra/cartridge/static/default/js/afterpay.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"clearpay": {
"paymentMethod": "CLEARPAY",
"paymentProcessor": "*/cartridge/scripts/payment/processor/CLEARPAY"
},
"cashapp": {
"paymentMethod": "CASHAPPPAY",
"paymentProcessor": "*/cartridge/scripts/payment/processor/CASHAPPPAY"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ var Logger = LogUtils.getLogger('afterpayGetToken');
* @param {Object} basket - basket
* @returns {Object} - Token
*/
function getToken(basket) {
function getToken(basket,isCashAppPay) {
var AfterpayToken;
var isCashAppPay = isCashAppPay || false;
try {
orderCreateService.generateRequest(basket);
orderCreateService.generateRequest(basket,isCashAppPay);
var response = orderCreateService.getResponse();
var res = new TokenModel();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ var parsePaymentStatus = function (paymentStatus) {
* @param {number} paymentStatus - payment status
* @returns {number} - payment status
*/
function getPaymentStatus(order, paymentStatus, expressCheckoutModel) {
function getPaymentStatus(order, paymentStatus, expressCheckoutModel, isCashAppPay) {
var parsedPaymentStatus = parsePaymentStatus(paymentStatus);
var isCashAppPay = isCashAppPay || false;
Logger.debug('parsed payment status : ' + parsedPaymentStatus);
var paymentResult;
try {
paymentResult = baseUpdateOrderService.handleOrder(order, parsedPaymentStatus, expressCheckoutModel);
paymentResult = baseUpdateOrderService.handleOrder(order, parsedPaymentStatus, expressCheckoutModel, isCashAppPay);
if (paymentResult && paymentResult.status === 'DECLINED') {
parsedPaymentStatus = paymentResult.status;
}
afterpayUpdateOrder.handleUpdateOrder(order, paymentResult, sitePreferencesUtilities.getPaymentMode().value);
afterpayUpdateOrder.handleUpdateOrder(order, paymentResult, sitePreferencesUtilities.getPaymentMode().value,isCashAppPay);
Logger.debug('UpdatedOrder service status : ' + parsedPaymentStatus);
} catch (exception) {
Logger.error('Exception occured while updating order status ' + exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var afterpayConstants = require('*/cartridge/scripts/util/afterpayConstants');
var apUtilities = require('*/cartridge/scripts/util/afterpayUtilities');
var apCheckoutUtilities = apUtilities.checkoutUtilities;
var Transaction = require('dw/system/Transaction');
var Logger = require('dw/system/Logger');

var afterpayUpdateOrder = {
/**
Expand All @@ -11,14 +10,14 @@ var afterpayUpdateOrder = {
* @param {Object} paymentResult - payment result
* @param {'DIRECT_CAPTURE'|'AUTHORISE'} paymentMode - payment mode
*/
handleUpdateOrder: function (order, paymentResult, paymentMode) {
handleUpdateOrder: function (order, paymentResult, paymentMode, isCashAppPay) {
var paymentTransaction;

try {
paymentTransaction = this.getPaymentTransaction(order);
paymentTransaction = this.getPaymentTransaction(order,isCashAppPay);
if (paymentResult.status !== afterpayConstants.PAYMENT_STATUS.DECLINED) {
this.savePaymentTransaction(paymentTransaction, paymentResult, paymentMode);
this.saveOrder(order, paymentResult);
this.savePaymentTransaction(paymentTransaction, paymentResult, paymentMode, isCashAppPay);
this.saveOrder(order, paymentResult, isCashAppPay);
} else {
this.savePaymentTransactionDeclined(paymentTransaction, paymentMode);
}
Expand All @@ -36,15 +35,15 @@ var afterpayUpdateOrder = {
* @returns {dw.order.PaymentTransaction} - transaction
*/
// eslint-disable-next-line no-unused-vars
savePaymentTransaction: function (paymentTransaction, paymentResult, paymentMode) {
savePaymentTransaction: function (paymentTransaction, paymentResult, paymentMode, isCashAppPay) {
var Money = require('dw/value/Money');
var BrandUtilities = apUtilities.brandUtilities;
var payTrans = paymentTransaction;
var amount = null;

Transaction.wrap(function () {
payTrans.setTransactionID(paymentResult.id || null);
payTrans.setPaymentProcessor(afterpayUpdateOrder.getPaymentProcessor());
payTrans.setPaymentProcessor(afterpayUpdateOrder.getPaymentProcessor(isCashAppPay));
payTrans.custom.apPaymentID = paymentResult.id || null;
payTrans.custom.apPaymentMode = paymentMode;
payTrans.custom.apCountryCode = BrandUtilities.getCountryCode();
Expand All @@ -67,9 +66,9 @@ var afterpayUpdateOrder = {
* @param {dw.order.Order} order - order
* @returns {dw.order.PaymentTransaction} - payment transaction
*/
getPaymentTransaction: function (order) {
getPaymentTransaction: function (order,isCashAppPay) {
var paymentTransaction;
var paymentMethodName = apCheckoutUtilities.getPaymentMethodName();
var paymentMethodName = apCheckoutUtilities.getPaymentMethodName(isCashAppPay);

if (!paymentMethodName) {
return null;
Expand All @@ -88,9 +87,9 @@ var afterpayUpdateOrder = {
* retrieves payment processor
* @returns {dw.order.PaymentProcessor} - processor
*/
getPaymentProcessor: function () {
getPaymentProcessor: function (isCashAppPay) {
var PaymentMgr = require('dw/order/PaymentMgr');
var paymentMethodName = apCheckoutUtilities.getPaymentMethodName();
var paymentMethodName = apCheckoutUtilities.getPaymentMethodName(isCashAppPay);

if (!paymentMethodName) {
return null;
Expand All @@ -105,11 +104,16 @@ var afterpayUpdateOrder = {
* @param {Object} paymentResult - result
* @returns {dw.order.Order} - order
*/
saveOrder: function (order, paymentResult) {
saveOrder: function (order, paymentResult, isCashAppPay) {
var Order = require('dw/order/Order');
var outOrder = order;
Transaction.begin();
outOrder.custom.apIsAfterpayOrder = true;
if(!isCashAppPay){
outOrder.custom.apIsAfterpayOrder = true;
} else {
outOrder.custom.isCashAppPayOrder = true;
}

if (paymentResult.status === afterpayConstants.PAYMENT_STATUS.APPROVED) {
outOrder.setPaymentStatus(Order.PAYMENT_STATUS_PAID);
} else {
Expand All @@ -125,10 +129,10 @@ var afterpayUpdateOrder = {
* @param {'DIRECT_CAPTURE'|'AUTHORISE'} paymentMode - payment mode
* @returns {dw.order.PaymentTransaction} - transaction
*/
savePaymentTransactionDeclined: function (paymentTransaction, paymentMode) {
savePaymentTransactionDeclined: function (paymentTransaction, paymentMode,isCashAppPay) {
var payTrans = paymentTransaction;
Transaction.begin();
payTrans.setPaymentProcessor(this.getPaymentProcessor());
payTrans.setPaymentProcessor(this.getPaymentProcessor(isCashAppPay));
payTrans.custom.apPaymentMode = paymentMode;
payTrans.custom.apInitialStatus = afterpayConstants.PAYMENT_STATUS.DECLINED;
payTrans.custom.apToken = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ function parsePreapprovalResult(parameter) {
* @param {Object} preapprovalModel - preApproval Model
* @param {Object} lineItemCtnr - line Item Container
*/
function updatePreapprovalStatus(preapprovalModel, lineItemCtnr) {
function updatePreapprovalStatus(preapprovalModel, lineItemCtnr, parameterMap) {
const { checkoutUtilities } = require('*/cartridge/scripts/util/afterpayUtilities');
var paymentTransaction = checkoutUtilities.getPaymentTransaction(lineItemCtnr);
var isCashAppPay = parameterMap.isCashAppPay || false;
var paymentMethodName = checkoutUtilities.getPaymentMethodName(isCashAppPay);
var paymentInstrument = lineItemCtnr.getPaymentInstruments(paymentMethodName)[0];
var paymentTransaction = paymentInstrument ? paymentInstrument.getPaymentTransaction() : null;
if (paymentTransaction) {
Logger.debug('Payment status after token generation : ' + preapprovalModel.status);
Transaction.begin();
Expand All @@ -52,7 +55,7 @@ function getPreApprovalResult(lineItemCtnr, parameterMap) {
return { error: true };
}
try {
updatePreapprovalStatus(preapprovalModel, lineItemCtnr);
updatePreapprovalStatus(preapprovalModel, lineItemCtnr, parameterMap);
} catch (exception) {
var e = exception;
Logger.error('Update payment transaction: ' + e);
Expand Down
3 changes: 3 additions & 0 deletions cartridges/int_afterpay_core/cartridge/scripts/hooks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hooks": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ function getAfterpayHttpService() {
service.setRequestMethod(requestBody.requestMethod);
service.addHeader('Content-Type', 'application/json');

const afterpayCartridge = 'AfterpayCartridge/23.1.0';
const merchantID = 'Merchant/' + service.configuration.credential.user;
const afterpayCartridge = 'AfterpayCartridge/23.2.0-beta.1';
const merchantID = service.configuration.credential.user;
const siteURL = URLUtils.httpsHome().toString();
const storeFront = Site.getCurrent().getID();
const hostURL = siteURL.substring(0, siteURL.indexOf('/', 14));
const compatibilityMode = dw.system.System.getCompatibilityMode();
const cashAppEnabled = apSitePreferencesUtilities.isCashAppEnabled() ? '1' : '0';
var storefrontVersion = '';
if (storeFront.includes('SiteGenesis')) {
storefrontVersion = Resource.msg('revisioninfo.revisionnumber', 'revisioninfo', null);
} else if (storeFront.includes('RefArch')) {
storefrontVersion = Resource.msg('global.version.number', 'version', null);
}

var userAgent = afterpayCartridge + ' (SalesforceCommmerceCloud; ' + storeFront + '/' + storefrontVersion + '; CompatibilityMode/' + compatibilityMode + '; Merchant/' + merchantID + ') ' + hostURL;
var userAgent = afterpayCartridge + ' (SalesforceCommmerceCloud; ' + storeFront + '/' + storefrontVersion + '; CompatibilityMode/' + compatibilityMode + '; Merchant/' + merchantID + '; CashAppEnabled/' + cashAppEnabled + ') ' + hostURL;

service.addHeader('User-Agent', userAgent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var afterpayDirectCaptureService = require('*/cartridge/scripts/logic/services/a
var afterpayAuthoriseService = require('*/cartridge/scripts/logic/services/afterpayAuthorisePaymentService');
var PAYMENT_MODE = require('*/cartridge/scripts/util/afterpayConstants').PAYMENT_MODE;
var PAYMENT_STATUS = require('*/cartridge/scripts/util/afterpayConstants').PAYMENT_STATUS;
var { checkoutUtilities: apCheckoutUtilities } = require('*/cartridge/scripts/util/afterpayUtilities');
var { checkoutUtilities: apCheckoutUtilities, sitePreferencesUtilities : apSitePreferencesUtilities } = require('*/cartridge/scripts/util/afterpayUtilities');

var Site = require('dw/system/Site');
var Resource = require('dw/web/Resource');
Expand All @@ -16,29 +16,29 @@ var OrderMgr = require('dw/order/OrderMgr');
* processes all the transaction details related to the payment and order
*/
var UpdateOrderService = {
handleOrder: function (order, paymentStatus, expressCheckoutModel) {
handleOrder: function (order, paymentStatus, expressCheckoutModel,isCashAppPay) {
var isCashAppPay = isCashAppPay || false
var authoriseDirectCaptureResult = null;
if (paymentStatus === PAYMENT_STATUS.DECLINED) {
authoriseDirectCaptureResult = this.updateDeclinedOrder(order);
} else if (paymentStatus === PAYMENT_STATUS.FAILED) {
authoriseDirectCaptureResult = this.updateFailedOrder(order);
} else if (paymentStatus === PAYMENT_STATUS.APPROVED) {
authoriseDirectCaptureResult = this.handleApprovalOrder(order, expressCheckoutModel);
authoriseDirectCaptureResult = this.handleApprovalOrder(order, expressCheckoutModel, isCashAppPay);
} else if (paymentStatus === PAYMENT_STATUS.PENDING) {
authoriseDirectCaptureResult = this.handlePendingOrder(order);
}
return authoriseDirectCaptureResult;
},

handleApprovalOrder: function (order, expressCheckoutModel) {
var paymentAmount = this.getPaymentAmount(order);
handleApprovalOrder: function (order, expressCheckoutModel,isCashAppPay) {
var paymentAmount = this.getPaymentAmount(order,isCashAppPay);
// express checkout needs payment amount, even for direct capture.
// express checkout with deferred flows needs checksum and amount
// these are all in expressCheckoutModel
var authoriseDirectCaptureResult = null;
var authoriseDirectCaptureService = this.getAuthoriseDirectCaptureService(order);
var requestValues = authoriseDirectCaptureService.generateRequest(order, this.getToken(order), order.orderNo, paymentAmount, expressCheckoutModel);

var requestValues = authoriseDirectCaptureService.generateRequest(order, this.getToken(order,isCashAppPay), order.orderNo, paymentAmount, expressCheckoutModel);
try {
authoriseDirectCaptureResult = authoriseDirectCaptureService.getResponse(requestValues.requestUrl, requestValues.requestBody);
} catch (e) {
Expand All @@ -53,7 +53,7 @@ var UpdateOrderService = {
},

getAuthoriseDirectCaptureService: function (order) {
var paymentMode = apCheckoutUtilities.getPaymentMode(order);
var paymentMode = apSitePreferencesUtilities.getPaymentMode();
if (paymentMode === PAYMENT_MODE.AUTHORISE) {
return afterpayAuthoriseService;
}
Expand Down Expand Up @@ -90,9 +90,9 @@ var UpdateOrderService = {
return apPaymentID;
},

getToken: function (order) {
getToken: function (order,isCashAppPay) {
var apToken;
var paymentMethodName = apCheckoutUtilities.getPaymentMethodName();
var paymentMethodName = apCheckoutUtilities.getPaymentMethodName(isCashAppPay);

if (!paymentMethodName) {
return null;
Expand All @@ -110,8 +110,8 @@ var UpdateOrderService = {
return apToken;
},
// Need amount for express checkouts
getPaymentAmount: function (order) {
var paymentMethodName = apCheckoutUtilities.getPaymentMethodName();
getPaymentAmount: function (order,isCashAppPay) {
var paymentMethodName = apCheckoutUtilities.getPaymentMethodName(isCashAppPay);
return order.getPaymentInstruments(paymentMethodName)[0].getPaymentTransaction().amount;
},
updateOrder: function (order, status) {
Expand All @@ -135,6 +135,7 @@ var UpdateOrderService = {
},

updateApprovedOrder: function (order, containerView) {
var containerView = containerView || 'basket'
Transaction.begin();
order.setPaymentStatus(Order.PAYMENT_STATUS_PAID);
Transaction.commit();
Expand All @@ -161,7 +162,7 @@ var UpdateOrderService = {

var orderModel = new OrderModel(order, {
containerView: containerView || 'basket',
countryCode: order.getBillingAddress().getCountryCode().value
countryCode: brandUtilities.getCountryCode()
});

var orderObject = { order: orderModel };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,19 @@ OrderRequestBuilder.prototype.buildItems = function (basket) {
var item = new LineItem();
var product = li.product;

// Some lineitems may not be products
// e.g. extended warranties
// Some lineitems may not be products
// e.g. extended warranties
if (!product) {
item.name = li.getLineItemText();
item.quantity = li.getQuantity().value;
item.price.amount = li.adjustedNetPrice.value;
item.sku = li.productID;
item.price.currency = li.adjustedNetPrice.currencyCode;
} else {
item.name = product.name;
item.sku = product.ID;
item.quantity = li.getQuantity().value;
item.price.amount = product.getPriceModel().getPrice().value;
item.price.currency = product.getPriceModel().getPrice().currencyCode;
item.name = product.name;
item.sku = product.ID;
item.price.currency = product.getPriceModel().getPrice().currencyCode;
}
item.quantity = li.getQuantity().value;
item.price.amount = (li.adjustedPrice.value/item.quantity).toString();
return item;
});

Expand Down
Loading

0 comments on commit 8c26ee4

Please sign in to comment.