How to add already created label to fedex pickup

252 Views Asked by At

I'm creating label, and everything works well. But how can I connect an already created label to FEDEX pickup.

try {
                /*
                 * Per explicit request fromthe FROM
                 * address should always be the address of their headquarters
                 */

                /** @var Address $fromAddress */
                $fromAddress = Address::create([
                    "street1" => $params['street'],
                    "street2" => "",
                    "city" => $params['city'],
                    "state" => $params['state'],
                    "zip" => $params['zipCode'],
                    "country" => $params['country'],
                    "company" => $from['name'],
                    "phone" => $from['phone']
                ]);
                if (!$fromAddress->valid()) return self::jsonError('There was an error configuring your shipment. Please try again.');

                /** @var Address $toAddress */
                $toAddress = Address::create([
                    "street1" => $params['toStreet'],
                    "street2" => "",
                    "city" => $params['toCity'],
                    "state" => $params['toState'],
                    "zip" => $params['toZipCode'],
                    "country" => $params['toCountry'],
                    "company" => $params['toName'],
                    "phone" => $params['toPhone']
                ]);
                if (!$toAddress->valid()) return self::jsonError('Address of the requested destination is invalid. Please choose another');

                /** @var Parcel $parcel */
                $parcel = Parcel::create(array(
                    "length" => 13,
                    "width" => 11,
                    "height" => 2,
                    "weight" => 1,
                    "predefined_package" => "FedExPak"
                ));
                if (!$parcel->valid()) return self::jsonError('There was a problem validating parcel dimensions.');

                /** @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 self::jsonError('There was a problem creating a valid shipment. Please try again.');
            } catch (\Exception $e) {
                return self::jsonError('There was a problem contacting the shipping service.' . $e->getMessage());
            }

            try {
                // determine rate(s)
                $shipment->get_rates();
                $shippingService = 'FEDEX_2_DAY';
                $defaultService = 'FEDEX_2_DAY';

                if (in_array($params['provide_option'], ['FedEx Dropoff', 'Dropoff'])) {
                    $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) {
                return self::jsonError('There was a problem determining shipping rates for the given shipping options. Please contact your customer representative.' . $e->getMessage());
            }

            // 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 self::jsonError('There was a problem creating the shipment. Please review your shipping information.' . $e->getMessage());
            }

$_SESSION['createdShipment'] = $shipment;

here I am trying to add an already created label to pickup $_SESSION['createdShipment'] it works on condition. seen in code but i get error Unable to create pickup, the carrier associated with the batch shipments is inconsistent.

try {
            $mindate = $params['pickup-date-from'] . " " . $params['pickup-time-from'];
            $maxdate = $params['pickup-date-to'] . " " . $params['pickup-time-to'];

            if(!empty($params['addressPickup']) AND count($params['addressPickup']) == 4){
                $address = Address::create([
                    "street1" => $params['addressPickup']['pickup_street'],
                    "street2" => $params['street2'],
                    "city"    => $params['addressPickup']['pickup_city'],
                    "state"   => $params['addressPickup']['pickup_state'],
                    "zip"     => $params['addressPickup']['pickup_zipCode'],
                    "country" => $params['country'],
                    "company" => $from['name'],
                    "phone"   => $from['phone']
                ]);

                $shipment = $_SESSION['createdShipment'];
            }else{
                $address = Address::create([
                    "street1" => $params['street'],
                    "street2" => $params['street2'],
                    "city"    => $params['city'],
                    "state"   => $params['state'],
                    "zip"     => $params['zipCode'],
                    "country" => $params['country'],
                    "company" => $from['name'],
                    "phone"   => $from['phone']
                ]);
            }

            // Cancel FedEx Pickup if has been bought   
            $this->requestCancelFedExPickup($params, $user, $project, $db, $api, $accountConfig, $oldsession);

            $pickup = \EasyPost\Pickup::create(array(
                "address" => $address,
                "shipment" => $shipment,
                "reference" => "fedex_pickup_refr",
                "min_datetime" => date("Y-m-d H:i:s", strtotime($mindate)),
                "max_datetime" => date("Y-m-d H:i:s", strtotime($maxdate)),
                "is_account_address" => false,
                "instructions" => ""
            ));

            if (!$pickup->valid()) {
                $this->updateProjectCrmByShipmentAndPickup($project, $params, $api, $fromAddress, $toAddress, $shipment);
                return self::jsonError('There was a problem creating a valid pickup. Please try again.');
            }

        } catch (\Exception $e) {
            $this->updateProjectCrmByShipmentAndPickup($project, $params, $api, $fromAddress, $toAddress, $shipment);
            return self::jsonError(
            'There was a problem of the Pickup creation. Please contact your  customer representative.',
            ['easy-post' => $e->getMessage()]
        ); }
1

There are 1 best solutions below

0
Justin Hammond On

The error you are seeing often means that either the shipment hasn't been bought or that the pickup and the shipment label are associated with different carriers (eg: label is for UPS but you are trying to schedule a FedEx pickup.)

If you continue to see this error, you can reference the API docs here: https://www.easypost.com/docs/api#pickups or email [email protected] - the support team is great at finding what is causing errors like these.