I am trying to integrate FedEx Create Shipment API in my E-Commerce Project. I am using laravel as the backend framework. Below is my code:-
$product = Product::find($orderDetail->product_id);
if ($product->type == 'digital') return;
$fedexRestApiCredentials = FedExRestApi::whereSellerId(auth()->id())->latest()->first();
$fedexRestApiCredentials->fedex_client_id = decrypt($fedexRestApiCredentials->fedex_client_id);
$fedexRestApiCredentials->fedex_client_secret = decrypt($fedexRestApiCredentials->fedex_client_secret);
$fedexRestApiCredentials->fedex_account_number = decrypt($fedexRestApiCredentials->fedex_account_number);
$sellerId = $orderDetail->seller_id;
$seller = User::find($sellerId);
$sellerProfile = SellerProfile::whereUserId($sellerId)->first();
$seller->phone = str_replace(' ', '', $seller->phone);
$seller->phone = str_replace('(', '', $seller->phone);
$seller->phone = str_replace(')', '', $seller->phone);
$seller->phone = str_replace('-', '', $seller->phone);
$seller->phone = '1' . $seller->phone;
$sellerAddress = SellerAddress::whereSellerId($sellerId)->first();
$streetLines[] = $sellerAddress->line1;
$streetLines[] = $sellerAddress->line2;
$streetLines = array_filter($streetLines);
$buyerAddress = BuyerAddress::whereBuyerId($orderDetail->order->buyer_id)->whereIsDefault('1')->first();
$streetLinesBuyer = [];
$streetLinesBuyer[] = $buyerAddress->street_address;
$streetLinesBuyer[] = $buyerAddress->others;
$streetLinesBuyer = array_filter($streetLinesBuyer);
$userId = auth()->id();
$buyer = User::find($userId);
$buyer->phone = str_replace(' ', '', $buyer->phone);
$buyer->phone = str_replace('(', '', $buyer->phone);
$buyer->phone = str_replace(')', '', $buyer->phone);
$buyer->phone = str_replace('-', '', $buyer->phone);
$buyer->phone = '1' . $buyer->phone;
$fedexStateToken = FedexStateToken::notExpired()->whereCreatedBy($sellerId)->latest()->value('token');
if (is_null($fedexStateToken)) {
$auth = (new \FedexRest\Authorization\Authorize())
->setClientId($fedexRestApiCredentials->fedex_client_id)
->setClientSecret($fedexRestApiCredentials->fedex_client_secret)
->authorize();
FedexStateToken::create([
'token' => $auth->access_token,
'expiry_date' => now()->addHour(),
'token_type' => $auth->token_type,
'scope' => $auth->scope,
'created_by' => $sellerId
]);
$fedexStateToken = $auth->access_token;
}
$headers = [
'Authorization' => 'Bearer ' . $fedexStateToken,
'Content-Type' => 'application/json'
];
$requestedPackageLineItems[] = (object) [
'sequenceNumber' => $product->id,
'groupPackageCount' => 1,
'weight' => (object) [
'units' => $product->weight_unit,
'value' => (float) $product->weight_value
],
'dimensions' => (object) [
'length' => $product->length,
'width' => $product->width,
'height' => $product->height,
'units' => $product->dimension_unit
],
'itemDescription' => $product->description
];
$input = new \stdClass();
$input = (object) [
'mergeLabelDocOption' => "LABELS_AND_DOCS",
'requestedShipment' => (object) [
'shipDatestamp' => now()->format('Y-m-d'),
'shipper' => (object) [
'address' => (object) [
'streetLines' => $streetLines,
'city' => $sellerAddress->city,
'stateOrProvinceCode' => $sellerAddress->state,
'postalCode' => $sellerAddress->postcode,
'countryCode' => $sellerAddress->country_code
],
'contact' => (object) [
'personName' => $seller->full_name,
'emailAddress' => $seller->email,
'phoneNumber' => $seller->phone,
'companyName' => $sellerProfile->business_name
]
],
'soldTo' => (object) [
'address' => (object) [
'streetLines' => $streetLines,
'city' => $sellerAddress->city,
'stateOrProvinceCode' => $sellerAddress->state,
'postalCode' => $sellerAddress->postcode,
'countryCode' => $sellerAddress->country_code
],
'contact' => (object) [
'personName' => $seller->full_name,
'emailAddress' => $seller->email,
'phoneNumber' => $seller->phone,
'companyName' => $sellerProfile->business_name
],
'accountNumber' => (object) [
'value' => $fedexRestApiCredentials->fedex_account_number
]
],
'recipients' => (object) [
'address' => (object) [
'streetLines' => $streetLinesBuyer,
'city' => $buyerAddress->city,
'stateOrProvinceCode' => $buyerAddress->state,
'postalCode' => $buyerAddress->zip,
'countryCode' => $buyerAddress->country_code
],
'contact' => (object) [
'personName' => $buyer->full_name,
'emailAddress' => $buyer->email,
'phoneNumber' => $buyer->phone
]
],
'pickupType' => "USE_SCHEDULED_PICKUP",
"serviceType" => $orderDetail->service_type,
"packagingType" => "YOUR_PACKAGING",
'totalWeight' => (float) $product->weight_value,
'origin' => (object) [
'address' => (object) [
'streetLines' => $streetLines,
'city' => $sellerAddress->city,
'stateOrProvinceCode' => $sellerAddress->state,
'postalCode' => $sellerAddress->postcode,
'countryCode' => $sellerAddress->country_code
],
'contact' => (object) [
'personName' => $seller->full_name,
'emailAddress' => $seller->email,
'phoneNumber' => $seller->phone,
'companyName' => $sellerProfile->business_name
]
],
'shippingChargesPayment' => (object) [
'paymentType' => "SENDER",
'payor' => (object) [
'responsibleParty' => (object) [
'address' => (object) [
'streetLines' => $streetLines,
'city' => $sellerAddress->city,
'stateOrProvinceCode' => $sellerAddress->state,
'postalCode' => $sellerAddress->postcode,
'countryCode' => $sellerAddress->country_code
],
'contact' => (object) [
'personName' => $seller->full_name,
'emailAddress' => $seller->email,
'phoneNumber' => $seller->phone,
'companyName' => $sellerProfile->business_name
],
'accountNumber' => (object) [
'value' => $fedexRestApiCredentials->fedex_account_number
]
]
]
],
'emailNotificationDetail' => (object) [
'aggregationType' => "PER_PACKAGE",
'emailNotificationRecipients' => [
(object)[
'name' => $buyer->full_name,
'emailNotificationRecipientType' => "RECIPIENT",
'emailAddress' => $buyer->email,
"notificationFormatType" => "HTML",
"notificationType" => "EMAIL",
"notificationEventType" => [
"ON_DELIVERY",
"ON_EXCEPTION",
"ON_SHIPMENT",
"ON_TENDER",
"ON_PICKUP",
"ON_ESTIMATED_DELIVERY",
"ON_LABEL",
"ON_PICKUP_DRIVER_ARRIVED",
"ON_PICKUP_DRIVER_ASSIGNED",
"ON_PICKUP_DRIVER_DEPARTED",
"ON_PICKUP_DRIVER_EN_ROUTE"
]
],
(object)[
'name' => $seller->full_name,
'emailNotificationRecipientType' => "SHIPPER",
'emailAddress' => $seller->email,
"notificationFormatType" => "HTML",
"notificationType" => "EMAIL",
"notificationEventType" => [
"ON_DELIVERY",
"ON_EXCEPTION",
"ON_SHIPMENT",
"ON_TENDER",
"ON_PICKUP",
"ON_ESTIMATED_DELIVERY",
"ON_LABEL",
"ON_PICKUP_DRIVER_ARRIVED",
"ON_PICKUP_DRIVER_ASSIGNED",
"ON_PICKUP_DRIVER_DEPARTED",
"ON_PICKUP_DRIVER_EN_ROUTE"
]
]
]
],
'blockInsightVisibility' => FALSE,
'labelSpecification' => (object) [
'labelFormatType' => "COMMON2D",
'labelStockType' => "PAPER_7X475",
'labelRotation' => "LEFT",
'imageType' => "PDF"
],
'rateRequestType' => ["PREFERRED"],
'preferredCurrency' => 'USD',
'totalPackageCount' => 1,
'requestedPackageLineItems' => $requestedPackageLineItems,
],
'labelResponseOptions' => "URL_ONLY",
'accountNumber' => (object) [
'value' => $fedexRestApiCredentials->fedex_account_number
],
'shipAction' => "CONFIRM",
'processingOptionType' => 'SYNCHRONOUS_ONLY',
'oneLabelAtATime' => FALSE
];
$client = new Client();
$request = $client->request('POST', env('FEDEX_REST_API_URL') . '/ship/v1/shipments', [
'headers' => $headers,
'json' => $input,
]);
$response = $request->getBody()->getContents();
$response = json_decode($response);
Following is the Payload object that is being generated:-
{
"mergeLabelDocOption": "LABELS_AND_DOCS",
"requestedShipment": {
"shipDatestamp": "2023-05-17",
"shipper": {
"address": {
"streetLines": [
"1309 S Agnew Avenue",
"Apt 303"
],
"city": "Oklahoma City",
"stateOrProvinceCode": "OK",
"postalCode": "73108",
"countryCode": "US"
},
"contact": {
"personName": "Kattie Ortiz",
"emailAddress": "[email protected]",
"phoneNumber": "17182934300",
"companyName": "Kyle Wilderman LLC"
}
},
"soldTo": {
"address": {
"streetLines": [
"1309 S Agnew Avenue",
"Apt 303"
],
"city": "Oklahoma City",
"stateOrProvinceCode": "OK",
"postalCode": "73108",
"countryCode": "US"
},
"contact": {
"personName": "Kattie Ortiz",
"emailAddress": "[email protected]",
"phoneNumber": "17182934300",
"companyName": "Kyle Wilderman LLC"
},
"accountNumber": {
"value": "XXXXXXXXX"
}
},
"recipients": {
"address": {
"streetLines": [
"13450 Farmcrest Ct"
],
"city": "Herndon",
"stateOrProvinceCode": "VA",
"postalCode": "20171",
"countryCode": "US"
},
"contact": {
"personName": "Kattie Ortiz",
"emailAddress": "[email protected]",
"phoneNumber": "17182934300"
}
},
"pickupType": "USE_SCHEDULED_PICKUP",
"serviceType": "FEDEX_GROUND",
"packagingType": "YOUR_PACKAGING",
"totalWeight": 50,
"origin": {
"address": {
"streetLines": [
"1309 S Agnew Avenue",
"Apt 303"
],
"city": "Oklahoma City",
"stateOrProvinceCode": "OK",
"postalCode": "73108",
"countryCode": "US"
},
"contact": {
"personName": "Kattie Ortiz",
"emailAddress": "[email protected]",
"phoneNumber": "17182934300",
"companyName": "Kyle Wilderman LLC"
}
},
"shippingChargesPayment": {
"paymentType": "SENDER",
"payor": {
"responsibleParty": {
"address": {
"streetLines": [
"1309 S Agnew Avenue",
"Apt 303"
],
"city": "Oklahoma City",
"stateOrProvinceCode": "OK",
"postalCode": "73108",
"countryCode": "US"
},
"contact": {
"personName": "Kattie Ortiz",
"emailAddress": "[email protected]",
"phoneNumber": "17182934300",
"companyName": "Kyle Wilderman LLC"
},
"accountNumber": {
"value": "XXXXXXXXX"
}
}
}
},
"emailNotificationDetail": {
"aggregationType": "PER_PACKAGE",
"emailNotificationRecipients": [
{
"name": "Kattie Ortiz",
"emailNotificationRecipientType": "RECIPIENT",
"emailAddress": "[email protected]",
"notificationFormatType": "HTML",
"notificationType": "EMAIL",
"notificationEventType": [
"ON_DELIVERY",
"ON_EXCEPTION",
"ON_SHIPMENT",
"ON_TENDER",
"ON_PICKUP",
"ON_ESTIMATED_DELIVERY",
"ON_LABEL",
"ON_PICKUP_DRIVER_ARRIVED",
"ON_PICKUP_DRIVER_ASSIGNED",
"ON_PICKUP_DRIVER_DEPARTED",
"ON_PICKUP_DRIVER_EN_ROUTE"
]
},
{
"name": "Kattie Ortiz",
"emailNotificationRecipientType": "SHIPPER",
"emailAddress": "[email protected]",
"notificationFormatType": "HTML",
"notificationType": "EMAIL",
"notificationEventType": [
"ON_DELIVERY",
"ON_EXCEPTION",
"ON_SHIPMENT",
"ON_TENDER",
"ON_PICKUP",
"ON_ESTIMATED_DELIVERY",
"ON_LABEL",
"ON_PICKUP_DRIVER_ARRIVED",
"ON_PICKUP_DRIVER_ASSIGNED",
"ON_PICKUP_DRIVER_DEPARTED",
"ON_PICKUP_DRIVER_EN_ROUTE"
]
}
]
},
"blockInsightVisibility": false,
"labelSpecification": {
"labelFormatType": "COMMON2D",
"labelStockType": "PAPER_7X475",
"labelRotation": "LEFT",
"imageType": "PDF"
},
"rateRequestType": [
"PREFERRED"
],
"preferredCurrency": "USD",
"totalPackageCount": 1,
"requestedPackageLineItems": [
{
"sequenceNumber": 7,
"groupPackageCount": 1,
"weight": {
"units": "LB",
"value": 50
},
"dimensions": {
"length": 20,
"width": 20,
"height": 10,
"units": "IN"
},
"itemDescription": "Ea exercitation impe"
}
]
},
"labelResponseOptions": "URL_ONLY",
"accountNumber": {
"value": "XXXXXXXXX"
},
"shipAction": "CONFIRM",
"processingOptionType": "SYNCHRONOUS_ONLY",
"oneLabelAtATime": false
}
I am using the Sandbox API ('https://apis-sandbox.fedex.com') & I'm getting this error:-
code: "INVALID.INPUT.EXCEPTION"
message: "Invalid field value in the input"
transactionId: "18f933ca-a570-4907-9102-ab9dcd561165"
can anyone please tell me what mistake I'm making that will be really helpful. Thanks in advance.
Note: All the data are from the test environment(means fake), hence I did not remove them
I love how they're retiring the SOAP API but it's all the same garbage underneath, and a Fortune 500 company can't tell you which field in a 500-line JSON input file it didn't like.
So you just have to go over it with a fine-tooth comb in Postman / Fiddler looking for the mistake.
In my case, I had
"rateRequestType": "PREFERRED"instead of"rateRequestType": ["PREFERRED"].In your case, it looks like
recipientsshould be an array of objects, not an object. Why, you ask, since no FedEx service actually supports multiple recipients? Beats me, but I bet that's your problem.