Jira Code: OSI-65
The script calls the API for capture the authorization using the authorization id that we have got when we have been done an order in PayPal. And it reauthorize the transaction if the authorization id exceeds an honour period of three. And will be performed in the save action of a cash sale.
/**
* @NApiVersion 2.x
* @NScriptType workflowactionscript
*/
/**
* Script Description
* The script will call the API for capture the authorization using the authorization id that we have got when we has been done an order in paypal. And it will reauthorize the transaction if the authorization id exceeds a honor period of three. And will be performed in the save action of cash sale.
* Created on 18-March-2019
*/
/*******************************************************************************
* * OneSource IML | Aquverse *
* **************************************************************************
*
*
* Author: Jobin & Jismi IT Services LLP
*
* REVISION HISTORY
*
*
******************************************************************************/
define(['N/https', 'N/search', 'N/record'],
function(https, search, record) {
/**
* Definition of the Suitelet script trigger point.
*
* @param {Object}
* scriptContext
* @param {Record}
* scriptContext.newRecord - New record
* @param {Record}
* scriptContext.oldRecord - Old record
* @Since 2016.1
*/
function onAction(scriptContext) {
var currRecord = scriptContext.newRecord;
var id = currRecord.id;
var status = null;
var tranId = null;
var paypalSiteCat = currRecord.getValue({
fieldId: 'custbody_jj_paypal_site_category'
});
var paymentMthd = currRecord.getValue({
fieldId: 'paymentmethod'
});
var paypalAuthId = currRecord.getValue({
fieldId: 'paypalauthid'
});
var paypalOrderid = currRecord.getValue({
fieldId: 'paypalorderid'
});
var paypalStatus = currRecord.getValue({
fieldId: 'paypalstatus'
});
var paypalStatusJJ = currRecord.getValue({
fieldId: 'custbody_jj_paypal_status'
});
var currency = currRecord.getText({
fieldId: 'currency'
});
var createdFrom = currRecord.getValue({
fieldId: 'createdfrom'
});
var total = currRecord.getValue({
fieldId: 'total'
});
//total = toFixed(2);
log.debug('currency', currency);
log.debug('total', total);
log.debug('createdFrom', createdFrom);
if (paypalSiteCat == 1) { //from Aquverse
if ((paymentMthd == 10) //paypal
&&
(paypalAuthId) &&
(paypalOrderid) &&
(paypalStatus == "") &&
((paypalStatusJJ != 'Completed') || (paypalStatusJJ != 'COMPLETED')) &&
((paypalStatus != 'Completed') || (paypalStatus != 'COMPLETED'))) {
//reauthorize the transaction if the so created three days ago.
var salesorderSearchObj = search
.create({
type: "salesorder",
filters: [
["type", "anyof", "SalesOrd"],
"AND",
["datecreated", "notbefore", "threedaysago"],
"AND",
["internalidnumber", "equalto", createdFrom],
"AND",
["mainline", "is", "T"]
],
columns: [search.createColumn({
name: "tranid",
label: "Document Number"
})]
});
var searchResultCount = salesorderSearchObj.runPaged().count;
log.debug('searchResultCount', searchResultCount);
if (searchResultCount == 0) {
// paypalAuthId = '2RV635954T3646427';
// 7XG15622X5213870V
try {
// CALLING THE API to reauthorize the transaction
var headerObj = {
"Authorization": "Basic QVN1UEtQcXlKWDk4amthV3JlUWVIMkFuOEJudUtXbXplR2QtLWR0WnBiVkp2YkJFZExZQm9Da3k3YjdCTDdDSi1pZjBGU1JZUmJ1UmxYSG86RUotYTZSNGZrdFg1RnBycm04b3BTQUc3Z1pqNmxaakR1azNUNGE3Q2w3dHVBUUR0S0tyR2VoNkczekRxZndnSVk3dWxkMWotcmI3Nlh1QXk=",
"Content-Type": "application/json",
"Accept": "application/json"
};
var bodyObj = {
"amount": {
"value": total,
"currency_code": currency
}
};
// Posting a request for reauthorize
var reauthResponse = https
.post({
url: 'https://api.paypal.com/v2/payments/authorizations/' +
paypalAuthId +
'/reauthorize ',
body: JSON.stringify(bodyObj),
headers: headerObj
});
var statusCode = reauthResponse.code;
if (statusCode == '201') {
var data = JSON.parse(reauthResponse.body);
paypalAuthId = data.id;
}
log.debug('reauthResponse', reauthResponse);
log.debug('statusCode', statusCode);
log.debug('data', data);
} catch (e) {
log.debug({
title: e.name,
details: e.message
});
}
}
// sandbox: Basic
// QVNqaE9Sal9ZS0FtLTlkVlFQMWRlYVFySzE0UXpHUkQ5Skk0SkVacWlxOXNjQXFCdGdqa1lxSXVoeGVTejRQeWVpME9OZTNERXdhZGptaVk6RURVTnQ3T2lOVVE2RmxlUldJT2FjQlN3VF9YRFFTQXc5YlpEeXRvc0ZBWG55QUptd3IzRzRqTzcxT0UwOHVaTWtUaXpaOEhYUUNTaDEtd18=
// production: Basic
// QVN1UEtQcXlKWDk4amthV3JlUWVIMkFuOEJudUtXbXplR2QtLWR0WnBiVkp2YkJFZExZQm9Da3k3YjdCTDdDSi1pZjBGU1JZUmJ1UmxYSG86RUotYTZSNGZrdFg1RnBycm04b3BTQUc3Z1pqNmxaakR1azNUNGE3Q2w3dHVBUUR0S0tyR2VoNkczekRxZndnSVk3dWxkMWotcmI3Nlh1QXk=
try {
// CALLING THE API to capture the authorization
var headerObj = {
"Authorization": "Basic QVN1UEtQcXlKWDk4amthV3JlUWVIMkFuOEJudUtXbXplR2QtLWR0WnBiVkp2YkJFZExZQm9Da3k3YjdCTDdDSi1pZjBGU1JZUmJ1UmxYSG86RUotYTZSNGZrdFg1RnBycm04b3BTQUc3Z1pqNmxaakR1azNUNGE3Q2w3dHVBUUR0S0tyR2VoNkczekRxZndnSVk3dWxkMWotcmI3Nlh1QXk=",
"Content-Type": "application/json",
"Accept": "application/json"
};
var bodyObj = {
"amount": {
"value": total,
"currency_code": currency
},
"final_capture": true
};
//'https://api.sandbox.paypal.com/v2/payments/authorizations/'
// Posting a request for search shipments
var paypalResponse = https
.post({
url: 'https://api.paypal.com/v2/payments/authorizations/' +
paypalAuthId + '/capture',
body: JSON.stringify(bodyObj),
headers: headerObj
});
log.debug('paypalResponse', paypalResponse);
var statusCodeCapture = paypalResponse.code;
if (statusCodeCapture == '201') {
var dataCapture = JSON.parse(paypalResponse.body);
tranId = dataCapture.id;
status = dataCapture.status;
/* log.debug('tranId', tranId);
log.debug('status', status);*/
if (status == 'COMPLETED') {
status = 'Completed';
}
record.submitFields({
type: record.Type.CASH_SALE,
id: id,
values: {
'paypaltranid': tranId,
'paypalauthid': paypalAuthId,
'custbody_jj_paypal_status': status,
'paypalstatus': status
}
});
}
} catch (e) {
log.debug({
title: e.name,
details: e.message
});
}
}
}
}
return {
onAction: onAction
};
});