Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 265 lines (239 sloc) 9.54 KB
(function () {
/**
* Creates library for working with Order
* Middleware layer between Affirm API and SFCC orders
*
* @constructor
* @this {Order}
* @returns Order instance
*/
var Order = function () {
var logger = require('dw/system').Logger.getLogger('Affirm', ''),
OrderMgr = require('dw/order/OrderMgr'),
Order = require('dw/order/Order'),
Money = require('dw/value/Money'),
File = require('dw/io/File'),
FileReader = require('dw/io/FileReader'),
FileWriter = require('dw/io/FileWriter'),
data = require('~/cartridge/scripts/data/affirmData.ds'),
basket = require('~/cartridge/scripts/basket/affirmBasket.ds'),
api = require('~/cartridge/scripts/api/affirmAPI.ds'),
filepath = File.IMPEX + File.SEPARATOR + 'affirm' + File.SEPARATOR,
filename = 'affirm.dat';
function readDateFromFile() {
var file = new File(filepath + filename);
if (file.exists()) {
var fileReader = new FileReader(file);
var strDate = fileReader.readLine();
if (strDate) {
return new Date(Date.parse(strDate));
}
}
return new Date(0);
}
function saveDateToFile(date) {
var dir = new File(filepath);
if (!dir.exists()) {
dir.mkdirs();
}
var file = new File(filepath + filename);
if (!file.exists()) {
file.createNewFile();
}
var fileWriter = new FileWriter(file);
fileWriter.writeLine(date.toISOString());
fileWriter.flush();
fileWriter.close();
}
/**
* Updates PaymentInstrument and Order system objects
*
* @param {dw.order.Order} Order demnadware order instance
* @param {Object} Reponse auth response from Affirm
* @param {dw.order.PaymentProcessor} PaymentProcessor payment processor instance
* @param {dw.order.PaymentInstrument} PaymentInstrument payment isntrument instance
*/
this.updateAttributes = function (order, reponse, paymentProcessor, paymentInstrument) {
try {
paymentInstrument.paymentTransaction.transactionID = reponse.events[0].id;
paymentInstrument.paymentTransaction.amount = new Money(reponse.amount, order.currencyCode).divide(100);
paymentInstrument.paymentTransaction.setPaymentProcessor(paymentProcessor);
order.custom.AffirmToken = reponse.events[0].id;
order.custom.AffirmExternalId = reponse.id;
order.custom.AffirmStatus = 'AUTH';
order.custom.AffirmPaymentAction = data.getAffirmPaymentAction();
} catch (e) {
logger.error('Affirm. File - affirmOrder.ds. Error - {0}', e);
}
};
this.orderCanceledVoid = function (order) {
api.void(order.custom.AffirmExternalId);
order.custom.AffirmStatus = 'VOIDED';
return order;
};
this.authOrder = api.auth;
this.voidOrder = api.void;
this.captureOrder = api.capture;
/**
* Capture new orders and update their afirm status. Used in Affirm job.
*
* @see pipeline 'AffirmJob'
*/
this.captureOrders = function () {
OrderMgr.processOrders(function (order) {
try {
api.capture(order.custom.AffirmExternalId, order.orderNo);
order.custom.AffirmStatus = 'CAPTURE';
order.setPaymentStatus(Order.PAYMENT_STATUS_PAID);
order.setStatus(Order.ORDER_STATUS_COMPLETED);
} catch (e) {
logger.error('Affirm. File - affirmOrder.ds. Error - {0}', e);
}
}, '(status = {0} OR status = {1}) AND custom.AffirmPaymentAction = {2} AND custom.AffirmStatus = {3}', Order.ORDER_STATUS_NEW, Order.ORDER_STATUS_OPEN, 'AUTH', 'AUTH');
};
/**
* Void cancelled orders and update their afirm status. Used in Affirm job.
*
* @see pipeline 'AffirmJob'
*/
this.voidOrders = function () {
OrderMgr.processOrders(function (order) {
try {
api.void(order.custom.AffirmExternalId);
order.custom.AffirmStatus = 'VOIDED';
} catch (e) {
logger.error('Affirm. File - affirmOrder.ds. Error - {0}', e);
}
}, 'status = {0} AND custom.AffirmStatus = {1}', Order.ORDER_STATUS_CANCELLED, 'AUTH');
};
/**
* Refund captured orders and update their afirm status. Used in Affirm job.
*
* @see pipeline 'AffirmJob'
*/
this.refundOrders = function () {
OrderMgr.processOrders(function (order) {
try {
api.refund(order.custom.AffirmExternalId);
order.custom.AffirmStatus = 'REFUNDED';
} catch (e) {
logger.error('Affirm. File - affirmOrder.ds. Error - {0}', e);
}
}, 'status = {0} AND custom.AffirmStatus = {1}', Order.ORDER_STATUS_CANCELLED, 'CAPTURE');
};
/**
* Update orders on Affirm side. Used in Affirm job.
*
* @see pipeline 'AffirmJob'
*/
this.updateOrders = function () {
var lastUpdateDateTime = readDateFromFile();
var startDate = new Date();
OrderMgr.processOrders(function (order) {
try {
var orderShipment = order.getDefaultShipment();
var shippingAddress = orderShipment.shippingAddress;
var updateObject = {
'order_id': order.orderNo,
'shipping': basket.getShippingAddress(order)
}
if (orderShipment.shippingMethod && orderShipment.shippingMethod.displayName) {
updateObject.shipping_carrier = orderShipment.shippingMethod.displayName;
}
if (orderShipment.trackingNumber) {
updateObject.shipping_confirmation = orderShipment.trackingNumber;
}
api.update(order.custom.AffirmExternalId, updateObject);
} catch (e) {
logger.error('Affirm. File - affirmOrder.ds. Error - {0}', e);
}
}, 'custom.AffirmStatus != NULL AND lastModified > {0}', lastUpdateDateTime.toISOString());
saveDateToFile(startDate);
};
/**
* Prepare order information for orderconfirmation page.
*
*/
this.trackOrderConfirmed = function(order){
var orderInfo;
order = OrderMgr.getOrder(order);
if (order){
orderInfo = {
storeName: dw.system.Site.getCurrent().name,
coupon: getCouponList( order ),
currency: order.getCurrencyCode(),
discount: getOrderDiscount(order),
paymentMethod: order.getPaymentInstruments()[0].paymentMethod,
revenue: priceToInteger( order.getMerchandizeTotalNetPrice()),
shipping: priceToInteger(order.getShippingTotalPrice()),
shippingMethod: (order.getShipments().length === 1 && order.getShipments()[0].shippingMethod) ? order.getShipments()[0].shippingMethod.displayName : "other",
tax: priceToInteger( order.getTotalTax() ),
orderId : order.orderNo,
total: priceToInteger( order.getTotalGrossPrice())
}
}
var productArray = []
var products = order.getAllProductLineItems().iterator();
while (products.hasNext()){
var lineItem = products.next();
if(!lineItem.bundledProductLineItem){
var obj = {
name: lineItem.product ? lineItem.product.name : lineItem.productName,
price: priceToInteger(lineItem.adjustedPrice),
productId: lineItem.product ? lineItem.product.ID : lineItem.optionID,
quantity: lineItem.quantity.value,
};
if( lineItem.product){
if(lineItem.product.brand ){
obj.brand = lineItem.product.brand;
}
if( lineItem.product.primaryCategory ){
obj.category = lineItem.product.primaryCategory.ID
} else {
if (lineItem.product.classificationCategory){
obj.category = lineItem.product.classificationCategory.ID
}
}
}
productArray.push(obj);
}
}
return {orderInfo : orderInfo, productInfo : productArray};
};
};
module.exports = new Order();
}());
/**
* function that converts price to integer (cents) using java methods
* @param price Money
* @returns Number
*/
function priceToInteger(price) {
var dec = price.getDecimalValue();
dec = dec.multiply(100);
dec = dec.get();
return dec;
}
/**
* function that returns order discount in cents
* @param order Order
* @returns Number
*/
function getOrderDiscount( order ) {
var discount = 0;
order.getPriceAdjustments().toArray().map(function(elem){
discount += priceToInteger(elem.price);
});
return -discount;
}
/**
* function that returns coupons applied to order
* @param order Order
* @returns String
*/
function getCouponList( order ) {
var coupons = order.getCouponLineItems().toArray().map(function(elem){
return elem.couponCode;
});
return coupons.join(',')
}
You can’t perform that action at this time.