Wednesday, November 25, 2015

How to do Avalara tax API Integration With Salesforce?

Special thanks to Ashish Agarwal

Step 1: go to below link and fill the form to get your token and secret

               http://developer.avalara.com/api-get-started/

after submitting the form you will receive an email to your email address. Go there and click on the developer area link provided in email. In this email you will get your login ID and password also.

On developer page you will prompted to give your Id and password . Please fill provide them and login.

Now reset your new password and save.

Step 2: Now go to setting tab and click on Reset Licence Key and then click on generate button.

Step 3: Now check your email .you will find an email from avalara support .open it and get your account number and Licence Key.

Step 4: Setup Avatax Select the Country and set Jurisdictions see below image


Step 5: Now go to SETUP | ADMINISTER | SECURITY CONTROLS | REMOTE SITE SETTINGS and create a new Remote site.

Give remote site name avalara and Remote site url as “https://development.avalara.net “ .

Step 6 : Now create a apex class namely “avalaraController” and copy the code from below link and paste it .

Apex Class:
public class avalaraController
 {

//string variable to show api result
    public string result{get;set;}
//string variable to hold amount of line item
    public String amount { get; set; }
//string varriable to hold quantity of line item
    public String count { get; set; }
//string varriable to hold postal code of address
    public String PostalCode { get; set; }
//line 1 of address
    public String Line1 { get; set; }
//line 2 of address
    public String Line2 { get; set; }

//method to get tex value from avalara tex api    
public String RestRequest(String body,String endPoint,String method){
        
         Blob headerValue = Blob.valueOf("acount no"+':'+"Licence key");
         String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
                
         //accessing end point for avalara request type
        
         Http h=new Http();
         HttpRequest req = new HttpRequest();
         req.setMethod(method);
  
         req.setHeader('Authorization', authorizationHeader);
         req.setEndPoint(endPoint);
         req.setBody(body);
         HttpResponse res = h.send(req);
         result = res.getBody();
         return result;



}

//method to get Text
       public void getTex(){
//calling method to create json body for request body from user provided data
        
         String texString = createTexRequestBody();
         RestRequest(texString ,"https://development.avalara.net/1.0/tax/get" ,'POST');
               }
  //method to validate address             
      public void validateAddress() {
     
        endpoint = 'https://development.avalara.net/1.0/address/validate?Line1='+EncodingUtil.urlEncode(Line1+Line2,'UTF-8')+'&PostalCode='+EncodingUtil.urlEncode(PostalCode,'UTF-8');
        RestRequest('' ,endPoint ,'GET');
    }
 //method to create json string for rest request body        
      public String createTexRequestBody(){
      
      
       JSONGenerator gen= JSON.createGenerator(true);
        gen.writeStartObject();
        gen.writeStringField('DocDate', '2013-06-19');
        gen.writeStringField('CustomerCode', 'CUST1');
        gen.writeFieldName('Addresses');
        gen.writeStartArray();
        gen.writeStartObject();
        gen.writeStringField('AddressCode', '1');
        gen.writeStringField('Line1',Line1 );
        gen.writeStringField('Line2', Line2 );
        
        gen.writeStringField('PostalCode',PostalCode);
        gen.writeEndObject();
        gen.writeEndArray();
        gen.writeFieldName('Lines');
        gen.writeStartArray();
        gen.writeStartObject();
        gen.writeStringField('LineNo', '1');
        gen.writeStringField('DestinationCode', '1');
        gen.writeStringField('OriginCode', '1');
        gen.writeNumberField('Qty',Integer.valueOf(count));
        gen.writeNumberField('Amount',Integer.valueOf(amount));
        
        gen.writeEndObject();
        gen.writeEndArray();
        gen.writeEndObject();
        return gen.getAsString();
      
      
      }

}

Now in header section of the code replace account Number with your account no and licence key with your licence key provided in email and DocDate should be the date given in the AVATAX Starting date.

Step 7: Now create a visualforce page and give it name as “ avalara” and copy the code from following link and paste it in page
Visualforce Page:

    
        
 
                
                    HP Printer
                

                    Quatity
                
                
                    amount
                
                        
                            
                
                    Address Line1 
                       
                

                    Address  Line2  
                    
                

                    PostalCode 
                    
                
{!tax}
            
            
            
                
                    
Loading...
{!result}


Run your visualforce page and fill the form with quantity ,amount(price) and any right address with valid postal code(required) ( only america and canada address).

And click on get Tex.

And you will find the response.

Check the response string and you will find tax for amount you entered.


Output:

2 comments:

  1. Link does not work to create account for Avalara developer account( " http://developer.avalara.com/api-get-started/")

    ReplyDelete
  2. The article provided by you is very nice and it is very helpful to know the more information about salesforce API integration.keep update with your blogs.....
    salesforce integration

    ReplyDelete