Breaking

Wednesday, August 29, 2018

Consume FedEx API service in Dynamics Ax 2012

Overview:

In this section we will consume FedEx API in Dynamics Ax. Here I have download API from FedEx website. For FedEx API you need to check for which purpose you need to call FedEx API, like what you want to call from FedEx API Shipment validation, Package Rates request, address validation etc…

Here I have download FedEx API for Package rates request, so I will create a package and process rates request for getting the rates of the items which I have used in the particular package.

First you need to register in FedEx website to get all the required information for web authentication details. Go to the FedEx website and login with your credentials. After your login click on the FedEx Web Services Tab for the Documents and Download. You can get here source code, documentation, certifications, production keys and other information you will need to integrate FedEx into a website or application you’re developing. Click on the 1) Documentation and Downloads under the FedEx Web Services.

In the Documentation and Download you can download WSDL file and source code file with PDF file under the Standard service and Advance service Tab. for every request you can download WSDL or XSD file with documentation. After download you will get Service Zip file one has WSDL file and another has source code.

1.       RateService_v22_cs.zip
2.       RateService.zip

Now open RateService_v22_cs.zip file and check for the service available in the Rates service, there are Freight, Rate and SmartPost service available for the FedEx Rates API. You can call one of this to get Rates from the FedEx API.

Download RateService_v22.wsdl

Now create Class Library project in visual studio to add WSDL web reference in you project to consume FedEx API service.

To add a Web reference to a project

·      In Solution Explorer, right-click the name of the project that you want to add the service to, and then click Add Service Reference.
·        The Add Service Reference dialog box appears.
·         In the Add Service Reference dialog box, click the advanced button.
·        The Service Reference Settings dialog box appears.
·        In the Service Reference Settings dialog box, click Add Web Reference.
·        The Add Web Reference dialog box appears.
·       In the URL box, enter the URL of the Web service to use. If you do not know the URL, use the links in the browse pane to locate the Web service you want.
·         In the Web services found at this URL box, select the Web service to use.
·   Verify that your project can use the Web service, and that any external code provided is trustworthy.
·     In the Web reference name field, enter a name that you will use in your code to access the selected Web service programmatically.
·      Click Add Reference.

Now open the Rate Project from the Downloaded file in the visual studio to create dynamic parameter in the class. If you want to add dynamic parameter for Request and Response you have to create separate class for that and add the following parameter in that class. Before that download the below file for the Rate Request code.

Request class:
public class Request
{
 public string Key { get; set; }
 public string Password { get; set; }
 public string ServiceUrl { get; set; }
 public string AccountNumber { get; set; }
 public string MeterNumber { get; set; }
 public string CustomerTransactionId { get; set; }
 public string PackageCount { get; set; }
 public string FromName { get; set; }
 public string FromCompany { get; set; }
 public string FromPhone { get; set; }
 public string FromAddressLine { get; set; }
 public string FromCity { get; set; }
 public string FromState { get; set; }
 public string FromPostalCode { get; set; }
 public string FromCountryCode { get; set; }
 public string ToName { get; set; }
 public string ToCompany { get; set; }
 public string ToPhone { get; set; }
 public string ToAddressLine { get; set; }
 public string ToCity { get; set; }
 public string ToState { get; set; }
 public string ToPostalCode { get; set; }
 public string ToCountryCode { get; set; }
 public decimal PackageWeight { get; set; }
 public string PackageLength { get; set; }
 public string PackageWidth { get; set; }
 public string PackageHeight { get; set; }
}
Now add the following parameter in the response class to get the report from the FedEx API service.
public class Response
{
 public string Status { get; set; }
 public string TotalBillingWeight { get; set; }
 public string TotalBaseCharge { get; set; }
 public string TotalFreightDiscounts { get; set; }
 public string TotalSurcharges { get; set; }
 public string TotalNetCharge { get; set; }  
}
Now open Program.cs file from the Rate service project and check the value where we need to set. Here I have given code which is in the Program.cs file you need to set the request object in the new class or in the existing class. Here is the sequence to follow the step for getting the rates from the FedEx API.
1.       Create Rate Request
2.       Set Web Authentication Detail
3.       Set Client Detail
4.       Set Transaction Detail
5.       Set Version
6.       Set Shipment Details
7.       Set Origin
8.       Set Destination
9.       Set Package Line Items
10.   Set Insured Value
After all the declaration you will get response from the FedEx service and will return data related to the Package Rates. There are many Severity types in the FedEx API to get response from the FedEx API Service.
If its response is SUCCESS, NOTE, WARNING then you will get Package Rates from the FedEx API service. Like below FedEx API will response.
private static void ShowNotifications(RateReply reply)
{
 Console.WriteLine("Notifications");
 for (int i = 0; i < reply.Notifications.Length; i++)
 {
  Notification notification = reply.Notifications[i];
  Console.WriteLine("Notification no. {0}", i);
  Console.WriteLine(" Severity: {0}", notification.Severity);
  Console.WriteLine(" Code: {0}", notification.Code);
  Console.WriteLine(" Message: {0}", notification.Message);
  Console.WriteLine(" Source: {0}", notification.Source);
  Console.ReadLine();
 }
}
You will get all the Notifications, Severity, Code, Message and Source in the Response and also below Rates information.
private static void ShowShipmentRateDetails(RatedShipmentDetail shipmentDetail)
{
 if (shipmentDetail == null) return;
 if (shipmentDetail.ShipmentRateDetail == null) return;
 ShipmentRateDetail rateDetail = shipmentDetail.ShipmentRateDetail;
 Console.WriteLine("--- Shipment Rate Detail ---");
 //
 Console.WriteLine("RateType: {0} ", rateDetail.RateType);
 if (rateDetail.TotalBillingWeight != null) Console.WriteLine("Total Billing Weight: {0} {1}", rateDetail.TotalBillingWeight.Value, shipmentDetail.ShipmentRateDetail.TotalBillingWeight.Units);
 if (rateDetail.TotalBaseCharge != null) Console.WriteLine("Total Base Charge: {0} {1}", rateDetail.TotalBaseCharge.Amount, rateDetail.TotalBaseCharge.Currency);
 if (rateDetail.TotalFreightDiscounts != null) Console.WriteLine("Total Freight Discounts: {0} {1}", rateDetail.TotalFreightDiscounts.Amount, rateDetail.TotalFreightDiscounts.Currency);
 if (rateDetail.TotalSurcharges != null) Console.WriteLine("Total Surcharges: {0} {1}", rateDetail.TotalSurcharges.Amount, rateDetail.TotalSurcharges.Currency);
 if (rateDetail.Surcharges != null)
 {
  // Individual surcharge for each package
  foreach (Surcharge surcharge in rateDetail.Surcharges)
   Console.WriteLine(" {0} surcharge {1} {2}", surcharge.SurchargeType, surcharge.Amount.Amount, surcharge.Amount.Currency);
 }
 if (rateDetail.TotalNetCharge != null) Console.WriteLine("Total Net Charge: {0} {1}", rateDetail.TotalNetCharge.Amount, rateDetail.TotalNetCharge.Currency);
}
Below is the information which you will get after the Rates Request process. Here I have my custom value in the required file for process the Rate request to FedEx API service. But you can set your value it could be static or dynamic.

No comments:

Post a Comment

Thanks for your comment.