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

Add shipping option for insurance #43

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bea87fd
Added insurance
avido Nov 18, 2020
76124d6
Set currency for insurance
avido Nov 18, 2020
0823fd2
Added unit test for insurance
avido Nov 18, 2020
29bed45
Updated readme
avido Nov 18, 2020
0854014
Merge branch 'main' into avido/feature-shipping-option-insurance
mvdnbrk Nov 18, 2020
83db9f0
Formatting
mvdnbrk Nov 18, 2020
aa460b3
Merge branch 'main' into avido/feature-shipping-option-insurance
mvdnbrk Nov 18, 2020
75434e3
Review changes
avido Nov 18, 2020
b013f05
Updated readme
avido Nov 18, 2020
12229b3
Merge branch 'main' into avido/feature-shipping-option-insurance
avido Nov 18, 2020
3b6a1c9
Merge branch 'avido/feature-shipping-option-insurance' of github.com:…
avido Nov 18, 2020
072cef8
Update insurance attribute argument to array, updated default options
avido Nov 18, 2020
b41f41a
Set insurance empty array, refactored toArray to remove empty arrays
avido Nov 18, 2020
acef318
Cast response to array
avido Nov 19, 2020
20702a4
Added test for retrieval of shipment with insurance
avido Nov 19, 2020
0aa3251
Updated readme
avido Nov 19, 2020
573e7ab
Formatting
mvdnbrk Nov 19, 2020
0d2740d
Change `reject` method
mvdnbrk Nov 19, 2020
a5f2b5c
Formatting
mvdnbrk Nov 19, 2020
477c12a
Formatting
mvdnbrk Nov 20, 2020
82513d3
Added money resource
avido Nov 23, 2020
fa3eeca
Updated insurance related tests
avido Nov 23, 2020
ef72358
Restred Shipments endpoint
avido Nov 23, 2020
1aac64e
Updated insurance for shippingOptions
avido Nov 23, 2020
fc56301
Updated method 'insurance' for parcel resource
avido Nov 23, 2020
563433c
Updated insurance attribute
avido Nov 23, 2020
06ed09c
Fix wrong type for insurance in defaultOptions
avido Nov 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
mvdnbrk marked this conversation as resolved.
Show resolved Hide resolved
->returnToSender()
->signature();
```
**Note:** The insurance value should be in cents


**Mailbox package**

Expand Down
27 changes: 27 additions & 0 deletions src/Resources/Money.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Mvdnbrk\MyParcel\Resources;

class Money extends BaseResource
{
/** @var int */
protected $amount;

/** @var string */
protected $currency;

public function __construct($attributes)
{
parent::__construct($attributes);
}

public function setAmountAttribute(int $value): void
{
$this->amount = $value;
}

public function setCurrencyAttribute(string $value): void
{
$this->currency = $value;
}
}
15 changes: 15 additions & 0 deletions src/Resources/Parcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
mvdnbrk marked this conversation as resolved.
Show resolved Hide resolved
$this->onlyRecipient();
$this->signature();
}

return $this;
mvdnbrk marked this conversation as resolved.
Show resolved Hide resolved
mvdnbrk marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param array|object $value
*/
Expand Down
22 changes: 20 additions & 2 deletions src/Resources/ShipmentOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class ShipmentOptions extends BaseResource
/** @var int */
public $delivery_type;

/** @var Money|null */
public $insurance;

/** @var string */
public $label_description;

Expand Down Expand Up @@ -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;

mvdnbrk marked this conversation as resolved.
Show resolved Hide resolved
return $this;
}
Expand All @@ -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();
Expand Down
67 changes: 67 additions & 0 deletions tests/Feature/Endpoints/ShipmentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
avido marked this conversation as resolved.
Show resolved Hide resolved
$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()
{
Expand Down Expand Up @@ -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()
{
Expand Down