Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes for BM Table #442

Open
wants to merge 7 commits into
base: replicate-20.2.2
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ node_modules/
Cartridges/app_storefront_base/
Cartridges/demo_core/
Cartridges/demo_controllers/
Cartridges/plugin_cartridge_merge/
bin/
150 changes: 91 additions & 59 deletions Cartridges/bm_checkoutcom/cartridge/scripts/helpers/CKOHelper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

/* API Includes */
var SystemObjectMgr = require('dw/object/SystemObjectMgr');
var Calendar = require('dw/util/Calendar');
var StringUtils = require("dw/util/StringUtils");
var OrderMgr = require('dw/order/OrderMgr');
var PaymentMgr = require('dw/order/PaymentMgr');
var PaymentTransaction = require('dw/order/PaymentTransaction');
Expand All @@ -11,6 +12,7 @@ var Site = require('dw/system/Site');

// Card Currency Config
var ckoCurrencyConfig = require('~/cartridge/scripts/config/ckoCurrencyConfig');
var totalPages;

/**
* Helper functions for the Checkout.com cartridge integration.
Expand All @@ -33,26 +35,45 @@ var CKOHelper = {
getCkoOrders: function() {
// Prepare the output array
var data = [];
var formattedResult = [];
var generalCounter = 0;
var sizeCounter = 0;

// Query the orders
var result = SystemObjectMgr.querySystemObjects('Order', '', 'creationDate desc');
var result = OrderMgr.searchOrders('custom.isCheckoutOrder = {0}', 'creationDate desc', true);

// Loop through the results
while (result.hasNext()) {
var item = result.next();
var query = this.parseQuery(request.httpQueryString);

// Get the payment instruments
var paymentInstruments = item.getPaymentInstruments().toArray();
if (!query.page) {
return result.asList();
}

// Loop through the payment instruments
for (var i = 0; i < paymentInstruments.length; i++) {
if (this.isCkoItem(paymentInstruments[i].paymentMethod) && !this.containsObject(item, data)) {
data.push(item);
}
var page = query.page,
pagination = query.size,
start = (page - 1) * pagination;

totalPages = Math.ceil(result.getCount() / pagination);

while (result.hasNext()) {
var pi = result.next();
var piLength = pi.paymentInstruments.length - 1 ;
if (start == 0 || generalCounter > start) {
formattedResult[sizeCounter] = pi;
sizeCounter++;
} else {
generalCounter++;
}

if (sizeCounter > pagination) {
return formattedResult;
}
}

return data;
if (!result.hasNext()) {
var queryDate = StringUtils.formatCalendar(new Calendar(pi.creationDate), "yyyy-MM-dd'T'HH:mm:ss'+Z'");
var queryString = 'creationDate <= ' + queryDate;
result = OrderMgr.searchOrders(queryString, 'creationDate desc');
}
}
},

/**
Expand All @@ -71,45 +92,58 @@ var CKOHelper = {
for (var j = 0; j < result.length; j++) {
// Get the payment instruments
var paymentInstruments = result[j].getPaymentInstruments().toArray();

// Loop through the payment instruments
for (var k = 0; k < paymentInstruments.length; k++) {
// Get the payment transaction
var paymentTransaction = paymentInstruments[k].getPaymentTransaction();

// Add the payment transaction to the output
if (!this.containsObject(paymentTransaction, data) && this.isTransactionNeeded(paymentTransaction, paymentInstruments[k])) {
// Build the row data
var row = {
id: i,
order_no: result[j].orderNo,
transaction_id: paymentTransaction.transactionID,
payment_id: paymentTransaction.custom.ckoPaymentId,
opened: paymentTransaction.custom.ckoTransactionOpened,
amount: paymentTransaction.amount.value,
currency: paymentTransaction.amount.currencyCode,
creation_date: paymentTransaction.getCreationDate().toDateString(),
type: paymentTransaction.type.displayValue,
processor: this.getProcessorId(paymentInstruments[k]),
refundable_amount: 0,
data_type: paymentTransaction.type.toString(),
};

// Calculate the refundable amount
var condition1 = row.data_type === PaymentTransaction.TYPE_CAPTURE;
var condition2 = row.opened !== false;
if (condition1 && condition2) {
row.refundable_amount = this.getRefundableAmount(paymentInstruments);
}

// Add the transaction
data.push(row);
i++;
var k = paymentInstruments.length - 1;

var paymentTransaction = paymentInstruments[k].getPaymentTransaction();
// Add the payment transaction to the output
if (!this.containsObject(paymentTransaction, data) && this.isTransactionNeeded(paymentTransaction, paymentInstruments[k])) {
// Build the row data
var row = {
id: i,
order_no: result[j].orderNo,
transaction_id: paymentTransaction.transactionID,
payment_id: paymentTransaction.custom.ckoPaymentId,
opened: paymentTransaction.custom.ckoTransactionOpened,
amount: paymentTransaction.amount.value,
currency: paymentTransaction.amount.currencyCode,
creation_date: paymentTransaction.getCreationDate().toDateString(),
type: paymentTransaction.type.displayValue,
processor: this.getProcessorId(paymentInstruments[k]),
refundable_amount: 0,
data_type: paymentTransaction.type.toString(),
};

// Calculate the refundable amount
var condition1 = row.data_type === PaymentTransaction.TYPE_CAPTURE;
var condition2 = row.opened !== false;
if (condition1 && condition2) {
row.refundable_amount = this.getRefundableAmount(paymentInstruments);
}

// Add the transaction
data.push(row);
i++;
}
}

return data;
return {
"last_page" : totalPages,
"data" : data
};
},

/**
* Parse a query string
* @param {String} queryString The query string of the URL
*/
parseQuery: function(queryString) {
var query = {};
var pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split('=');
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
}
return query;
},

/**
Expand Down Expand Up @@ -153,16 +187,14 @@ var CKOHelper = {
isTransactionNeeded: function(paymentTransaction, paymentInstrument) {
// Get an optional transaction id
// eslint-disable-next-line
var tid = request.httpParameterMap.get('tid').stringValue;
var pid = request.httpParameterMap.get('tid').stringValue;

// Return true only if conditions are met
var condition1 = (tid && paymentTransaction.transactionID === tid) || !tid;
var condition2 = this.isCkoItem(paymentInstrument.paymentMethod);
var condition3 = this.isCkoItem(this.getProcessorId(paymentInstrument));
var condition4 = paymentTransaction.custom.ckoPaymentId !== null && paymentTransaction.custom.ckoPaymentId !== '';
var condition5 = paymentTransaction.transactionID && paymentTransaction.transactionID !== '';
var condition1 = pid && (paymentTransaction.custom.ckoPaymentId == pid || paymentTransaction.transactionID == pid) || !pid;
var condition2 = this.isCkoItem(this.getProcessorId(paymentInstrument));
var condition3 = paymentTransaction.transactionID && paymentTransaction.transactionID !== '';

if (condition1 && condition2 && condition3 && condition4 && condition5) {
if (condition1 && condition2 && condition3) {
return true;
}

Expand Down Expand Up @@ -319,7 +351,7 @@ var CKOHelper = {
*/
getFormattedPrice: function(amount, currency) {
var totalFormated;
if (currency) {
if (currency && typeof (currency) == 'string') {
var ckoFormateBy = this.getCkoFormatedValue(currency);
totalFormated = amount * ckoFormateBy;

Expand Down Expand Up @@ -367,7 +399,7 @@ var CKOHelper = {
*/
getAccountKeys: function() {
var keys = {};
var str = this.getValue('ckoMode') === 'live' ? 'Live' : 'Sandbox';
var str = this.getValue('ckoMode') == 'live' ? 'Live' : 'Sandbox';

keys.publicKey = this.getValue('cko' + str + 'PublicKey');
keys.secretKey = this.getValue('cko' + str + 'SecretKey');
Expand All @@ -381,4 +413,4 @@ var CKOHelper = {
* Module exports
*/

module.exports = CKOHelper;
module.exports = CKOHelper;
42 changes: 42 additions & 0 deletions Cartridges/bm_checkoutcom/cartridge/scripts/jobs/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

/* API Includes */
var SystemObjectMgr = require('dw/object/SystemObjectMgr');
var Calendar = require('dw/util/Calendar');
var Order = require('dw/order/Order');
var OrderMgr = require('dw/order/OrderMgr');
var Status = require('dw/system/Status');
var PaymentMgr = require('dw/order/PaymentMgr');
var PaymentTransaction = require('dw/order/PaymentTransaction');
var PaymentInstrument = require('dw/order/PaymentInstrument');
var Resource = require('dw/web/Resource');
var Site = require('dw/system/Site');
var StringUtils = require('dw/util/StringUtils');
var Transaction = require('dw/system/Transaction');
var Logger = require('dw/system/Logger').getLogger('ProcessOut', 'processoutHelper');
var Money = require('dw/value/Money');
var totalPages;

function test() {
// Query the orders
var result = OrderMgr.searchOrders('', 'creationDate desc');

while (result.hasNext()) {
var pi = result.next();
var piLength = pi.paymentInstruments.length - 1 ;
if (pi.paymentTransaction.paymentProcessor
&& pi.paymentTransaction.paymentProcessor.ID.indexOf('CHECKOUTCOM_') != -1) {
Transaction.wrap(function() {
pi.custom.isCheckoutOrder = true;
});
}

if (!result.hasNext()) {
var queryDate = StringUtils.formatCalendar(new Calendar(pi.creationDate), "yyyy-MM-dd'T'HH:mm:ss'+Z'");
var queryString = 'creationDate <= ' + queryDate;
result = OrderMgr.searchOrders(queryString, 'creationDate desc');
}
}
}

module.exports = {test: test};
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function openModal(elt) {

// Get the transaction data
var tidExists = members[2] !== null && members[2] !== 'undefined';
var isValidTid = members[2].length > 0 && members[2].indexOf('act_') === 0;
var isValidTid = members[2].length > 0 && members[2].indexOf('pay_') === 0;
if (tidExists && isValidTid) {
// eslint-disable-next-line
getTransactionData(members);
Expand Down Expand Up @@ -94,7 +94,7 @@ function getTransactionData(members) {
data: { tid: transactionId },
success: function(data) {
// Get the data
var transaction = JSON.parse(data)[0];
var transaction = JSON.parse(data).data[0];

// Set the transation data field ids
var field1Id = '[id="' + task + '_value"]';
Expand Down Expand Up @@ -157,18 +157,18 @@ function performAction(task) {
// Set the transaction id
var paymentId = jQuery('[id="' + task + '_payment_id"]').text();

// Set the currency
var currency = jQuery('[id="' + task + '_currency"]').text();

// Set the transaction value field id
var amount = jQuery('[id="' + task + '_value"]').val();

if(paymentId.indexOf('pay_') == -1) {
paymentId = jQuery('[id="' + task + '_transaction_id"]').text();
}

// Prepare the action data
var data = {
pid: paymentId,
task: task,
amount: amount,
currency: currency
};

// Send the AJAX request
Expand Down Expand Up @@ -201,8 +201,9 @@ function performAction(task) {
* @param {string} tableData The table data
*/
function reloadTable(tableData) {
var data = JSON.parse(tableData);
// Update the row data
window.ckoTransactionsTable.replaceData(tableData);
window.ckoTransactionsTable.replaceData(data.data);

// Show the success message
// eslint-disable-next-line
Expand All @@ -222,4 +223,4 @@ function showSuccessMessage() {
}, 7000);
}
);
}
}
Loading