diff --git a/README.md b/README.md index af0f21b..68edf03 100644 --- a/README.md +++ b/README.md @@ -104,16 +104,19 @@ $parcel = new \Mvdnbrk\MyParcel\Resources\Parcel([ ]); ``` -Or you may use a method like `signature`, `onlyRecipient`, `returnToSender`, `ageCheck` and `labelDescription`. +Or you may use a method like `signature`, `onlyRecipient`, `returnToSender`, `ageCheck`, `insurance` and `labelDescription`. You may call any of these methods after constructing the parcel: ``` php $parcel->labelDescription('Your description.') ->ageCheck() + ->insurance(25000) ->onlyRecipient() ->returnToSender() ->signature(); ``` +**Note:** The insurance value should be in cents + **Mailbox package** diff --git a/src/Resources/Money.php b/src/Resources/Money.php new file mode 100644 index 0000000..cf16de2 --- /dev/null +++ b/src/Resources/Money.php @@ -0,0 +1,27 @@ +amount = $value; + } + + public function setCurrencyAttribute(string $value): void + { + $this->currency = $value; + } +} diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index 0f44330..befb136 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -85,6 +85,21 @@ public function ageCheck(): self return $this; } + public function insurance(int $cents, string $currency = 'EUR'): self + { + $this->options->insurance = new Money([ + 'amount' => $cents, + 'currency' => $currency + ]); + + if ($this->recipient->cc === 'NL') { + $this->onlyRecipient(); + $this->signature(); + } + + return $this; + } + /** * @param array|object $value */ diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index fc9cc45..8392722 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -13,6 +13,9 @@ class ShipmentOptions extends BaseResource /** @var int */ public $delivery_type; + /** @var Money|null */ + public $insurance; + /** @var string */ public $label_description; @@ -41,12 +44,13 @@ public function __construct(array $attributes = []) public function setDefaultOptions(): self { $this->age_check = false; + $this->insurance = null; $this->return = false; $this->signature = false; $this->large_format = false; + $this->only_recipient = false; $this->package_type = PackageType::PACKAGE; $this->delivery_type = DeliveryType::STANDARD; - $this->only_recipient = false; return $this; } @@ -61,14 +65,28 @@ public function setDescriptionAttribute(string $value): void $this->label_description = $value; } + public function setInsuranceAttribute($value): void + { + if ($value instanceof Money) { + $this->insurance = $value; + } + if (is_null($this->insurance)) { + $this->insurance = new Money($value); + } else { + $this->insurance->fill($value); + } + } + public function toArray(): array { return collect(parent::toArray()) + ->reject(function ($value, $key) { + return $key === 'insurance' && empty($value); + }) ->map(function ($value) { if (is_bool($value)) { return (int) $value; } - return $value; }) ->all(); diff --git a/tests/Feature/Endpoints/ShipmentsTest.php b/tests/Feature/Endpoints/ShipmentsTest.php index 1b86103..49812ea 100644 --- a/tests/Feature/Endpoints/ShipmentsTest.php +++ b/tests/Feature/Endpoints/ShipmentsTest.php @@ -80,6 +80,50 @@ public function create_a_new_shipment_concept_for_a_parcel() $this->assertTrue($this->cleanUp($shipment)); } + /** @test */ + public function create_a_new_shipment_concept_for_a_parcel_with_insurance() + { + $array = [ + 'reference_identifier' => 'test-123', + 'recipient' => [ + 'company' => 'Test Company B.V.', + 'first_name' => 'John', + 'last_name' => 'Doe', + 'email' => 'john@example.com', + 'phone' => '0101111111', + 'street' => 'Poststraat', + 'number' => '1', + 'number_suffix' => 'A', + 'postal_code' => '1234AA', + 'city' => 'Amsterdam', + 'region' => 'Noord-Holland', + 'cc' => 'NL', + ], + 'options' => [ + 'label_description' => 'Test label description', + 'large_format' => false, + 'only_recipient' => false, + 'package_type' => PackageType::PACKAGE, + 'return' => false, + 'signature' => true, + ], + ]; + + $parcel = new Parcel($array); + $parcel->insurance(500); + $shipment = $this->client->shipments->create($parcel); + + $this->assertInstanceOf(Shipment::class, $shipment); + $this->assertInstanceOf(ShipmentOptions::class, $shipment->options); + $this->assertNotNull($shipment->id); + $this->assertEquals(ShipmentStatus::CONCEPT, $shipment->status); + $this->assertEquals('John', $shipment->recipient->first_name); + $this->assertEquals('Doe', $shipment->recipient->last_name); + $this->assertEquals(500, $shipment->options->insurance->amount); + $this->assertEquals('EUR', $shipment->options->insurance->currency); + $this->assertTrue($this->cleanUp($shipment)); + } + /** @test */ public function create_a_shipment_with_invalid_data() { @@ -179,6 +223,29 @@ public function get_a_shipment_by_its_id() $this->assertTrue($this->cleanUp($shipment)); } + /** @test */ + public function get_a_shipment_by_its_id_with_insurance() + { + $array = [ + 'recipient' => $this->validRecipient(), + ]; + + $parcel = new Parcel($array); + $parcel->insurance(25000); + $concept = $this->client->shipments->concept($parcel); + + $shipment = $this->client->shipments->get($concept->id); + + $this->assertInstanceOf(Shipment::class, $shipment); + $this->assertNotNull($shipment->id); + $this->assertNotNull($shipment->created); + $this->assertNotNull($shipment->status); + $this->assertEquals(25000, $shipment->options->insurance->amount); + $this->assertEquals('EUR', $shipment->options->insurance->currency); + + $this->assertTrue($this->cleanUp($shipment)); + } + /** @test */ public function getting_a_shipment_with_an_invalid_id_should_throw_an_error() {