How to use 3rd party FedEx account shipping label via FedEx Rest API Oauth 2.0

By | May 28, 2024
Spread the love

Using a third-party FedEx account for generating shipping labels via the FedEx REST API with OAuth 2.0 involves several steps. This process includes obtaining OAuth credentials, authenticating, and making API requests with third-party billing information. Here’s a step-by-step guide on how to achieve this:

Step 1: Obtain FedEx REST API and OAuth 2.0 Credentials

  1. Register for a FedEx Account: If you don’t have one, register on the FedEx website.
  2. API Access: Obtain the client ID and client secret for OAuth 2.0 from the FedEx Developer Portal.

Step 2: Get OAuth 2.0 Token

You need to authenticate and get an OAuth 2.0 token before making API requests.

Example Code to Get OAuth 2.0 Token

use GuzzleHttp\Client;

function getFedexOauthToken($clientId, $clientSecret) {
    $client = new Client();
    $response = $client->post('https://apis-sandbox.fedex.com/oauth/token', [
        'headers' => [
            'Content-Type' => 'application/x-www-form-urlencoded',
        ],
        'auth' => [$clientId, $clientSecret],
        'form_params' => [
            'grant_type' => 'client_credentials'
        ],
    ]);

    $data = json_decode($response->getBody(), true);
    return $data['access_token'];
}

// Usage
$clientId = 'your_fedex_client_id';
$clientSecret = 'your_fedex_client_secret';
$accessToken = getFedexOauthToken($clientId, $clientSecret);

Step 3: Create Shipment Request with Third-Party Billing

Once you have the access token, you can make the shipment request.

Example JSON Payload for Shipment Request

Here’s an example JSON payload for a shipment request using third-party billing:

{
  "requestedShipment": {
    "shipper": {
      "address": {
        "streetLines": ["Address Line"],
        "city": "City",
        "stateOrProvinceCode": "State",
        "postalCode": "Postal Code",
        "countryCode": "Country Code"
      },
      "contact": {
        "personName": "Shipper Name",
        "phoneNumber": "1234567890"
      }
    },
    "recipient": {
      "address": {
        "streetLines": ["Address Line"],
        "city": "City",
        "stateOrProvinceCode": "State",
        "postalCode": "Postal Code",
        "countryCode": "Country Code"
      },
      "contact": {
        "personName": "Recipient Name",
        "phoneNumber": "0987654321"
      }
    },
    "shippingChargesPayment": {
      "paymentType": "THIRD_PARTY",
      "payor": {
        "responsibleParty": {
          "accountNumber": "Third-Party FedEx Account Number",
          "contact": null,
          "address": {
            "countryCode": "Third-Party Country Code",
            "postalCode": "Third-Party Postal Code"
          }
        }
      }
    },
    "labelSpecification": {
      "imageType": "PDF",
      "labelStockType": "PAPER_4X6"
    },
    "serviceType": "FEDEX_GROUND",
    "packagingType": "YOUR_PACKAGING",
    "packageCount": 1,
    "requestedPackageLineItems": [
      {
        "weight": {
          "units": "LB",
          "value": 5
        },
        "dimensions": {
          "length": 10,
          "width": 5,
          "height": 5,
          "units": "IN"
        }
      }
    ]
  }
}

Step 4: Send the API Request

Using Guzzle in PHP, you can send this request with the OAuth token.

Example Code to Create Shipment

function createFedexShipment($accessToken, $shipmentData) {
    $client = new Client();
    $response = $client->post('https://apis-sandbox.fedex.com/ship/v1/shipments', [
        'headers' => [
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ' . $accessToken,
        ],
        'body' => json_encode($shipmentData)
    ]);

    return json_decode($response->getBody(), true);
}

// Usage
$shipmentData = [
    "requestedShipment" => [
        "shipper" => [
            "address" => [
                "streetLines" => ["Address Line"],
                "city" => "City",
                "stateOrProvinceCode" => "State",
                "postalCode" => "Postal Code",
                "countryCode" => "Country Code"
            ],
            "contact" => [
                "personName" => "Shipper Name",
                "phoneNumber" => "1234567890"
            ]
        ],
        "recipient" => [
            "address" => [
                "streetLines" => ["Address Line"],
                "city" => "City",
                "stateOrProvinceCode" => "State",
                "postalCode" => "Postal Code",
                "countryCode" => "Country Code"
            ],
            "contact" => [
                "personName" => "Recipient Name",
                "phoneNumber" => "0987654321"
            ]
        ],
        "shippingChargesPayment" => [
            "paymentType" => "THIRD_PARTY",
            "payor" => [
                "responsibleParty" => [
                    "accountNumber" => "Third-Party FedEx Account Number",
                    "contact" => null,
                    "address" => [
                        "countryCode" => "Third-Party Country Code",
                        "postalCode" => "Third-Party Postal Code"
                    ]
                ]
            ]
        ],
        "labelSpecification" => [
            "imageType" => "PDF",
            "labelStockType" => "PAPER_4X6"
        ],
        "serviceType" => "FEDEX_GROUND",
        "packagingType" => "YOUR_PACKAGING",
        "packageCount" => 1,
        "requestedPackageLineItems" => [
            [
                "weight" => [
                    "units" => "LB",
                    "value" => 5
                ],
                "dimensions" => [
                    "length" => 10,
                    "width" => 5,
                    "height" => 5,
                    "units" => "IN"
                ]
            ]
        ]
    ]
];

$response = createFedexShipment($accessToken, $shipmentData);
print_r($response);

Step 5: Integrate with Magento 2

  1. Create a Custom Module: Develop a Magento 2 module to manage FedEx API calls.
  2. Intercept Order Shipment:
    • Use an observer or plugin to trigger shipment creation.
    • Call the createFedexShipment method from your custom module to generate the shipping label.
    • Store the shipping label information in the Magento order shipment record and update the order status.

Step 6: Testing and Verification

  1. Test Orders: Place test orders to ensure shipping labels are generated correctly and billed to the third-party FedEx account.
  2. Verify Billing: Confirm that charges are applied to the third-party FedEx account.

By following these steps and using the example code, you can configure your Magento 2 store to generate shipping labels via the FedEx REST API with OAuth 2.0 authentication and third-party billing. This setup ensures secure and accurate billing to the third-party account while integrating seamlessly with your Magento 2 store.

Buy plugin from marketplace

https://commercemarketplace.adobe.com/ecomplugins-customaccountlabel.html