Hi All, We all know about authorize.net. Its the most common payment gateway used to make payment transaction from Website. We can also use it with Salesforce to setup payment gateway where our users can make a purchase and do payments as well.
Its very easy to connect Salesforce with Authorize.net. In this post we will use Sandbox account to make transaction but you can follow same steps for live transactions.
- First Sign up for a Sandbox account here.
Now you will get Login Id and Transaction Key. Store them in some place so that we can use them later.
- Now there are various method from which we can make a payment, Authorize a payment, Charge a card later and void the transaction. In case of Production URl we need to change the base URl.
- Also make sure you add the base URL in remote site setting.
[code]
public with sharing class AuthorizePayment {
private string APIkey = ‘********’;
private string transKey = ‘************’;
private string Amt = ‘5’;
private string cardNum = ‘5424000000000015’;
private string expdate = ‘1220’;
private string cvvCode = ‘999’;
//Method to make payment and charge the card
public void makePayment(){
//Getting access token from Paypal
HttpRequest req1 = new HttpRequest();
req1.setMethod(‘POST’);
req1.setEndpoint(‘https://apitest.authorize.net/xml/v1/request.api’);
req1.setHeader(‘content-type’, ‘application/json’);
/*
Production URL: https://api.authorize.net/xml/v1/request.api
XSD URL: https://api.authorize.net/xml/v1/schema/AnetApiSchema.xsd
*/
string messageBody1 ='{"createTransactionRequest":{"merchantAuthentication":{"name":"’+APIkey+’","transactionKey":"’+transKey+’"},’
+’"refId":"123456","transactionRequest":{"transactionType":"authCaptureTransaction","amount":"’+Amt+’","payment":{"creditCard":’
+'{"cardNumber":"’+cardNum+’","expirationDate":"’+expdate+’","cardCode":"’+cvvCode+’"}},"lineItems":{"lineItem":{"itemId":"1","name":"vase",’
+’"description":"Cannes logo","quantity":"18","unitPrice":"45.00"}},"tax":{"amount":"4.26","name":"level2 tax name","description":"level2 tax"},’
+’"duty":{"amount":"8.55","name":"duty name","description":"duty description"},"shipping":{"amount":"4.26","name":"level2 tax name",’+
‘"description":"level2 tax"},"poNumber":"456654","customer":{"id":"99999456654"},"billTo":{"firstName":"Ellen","lastName":"Johnson",’+
‘"company":"Souveniropolis","address":"14 Main Street","city":"Pecan Springs","state":"TX","zip":"44628","country":"USA"},"shipTo":’+
‘{"firstName":"China","lastName":"Bayles","company":"Thyme for Tea","address":"12 Main Street","city":"Pecan Springs","state":"TX","zip":"44628",’+
‘"country":"USA"},"customerIP":"192.168.1.1","transactionSettings":{"setting":{"settingName":"testRequest","settingValue":"false"}},’+
‘"userFields":{"userField":[{"name":"MerchantDefinedFieldName1","value":"MerchantDefinedFieldValue1"},{"name":"favorite_color","value":"blue"}]}}}}’;
req1.setHeader(‘Content-length’, String.valueOf(messageBody1.length()));
req1.setBody(messageBody1);
req1.setTimeout(60*1000);
Http h1 = new Http();
String resp1;
HttpResponse res1 = h1.send(req1);
resp1 = res1.getBody();
Map<String,object> responseMap =(Map<String,object>)JSON.deserializeUntyped(res1.getBody().substring(1,res1.getBody().length())) ;
for(string keyStr : responseMap.keyset())
system.debug(keyStr+’———–Response——————>>>>’+responseMap.get(keyStr));
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Confirm, resp1));
}
//Method to authorize the card to make the payment later
public void authorizecard(){
HttpRequest req1 = new HttpRequest();
req1.setMethod(‘POST’);
req1.setEndpoint(‘https://apitest.authorize.net/xml/v1/request.api’);
req1.setHeader(‘content-type’, ‘application/json’);
/*
Production URL: https://api.authorize.net/xml/v1/request.api
XSD URL: https://api.authorize.net/xml/v1/schema/AnetApiSchema.xsd
*/
string messageBody1 ='{ "createTransactionRequest": { "merchantAuthentication": { "name":"’+APIkey+’","transactionKey":"’+transKey+’" }, ‘+
‘"refId": "123456", "transactionRequest": { "transactionType": "authOnlyTransaction", "amount": "’+Amt+’", "payment": { "creditCard": ‘+
‘{ "cardNumber":"’+cardNum+’","expirationDate":"’+expdate+’","cardCode":"’+cvvCode+’" } }, "lineItems": { "lineItem": { "itemId": "1", ‘+
‘"name": "vase", "description": "Cannes logo", "quantity": "18", "unitPrice": "45.00" } }, "tax": { "amount": "4.26", "name": "level2 tax name",’+
‘"description": "level2 tax" }, "duty": { "amount": "8.55", "name": "duty name", "description": "duty description" }, "shipping": ‘+
‘{ "amount": "4.26", "name": "level2 tax name", "description": "level2 tax" }, "poNumber": "456654", "customer": { "id": "99999456654" },’+
‘ "billTo": { "firstName": "Ellen", "lastName": "Johnson", "company": "Souveniropolis", "address": "14 Main Street", "city": "Pecan Springs",’+
‘ "state": "TX", "zip": "44628", "country": "USA" }, "shipTo": { "firstName": "China", "lastName": "Bayles", "company": "Thyme for Tea", ‘+
‘"address": "12 Main Street", "city": "Pecan Springs", "state": "TX", "zip": "44628", "country": "USA" }, "customerIP": "192.168.1.1", ‘+
‘"transactionSettings": { "setting": { "settingName": "testRequest", "settingValue": "false" } }, "userFields": { "userField": [ ‘+
‘{ "name": "MerchantDefinedFieldName1", "value": "MerchantDefinedFieldValue1" }, { "name": "favorite_color", "value": "blue" } ] } } } }’;
req1.setHeader(‘Content-length’, String.valueOf(messageBody1.length()));
req1.setBody(messageBody1);
req1.setTimeout(60*1000);
Http h1 = new Http();
String resp1;
HttpResponse res1 = h1.send(req1);
resp1 = res1.getBody();
Map<String,object> responseMap =(Map<String,object>)JSON.deserializeUntyped(res1.getBody().substring(1,res1.getBody().length())) ;
for(string keyStr : responseMap.keyset())
system.debug(keyStr+’———–Response——————>>>>’+responseMap.get(keyStr));
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Confirm, resp1));
}
//Method for refund the settled payment
public void refundPay(){
HttpRequest req1 = new HttpRequest();
req1.setMethod(‘POST’);
req1.setEndpoint(‘https://apitest.authorize.net/xml/v1/request.api’);
req1.setHeader(‘content-type’, ‘application/json’);
/*
Production URL: https://api.authorize.net/xml/v1/request.api
XSD URL: https://api.authorize.net/xml/v1/schema/AnetApiSchema.xsd
*/
string messageBody1 ='{ "createTransactionRequest": { "merchantAuthentication": { "name":"’+APIkey+’","transactionKey":"’+transKey+’" },’+
‘ "refId": "123456", "transactionRequest": { "transactionType": "refundTransaction", "amount": "5.00", "payment": { "creditCard": ‘+
‘{ "cardNumber":"’+cardNum+’","expirationDate":"’+expdate+’" } }, "refTransId": "60024747323" } } }’;
req1.setHeader(‘Content-length’, String.valueOf(messageBody1.length()));
req1.setBody(messageBody1);
req1.setTimeout(60*1000);
Http h1 = new Http();
String resp1;
HttpResponse res1 = h1.send(req1);
resp1 = res1.getBody();
Map<String,object> responseMap =(Map<String,object>)JSON.deserializeUntyped(res1.getBody().substring(1,res1.getBody().length())) ;
for(string keyStr : responseMap.keyset())
system.debug(keyStr+’———–Response——————>>>>’+responseMap.get(keyStr));
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Confirm, resp1));
}
//method for cancel the unseattled payment
public void voidPay(){
HttpRequest req1 = new HttpRequest();
req1.setMethod(‘POST’);
req1.setEndpoint(‘https://apitest.authorize.net/xml/v1/request.api’);
req1.setHeader(‘content-type’, ‘application/json’);
/*
Production URL: https://api.authorize.net/xml/v1/request.api
XSD URL: https://api.authorize.net/xml/v1/schema/AnetApiSchema.xsd
*/
string messageBody1 ='{ "createTransactionRequest": { "merchantAuthentication": { "name":"’+APIkey+’","transactionKey":"’+transKey+’" },’+
‘"refId": "123456", "transactionRequest": { "transactionType": "voidTransaction", "refTransId": "60024747323" } } }’;
req1.setHeader(‘Content-length’, String.valueOf(messageBody1.length()));
req1.setBody(messageBody1);
req1.setTimeout(60*1000);
Http h1 = new Http();
String resp1;
HttpResponse res1 = h1.send(req1);
resp1 = res1.getBody();Map<String,object> responseMap =(Map<String,object>)JSON.deserializeUntyped(res1.getBody().substring(1,res1.getBody().length())) ;
for(string keyStr : responseMap.keyset())
system.debug(keyStr+’———–Response——————>>>>’+responseMap.get(keyStr));
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Confirm, resp1));
}
}
[/code]
- From the transaction response you will get the transId which you can n cancellation and other processes. You can read more about them in detail from here.
So now you can setup a payment gateway in your force.com site or for internal user. If you like this post or want to add something let me know in comments section.
Happy Programming 🙂