diff --git a/README.md b/README.md index c032bce7..1e3239d9 100755 --- a/README.md +++ b/README.md @@ -128,7 +128,12 @@ Note: If you still make the request with additional options, bear in mind that y This package type is available for NL, EU and global shipments. The label for this shipment is unpaid meaning that you will need to pay the postal office/courier to send this letter/package. Therefore, it does not support additional options. #### 4: Digital stamp -This package type is only available for NL shipments and does not support any additional options. Its price is calculated using the package weight. +This package type is only available for NL shipments and does not support any additional options. Its price is calculated using the package weight, which is set using `setPhysicalProperties()`. + +```php + ->setPackageType(4) + ->setPhysicalProperties(['weight' => 300]); // weight in grams (required) +``` > Note: This shipment will appear on your invoice on shipment_status 2 (pending - registered) instead of all other shipment types, which don't appear until shipment status 3. Read more: https://myparcelnl.github.io/api/#6_A_1 @@ -204,20 +209,21 @@ $consignment = (new \MyParcelNL\Sdk\src\Model\Repository\MyParcelConsignmentRepo ->setPhone('+31 612345678') // Phone number ->setCompany('Piet BV') // Company - ->setFullStreet('Plein 1945 55b') // Street, number and suffix in one line + ->setFullStreet('Plein 1945 55b') // Street, number and suffix in one line // OR send the street data separately: - ->setStreet('Plein 1945') // Street - ->setNumber((string)55) // Number - ->setNumberSuffix('b') // Suffix + ->setStreet('Plein 1945') / Street + ->setNumber((string)55) // Number + ->setNumberSuffix('b') // Suffix - ->setCity('Amsterdam') // City - ->setPostalCode('2231JE') // Postal code - ->setCountry('NL') // Country + ->setCity('Amsterdam') // City + ->setPostalCode('2231JE') // Postal code + ->setCountry('NL') // Country // Available package types: // 1: Package (default) // 2: Mailbox package // 3: Letter + // 4: Digital stamp ->setPackageType(1) // Options (https://myparcelnl.github.io/api/#6_A_3) @@ -240,7 +246,10 @@ $consignment = (new \MyParcelNL\Sdk\src\Model\Repository\MyParcelConsignmentRepo ->setPickupNumber('32') ->setPickupPostalCode('1234 AB') ->setPickupCity('Hoofddorp') - + + // Physical properties + ->setPhysicalProperties(['weight' => 73]) // Array with physical properties of the shipment. Currently only used to set the weight in grams for digital stamps (which is required) + // Non-EU shipment attributes: see https://myparcelnl.github.io/api/#7_E ->setInvoice() ->setContents() @@ -294,6 +303,9 @@ $consignment ->getDeliveryRemark() ->getDeliveryType() + // Physical properties (digital stamps or non-EU shipments) + ->getPhysicalProperties() + // Non-EU attributes ->getInvoice() ->getContents() @@ -311,7 +323,7 @@ This object is embedded in the MyParcelConsignment object for global shipments a ->setClassification(0111) // Example: 0111 = "Growing of cereals (except rice), leguminous crops and oil seeds" ->setCountry('NL') // Country of origin ->setDescription('Cereal grains') - ->setItemValue({"amount": 200, "currency": "EUR"}) // Must be array with amount and currency like in the example + ->setItemValue(["amount" => 200, "currency" => "EUR"]) // Must be array with amount and currency like in the example ->setWeight() // The total weight for these items in whole grams. Between 0 and 20000 grams. ->getAmount() diff --git a/Tests/SendConsignments/SendDigitalStampTest.php b/Tests/SendConsignments/SendDigitalStampTest.php new file mode 100644 index 00000000..80b3ee9e --- /dev/null +++ b/Tests/SendConsignments/SendDigitalStampTest.php @@ -0,0 +1,133 @@ +additionProvider() as $consignmentTest) { + + $myParcelCollection = new MyParcelCollection(); + + $consignment = (new MyParcelConsignmentRepository()) + ->setApiKey($consignmentTest['api_key']) + ->setPackageType(4) + ->setCountry($consignmentTest['cc']) + ->setPerson($consignmentTest['person']) + ->setCompany($consignmentTest['company']) + ->setFullStreet($consignmentTest['full_street']) + ->setPostalCode($consignmentTest['postal_code']) + ->setCity($consignmentTest['city']) + ->setEmail('your_email@test.nl') + ->setPhone($consignmentTest['phone']) + ->setPhysicalProperties($consignmentTest['physical_properties']); + + if (key_exists('package_type', $consignmentTest)) { + $consignment->setPackageType($consignmentTest['package_type']); + } + + if (key_exists('label_description', $consignmentTest)) { + $consignment->setLabelDescription($consignmentTest['label_description']); + } + + $myParcelCollection + ->addConsignment($consignment) + ->setLinkOfLabels(); + + $this->assertEquals(true, preg_match("#^https://api.myparcel.nl/pdfs#", $myParcelCollection->getLinkOfLabels()), 'Can\'t get link of PDF'); + + echo "\033[32mGenerated digital stamp shipment: \033[0m"; + print_r($myParcelCollection->getLinkOfLabels()); + echo "\n\033[0m"; + } + } + + /** + * Data for the test + * + * @return array + */ + public function additionProvider() + { + return [ + [ + 'api_key' => getenv('API_KEY'), + 'cc' => 'NL', + 'person' => 'Reindert', + 'company' => 'Big Sale BV', + 'full_street_test' => 'Plein 1940-45 3b', + 'full_street' => 'Plein 1940-45 3 b', + 'street' => 'Plein 1940-45', + 'number' => 3, + 'number_suffix' => 'b', + 'postal_code' => '2231JE', + 'city' => 'Rijnsburg', + 'phone' => '123456', + 'package_type' => 4, + 'label_description' => 112345, + 'physical_properties' => [ + 'weight' => 76 + ] + ], + [ + 'api_key' => getenv('API_KEY'), + 'cc' => 'NL', + 'person' => 'Reindert', + 'company' => 'Big Sale BV', + 'full_street_test' => 'Plein 1940-45 3b', + 'full_street' => 'Plein 1940-45 3 b', + 'street' => 'Plein 1940-45', + 'number' => 3, + 'number_suffix' => 'b', + 'postal_code' => '2231JE', + 'city' => 'Rijnsburg', + 'phone' => '123456', + 'package_type' => 4, + 'label_description' => 112345, + 'physical_properties' => [ + 'weight' => 1999 + ] + ], + [ + 'api_key' => getenv('API_KEY'), + 'cc' => 'NL', + 'person' => 'Reindert', + 'company' => 'Big Sale BV', + 'full_street_test' => 'Plein 1940-45 3b', + 'full_street' => 'Plein 1940-45 3 b', + 'street' => 'Plein 1940-45', + 'number' => 3, + 'number_suffix' => 'b', + 'postal_code' => '2231JE', + 'city' => 'Rijnsburg', + 'phone' => '123456', + 'package_type' => 4, + 'label_description' => 112345, + 'physical_properties' => [ + 'weight' => 0 + ] + ], + ]; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 09eec29d..e1934326 100755 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "myparcelnl/sdk", - "version": "v1.4.1", + "version": "v1.5.0", "description": "This package is designed to send and receive data from MyParcel by means of an API.", "homepage": "https://www.myparcel.nl", "tags": ["MyParcel", "My Parcel", "Post NL", "PostNL"], diff --git a/src/Helper/MyParcelCollection.php b/src/Helper/MyParcelCollection.php index a9714797..0f88bf7e 100644 --- a/src/Helper/MyParcelCollection.php +++ b/src/Helper/MyParcelCollection.php @@ -213,7 +213,6 @@ public function createConcepts() ->sendRequest(); $consignment->setMyParcelConsignmentId($request->getResult()['data']['ids'][0]['id']); - } } } diff --git a/src/Model/MyParcelConsignment.php b/src/Model/MyParcelConsignment.php index 8c1d4edd..7356d646 100755 --- a/src/Model/MyParcelConsignment.php +++ b/src/Model/MyParcelConsignment.php @@ -174,6 +174,11 @@ class MyParcelConsignment */ private $insurance = 0; + /** + * @var array + */ + private $physical_properties = []; + /** * @var int */ @@ -1007,6 +1012,28 @@ public function setInsurance($insurance) return $this; } + /** + * Required: Yes for non-EU shipments and digital stamps + * + * @param array $physical_properties + * + * @return MyParcelConsignment + */ + public function setPhysicalProperties($physical_properties) + { + $this->physical_properties = $physical_properties; + + return $this; + } + + /** + * @return array + */ + public function getPhysicalProperties() + { + return $this->physical_properties; + } + /** * @return integer */ @@ -1090,9 +1117,6 @@ public function addItem($item) return $this; } - - - /** * @return string */ diff --git a/src/Model/Repository/MyParcelConsignmentRepository.php b/src/Model/Repository/MyParcelConsignmentRepository.php index ea5ca652..f3c4089c 100755 --- a/src/Model/Repository/MyParcelConsignmentRepository.php +++ b/src/Model/Repository/MyParcelConsignmentRepository.php @@ -54,6 +54,7 @@ class MyParcelConsignmentRepository extends MyParcelConsignment const DEFAULT_DELIVERY_TYPE = self::DELIVERY_TYPE_STANDARD; const PACKAGE_TYPE_NORMAL = 1; + const PACKAGE_TYPE_DIGITAL_STAMP = 4; const DEFAULT_PACKAGE_TYPE = self::PACKAGE_TYPE_NORMAL; @@ -515,7 +516,8 @@ private function encodeExtraOptions() { ); $this ->encodePickup() - ->encodeInsurance(); + ->encodeInsurance() + ->encodePhysicalProperties(); } if ($this->isEuCountry()) { @@ -569,6 +571,18 @@ private function encodeInsurance() return $this; } + private function encodePhysicalProperties() + { + if (empty($this->getPhysicalProperties()) && $this->getPackageType() != self::PACKAGE_TYPE_DIGITAL_STAMP) { + return $this; + } + if ($this->getPackageType() == self::PACKAGE_TYPE_DIGITAL_STAMP && !isset($this->getPhysicalProperties()['weight'])) { + throw new \Exception('Weight in physical properties must be set for digital stamp shipments.'); + } + + $this->consignmentEncoded['physical_properties'] = $this->getPhysicalProperties(); + } + /** * @return $this * @throws \Exception @@ -604,9 +618,7 @@ private function encodeCdCountry() 'items' => $items, 'invoice' => $this->getLabelDescription(), ], - 'physical_properties' => [ - 'weight' => $this->getTotalWeight() - ] + 'physical_properties' => $this->getPhysicalProperties() + ['weight' => $this->getTotalWeight()], ] );