SFCC Cartridge
...
Mapping Samples
CyberSource
4 min
1\ full forterorder ds file using cybersource below is the full forterorder ds file including the mapped authorization response for a merchant using the cybersource a gateway note that the majority of the mapping takes place in the fortercreditcard(auth, cc) function this specific function can be found in the next section, fortercreditcard(auth, cc) forterorder ds file var forterlogger = require("int forter/cartridge/scripts/lib/forter/forterlogger ds"); / forterorder class is the dto object for request to include this script use var forterorder = require("int forter/cartridge/scripts/lib/forter/dto/forterorder ds"); / function forterorder(args) { var log = new forterlogger("forterorder ds"), order = args order, site = dw\ system site getcurrent(), paymentinstruments = order getpaymentinstruments(), a = null, authresponse = null, shipment = null; for each (var paymentinstrument in paymentinstruments) { if (paymentinstrument paymentmethod == 'credit card') { a = paymentinstrument; authresponse = args creditcardauthresponse; } else if (paymentinstrument paymentmethod == 'paypal' || paymentinstrument paymentmethod == 'bml') { a = paymentinstrument; authresponse = {}; // not necessary as this info can be retreived from a instrument } else if (paymentinstrument paymentmethod == 'gift certificate') { // optional if this is an accepted a method in your store a = paymentinstrument; authresponse = args gcredemptiontransaction; } } // general parameters this orderid = order originalorderno; // required this ordertype = "web"; // required this timesenttoforter = (new date()) gettime(); // required this checkouttime = new number((order creationdate gettime()/1000) tofixed()); // required //must be seconds, not milliseconds this connectioninformation = new forterconnectioninformation(order); // required // calculate totals this totalamount = {}; // required this totalamount amountlocalcurrency = order totalgrossprice value tofixed(2); this totalamount currency = order totalgrossprice currencycode; this additionalidentifiers = { 'merchant' { 'merchantname' site id, 'merchantdomain' site id + ' com' } } //calculate totals for discounts var ordertotal = order getmerchandizetotalprice(), adjordertotal = order getadjustedmerchandizetotalprice(), shiptotal = order getshippingtotalprice(), adjshiptotal = order getadjustedshippingtotalprice(), total = ordertotal add(shiptotal), adjtotal = adjordertotal add(adjshiptotal); // discounts var discountprice = 0; var couponname = ''; var coupontype = ''; if (!order getcouponlineitems() isempty()) { var coupons = order getcouponlineitems(); var couponnames = \[]; for each (let coup in coupons) { couponnames push(coup getcouponcode()); if (!coup getpriceadjustments() isempty()) { var coupadjustments = coup getpriceadjustments(); for each (let coupadj in coupadjustments) { discountprice += coupadj pricevalue; } } } couponname = couponnames join(","); discountprice = discountprice ( 1); if (discountprice > 0) { this totaldiscount = {}; // optional this totaldiscount couponcodeused = couponname substring(0,20); // required this totaldiscount coupondiscountamount = {}; // required this totaldiscount coupondiscountamount amountlocalcurrency = discountprice tofixed(2); this totaldiscount coupondiscountamount currency = order currencycode; this totaldiscount discounttype = "coupon"; // required } } // customer's details this accountowner = new fortercustomer(order); // cart items (regular product) this cartitems = \[]; // required for each (var pli in order productlineitems) { this cartitems push(new fortercartitem(pli)); } // cart items (gift certificate) for each (var gcli in order giftcertificatelineitems) { this cartitems push(new fortercartitem(gcli)); } // payments this a = \[]; // required this a push(new forterpayment(order, authresponse, a , log)); // delivery and recipient (shipping information) if (order shipments length > 0) { shipment = order shipments\[0]; this primarydeliverydetails = {}; this primarydeliverydetails deliveryprice = {}; this primarydeliverydetails deliverymethod = (!empty(shipment getshippingmethod()) && !empty(shipment getshippingmethod() getdisplayname())) ? shipment getshippingmethod() getdisplayname() ""; //"by air"; var deliverytype = "physical"; //default value if (order productlineitems size() > 0 && order giftcertificatelineitems size() == 0) { deliverytype = "physical"; //if real products only } else if (order productlineitems size() == 0 && order giftcertificatelineitems size() > 0) { deliverytype = "digital"; //if gift certificates only this primarydeliverydetails deliverymethod = "email"; } else if (order productlineitems size() > 0 && order giftcertificatelineitems size() > 0) { deliverytype = "hybrid"; //if gift certificates and real products } this primarydeliverydetails deliverytype = deliverytype; this primarydeliverydetails deliveryprice amountlocalcurrency = shipment adjustedshippingtotalprice value tofixed(2); this primarydeliverydetails deliveryprice currency = shipment adjustedshippingtotalprice currencycode tostring(); if (shipment productlineitems size() > 0) { this primaryrecipient = {}; // optional this primaryrecipient personaldetails = {}; this primaryrecipient personaldetails firstname = shipment shippingaddress firstname; //from the shipping address this primaryrecipient personaldetails lastname = shipment shippingaddress lastname; //from the shipping address this primaryrecipient address = {}; this primaryrecipient address address1 = shipment shippingaddress address1; this primaryrecipient address address2 = !empty(shipment shippingaddress address2) ? shipment shippingaddress address2 ""; this primaryrecipient address zip = shipment shippingaddress postalcode; this primaryrecipient address city = shipment shippingaddress city; this primaryrecipient address region = shipment shippingaddress statecode; this primaryrecipient address country = shipment shippingaddress countrycode value touppercase(); this primaryrecipient phone = \[]; if (!empty(shipment shippingaddress phone)){ this primaryrecipient phone push(new forterphone(shipment shippingaddress phone)); } } else if (shipment giftcertificatelineitems size() > 0) { this primaryrecipient = {}; // optional this primaryrecipient personaldetails = {}; this primaryrecipient personaldetails fullname = shipment giftcertificatelineitems\[0] recipientname; //from the gift form this primaryrecipient personaldetails email = shipment giftcertificatelineitems\[0] recipientemail; //from the gift form } if (shipment gift == true) { this primaryrecipient comments = {}; this primaryrecipient comments messagetobeneficiary = shipment giftmessage ? shipment giftmessage ""; } } function forterconnectioninformation(order) { this customerip = request httpremoteaddress; // required this useragent = request httpuseragent; for (var i = 0; i < request httpcookies cookiecount; i++) { if (request httpcookies\[i] name == "fortertoken") { this fortertokencookie = request httpcookies\[i] value; // required } } this useragent = request httpuseragent; // required this fortertokencookie = ""; // conditional for (var i = 0; i < request httpcookies cookiecount; i++) { if (request httpcookies\[i] name == "fortertoken") { this fortertokencookie = request httpcookies\[i] value; } } } } function fortercustomer(order) { if (order customer profile != null) { this firstname = order customer profile firstname; this lastname = order customer profile lastname; this email = order customer profile email; this accountid = order customer id; this created = new number((order customer profile getcreationdate() gettime()/1000) tofixed()); var query = "customerno = {0} and paymentstatus = {1}"; var allorders seekableiterator = dw\ order ordermgr searchorders(query, "creationdate desc", order customer profile customerno, 2); this pastorderscount = new number(allorders count); } else { this firstname = order billingaddress firstname; this lastname = order billingaddress lastname; this email = order customeremail; } } function forterpayment(order, authresponse, a , log) { var billingaddress = order billingaddress; this billingdetails = {}; this billingdetails personaldetails = {}; this billingdetails personaldetails firstname = billingaddress firstname; this billingdetails personaldetails lastname = billingaddress lastname; this billingdetails personaldetails email = order customeremail; this billingdetails address = {}; this billingdetails address address1 = billingaddress address1; this billingdetails address address2 = !empty(billingaddress address2) ? billingaddress address2 ""; this billingdetails address zip = billingaddress postalcode; this billingdetails address city = billingaddress city; this billingdetails address region = billingaddress statecode; this billingdetails address country = billingaddress countrycode value touppercase(); if (!empty(billingaddress phone)) { this billingdetails phone = \[]; this billingdetails phone push(new forterphone(billingaddress phone)); } this amount = {}; this amount amountlocalcurrency = order totalgrossprice value tofixed(2); this amount currency = order totalgrossprice currencycode; if (!empty(a )) { if (a paymentmethod == 'paypal' || a paymentmethod == 'bml') { this paypal = new forterpaypal(order, a ); } else if (a paymentmethod == 'credit card') { this creditcard = new fortercreditcard(authresponse, a ); } else if (a paymentmethod == 'gift certificate') { //optional if this is an accepted a method this giftcard = new fortergiftcard(authresponse, a ); } } else { log error('no a method information for order ' + order originalorderno); } } function forterphone(phone) { this phone = phone; // had to add this due to api errors without it this updatetimes = { creationtime (new date()) gettime() 	}; } function forterpaypal(order, a ) { //var transactiondetails = {}; this payerid = a custom paypalpayerid this payeremail = order customeremail; this payerstatus = ""; this payeraddressstatus = ""; //"confirmed" this paymentmethod = order custom paypalpaymentmethod; this paymentstatus = a custom paypalpaymentstatus touppercase(); //"pending" this payeraccountcountry = "us"; this protectioneligibility = a custom paypalprotectioneligibility touppercase(); //"eligible"; this correlationid = a custom paypalcorrelationid correlationid; //"fdshkj2390sdf"; this paymentid = ""; this authorizationid = a custom paypalauthid; //"2wc75407lv7300439"; this fullpaypalresponsepayload = {}; //this fullpaypalresponsepayload = transactiondetails; } function fortercreditcard(auth, cc) { var processorresponsecode = "", processorresponsetext = ""; // format the expiration month from 1 to 01, etc if (cc creditcardexpirationmonth tostring() length == 1) { var creditcardexpmonth = "0" + cc creditcardexpirationmonth tostring(); } else { var creditcardexpmonth = cc creditcardexpirationmonth tostring(); } this nameoncard = cc creditcardholder; this cardbrand = cc creditcardtype; if (cc creditcardnumber substring(0,6) indexof(' ') > 1) { this bin = session forms billing paymentmethods creditcard number value tostring() substring(0,6); } else { this bin = cc creditcardnumber tostring() substring(0,6); } this lastfourdigits = cc creditcardnumberlastdigits; this expirationmonth = creditcardexpmonth; this expirationyear = cc creditcardexpirationyear tostring(); this verificationresults = {}; this verificationresults avsfullresult = auth avs tostring(); this verificationresults cvvresult = auth cvd code tostring(); this verificationresults authorizationcode = auth authorization num tostring(); this paymentgatewaydata = {}; this paymentgatewaydata gatewayname = "firstdata credit"; // adjust based on your processor this paymentgatewaydata gatewaytransactionid = auth retrieval ref no tostring(); 	processorresponsecode = auth exact resp code tostring(); 	processorresponsetext = auth exact message tostring(); this verificationresults processorresponsecode = processorresponsecode; this verificationresults processorresponsetext = processorresponsetext; } function fortergiftcard(auth, gc) { // optional object for gift card purchases 	this merchantpaymentid = gc paymenttransaction transactionid; 	this value = {}; 	this value currency = auth currency code; 	this value amountusd = auth amount tostring(); 	this value amountlocalcurrency = auth amount tostring(); } function fortercartitem(item) { this basicitemdata = {}; if (item instanceof dw\ order productlineitem) { this basicitemdata productid = item productid; //optional this basicitemdata name = item productname; //required this basicitemdata quantity = item quantityvalue; //required var product = item getproduct(); if (product getcategories() isempty() && !empty(product getvariationmodel())) { product = product getvariationmodel() getmaster(); } this basicitemdata category = empty(product) ? "" product getcategories()\[0] getdisplayname(); this basicitemdata type = "tangible"; // add if type is available change according to the actual item type //if any adjustements this basicitemdata price = {}; this basicitemdata price amountlocalcurrency = item adjustedprice value tofixed(2); this basicitemdata price currency = item adjustedprice currencycode tostring(); } if (item instanceof dw\ order giftcertificatelineitem) { this basicitemdata name = item lineitemtext; //required this basicitemdata quantity = 1; //required (set 1 by default for a gift cert?) this basicitemdata type = "non tangible"; // add if type is available change according to the actual item type this deliverydetails = {}; this deliverydetails deliverytype = "digital"; this deliverydetails deliverymethod = "email"; this basicitemdata price = {}; this basicitemdata price amountlocalcurrency = item price value tofixed(2); this basicitemdata price currency = item price currencycode tostring(); this beneficiaries = \[]; this beneficiaries push(new forterbeneficiarydetailsfromgiftcard(item)); } } function forterbeneficiarydetailsfromgiftcard(item) { this personaldetails = {}; this comments = {}; this personaldetails fullname = item recipientname; this personaldetails email = item recipientemail; this comments messagetobeneficiary = item message ? item message ""; } module exports = forterorder; 2\ fortercreditcard(auth, cc) the code featured below features the mapped cybersource authorization response and pci compliant a details (bin, last 4, expiration) for a credit card transaction in the salesforce commercecloud cartridge please note the verificationresults object which should contain the authorization result from your cybersource cartridge non credit card transactions such as paypal or gift cards should be mapped to their associated functions in the code, forterpaypal (order, a ) , and fortergiftcard (auth, gc) fortercreditcard(auth, cc) function fortercreditcard(auth, cc) { var processorresponsecode = "", processorresponsetext = ""; // format the expiration month from 1 to 01, etc if (cc creditcardexpirationmonth tostring() length == 1) { var creditcardexpmonth = "0" + cc creditcardexpirationmonth tostring(); } else { var creditcardexpmonth = cc creditcardexpirationmonth tostring(); } this nameoncard = cc creditcardholder; this cardbrand = cc creditcardtype; if (cc creditcardnumber substring(0,6) indexof(' ') > 1) { this bin = session forms billing paymentmethods creditcard number value tostring() substring(0,6); } else { this bin = cc creditcardnumber tostring() substring(0,6); } this lastfourdigits = cc creditcardnumberlastdigits; this expirationmonth = creditcardexpmonth; this expirationyear = cc creditcardexpirationyear tostring(); this verificationresults = {}; this verificationresults avsfullresult = auth avs tostring(); this verificationresults cvvresult = auth cvd code tostring(); this verificationresults authorizationcode = auth authorization num tostring(); this paymentgatewaydata = {}; this paymentgatewaydata gatewayname = "cybersource"; // adjust based on your processor this paymentgatewaydata gatewaytransactionid = auth retrieval ref no tostring(); 	processorresponsecode = auth exact resp code tostring(); 	processorresponsetext = auth exact message tostring(); this verificationresults processorresponsecode = processorresponsecode; this verificationresults processorresponsetext = processorresponsetext; } 3\ forter giftcard (auth, gc) the below fortergiftcard (auth, gc) function outlines the details that should be passed to forter, in order to receive decisions for customers using a gift card as a payment method during checkout note that this can be disregarded if your store does not currently accept gift cards as a payment method fortergiftcard (auth, gc) function fortergiftcard(auth, gc) { // optional object for gift card purchases 	this merchantpaymentid = gc paymenttransaction transactionid; 	this value = {}; 	this value currency = auth currency code; 	this value amountusd = auth amount tostring(); 	this value amountlocalcurrency = auth amount tostring(); }