Skip to content

Commit

Permalink
SFCC Cartridge v23.2.0-beta
Browse files Browse the repository at this point in the history
 * Customers can place pre-orders/backorders without paying upfront
  • Loading branch information
ghatamehta-afterpay committed Mar 28, 2022
1 parent cd4c527 commit 2a8f162
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ var afterpayUpdateOrder = {
amount = empty(paymentResult.openToCaptureAmount) ? null : new Money(parseFloat(paymentResult.openToCaptureAmount.amount), paymentResult.openToCaptureAmount.currency);
}

if(paymentResult.status === afterpayConstants.PAYMENT_STATUS.ACTIVE && amount === null ){
amount = empty(paymentResult.amount) ? null : new Money(parseFloat(paymentResult.amount.amount), paymentResult.amount.currency);
}
payTrans.setAmount(amount);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function getAfterpayHttpService() {
service.setRequestMethod(requestBody.requestMethod);
service.addHeader('Content-Type', 'application/json');

const afterpayCartridge = 'AfterpayCartridge/23.1.0';
const afterpayCartridge = 'AfterpayCartridge/23.2.0-beta';
const merchantID = 'Merchant/' + service.configuration.credential.user;
const siteURL = URLUtils.httpsHome().toString();
const storeFront = Site.getCurrent().getID();
Expand All @@ -67,7 +67,7 @@ function getAfterpayHttpService() {
},

parseResponse: function (service, httpClient) {
if (httpClient.statusCode === 200 || httpClient.statusCode === 201) {
if (httpClient.statusCode === 200 || httpClient.statusCode === 201 || httpClient.statusCode === 202) {
var parseResponse = httpClient.text;
var filterResponse = parseResponse;
Logger.debug('Parsed Response : ' + afterpayUtils.filterLogData(filterResponse));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var Resource = require('dw/web/Resource');
var TaxMgr = require('dw/order/TaxMgr');

var Builder = require('../util/builder');
var { checkoutUtilities, brandUtilities, sitePreferencesUtilities } = require('*/cartridge/scripts/util/afterpayUtilities');
var Order = require('*/cartridge/scripts/order/order');
Expand Down Expand Up @@ -105,6 +104,7 @@ OrderRequestBuilder.prototype.buildRequest = function (params) {
.buildItems(basket)
.applyDiscounts(basket)
.buildTotalAmount(basket)
.buildInitialOrderAmount(basket)
.buildShippingAmount(basket)
.buildTotalTax(basket)
.buildMerchantInformation(url)
Expand Down Expand Up @@ -234,7 +234,7 @@ OrderRequestBuilder.prototype.buildItems = function (basket) {
var lineItems = basket.getAllProductLineItems().toArray();

this.context.items = lineItems.map(function (li) {
var item = new LineItem();
var item = new LineItem();
var product = li.product;

// Some lineitems may not be products
Expand All @@ -246,11 +246,11 @@ OrderRequestBuilder.prototype.buildItems = function (basket) {
item.price.amount = li.adjustedNetPrice.value;
item.price.currency = li.adjustedNetPrice.currencyCode;
} else {
item.name = product.name;
item.sku = product.ID;
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.price.currency = product.getPriceModel().getPrice().currencyCode;
}
return item;
});
Expand Down Expand Up @@ -300,6 +300,57 @@ OrderRequestBuilder.prototype.applyDiscounts = function (basket) {

return this;
};
/**
* builds initial order amount details
* @param {dw.order.Basket} basket - basket
* @returns {Object} - this object
*/
// eslint-disable-next-line no-unused-vars
OrderRequestBuilder.prototype.buildInitialOrderAmount = function (basket) {
var paymentTransaction = this._getPaymentTransaction(basket);
var preOrderHelper = require('*/cartridge/scripts/checkout/afterpayPreOrderHelpers');
var productAvailabilityModel = require('dw/catalog/ProductAvailabilityModel');
var ProductMgr = require('dw/catalog/ProductMgr');
var Amount = require('*/cartridge/scripts/order/amount');

if (!paymentTransaction) {
return this;
}
var productLineItems = basket.getAllProductLineItems().iterator();
var preOrderAmount = 0.00;

while (productLineItems.hasNext()) {
var productLineItem = productLineItems.next();
var product = productLineItem.product;

if (!product) {
var parentProductID = productLineItem.parent.productID;
product = ProductMgr.getProduct(parentProductID);
}

if(product){
var productAvailabiltyStatus = product.availabilityModel.getAvailabilityStatus();
if(productAvailabiltyStatus == productAvailabilityModel.AVAILABILITY_STATUS_PREORDER || productAvailabiltyStatus == productAvailabilityModel.AVAILABILITY_STATUS_BACKORDER){
var productPrice = productLineItem.proratedPrice.value;
preOrderAmount += productPrice;
}
}
}

var orderSubTotal = preOrderHelper.getCartSubtotal(basket);;

if(preOrderAmount > 0 && orderSubTotal){
var initialOrderAmount = (paymentTransaction.amount.value - preOrderAmount).toFixed(2);
if((orderSubTotal - preOrderAmount).toFixed(2) == 0){
initialOrderAmount = 0.00;
}
this.context.initialOrderAmount = new Amount();
this.context.initialOrderAmount.amount = initialOrderAmount;
this.context.initialOrderAmount.currency = basket.getCurrencyCode();
}

return this;
};

/**
* builds total amount details
Expand Down Expand Up @@ -375,5 +426,4 @@ OrderRequestBuilder.prototype._buildShiptoStore = function(type, store) {
this.context[type].phoneNumber = store.phone || '';
};


module.exports = OrderRequestBuilder;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var PAYMENT_STATUS = {
FAILED: 'FAILED',
PENDING: 'PENDING',
SUCCESS: 'SUCCESS',
ACTIVE: 'ACTIVE',
UNKNOWN: 'Payment was Declined by Afterpay'
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var afterpayPreOrderTools = {
getCartSubtotal: function (basket) {
var parsePrice = require('~/cartridge/scripts/util/parsePriceAfterpay.js');
if(basket){
var TotalsModel = require('*/cartridge/models/totals');
var totalsModel = new TotalsModel(basket);
var orderSubTotal = parsePrice(totalsModel.subTotal);
var cartDiscount = totalsModel.orderLevelDiscountTotal.value;
if(cartDiscount !== 0){
orderSubTotal = (orderSubTotal - cartDiscount).toFixed(2);
}
return orderSubTotal;
} else {
return false;
}
}
}

module.exports = afterpayPreOrderTools;
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ updatePaymentStatus.handlePaymentStatus = function (order) {

Logger.debug('Afterpay final payment status :' + finalPaymentStatus);

if (finalPaymentStatus === 'APPROVED') {
if (finalPaymentStatus === 'APPROVED' || finalPaymentStatus === 'ACTIVE') {
return { authorized: true };
} else if (finalPaymentStatus === 'PENDING') {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var afterpayPreOrderTools = {
getCartSubtotal: function (basket) {
var parsePrice = require('~/cartridge/scripts/util/parsePriceAfterpay.js');
if(basket){
var TotalsModel = require('*/cartridge/models/totals');
var totalsModel = new TotalsModel(basket);
var orderSubTotal = parsePrice(totalsModel.subTotal);
var cartDiscount = totalsModel.orderLevelDiscountTotal.value;
if(cartDiscount !== 0){
orderSubTotal = (orderSubTotal - cartDiscount).toFixed(2);
}
return orderSubTotal;
} else {
return false;
}
}
}

module.exports = afterpayPreOrderTools;
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ updatePaymentStatus.handlePaymentStatus = function (order) {

Logger.debug('Afterpay final payment status :' + finalPaymentStatus);

if (finalPaymentStatus === 'APPROVED') {
if (finalPaymentStatus === 'APPROVED' || finalPaymentStatus === 'ACTIVE') {
return { authorized: true };
} else if (finalPaymentStatus === 'PENDING') {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var afterpayPreOrderTools = {
getCartSubtotal: function (basket) {
return basket.getAdjustedMerchandizeTotalNetPrice();
}
}

module.exports = afterpayPreOrderTools;
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function Authorise(args) {

Logger.debug('Afterpay final payment status :' + finalPaymentStatus);

if (finalPaymentStatus === 'APPROVED') {
if (finalPaymentStatus === 'APPROVED' || finalPaymentStatus === 'ACTIVE') {
return { authorized: true };
} else if (finalPaymentStatus === 'PENDING') {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function Authorise(args) {

Logger.debug('Afterpay final payment status :' + finalPaymentStatus);

if (finalPaymentStatus === 'APPROVED') {
if (finalPaymentStatus === 'APPROVED' || finalPaymentStatus === 'ACTIVE') {
return { authorized: true };
} else if (finalPaymentStatus === 'PENDING') {
return {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Afterpay",
"version": "23.1.0",
"version": "23.2.0-beta",
"description": "Afterpay cartridge",
"main": "index.js",
"engines": {
Expand Down

0 comments on commit 2a8f162

Please sign in to comment.