0

我尝试为此购买联邦快递皮卡,我使用easyPost api。https://www.easypost.com/docs/api

代码运行良好,直到我尝试购买皮卡的那一刻

我的代码

# region Create and Buy EasyPost Shipment
        try {
            /*
             * address should always be the address of their headquarters
             */
            /** @var Address $fromAddress */
            $fromAddress = Address::create([
                "street1" => $params['street1'],
                "street2" => $params['street2'],
                "city" => $params['city'],
                "state" => $params['state'],
                "zip" => $params['zip'],
                "country" => $params['country'],
                "name" => $params['name'],
                "phone" => $params['phone']
            ]);

            if (!$fromAddress->valid()) {
                return new WP_Error('error',
                    __('There was an error configuring your shipment. Please try again.', 'rest-tutorial'),
                    array('status' => 200)
                );
            }

            /** @var Address $toAddress */
            $toAddress = Address::create([
                "street1" => '43 Alpha Park',
                "street2" => "",
                "city" => 'Cleveland',
                "state" => 'OH',
                "zip" => '44143',
                "country" => 'US',
                "company" => 'COMPANY Tested',
                "phone" => '8009723282'
            ]);

            if (!$toAddress->valid()) {
                
                return new WP_Error('error',
                    __('Address of the requested destination is invalid. Please choose another', 'rest-tutorial'),
                    array('status' => 200)
                );
            }
            /** @var Parcel $parcel */
            $parcel = Parcel::create(array(
                "length" => 13,
                "width" => 11,
                "height" => 2,
                "weight" => 1,
                "predefined_package" => "FedExPak"
            ));

            if (!$parcel->valid()) {
                
                return new WP_Error('error',
                    __('There was a problem validating parcel dimensions.', 'rest-tutorial'),
                    array('status' => 200)
                );
            }

            /** @var Shipment $shipment */
            $shipment = Shipment::create([
                "to_address" => $toAddress,
                "from_address" => $fromAddress,
                "parcel" => $parcel,
                "options" => [
                    "print_custom_1" => 'Case ' . $project->caseId->get()
                ]
            ]);
            if (!$shipment->valid()) {
                
                return new WP_Error('error',
                    __('There was a problem creating a valid shipment. Please try again.', 'rest-tutorial'),
                    array('status' => 200)
                );
            }
        } catch (Exception $e) {
            
            return new WP_Error('error',
                __('There was a problem contacting the shipping service.' . $e->getMessage(), 'rest-tutorial'),
                array('status' => 200)
            );
        }

        try {
        

            // determine rate(s)
            $shipment->get_rates();

            $shippingService = 'FEDEX_2_DAY';
            $defaultService = 'FEDEX_2_DAY';

            $shippingCarrier = 'FedEx';
            $defaultCarrier = 'FedEx';

            foreach ($shipment->rates as $rate) {
                /** @var Rate $rate */
                if ($rate->carrier == $shippingCarrier && $rate->service == $shippingService) $shippingRate = $rate;
                else if ($rate->carrier == $defaultCarrier && $rate->service == $defaultService) $defaultRate = $rate;
            }
        } catch (Exception $e) {
            if (empty($shipmentInstance)) {
        
                return new WP_Error('error',
                    __('There was a problem determining shipping rates for the given shipping options. Please contact your customer representative.', 'rest-tutorial'),
                    array('status' => 200)
                );
            }
        }

        // buy label
        try {
            if ($shippingRate == null) {
                // go with default rate if available or try to buy the lowest rate for available carriers
                $shippingRate = ($defaultRate != null) ?
                    $defaultRate : $shipment->lowest_rate($carriers);
            }
            $shipment->buy(['rate' => $shippingRate]);
        } catch (Exception $e) {    

            return new WP_Error('error',
                __('There was a problem creating the shipment. Please review your shipping information.', 'rest-tutorial'),
                array('status' => 200)
            );
        }

        try {
            $address = Address::create([
                "street1" => $params['street1'],
                "street2" => $subpremise,
                "city" => $params['city'],
                "state" => $params['state'],
                "zip" => $params['zip'],
                "country" => $params['country'],
                "company" => $params['name'],
                "phone" => $params['phone']
            ]);

            // Cancel FedEx Pickup if has been bought
            $pickupTime = $helper->pickupTimeToFedEx($params['pickupTime']);
    
            $pickup = Pickup::create(array(
                "address" => $address,
                "shipment" => $shipment,
                "reference" => "fedex_pickup_refr",
                "min_datetime" => $pickupTime['from'],
                "max_datetime" => $pickupTime['to'],
                "is_account_address" => false,
                "instructions" => ""
            ));

            if (!$pickup->valid()) {

                return new WP_Error('error',
                    __('There was a problem creating a valid pickup. Please try again.', 'rest-tutorial'),
                    array('status' => 200)
                );
            }

        } catch (Exception $e) {
            return new WP_Error('error',
                __('There was a problem of the Pickup creation. Please contact you customer representative.', 'rest-tutorial'),
                array('status' => 200)
            );
        }

        try {
            $pickup_rate = $pickup->pickup_rates[0];

            $carrier = $pickup_rate->__get('carrier');
            $service = $pickup_rate->__get('service');

            $pickup->buy(['carrier' => $carrier, 'service' => $service]);
        } catch (Exception $e) {

            var_dump($e);
            exit;
            return new WP_Error('error',
                __('There was a problem to purchase a Pickup. Please contact your customer representative.', 'rest-tutorial'),
                array('status' => 200)
            );
        }

但我得到错误

string(137) "{"error":{"code":"PICKUP.BUY.FAILED","message":"Unable to process request<!--Express dispatch returned invalid response-->","errors":[]}}"

对于这个错误,我在互联网上找不到任何东西

为什么不想买皮卡?

如何理解问题所在以及如何解决?

谢谢!

4

0 回答 0