Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure: The XML document is not well formed (10001) #311

Closed
tnduc opened this issue Dec 28, 2020 · 2 comments
Closed

Failure: The XML document is not well formed (10001) #311

tnduc opened this issue Dec 28, 2020 · 2 comments

Comments

@tnduc
Copy link

tnduc commented Dec 28, 2020

Everything still works, but today this error, I tried many ways but it didn't work, please help me.

@ptondereau
Copy link
Collaborator

could you please send your XML?

@tnduc
Copy link
Author

tnduc commented Dec 28, 2020

could you please send your XML?

No problem passed the input, it still works before

<?php

namespace App\Services;
class UpsAPI
{
    protected $user;
    protected $password;
    protected $access_key;
    protected $account_number;

    public function __construct()
    {
        $this->user = config('ups.user');
        $this->password = config('ups.password');
        $this->access_key = config('ups.access_key');
        $this->account_number = config('ups.account_number');
    }

    public function createShipment($params)
    {
        $shipment = $this->shipment($params['etc_name']);

        $shipment->setShipTo($this->shipTo($params['ship_to']));

        $shipment->setShipFrom($this->fromAddress());

        // Set service
        $service = new \Ups\Entity\Service;
        $service->setCode($params['ups_service']);
        $service->setDescription($service->getName());
        $shipment->setService($service);

        // Set description
        // $shipment->setDescription();

        // Add this package
        $shipment->addPackage($this->addPackage($params['add_package']));

        // Set Reference Number
        $referenceNumber = new \Ups\Entity\ReferenceNumber;
        
        $referenceNumber->setCode(\Ups\Entity\ReferenceNumber::CODE_PURCHASE_ORDER_NUMBER);
        $referenceNumber->setValue($params['sale_order_name']);

        $shipment->getPackages()[0]->setReferenceNumber($referenceNumber);

        // Set payment information
        $shipment->setPaymentInformation(new \Ups\Entity\PaymentInformation('prepaid', (object)array('AccountNumber' => $this->account_number)));

        // Ask for negotiated rates (optional)
        $rateInformation = new \Ups\Entity\RateInformation;
        $rateInformation->setNegotiatedRatesIndicator(1);
        $shipment->setRateInformation($rateInformation);

        $api = new \Ups\Shipping($this->access_key, $this->user, $this->password); 

        $confirm = $api->confirm(\Ups\Shipping::REQ_VALIDATE, $shipment);
        
        dd($confirm);
        if ($confirm) {
            $accept = $api->accept($confirm->ShipmentDigest);
        }

        return [
            'base64_string' => $accept->PackageResults->LabelImage->GraphicImage,
            'tracking_number' => $accept->ShipmentIdentificationNumber,
            'shipping_fee' => $accept->ShipmentCharges->TotalCharges->MonetaryValue
        ];
    }

    public function addressValidation($params)
    {
        $address = new \Ups\Entity\Address();
        $address->setAttentionName($params['attn_name']);
        $address->setBuildingName('Test address');
        $address->setAddressLine1($params['shipping_address']);
        $address->setAddressLine2($params['shipping_address_2']);
        $address->setStateProvinceCode($params['state']);
        $address->setCity($params['city']);
        $address->setCountryCode('US');
        $address->setPostalCode($params['zip_code']);

        $xav = new \Ups\AddressValidation($this->access_key, $this->user, $this->password);
        $xav->activateReturnObjectOnValidate(); //This is optional

        return $xav->validate($address, \Ups\AddressValidation::REQUEST_OPTION_ADDRESS_VALIDATION, 15);
    }

    private function shipment($etc_name) {
        // Start shipment
        $shipment = new \Ups\Entity\Shipment;

        // Set shipper
        $shipper = $shipment->getShipper($etc_name);
        $shipper->setShipperNumber('14292W');
        $shipper->setName($etc_name);
        $shipper->setAttentionName('HTH Communications, LLC');
        $shipperAddress = $shipper->getAddress();
        $shipperAddress->setAddressLine1('9920 Brooklet Drive');
        $shipperAddress->setPostalCode('77099');
        $shipperAddress->setCity('HOUSTON');
        $shipperAddress->setStateProvinceCode('TX'); // required in US
        $shipperAddress->setCountryCode('US');
        $shipper->setAddress($shipperAddress);
        $shipper->setEmailAddress('accounting@hthcomm.com'); 
        $shipper->setPhoneNumber('713-534-1950');
        $shipment->setShipper($shipper);

        return $shipment;
    }

    private function fromAddress() {
        $address = new \Ups\Entity\Address();
        $address->setAddressLine1('9920 Brooklet Drive');
        $address->setPostalCode('77099');
        $address->setCity('HOUSTON');
        $address->setStateProvinceCode('TX');  
        $address->setCountryCode('US');
        $shipFrom = new \Ups\Entity\ShipFrom();
        $shipFrom->setAddress($address);
        $shipFrom->setName('HTH Communications, LLC');
        $shipFrom->setAttentionName($shipFrom->getName());
        $shipFrom->setCompanyName($shipFrom->getName());
        $shipFrom->setEmailAddress('accounting@hthcomm.com');
        $shipFrom->setPhoneNumber('713-534-1950');

        return $shipFrom;
    }

    private function shipTo($params) {
        $address = new \Ups\Entity\Address();
        $address->setAddressLine1($params['address_line1']);
        $address->setAddressLine2($params['address_line2']);
        $address->setPostalCode($params['zip_code']);
        $address->setCity($params['city']);
        $address->setStateProvinceCode($params['province_code']);  // Required in US
        $address->setCountryCode($params['country_code']);
        $shipTo = new \Ups\Entity\ShipTo();
        $shipTo->setAddress($address);
        $shipTo->setCompanyName($params['company_name'] ?? '&nbsp;');
        $shipTo->setAttentionName($params['attention_name']);
        $shipTo->setEmailAddress($params['email_address']); 
        $shipTo->setPhoneNumber($params['phone_number']);

        return $shipTo;
    }

    private function addPackage($params) {
        $package = new \Ups\Entity\Package();
        $package->getPackagingType()->setCode(\Ups\Entity\PackagingType::PT_PACKAGE);
        $package->getPackageWeight()->setWeight($params['weight']);
        $unit = new \Ups\Entity\UnitOfMeasurement;
        $unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_LBS);
        $package->getPackageWeight()->setUnitOfMeasurement($unit);

        // Set Package Service Options
        $packageServiceOptions = new \Ups\Entity\PackageServiceOptions();
        $packageServiceOptions->setShipperReleaseIndicator(true);
        $package->setPackageServiceOptions($packageServiceOptions);

        // Set dimensions
        $dimensions = new \Ups\Entity\Dimensions();
        $dimensions->setHeight($params['height']);
        $dimensions->setWidth($params['width']);
        $dimensions->setLength($params['length']);
        $unit = new \Ups\Entity\UnitOfMeasurement;
        $unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_IN);
        $dimensions->setUnitOfMeasurement($unit);
        $package->setDimensions($dimensions);

        // Add descriptions because it is a package
        $package->setDescription($params['description']);

        return $package;
    }
}

@tnduc tnduc closed this as completed Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants