Using a third-party UPS account for generating shipping labels via the UPS REST API with OAuth 2.0 involves several steps, including obtaining OAuth credentials, authenticating, and making API requests with third-party billing information. Here’s a detailed guide on how to achieve this:
Step 1: Obtain UPS REST API and OAuth 2.0 Credentials
- Register for a UPS Account: If you don’t have one, register on the UPS website.
- API Access: Obtain the client ID and client secret for OAuth 2.0 from the UPS 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 getUpsOauthToken($clientId, $clientSecret) {
$client = new Client();
$response = $client->post('https://wwwcie.ups.com/security/v1/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_ups_client_id';
$clientSecret = 'your_ups_client_secret';
$accessToken = getUpsOauthToken($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
{
"ShipmentRequest": {
"Shipment": {
"Description": "Description of goods",
"Shipper": {
"Name": "Shipper Name",
"AttentionName": "Attention Name",
"TaxIdentificationNumber": "123456789",
"Phone": {
"Number": "1234567890"
},
"ShipperNumber": "Your UPS Account Number",
"Address": {
"AddressLine": ["Address Line"],
"City": "City",
"StateProvinceCode": "State",
"PostalCode": "Postal Code",
"CountryCode": "Country Code"
}
},
"ShipTo": {
"Name": "Recipient Name",
"AttentionName": "Attention Name",
"Phone": {
"Number": "1234567890"
},
"Address": {
"AddressLine": ["Address Line"],
"City": "City",
"StateProvinceCode": "State",
"PostalCode": "Postal Code",
"CountryCode": "Country Code"
}
},
"ShipFrom": {
"Name": "Sender Name",
"AttentionName": "Attention Name",
"Phone": {
"Number": "1234567890"
},
"Address": {
"AddressLine": ["Address Line"],
"City": "City",
"StateProvinceCode": "State",
"PostalCode": "Postal Code",
"CountryCode": "Country Code"
}
},
"PaymentInformation": {
"ShipmentCharge": {
"Type": "01",
"BillThirdParty": {
"BillThirdPartyConsignee": {
"AccountNumber": "Third-Party UPS Account Number",
"Address": {
"PostalCode": "Third-Party Postal Code",
"CountryCode": "Third-Party Country Code"
}
}
}
}
},
"Service": {
"Code": "03",
"Description": "Ground"
},
"Package": [
{
"Description": "Description of the package",
"Packaging": {
"Code": "02",
"Description": "Package"
},
"Dimensions": {
"UnitOfMeasurement": {
"Code": "IN"
},
"Length": "10",
"Width": "5",
"Height": "5"
},
"PackageWeight": {
"UnitOfMeasurement": {
"Code": "LBS"
},
"Weight": "5"
}
}
],
"LabelSpecification": {
"LabelImageFormat": {
"Code": "GIF"
},
"HTTPUserAgent": "Mozilla/4.5"
}
},
"LabelSpecification": {
"LabelImageFormat": {
"Code": "GIF"
}
}
}
}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 createUpsShipment($accessToken, $shipmentData) {
$client = new Client();
$response = $client->post('https://onlinetools.ups.com/rest/Ship', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
],
'body' => json_encode($shipmentData)
]);
return json_decode($response->getBody(), true);
}
// Usage
$shipmentData = [
"ShipmentRequest" => [
"Shipment" => [
"Description" => "Description of goods",
"Shipper" => [
"Name" => "Shipper Name",
"AttentionName" => "Attention Name",
"TaxIdentificationNumber" => "123456789",
"Phone" => [
"Number" => "1234567890"
],
"ShipperNumber" => "Your UPS Account Number",
"Address" => [
"AddressLine" => ["Address Line"],
"City" => "City",
"StateProvinceCode" => "State",
"PostalCode" => "Postal Code",
"CountryCode" => "Country Code"
]
],
"ShipTo" => [
"Name" => "Recipient Name",
"AttentionName" => "Attention Name",
"Phone" => [
"Number" => "1234567890"
],
"Address" => [
"AddressLine" => ["Address Line"],
"City" => "City",
"StateProvinceCode" => "State",
"PostalCode" => "Postal Code",
"CountryCode" => "Country Code"
]
],
"ShipFrom" => [
"Name" => "Sender Name",
"AttentionName" => "Attention Name",
"Phone" => [
"Number" => "1234567890"
],
"Address" => [
"AddressLine" => ["Address Line"],
"City" => "City",
"StateProvinceCode" => "State",
"PostalCode" => "Postal Code",
"CountryCode" => "Country Code"
]
],
"PaymentInformation" => [
"ShipmentCharge" => [
"Type" => "01",
"BillThirdParty" => [
"BillThirdPartyConsignee" => [
"AccountNumber" => "Third-Party UPS Account Number",
"Address" => [
"PostalCode" => "Third-Party Postal Code",
"CountryCode" => "Third-Party Country Code"
]
]
]
]
],
"Service" => [
"Code" => "03",
"Description" => "Ground"
],
"Package" => [
[
"Description" => "Description of the package",
"Packaging" => [
"Code" => "02",
"Description" => "Package"
],
"Dimensions" => [
"UnitOfMeasurement" => [
"Code" => "IN"
],
"Length" => "10",
"Width" => "5",
"Height" => "5"
],
"PackageWeight" => [
"UnitOfMeasurement" => [
"Code" => "LBS"
],
"Weight" => "5"
]
]
],
"LabelSpecification" => [
"LabelImageFormat" => [
"Code" => "GIF"
],
"HTTPUserAgent" => "Mozilla/4.5"
]
],
"LabelSpecification" => [
"LabelImageFormat" => [
"Code" => "GIF"
]
]
]
];
$response = createUpsShipment($accessToken, $shipmentData);
print_r($response);Step 5: Integrate with Magento 2
- Create a Custom Module: Develop a Magento 2 module to manage UPS API calls.
- Intercept Order Shipment:
- Use an observer or plugin to trigger shipment creation.
- Call the
createUpsShipmentmethod 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
- Test Orders: Place test orders to ensure shipping labels are generated correctly and billed to the third-party UPS account.
- Verify Billing: Confirm that charges are applied to the third-party UPS account.
By following these steps and using the example code, you can configure your Magento 2 store to generate shipping labels via the UPS 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
