Skip to content

Commit

Permalink
Clearpay v23.2.0-beta.1
Browse files Browse the repository at this point in the history
- European translations.
- Updated a service definitions to use the Global API.
- Fixed a defect that sometimes caused a discrepancy in the presentation of discount prices between Clearpay and the SFCC BM.
- Minor code improvements
  • Loading branch information
ghatamehta-afterpay committed Oct 20, 2022
1 parent 43f5f56 commit b7c3d07
Show file tree
Hide file tree
Showing 165 changed files with 1,909 additions and 591 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_clearpay_sfra/cartridge/static/default/css
cartridges/int_clearpay_sfra/cartridge/static/default/js/clearpay.js
Expand Down
2 changes: 1 addition & 1 deletion cartridges/bm_clearpay/cartridge/scripts/transActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function refund(orderNo, amountString) {

while (iter.hasNext()) {
cpPaymentInstrument = iter.next();
if (cpPaymentInstrument.paymentMethod === 'AFTERPAY' || cpPaymentInstrument.paymentMethod === 'CLEARPAY') {
if (cpPaymentInstrument.paymentMethod === 'CLEARPAY') {
paymentInstrument = cpPaymentInstrument;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<tr>
<td class="table_detail left e w s" nowrap="nowrap"><label class="item-label" for="">${Resource.msg('transaction.status','clearpay',null)}</label></td>
<isloop items="${order.getPaymentInstruments()}" var="paymentInstr" status="loopstate">
<isif condition="${paymentInstr.paymentMethod == 'AFTERPAY' || paymentInstr.paymentMethod == 'CLEARPAY'}">
<isif condition="${paymentInstr.paymentMethod == 'CLEARPAY'}">
<isset name="paymentInstrument" value="${paymentInstr}" scope="page"/>
</isif>
</isloop>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<a href="${URLUtils.url('ClearPay-OrderDetails', 'OrderNo', order.orderNo)}" target="_blank">${order.orderNo}</a>
</td>
<isloop items="${order.getPaymentInstruments()}" var="paymentInstr" status="loopstate">
<isif condition="${paymentInstr.paymentMethod == 'AFTERPAY' || paymentInstr.paymentMethod == 'CLEARPAY'}">
<isif condition="${paymentInstr.paymentMethod == 'CLEARPAY'}">
<isset name="paymentInstrument" value="${paymentInstr}" scope="page"/>
</isif>
</isloop>
Expand Down
10 changes: 5 additions & 5 deletions cartridges/int_clearpay_core/cartridge/scripts/brandMapping.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"afterpay": {
"paymentMethod": "AFTERPAY",
"paymentProcessor": "*/cartridge/scripts/payment/processor/AFTERPAY"
},
"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('clearpayGetToken');
* @param {Object} basket - basket
* @returns {Object} - Token
*/
function getToken(basket) {
function getToken(basket,isCashAppPay) {
var ClearpayToken;
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;
}
clearpayUpdateOrder.handleUpdateOrder(order, paymentResult, sitePreferencesUtilities.getPaymentMode().value);
clearpayUpdateOrder.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 clearpayConstants = require('*/cartridge/scripts/util/clearpayConstants');
var cpUtilities = require('*/cartridge/scripts/util/clearpayUtilities');
var cpCheckoutUtilities = cpUtilities.checkoutUtilities;
var Transaction = require('dw/system/Transaction');
var Logger = require('dw/system/Logger');

var clearpayUpdateOrder = {
/**
Expand All @@ -11,14 +10,14 @@ var clearpayUpdateOrder = {
* @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 !== clearpayConstants.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 clearpayUpdateOrder = {
* @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 = cpUtilities.brandUtilities;
var payTrans = paymentTransaction;
var amount = null;

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

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

if (!paymentMethodName) {
return null;
Expand All @@ -105,11 +104,16 @@ var clearpayUpdateOrder = {
* @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.cpIsClearpayOrder = true;
if(!isCashAppPay){
outOrder.custom.cpIsClearpayOrder = true;
} else {
outOrder.custom.isCashAppPayOrder = true;
}

if (paymentResult.status === clearpayConstants.PAYMENT_STATUS.APPROVED) {
outOrder.setPaymentStatus(Order.PAYMENT_STATUS_PAID);
} else {
Expand All @@ -125,10 +129,10 @@ var clearpayUpdateOrder = {
* @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.cpPaymentMode = paymentMode;
payTrans.custom.cpInitialStatus = clearpayConstants.PAYMENT_STATUS.DECLINED;
payTrans.custom.cpToken = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
var Transaction = require('dw/system/Transaction');
var PreapprovalModel = require('*/cartridge/scripts/models/preapprovalModel');
var clearpayUtilities = require('*/cartridge/scripts/util/clearpayUtilities');
var LogUtils = require('*/cartridge/scripts/util/clearpayLogUtils');
var Logger = LogUtils.getLogger('afterpaUpdatePreapprovalStatus');

Expand All @@ -24,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/clearpayUtilities');
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 @@ -53,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_clearpay_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 getClearpayHttpService() {
service.setRequestMethod(requestBody.requestMethod);
service.addHeader('Content-Type', 'application/json');

const clearpayCartridge = 'ClearpayCartridge/23.1.0';
const merchantID = 'Merchant/' + service.configuration.credential.user;
const clearpayCartridge = 'ClearpayCartridge/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 = cpSitePreferencesUtilities.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 = clearpayCartridge + ' (SalesforceCommmerceCloud; ' + storeFront + '/' + storefrontVersion + '; CompatibilityMode/' + compatibilityMode + '; Merchant/' + merchantID + ') ' + hostURL;
var userAgent = clearpayCartridge + ' (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 clearpayDirectCaptureService = require('*/cartridge/scripts/logic/services/c
var clearpayAuthoriseService = require('*/cartridge/scripts/logic/services/clearpayAuthorisePaymentService');
var PAYMENT_MODE = require('*/cartridge/scripts/util/clearpayConstants').PAYMENT_MODE;
var PAYMENT_STATUS = require('*/cartridge/scripts/util/clearpayConstants').PAYMENT_STATUS;
var { checkoutUtilities: cpCheckoutUtilities } = require('*/cartridge/scripts/util/clearpayUtilities');
var { checkoutUtilities: cpCheckoutUtilities, sitePreferencesUtilities : cpSitePreferencesUtilities } = require('*/cartridge/scripts/util/clearpayUtilities');

var Site = require('dw/system/Site');
var Resource = require('dw/web/Resource');
Expand All @@ -16,28 +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);
Expand All @@ -53,7 +54,7 @@ var UpdateOrderService = {
},

getAuthoriseDirectCaptureService: function (order) {
var paymentMode = cpCheckoutUtilities.getPaymentMode(order);
var paymentMode = cpCheckoutUtilities.getPaymentMode();
if (paymentMode === PAYMENT_MODE.AUTHORISE) {
return clearpayAuthoriseService;
}
Expand Down Expand Up @@ -90,9 +91,9 @@ var UpdateOrderService = {
return cpPaymentID;
},

getToken: function (order) {
getToken: function (order,isCashAppPay) {
var cpToken;
var paymentMethodName = cpCheckoutUtilities.getPaymentMethodName();
var paymentMethodName = cpCheckoutUtilities.getPaymentMethodName(isCashAppPay);

if (!paymentMethodName) {
return null;
Expand All @@ -110,8 +111,8 @@ var UpdateOrderService = {
return cpToken;
},
// Need amount for express checkouts
getPaymentAmount: function (order) {
var paymentMethodName = cpCheckoutUtilities.getPaymentMethodName();
getPaymentAmount: function (order,isCashAppPay) {
var paymentMethodName = cpCheckoutUtilities.getPaymentMethodName(isCashAppPay);
return order.getPaymentInstruments(paymentMethodName)[0].getPaymentTransaction().amount;
},
updateOrder: function (order, status) {
Expand All @@ -135,6 +136,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 +163,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 b7c3d07

Please sign in to comment.