Antidot-be / bpost-api-library

Other
24 stars 18 forks source link

Unable create label international parcel #38

Closed visualight closed 2 months ago

visualight commented 5 months ago

I try to send a parcel from BE to FR (at client home).

I get always an error like : "The delivery method bpack BUSINESS with product group bpack World Express Pro is not valid for the country FR (receiver)"

Anyone could help me ?

protected function genBpostIntLabel()
    {
        $transporter = Transporter::find(1);

        $carrierData = [
            'reference' => 'TR_103',
            'company' => 'Company',
            'name' => 'Firstname Lastname',
            'address1' => 'Rue de xxx',
            'number' => '3',
            'zip' => '88120',
            'city' => 'Charmes',
            'country' => 'FR',
            'phone' => 'xxxxxx',
            'email' => 'xxx@gmail.com'
        ];

        if($transporter)
        {
            try
            {
                $apiUrl = "https://api-parcel.bpost.be/services/shm/";
                $orderReference = $carrierData['reference'];

                $bpost = new Bpost($transporter->api_key, $transporter->api_secret, $apiUrl);

                $receiverAddress = new Address();
                $receiverAddress->setStreetName($carrierData['address1']);
                $receiverAddress->setNumber($carrierData['number']);
                $receiverAddress->setPostalCode($carrierData['zip']);
                $receiverAddress->setLocality($carrierData['city']);
                $receiverAddress->setCountryCode($carrierData['country']);

                $receiver = new Receiver();
                $receiver->setAddress($receiverAddress);

                if($carrierData['company']){
                    $receiver->setCompany($carrierData['company']);
                }

                $receiver->setName($carrierData['name']);
                $receiver->setPhoneNumber($carrierData['phone']);
                $receiver->setEmailAddress($carrierData['email']);

                $box = new Box();

                $customsInfo = new Box\CustomsInfo\CustomsInfo();
                $customsInfo->setParcelValue(700);
                $customsInfo->setContentDescription('FASHION');
                $customsInfo->setShipmentType(Bpost\Order\Box\CustomsInfo\CustomsInfo::CUSTOM_INFO_SHIPMENT_TYPE_GOODS);
                $customsInfo->setParcelReturnInstructions(Bpost\Order\Box\CustomsInfo\CustomsInfo::CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTS);
                $customsInfo->setPrivateAddress(false);

                $international = new Box\International();
                $international->setProduct(Bpost\ProductConfiguration\Product::PRODUCT_NAME_BPACK_WORLD_EXPRESS_PRO);
                $international->setReceiver($receiver);
                $international->setParcelWeight(2000);
                $international->setCustomsInfo($customsInfo);

                $box->setInternationalBox($international);

                $order = new Bpost\Order($orderReference);
                $order->addBox($box);

                $bpost->createOrReplaceOrder($order);

                $labels = $bpost->createLabelForOrder(
                    $orderReference,
                    Bpost::LABEL_FORMAT_A6,
                    false,
                    true
                );

                foreach ($labels as $label) {
                    $barcode = $label->getBarcode();
                    $bytes = $label->getBytes();

                    $local = Storage::disk('public');
                    $local->put('labels/bpost/'.$barcode.'.pdf', $bytes, 'public');
                }

                dd(json_encode(['provider' => 'bpost', 'tracking' => $barcode, 'file' => $barcode.'.pdf']));
            }
            catch (\Exception $e) {
                dd($e->getMessage());
            }
        }
        return false;
    }
kouinkouin commented 5 months ago

Hi @visualight ,

When I try your code, all work.

Did the same code works some in the past or the issue appears since an update of the lib?

As sender (in your bpost SHM settings), are you allowed to send to international, or your shop is well based on Belgium? Maybe you can contact esolutions@bpost.be, with your error message and your account ID, they can check with you your "business" settings and confirm all is good.

Little tips, you can remove the third argument of the constructor of Bpost because the new URL of the API is now https://shm-rest.bpost.cloud/services/shm (the default value):

-  $bpost = new Bpost($transporter->api_key, $transporter->api_secret, $apiUrl);
+  $bpost = new Bpost($transporter->api_key, $transporter->api_secret);
visualight commented 5 months ago

I use "antidot-be/bpost-api-library": "^3.4" 3.4.11

kouinkouin commented 5 months ago

Can you try the update to with composer update ? (not needed to update the composer.json). The last version is 3.5.1 .

visualight commented 5 months ago

Composer says : Nothing to install, update or remove

visualight commented 5 months ago

"require": { "php": "^8.2", "antidot-be/bpost-api-library": "^3.4", "barryvdh/laravel-dompdf": "^2.0", "biscolab/laravel-recaptcha": "^6.0", "cba85/php-mondialrelay-webservice": "^1.0", "danielebarbaro/laravel-vat-eu-validator": "^1.1", "darryldecode/cart": "^4.2", "guzzlehttp/guzzle": "^7.2", "iio/libmergepdf": "^4.0", "intervention/image-laravel": "^1.1", "laravel/framework": "^10.10", "laravel/tinker": "^2.8", "league/flysystem-aws-s3-v3": "^3.24", "mailjet/laravel-mailjet": "^3.0", "mollie/laravel-mollie": "^3.0", "realrashid/sweet-alert": "^7.1", "spatie/laravel-honeypot": "^4.4", "spatie/laravel-html": "^3.5", "spatie/laravel-permission": "^6.3", "watson/sitemap": "^5.1", "yajra/laravel-datatables": "^10" },

kouinkouin commented 5 months ago

You have to add all countries you're sending to in your SHM settings. I hope that will help you:

image

visualight commented 5 months ago

Merci Koinkoin 🎉

visualight commented 5 months ago

Last question: I'd like to send an international parcel to client home address.

What is the purpose of $customsInfo->setPrivateAddress();? I've currently set it to false. Correct? Thanks

kouinkouin commented 5 months ago

true or false, that seems correct (depending you send to a private or "business" address).

https://bpost.freshdesk.com/helpdesk/attachments/4145363325: image