Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"endpoints": [
{"endpoint": "createCheckoutSession", "schema": "schema.yaml", "implementation": "script"},
{"endpoint": "getNextOrderId", "schema": "schema.yaml", "implementation": "script"},
{"endpoint": "getPaymentMethodConfigration", "schema": "schema.yaml", "implementation": "script"}
"endpoints": [
{"endpoint": "createCheckoutSession", "schema": "schema.yaml", "implementation": "script"}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ paths:
schema:
type: string
minLength: 1
- in: query
name: c_customerId
required: true
schema:
type: string
minLength: 1
- in: query
name: locale
required: false
Expand All @@ -44,6 +50,7 @@ paths:
required:
- order
- configuration
- callbacks
properties:
order:
type: object
Expand Down Expand Up @@ -121,6 +128,8 @@ paths:
transactionInfo:
type: object
additionalProperties: true
callbacks:
type: object
configuration:
type: object
required:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
const RESTResponseMgr = require('dw/system/RESTResponseMgr');
const marketPay = require('*/cartridge/scripts/services/marketPay')
const Logger = require('dw/system/Logger');
const Logger = require('dw/system/Logger').getLogger('MarketPay','MarketPay');
const SCAPIService = require('*/cartridge/scripts/services/scapiService');

exports.createCheckoutSession = function () {
var requestBody = request.httpParameterMap.requestBodyAsString;
var requestData = JSON.parse(requestBody);
var result = marketPay.getTokenAndSessionId(requestData);
var paymentMethods = marketPay.getPaymentMethods(result.token, result.sessionId);

Logger.info(`MarketPayLog: ${JSON.stringify(paymentMethods)}`);
var customerId = request.httpParameterMap.c_customerId;
var customerIdValue = customerId.stringValue;

try {
var requestBody = request.httpParameterMap.requestBodyAsString;
var requestData = JSON.parse(requestBody);
var result = marketPay.getTokenAndSessionId(requestData);
var paymentMethods = marketPay.getPaymentMethods(result.token, result.sessionId);

const Transaction = require('dw/system/Transaction');
var CustomObjectMgr = require('dw/object/CustomObjectMgr');

Transaction.wrap(function () {
var marketPayDataObj = CustomObjectMgr.getCustomObject('MarketPayData', customerIdValue);
if (!marketPayDataObj) {
marketPayDataObj = CustomObjectMgr.createCustomObject('MarketPayData', customerIdValue);
}
marketPayDataObj.custom.sessionID = result.sessionId;
marketPayDataObj.custom.customerID = customerIdValue;
marketPayDataObj.custom.token = result.token;
marketPayDataObj.custom.paymentMethods = JSON.stringify(paymentMethods);
});

RESTResponseMgr
.createSuccess(result)
.createEmptySuccess(200)
.render();
} catch (error) {

Logger.error('Error creating session ' + error.message);

RESTResponseMgr
.createError(404, 'Session-error', 'Not created', 'please reach out the SFCC developers.')
.render();
}
};

exports.getNextOrderId = function () {
};

exports.getPaymentMethodConfigration = function () {

}
};

exports.createCheckoutSession.public = true;
exports.getPaymentMethodConfigration.public = true;
exports.getNextOrderId.public = true;

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const Site = require('dw/system/Site');
const Logger = require('dw/system/Logger');

function getFormattedDataForMarketPaySession(basket) {

const Locale = require('dw/util/Locale');
const URLUtils = require('dw/web/URLUtils');
var currentLocale = Locale.getLocale(request.locale);
Expand All @@ -13,7 +11,7 @@ function getFormattedDataForMarketPaySession(basket) {
// Initialize the order data object
var orderData = {
order: {
orderId: basket.custom.marketPayUsedOrderNo,
orderId: basket.getUUID(),
amount: {
value: basket.getTotalGrossPrice().getValue(),
currency: basket.getCurrencyCode()
Expand All @@ -32,16 +30,16 @@ function getFormattedDataForMarketPaySession(basket) {
type: "URL",
value: URLUtils.https('MarketPay-PaymentFail').toString()
},
redirect: URLUtils.https('MarketPay-Redirect').toString()
//notification: URLUtils.https('MarketPay-PaymentNotification').toString()
redirect: URLUtils.https('MarketPay-Redirect').toString(),
notification: URLUtils.https('MarketPay-PaymentNotification').toString()
},
configuration: {
paymentType: "PAYMENT",
bodyFormat: "JSON",
autoCapture: false,
paymentDisplayType: "REDIRECT",
// country: countryCode, @todo Get the country from basket or customer profile
language: Site.getCurrent().getDefaultLocale().split('_')[0] || "en"
language: Site.getCurrent().getDefaultLocale().split('_')[0] || 'en_US'
}
};

Expand All @@ -51,7 +49,7 @@ function getFormattedDataForMarketPaySession(basket) {
for (var i = 0; i < productLineItems.length; i++) {
var lineItem = productLineItems[i];
orderData.order.orderLines.push({
itemId: lineItem.getProductID() || lineItem.getUUID(),
itemId: lineItem.getProductID(),
description: lineItem.getProductName() || '',
quantity: lineItem.getQuantityValue(),
unitPrice: lineItem.getAdjustedPrice().getValue()
Expand Down Expand Up @@ -109,6 +107,124 @@ function getFormattedDataForMarketPaySession(basket) {
return orderData;
}

function getDataForUpdateSession(order) {
const Locale = require('dw/util/Locale');
const URLUtils = require('dw/web/URLUtils');
var currentLocale = Locale.getLocale(request.locale);

var orderData = {
order: {
orderId: order.getOrderNo(),
amount: {
value: order.getTotalGrossPrice().getValue(),
currency: order.getCurrencyCode()
},
orderLines: [],
customer: null,
transactionInfo: {}
},
callbacks: {
formStyling: URLUtils.https('MarketPay-CallbackForm').toString(),
success: {
type: "URL",
value: URLUtils.https('MarketPay-PaymentSuccess').toString()
},
failure: {
type: "URL",
value: URLUtils.https('MarketPay-PaymentFail').toString()
},
redirect: URLUtils.https('MarketPay-Redirect').toString(),
notification: URLUtils.https('MarketPay-PaymentNotification').toString()
},
configuration: {
paymentType: "PAYMENT",
bodyFormat: "JSON",
autoCapture: false,
paymentDisplayType: "REDIRECT",
language: Site.getCurrent().getDefaultLocale().split('_')[0] || 'en_US'
}
};

// Format order lines from order product line items
var productLineItems = order.getProductLineItems();
if (productLineItems && productLineItems.length > 0) {
for (var i = 0; i < productLineItems.length; i++) {
var lineItem = productLineItems[i];
orderData.order.orderLines.push({
itemId: lineItem.getProductID(),
description: lineItem.getProductName() || '',
quantity: lineItem.getQuantityValue(),
unitPrice: lineItem.getAdjustedPrice().getValue()
});
}
}

// Format customer information with validation
var customer = order.getCustomer();
var billingAddress = order.getBillingAddress();
var defaultShipment = order.getDefaultShipment();
var shippingAddress = defaultShipment ? defaultShipment.getShippingAddress() : null;

if (customer || billingAddress || shippingAddress) {
orderData.order.customer = {};

if (customer && customer.isRegistered()) {
var profile = customer.getProfile();
if (profile) {
orderData.order.customer.firstName = profile.getFirstName() || '';
orderData.order.customer.lastName = profile.getLastName() || '';
orderData.order.customer.email = profile.getEmail() || '';
}
}

if (!orderData.order.customer.email && billingAddress) {
orderData.order.customer.firstName = billingAddress.getFirstName() || '';
orderData.order.customer.lastName = billingAddress.getLastName() || '';
orderData.order.customer.email = order.getCustomerEmail() || '';
}

if (billingAddress) {
orderData.order.customer.billingAddress = {
street: billingAddress.getAddress1() || '',
city: billingAddress.getCity() || '',
country: billingAddress.getCountryCode() ? billingAddress.getCountryCode().getValue() : '',
zipCode: billingAddress.getPostalCode() || ''
};
}

if (shippingAddress) {
orderData.order.customer.shippingAddress = {
street: shippingAddress.getAddress1() || '',
city: shippingAddress.getCity() || '',
country: shippingAddress.getCountryCode() ? shippingAddress.getCountryCode().getValue() : '',
zipCode: shippingAddress.getPostalCode() || ''
};
}
}

return orderData;
}

function getOnInitiatePaymentURL(selectedPaymentMethod, marketPayPaymentMethods) {
marketPayPaymentMethods = JSON.parse(marketPayPaymentMethods);

if (!selectedPaymentMethod || !marketPayPaymentMethods || !marketPayPaymentMethods.methods) {
return null;
}

for (var i = 0; i < marketPayPaymentMethods.methods.length; i++) {
var method = marketPayPaymentMethods.methods[i];
if (method.id === selectedPaymentMethod) {
return method.onInitiatePayment && method.onInitiatePayment.value ? method.onInitiatePayment.value : null;
}
}

return null;
}


module.exports = {
getFormattedDataForMarketPaySession: getFormattedDataForMarketPaySession
getFormattedDataForMarketPaySession: getFormattedDataForMarketPaySession,
getDataForUpdateSession: getDataForUpdateSession,
getOnInitiatePaymentURL: getOnInitiatePaymentURL
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const Logger = require('dw/system/Logger');
const Site = require('dw/system/Site');

/**
Expand Down
Loading