Sample Code

If you are a LINK partner, you can use a single cartridge to handle all transactions, or you can write a new cartridge to hand the additional transactions (capture, void, refund, or credit).

Sample code is provided for the following.

Authorization Sample Code

importScript('helper/ConfigHelper.js');
importScript('helper/TranslateRequest.js');
importScript('helper/TranslateResponse.js');

var link = require( 'dw/om/link/payment' );
var IS = require('dw/om/link/payment/IdempotentStatusEnum' );

var csReference = webreferences.CyberSourceTransaction;
var service : Stub = csReference.getService("TransactionProcessor", "portXML");

exports.doAuthorize = function (omRequest : Object, config : Object) 
{
    var request = {}
    request = TranslateRequest(omRequest);
    
    var token = "";
    
    var configHelper = new ConfigHelper();
    RetrieveConfiguration(config, configHelper);
    SetEndpoint(service, configHelper);
    
       if(!CreateHeader(service, configHelper)){
        return null;
    }
    
    var response = null;
    try
    {
        response = serviceCall(request);
    }
    catch(e)
    {
        var omResponse = new link.AuthorizeResponse();
        return ContructOMResponse(omResponse, link.StatusEnum.ERROR, e.message);
    }
    
    var authResponse = TranslateResponse(response, "Authorize");
    authResponse.tokenizedPaymentInstrument = "Auth Token";
    
    return authResponse;
}

Sale Sample Code

importScript('helper/ConfigHelper.js');
importScript('helper/TranslateRequest.js');
importScript('helper/TranslateResponse.js');

var link = require( 'dw/om/link/payment' );
var IS = require('dw/om/link/payment/IdempotentStatusEnum' );

var csReference = webreferences.CyberSourceTransaction;
var service : Stub = csReference.getService("TransactionProcessor", "portXML");

exports.doSale = function (omRequest : Object, config : Object) 
{
    var request = {}
    request = TranslateRequest(omRequest);
    
    var configHelper = new ConfigHelper();
    RetrieveConfiguration(config, configHelper);
    SetEndpoint(service, configHelper);
    
       if(!CreateHeader(service, configHelper)){
        return null;
    }
    
    var response = null;
    try
    {
        response = serviceCall(request);
    }
    catch(e)
    {
        var omResponse = new link.SaleResponse();
        return ContructOMResponse(omResponse, link.StatusEnum.ERROR, e.message);
    }
       
    var saleResponse = TranslateResponse(response, "Sale");
    saleResponse.tokenizedPaymentInstrument = "Sale Token";
    
    return saleResponse;

}

Capture Sample Code

importScript('helper/ConfigHelper.js');
importScript('helper/TranslateRequest.js');
importScript('helper/TranslateResponse.js');

var link = require( 'dw/om/link/payment' );
var IS = require('dw/om/link/payment/IdempotentStatusEnum' );

var csReference = webreferences.CyberSourceTransaction;
var service : Stub = csReference.getService("TransactionProcessor", "portXML");

/**
 * Capture Credit Card paid amount, authorize if decision ACCEPT
 */
exports.doCapture = function (omRequest : Object, config : Object) 
{
    var request = {}
    request = TranslateRequest(omRequest);
    
    var configHelper = new ConfigHelper();
    RetrieveConfiguration(config, configHelper);
    SetEndpoint(service, configHelper);
    
       if(!CreateHeader(service, configHelper)){
        return null;
    }
    
    var response = null;
    try
    {
        response = serviceCall(request);
    }
    catch(e)
    {
        var omResponse = new link.CaptureResponse();
        return ContructOMResponse(omResponse, link.StatusEnum.ERROR, e.message);
    }
    
    return TranslateResponse(response, "Capture");
}

Refund Sample Code

importScript('helper/ConfigHelper.js');
importScript('helper/TranslateRequest.js');
importScript('helper/TranslateResponse.js');

var link = require( 'dw/om/link/payment' );
var IS = require('dw/om/link/payment/IdempotentStatusEnum' );

var csReference = webreferences.CyberSourceTransaction;
var service : Stub = csReference.getService("TransactionProcessor", "portXML");

exports.doRefund = function (omRequest : Object, config : Object) 
{
    var request = {}
    request = TranslateRequest(omRequest);
    
    var configHelper = new ConfigHelper();
    RetrieveConfiguration(config, configHelper);
    SetEndpoint(service, configHelper);
    
       if(!CreateHeader(service, configHelper)){
        return null;
    }
    
    var response = null;
    try
    {
        response = serviceCall(request);
    }
    catch(e)
    {
        var omResponse = new link.RefundResponse();
        return ContructOMResponse(omResponse, link.StatusEnum.ERROR, e.message);
    }
    
    return TranslateResponse(response, "Refund");
}

Void Sample Code

importScript('helper/ConfigHelper.js');
importScript('helper/TranslateRequest.js');
importScript('helper/TranslateResponse.js');

var link = require( 'dw/om/link/payment' );
var IS = require('dw/om/link/payment/IdempotentStatusEnum' );

var csReference = webreferences.CyberSourceTransaction;
var service : Stub = csReference.getService("TransactionProcessor", "portXML");

/**
 * Void Credit Card paid amount, authorize if decision ACCEPT
 */
exports.doVoid = function (omRequest : Object, config : Object) 
{
    var request = {}
    request = TranslateRequest(omRequest);
    
    var configHelper = new ConfigHelper();
    RetrieveConfiguration(config, configHelper);
    SetEndpoint(service, configHelper);
    
       if(!CreateHeader(service, configHelper)){
        return null;
    }
    
    var response = null;
    try
    {
        response = serviceCall(request);
    }
    catch(e)
    {
        return ContructOMResponse(omResponse, link.StatusEnum.ERROR, e.message);
    }
    
    return TranslateResponse(response, "Void");
}

Credit Sample Code

importScript('helper/ConfigHelper.js');
importScript('helper/TranslateRequest.js');
importScript('helper/TranslateResponse.js');

var link = require( 'dw/om/link/payment' );
var IS = require('dw/om/link/payment/IdempotentStatusEnum' );

var csReference = webreferences.CyberSourceTransaction;
var service : Stub = csReference.getService("TransactionProcessor", "portXML");

exports.doCredit = function (omRequest : Object, config : Object) 
{
    var request = {}
    request = TranslateRequest(omRequest);
    
    var configHelper = new ConfigHelper();
    RetrieveConfiguration(config, configHelper);
    SetEndpoint(service, configHelper);
    
       if(!CreateHeader(service, configHelper)){
        return null;
    }
    
    var response = null;
    try
    {
        response = serviceCall(request);
    }
    catch(e)
    {
        var omResponse = new link.CreditResponse();
        return ContructOMResponse(omResponse, link.StatusEnum.ERROR, e.message);
    }
    
    var creditResponse = TranslateResponse(response, "Credit");
    creditResponse.tokenizedPaymentInstrument = "Credit Token";
    
    return creditResponse;
    
}

Translate Order Management Request Sample Code

importPackage( dw.object );

function TranslateRequest(omRequest){
    
    var request = {};
    
    request = GetRequestMessage(omRequest);
    
    return request;
}


function GetRequestMessage(omRequest)
{
    var request = {};

    if (Object.prototype.hasOwnProperty.call(omRequest, 'billing') && omRequest.billing != null) 
    {
        var billTo = {};
        
        billTo.firstName = omRequest.billing.firstName;
        billTo.lastName = omRequest.billing.lastName;
        billTo.street1 = omRequest.billing.address.address1;
        billTo.city = omRequest.billing.address.city;
        billTo.state = omRequest.billing.address.stateCode;
        billTo.postalCode = omRequest.billing.address.postalCode;
        billTo.country = omRequest.billing.address.countryCode;
        
        billTo.email = omRequest.billing.contact.email;
        billTo.phoneNumber = omRequest.billing.contact.phone;
        
        request.billTo = billTo;
    }

    if (Object.prototype.hasOwnProperty.call(omRequest, 'payment') && omRequest.payment != null && omRequest.payment.instrument != null)
    {
        var card = {};
        
        card.accountNumber = omRequest.payment.instrument.cardNumber;
        card.expirationMonth = omRequest.payment.instrument.expirationMonth;
        card.expirationYear = omRequest.payment.instrument.expirationYear;
        card.cardType = omRequest.payment.instrument.paymentMethod;
        card.cvIndicator = "1";
        card.cvNumber = omRequest.payment.instrument.verificationCode;
            
        request.card = card;    
    }
    
    request.merchantReferenceCode = "test reference id";
    request.Amount = omRequest.transactionTotal;
    
    return request;
}

Translate Responses Sample Code

```importPackage( dw.object );
importScript('helper/om/PaymentTransactionResponse.js');

var link = require( 'dw/om/link/payment' );
var IS = require('dw/om/link/payment/IdempotentStatusEnum' );

var csReference = webreferences.CyberSourceTransaction;

function TranslateResponse(response: Object, transactionType: String){
    
    var omResponse = GetResponseObject(transactionType);
    
    try 
    {
        omResponse.idempotentStatus=IS.PROCESSED;
        
        if (response != null)
        {
            omResponse.result = new link.ResponseResult();
            omResponse.result.status = link.StatusEnum.APPROVED;
            omResponse.result.description = "Processed";
            omResponse.result.code = 200;
            
            omResponse.transactionId = GetTransactionId(transactionType);

            omResponse.rawResponse = "Raw Response";
            
            var Decimal = require( 'dw/util/Decimal' );     
            omResponse.transactionTotal = new Decimal(10.00);
            
            return omResponse;
        }
        
        return ContructOMResponse(omResponse, link.StatusEnum.ERROR, "Null Response");
    }
    catch(e)
    {    
        return ContructOMResponse(omResponse, link.StatusEnum.ERROR, e.message);
    }
    
}

function TranslateResponseAuthorize(response: Object, transactionType: String, token : String){
    
    var omResponse = GetResponseObject(transactionType);
    
    try 
    {
        omResponse.idempotentStatus=IS.PROCESSED;
        
        if (response != null)
        {
            omResponse.result = new link.ResponseResult();
            omResponse.result.status = link.StatusEnum.APPROVED;
            omResponse.result.description = "Processed - Token: " + token;
            omResponse.result.code = 200;
            
            omResponse.transactionId = GetTransactionId(transactionType);

            omResponse.rawResponse = "Raw Response Token";
            
            var Decimal = require( 'dw/util/Decimal' );     
            omResponse.transactionTotal = new Decimal(10.00);
            
            return omResponse;
        }
        
        return ContructOMResponse(omResponse, link.StatusEnum.ERROR, "Null Response");
    }
    catch(e)
    {    
        return ContructOMResponse(omResponse, link.StatusEnum.ERROR, e.message);
    }
    
}

function GetResponseObject(transactionType: String)
{
    switch ( transactionType )
    {
        case "Authorize": return new link.AuthorizeResponse();
        case "Sale": return new link.SaleResponse();
        case "Capture": return new link.CaptureResponse();
        case "Void": return new link.VoidPaymentResponse();
        case "Refund": return new link.RefundResponse();
        case "Credit": return new link.CreditResponse();
    }
    
    return null;
}

function GetTransactionId(transactionType: String)
{
    switch ( transactionType )
    {
        case "Authorize": return 1111;
        case "Sale": return 2222;
        case "Capture": return 3333;
        case "Void": return 4444;
        case "Refund": return 5555;
        case "Credit": return 6666;
    }
    
    return 0000;
}

function ContructOMResponse (omResponse, status, message) 
{
    omResponse.result = new link.ResponseResult();
    omResponse.result.status = status;
    omResponse.result.description = message;
    omResponse.idempotentStatus=IS.PROCESSED;
    omResponse.rawResponse = "";
    return omResponse;
}```

ConfigHelper Sample Code

//importScript('int_cybersource:init/SoapServiceInit.ds');
importScript('helper/SoapServiceInit.ds');
var SOAPUtil = require ("dw/rpc/SOAPUtil");


function ConfigHelper() {

    this.merchantID = "";
    this.setMerchantID = setMerchantID;
    this.getMerchantID = getMerchantID;

    this.merchantPassword = "";
    this.setMerchantPassword = setMerchantPassword;
    this.getMerchantPassword = getMerchantPassword;
        
    this.testMode = true;
    this.setTestMode = setTestMode;
    this.getTestMode = getTestMode;
}

function setMerchantID(value){
    this.merchantID = value;
}
function getMerchantID(){
    return this.merchantID;
}
function setMerchantPassword(value){
    this.merchantPassword = value;
}
function getMerchantPassword(){
    return this.merchantPassword;
}
function setTestMode(value){
    this.testMode = value;
}
function getTestMode(){
    return this.testMode;
}                

function RetrieveConfiguration (config : Object, configHelper: ConfigHelper)
{
    configHelper.setMerchantID(config.merchantID);
    configHelper.setMerchantPassword(config.merchantPassword);
    configHelper.setTestMode(config.testMode);
}

function SetEndpoint (service : Stub, configHelper: ConfigHelper)
{
    var Stub = require('dw/rpc/Stub');
    
    if (configHelper.getTestMode())
    {        
        //service._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,'TestEndPoint);
    }
    else
    {
        //service._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,'LiveEndPoint');
    }
}

function CreateHeader (service : Stub, configHelper: ConfigHelper){
    
    if(empty(configHelper.getMerchantID()) || empty(configHelper.getMerchantPassword())){
        Logger.error("MerchantID and/or MerchantPassword are missing.");    
        return false;
    }
    
    // create Security Header
    var usernameToken : XML = 
        <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:Username>{configHelper.getMerchantID()}</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">
                {configHelper.getMerchantPassword()}
            </wsse:Password>
        </wsse:UsernameToken>;
    
    SOAPUtil.setHeader( service, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","Security", usernameToken, true, null );
    
    return true;
}

function serviceCall()
{
    //Call to external system
    return "Success";
}

Related Links

Configure the B2C Commerce Processor

LINK Payment Provider

Sample API Helper Contracts

Sample API Transaction Contracts

X Privacy Update: We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used. By continuing to use this site you are giving us your consent to do this. Privacy Policy.