Fedex Rest Create Shipment API 输入错误字段值无效

问题描述 投票:0回答:1

我正在尝试将 FedEx Create Shipment API 集成到我的电子商务项目中。我使用 laravel 作为后端框架。下面是我的代码:-

$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);

以下是正在生成的 Payload 对象:-

{
    "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
}

我正在使用 Sandbox API('https://apis-sandbox.fedex.com')并且收到此错误:-

code: "INVALID.INPUT.EXCEPTION"
message: "Invalid field value in the input"
transactionId: "18f933ca-a570-4907-9102-ab9dcd561165"

任何人都可以告诉我我犯了什么错误,这将非常有帮助。预先感谢。

注:所有数据均来自测试环境(假的),所以我没有删除它们

laravel rest guzzle fedex
1个回答
0
投票

我喜欢他们淘汰 SOAP API 的方式,但它的底层都是同样的垃圾,财富 500 强公司无法告诉您它不喜欢 500 行 JSON 输入文件中的哪个字段。

所以你只需要在 Postman / Fiddler 中仔细检查一下,寻找错误即可。

就我而言,我有

"rateRequestType": "PREFERRED"
而不是
"rateRequestType": ["PREFERRED"]

在你的例子中,看起来

recipients
应该是一个对象数组,而不是一个对象。您可能会问,为什么联邦快递服务实际上不支持多个收件人?打败了我,但我敢打赌这就是你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.