SFCC Cartridge
...
Mapping Samples
Braintree
2 min
1\ full forterorder ds file using braintree gateway below is the full forterorder ds file including the mapped authorization response for a merchant using the braintree as the primary 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(), payment = null, authresponse = null, shipment = null; for each (var paymentinstrument in paymentinstruments) { if (paymentinstrument paymentmethod == 'credit card') { //todo adjust to the existing payment instruments payment = paymentinstrument; authresponse = { token paymentinstrument custom braintreepaymentmethodtoken, transactionid paymentinstrument getpaymenttransaction() gettransactionid(), payloaddata json parse(paymentinstrument custom braintreeresponsepayloaddata) }; } else if (paymentinstrument paymentmethod == 'paypal' || paymentinstrument paymentmethod == 'bml') { //todo adjust to the existing payment instruments payment = paymentinstrument; var paymenttransaction = paymentinstrument getpaymenttransaction(); authresponse = { transactionid paymenttransaction gettransactionid(), payerid paymentinstrument custom paypalpayerid, paypalpaymentstatus paymentinstrument custom paypalpaymentstatus, paypalemail paymentinstrument custom paypalemail, paypalcorrelationid paymentinstrument custom paypalcorrelationid } } } // 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 amountusd = order totalgrossprice value tofixed(2); //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 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 payment = \[]; // required this payment push(new forterpayment(order, authresponse, payment, 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; 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 displayvalue 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, payment, log) { var billingaddress = order billingaddress; this billingdetails = {}; this billingdetails personaldetails = {}; this billingdetails personaldetails firstname = billingaddress firstname; this billingdetails personaldetails lastname = billingaddress lastname; 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 displayvalue touppercase(); if (!empty(billingaddress phone)) { this billingdetails phone = \[]; this billingdetails phone push(new forterphone(billingaddress phone)); } this amount = {}; this amount amountusd = order totalgrossprice value tofixed(2); if (!empty(payment)) { if (payment paymentmethod == 'paypal' || payment paymentmethod == 'bml') { this paypal = new forterpaypal(authresponse, billingaddress countrycode value touppercase()); } else if (payment paymentmethod == 'credit card') { this creditcard = new fortercreditcard(authresponse, payment); } } else { log error('no payment method information for order ' + order originalorderno); } } function forterphone(phone) { this phone = phone; this updatetimes = { "creationtime" (new date()) gettime(), "removaltime" (new date()) gettime() }; } function forterpaypal(authresponse, countrycode) { this payerid = authresponse payerid; //"fd000a45p"; this payeremail = authresponse paypalemail; this payerstatus = "verified"; //"verified"; this payeraddressstatus = "confirmed";//"confirmed"; this paymentmethod = "paypal"; this paymentstatus = authresponse paypalpaymentstatus touppercase(); //"pending" this payeraccountcountry = countrycode; //"us"; this protectioneligibility = "eligible"; //"eligible"; this correlationid = authresponse paypalcorrelationid; //"fdshkj2390sdf"; this paymentid = ""; this authorizationid = authresponse transactionid; //"2wc75407lv7300439"; this fullpaypalresponsepayload = authresponse; this paymentgatewaydata = {}; this paymentgatewaydata gatewayname = "paypal"; //todo must be adjusted according to the payment gateway used this paymentgatewaydata gatewaytransactionid = authresponse transactionid; } function fortercreditcard(auth, cc) { // 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; this bin = auth payloaddata bin; this lastfourdigits = cc creditcardnumberlastdigits; this expirationmonth = creditcardexpmonth; this expirationyear = cc creditcardexpirationyear tostring(); this verificationresults = {}; this verificationresults avsstreetresult = auth payloaddata avsstreetresult; this verificationresults avszipresult = auth payloaddata avszipresult; this verificationresults avsfullresult = auth payloaddata avsfullresult; this verificationresults cvvresult = auth payloaddata cvvresult; this verificationresults authorizationcode = auth token; this paymentgatewaydata = {}; this paymentgatewaydata gatewayname = "braintree"; this paymentgatewaydata gatewaytransactionid = auth transactionid; this verificationresults processorresponsecode = ""; this verificationresults processorresponsetext = ""; } 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 amountusd = item adjustedprice value tofixed(2); } 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 amountusd = item price value tofixed(2); 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; 3\ fortercreditcard(auth, cc) function the fortercreditcard function includes the recommended mapping from the braintree authorization response payload received from the braintree cartridge note in this example, that the path includes the auth payloaddata objects braintree fortercreditcard (auth, cc) function fortercreditcard(auth, cc) { // 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; this bin = auth payloaddata bin; this lastfourdigits = cc creditcardnumberlastdigits; this expirationmonth = creditcardexpmonth; this expirationyear = cc creditcardexpirationyear tostring(); this verificationresults = {}; this verificationresults avsstreetresult = auth payloaddata avsstreetresult; this verificationresults avszipresult = auth payloaddata avszipresult; this verificationresults avsfullresult = auth payloaddata avsfullresult; this verificationresults cvvresult = auth payloaddata cvvresult; this verificationresults authorizationcode = auth token; this paymentgatewaydata = {}; this paymentgatewaydata gatewayname = "braintree"; this paymentgatewaydata gatewaytransactionid = auth transactionid; this verificationresults processorresponsecode = ""; this verificationresults processorresponsetext = ""; }