UPS错误:250003无效的访问许可证号

时间:2018-09-07 07:51:56

标签: opencart2.x shipping ups

我试图在opencart中设置UPS运送API,并收到错误250003无效的访问许可证号。我从UPS的官方网站下载了UPS运输开发套件。我查看了论坛,发现问题出在凭据上。我重新生成了访问密钥,仍然出现相同的错误。

我创建了一个控制器文件,这是我的代码:

    class ControllerSaleUpsShipping extends Controller {
    //Configuration
    private $access = "my_access_key";
    private $userid = "my_account_username";
    private $passwd = "my_account_password";
    private $wsdl = HTTPS_UPS."Ship.wsdl"; // HTTPS_UPS this is the https url to the UPS folder where all schemas file are placed.

    private $operation = "ProcessShipment";
    private $endpointurl = 'https://wwwcie.ups.com/webservices/Ship';
    //private $endpointurl = https://onlinetools.ups.com/webservices/Ship //production URL

    private $outputFileName = DIR_UPS."XOLTResult.xml"; // DIR_UPS this is the absolute path to the UPS folder where all schemas file are placed.
    public function index()
    {
        try
        {
            $opts = array(
                'http' => array(
                    'user_agent' => 'PHPSoapClient'
                )
            );
            $context = stream_context_create($opts);
            $mode = array
            (
                'soap_version' => 'SOAP_1_1',  // use soap 1.1 client
                'trace' => 1,
                'stream_context' => $context
            );

            // initialize soap client
            $client = new SoapClient($this->wsdl , $mode);

            //set endpoint url
            $client->__setLocation($this->endpointurl);


            //create soap header
            $usernameToken['Username'] = $this->userid;
            $usernameToken['Password'] = $this->passwd;
            $serviceAccessLicense['AccessLicenseNumber'] = $this->access;
            $upss['UsernameToken'] = $this->usernameToken;
            $upss['ServiceAccessToken'] = $this->serviceAccessLicense;

            $header = new SoapHeader('http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','UPSSecurity',$upss);
            $client->__setSoapHeaders($header);

            if(strcmp($this->operation,"ProcessShipment") == 0 )
            {
                //get response
            $resp = $client->__soapCall('ProcessShipment',array($this->processShipment()));
                 //get status
                echo "Response Status: " . $resp->Response->ResponseStatus->Description ."\n";

                //save soap request and response to file
                echo $this->outputFileName."demo";
                $fw = fopen($this->outputFileName , 'w');
                fwrite($fw , "Request: \n" . $client->__getLastRequest() . "\n");
                fwrite($fw , "Response: \n" . $client->__getLastResponse() . "\n");
                fclose($fw);

            }
            else if (strcmp($this->operation , "ProcessShipConfirm") == 0)
            {
                    //get response
            $resp = $client->__soapCall('ProcessShipConfirm',array($this->processShipConfirm()));

                 //get status
                echo "Response Status: " . $resp->Response->ResponseStatus->Description ."\n";

                //save soap request and response to file
                $fw = fopen($this->outputFileName , 'w');
                fwrite($fw , "Request: \n" . $client->__getLastRequest() . "\n");
                fwrite($fw , "Response: \n" . $client->__getLastResponse() . "\n");
                fclose($fw);

            }
            else
            {
                $resp = $client->__soapCall('ProcessShipeAccept',array($this->processShipAccept()));

                //get status
                echo "Response Status: " . $resp->Response->ResponseStatus->Description ."\n";

            //save soap request and response to file
            $fw = fopen($this->outputFileName ,'w');
            fwrite($fw , "Request: \n" . $client->__getLastRequest() . "\n");
                fwrite($fw , "Response: \n" . $client->__getLastResponse() . "\n");
                fclose($fw);
            }
        }
        catch(Exception $ex)
        {
            echo "<pre>";print_r ($ex);echo "</pre>";
        }
    }

    public function processShipment()
    {
        $this->load->model('sale/order');
        $order_id = 394;
        $order_info = $this->model_sale_order->getOrder($order_id);
        $products = $this->model_sale_order->getOrderProducts($order_id);

        //create soap request
        $requestoption['RequestOption'] = 'nonvalidate';
        $request['Request'] = $requestoption;

        $shipment['Description'] = '';
        $counter = 1;
        foreach ($products as $row_product) {
            $shipment['Description'] .= $row_product['name'];
            if($counter != count($products))
            {
                $shipment['Description'] .= ", ";
            }
            $counter = $counter+1;
        }
        $shipper['Name'] = 'name';
        $shipper['AttentionName'] = 'attention name';
        $shipper['TaxIdentificationNumber'] = 'my_tax_number';
        $shipper['ShipperNumber'] = 'my_shipper_number ';
        $address['AddressLine'] = 'my_address';
        $address['City'] = 'my_city';
        $address['StateProvinceCode'] = 'CA';
        $address['PostalCode'] = '90220';
        $address['CountryCode'] = 'US';
        $shipper['Address'] = $address;
        $phone['Number'] = 'my_number';
        $phone['Extension'] = '1';
        $shipper['Phone'] = $phone;
        $shipment['Shipper'] = $shipper;

        $shipto['Name'] = $order_info['shipping_firstname']." ".$order_info['shipping_lastname'];
        $shipto['AttentionName'] = 'Name';
        $addressTo['AddressLine'] = $order_info['shipping_address_1'];
        $addressTo['City'] = $order_info['shipping_city'];
        $addressTo['PostalCode'] = $order_info['shipping_postcode'];
        $addressTo['CountryCode'] = $this->getCountryCode($order_info['shipping_country']);
        $phone2['Number'] = '';
        $shipto['Address'] = $addressTo;
        $shipto['Phone'] = $phone2;
        $shipment['ShipTo'] = $shipto;

        $shipfrom['Name'] = 'my_company_name';
        $shipfrom['AttentionName'] = 'my_AttentionName';
        $addressFrom['AddressLine'] = 'my_Address';
        $addressFrom['City'] = 'my_city';
        $addressFrom['StateProvinceCode'] = 'CA';
        $addressFrom['PostalCode'] = '90220';
        $addressFrom['CountryCode'] = 'US';
        $phone3['Number'] = 'my_number'; 
        $shipfrom['Address'] = $addressFrom;
        $shipfrom['Phone'] = $phone3;
        $shipment['ShipFrom'] = $shipfrom;

        $shipmentcharge['Type'] = '01';

        $billshipper['AccountNumber'] = 'my_shipper_number';
        $shipmentcharge['BillShipper'] = $billshipper;
        $paymentinformation['ShipmentCharge'] = $shipmentcharge;
        $shipment['PaymentInformation'] = $paymentinformation;

        $service['Code'] = '08';
        $service['Description'] = 'Expedited';
        $shipment['Service'] = $service;

        $internationalForm['FormType'] = '01';
        $internationalForm['InvoiceNumber'] = $order_id;
        $internationalForm['InvoiceDate'] = date("yyyy").date("mm").date("dd");
        $internationalForm['PurchaseOrderNumber'] = $order_id;
        $internationalForm['TermsOfShipment'] = 'CFR';
        $internationalForm['ReasonForExport'] = 'Sale';

        if($order_info['shipping_firstname'] == "")
        {
            $customer_name = $order_info['payment_firstname']." ".$order_info['payment_lastname'];
            $customer_address = $order_info['payment_address_1'];
            $customer_city = $order_info['payment_city'];
            $customer_postcode = $order_info['payment_postcode'];
            $customer_state = $order_info['payment_zone'];
            $customer_country = $order_info['payment_country'];
        }
        else
        {
            $customer_name = $order_info['shipping_firstname']." ".$order_info['shipping_lastname'];
            $customer_address = $order_info['shipping_address_1'];
            $customer_city = $order_info['shipping_city'];
            $customer_postcode = $order_info['shipping_postcode'];
            $customer_state = $order_info['shipping_zone'];
            $customer_country = $order_info['shipping_country'];
        }

        $shipto['Name'] = $customer_name;
        $shipto['AttentionName'] = 'name';
        $addressTo['AddressLine'] = $customer_address;
        $addressTo['City'] = $customer_city;
        $addressTo['StateProvinceCode'] = $customer_state;
        $addressTo['PostalCode'] = $customer_postcode;
        $addressTo['CountryCode'] = $this->getCountryCode($customer_country);
        $phone2['Number'] = '';
        $shipto['Address'] = $addressTo;
        $shipto['Phone'] = $phone2;
        $shipment['ShipTo'] = $shipto;

        $soldTo['Option'] = '01';
        $soldTo['AttentionName'] = $customer_name;
        $soldTo['Name'] = $customer_name;
        $soldToPhone['Number'] = $order_info['telephone'];

        $soldTo['Phone'] = $soldToPhone;
        $soldToAddress['AddressLine'] = $customer_address;
        $soldToAddress['City'] =  $customer_city;
        $soldToAddress['StateProvinceCode'] = $customer_state;
        $soldToAddress['PostalCode'] = $customer_postcode;
        $soldToAddress['CountryCode'] = $this->getCountryCode($customer_country);
        $soldTo['Address'] = $soldToAddress;
        $contact['SoldTo'] = $soldTo;
        $internationalForm['Contacts'] = $contact;
        $product['Description'] = $shipment['Description'];
        $product['OriginCountryCode'] = 'US';

        foreach ($products as $row_product) {
            $options = $this->model_sale_order->getOrderOptions($order_id, $row_product['order_product_id']);
            if(count($products) > 1)
            {
                $unitProduct['Number'.$counter] = $row_product['quantity'];
                $unitProduct['Value'.$counter] = $row_product['price'];
                foreach ($options as $option) {
                    $uom['Code'.$counter] = $option['sku'];
                    $uom['Description'.$counter] = $option['sku'];
                }
            }
            else{
                $unitProduct['Number'] = $row_product['quantity'];
                $unitProduct['Value'] = $row_product['price'];
                foreach ($options as $option) {
                    $uom['Code'] = $option['sku'];
                    $uom['Description'] = $option['sku'];
                }
            }

            $counter = $counter+1;
        }
        $unitProduct['UnitOfMeasurement'] = $uom;
        $product['Unit'] = $unitProduct;
        $productWeight['Weight'] = '3';
        $uomForWeight['Code'] = 'LBS';
        $uomForWeight['Description'] = 'LBS';
        $productWeight['UnitOfMeasurement'] = $uomForWeight;
        $product['ProductWeight'] = $productWeight;
        $internationalForm['Product'] = $product;
        $internationalForm['CurrencyCode'] = 'USD';
        $shpServiceOptions['InternationalForms'] = $internationalForm;
        $shipment['ShipmentServiceOptions'] = $shpServiceOptions;


        $package['Description'] = '';
        $packaging['Code'] = '02';
        $packaging['Description'] = 'Products';
        $package['Packaging'] = $packaging;
        $unit['Code'] = 'IN';
        $unit['Description'] = 'Inches';
        $dimensions['UnitOfMeasurement'] = $unit;
        $dimensions['Length'] = '7';
        $dimensions['Width'] = '5';
        $dimensions['Height'] = '2';
        $package['Dimensions'] = $dimensions;
        $unit2['Code'] = 'LBS';
        $unit2['Description'] = 'Pounds';
        $packageweight['UnitOfMeasurement'] = $unit2;
        $packageweight['Weight'] = '10';
        $package['PackageWeight'] = $packageweight;
        $shipment['Package'] = $package;

        $labelimageformat['Code'] = 'GIF';
        $labelimageformat['Description'] = 'GIF';
        $labelspecification['LabelImageFormat'] = $labelimageformat;
        $labelspecification['HTTPUserAgent'] = 'Mozilla/4.5';
        $shipment['LabelSpecification'] = $labelspecification;
        $request['Shipment'] = $shipment;

        echo "Request.......\n";
        //echo "<pre>";print_r($request);echo "</pre>";
        echo "\n\n";
        return $request;

    }

    function processShipConfirm()
    {

    //create soap request

    }

    function processShipAccept()
    {
    //create soap request
    }

    /** Function to get 2 digit country code **/
    function getCountryCode($country)
    {
        $country = ($country == "United States" ? "United States of America" : $country);
        $data = @file_get_contents("https://restcountries.eu/rest/v2/name/".$country);
        $result = json_decode($data);
        //echo "<pre>";print_r($result);echo "</pre>";
        return $result[0]->alpha2Code;
    }
}

有什么方法可以调试问题?

预先感谢

1 个答案:

答案 0 :(得分:0)

那永远都不容易,

好尝试一下,先将UPS卸载然后再安装,这应该为您提供新的访问密钥,现在使用此新密钥并查看其是否有效,这是一些对其他开发人员有效的方法

(1)”“对我来说,这只是错误的访问代码,我需要xml访问代码。我还必须返回UPS站点并要求使用XML访问密钥(代码)而不是HTML访问密钥,我首先在UPS模块的“ UPS访问代码”字段中使用。现在可以使用-或至少在结帐时显示UPS运送选项。”

(2)”我的问题解决了。我必须确保已发布,必填且在所有位置都可见的“国家,州和邮编用户”字段。此外,您可能想尝试将“注册方法”设置为“必填”。进行该设置后,您必须手动设置必需的用户字段,VM的注册方法无法与用户字段通信,这是一个错误。 “

如果没有,我建议您直接与UPS联系。