Skip to content

Commit a460b31

Browse files
authored
Add Cash on Delivery (COD) delivery option (#48)
1 parent c16ad5a commit a460b31

5 files changed

Lines changed: 94 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,20 @@ $parcel = new \Mvdnbrk\MyParcel\Resources\Parcel([
115115
'description' => 'Order #123',
116116
'signature' => true,
117117
'only_recipient' => true,
118+
'cash_on_delivery' => 9.95,
118119
...
119120
],
120121
]);
121122
```
122123

123-
Or you may use a method like `signature()`, `onlyRecipient()` and `labelDescription()`.
124+
Or you may use a method like `signature()`, `onlyRecipient()`, `labelDescription()` and `cashOnDelivery()`.
124125
You may call any of these after constructing the parcel.
125126

126127
``` php
127128
$parcel->onlyRecipient()
128129
->signature()
129-
->labelDescription('Order #123');
130+
->labelDescription('Order #123')
131+
->cashOnDelivery(9.95);
130132
```
131133

132134
**Mailbox package**

src/Resources/Parcel.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ public function servicePoint(string $value)
147147
return $this;
148148
}
149149

150+
/**
151+
* Set the amount for "Cash On Delivery" in EUR.
152+
*
153+
* @param int|float $value
154+
* @return $this
155+
*/
156+
public function cashOnDelivery($value)
157+
{
158+
$this->options->setCashOnDelivery($value);
159+
160+
return $this;
161+
}
162+
150163
/**
151164
* Set the sender for this parcel.
152165
*

src/Resources/ShipmentOptions.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class ShipmentOptions extends BaseResource
2424
*/
2525
protected $service_point_id;
2626

27+
/**
28+
* The amount for "Cash On Delivery" in EUR.
29+
*
30+
* @var int|float
31+
*/
32+
protected $cash_on_delivery;
33+
2734
/**
2835
* The description that will appear on the shipment label.
2936
*
@@ -133,6 +140,17 @@ public function setServicePointIdAttribute($value)
133140
$this->service_point_id = $value;
134141
}
135142

143+
/**
144+
* Set the amount for "Cash On Delivery" in EUR.
145+
*
146+
* @param int|float $value
147+
* @return void
148+
*/
149+
public function setCashOnDelivery($value)
150+
{
151+
$this->cash_on_delivery = $value;
152+
}
153+
136154
/**
137155
* Convert the ShipmenOptions resource to an array.
138156
*
@@ -152,6 +170,12 @@ public function toArray()
152170
'input' => $this->service_point_id,
153171
]);
154172
})
173+
->when(! empty($this->cash_on_delivery), function ($collection) {
174+
return $collection->push([
175+
'key' => 'COD_CASH',
176+
'input' => $this->cash_on_delivery,
177+
]);
178+
})
155179
->when(! empty($this->label_description), function ($collection) {
156180
return $collection->push([
157181
'key' => 'REFERENCE',

tests/Unit/Resources/ParcelTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,37 @@ public function calling_the_service_point_method_returns_the_same_parcel_instanc
216216
$this->assertSame($parcel, $parcel->servicePoint('8004-NL-272403'));
217217
}
218218

219+
/** @test */
220+
public function it_can_set_an_amount_for_cash_on_delivery()
221+
{
222+
$parcel = new Parcel([
223+
'options' => [
224+
'cash_on_delivery' => 9.99,
225+
],
226+
]);
227+
228+
$this->assertEquals([
229+
'key' => 'COD_CASH',
230+
'input' => '9.99',
231+
], $parcel->options->toArray()[1]);
232+
233+
$parcel = new Parcel();
234+
$parcel->cashOnDelivery(100);
235+
236+
$this->assertEquals([
237+
'key' => 'COD_CASH',
238+
'input' => '100',
239+
], $parcel->options->toArray()[1]);
240+
}
241+
242+
/** @test */
243+
public function calling_the_cash_on_delivery_method_returns_the_same_parcel_instance()
244+
{
245+
$parcel = new Parcel();
246+
247+
$this->assertSame($parcel, $parcel->cashOnDelivery(100));
248+
}
249+
219250
/** @test */
220251
public function it_sets_the_pieces_to_a_default_value_when_pieces_is_empty()
221252
{

tests/Unit/Resources/ShipmentOptionsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,26 @@ public function to_array_with_mailbox_package()
135135

136136
$this->assertEquals(['key' => 'BP'], $array[0]);
137137
}
138+
139+
/** @test */
140+
public function to_array_with_cash_on_delivery()
141+
{
142+
$options = new ShipmentOptions([
143+
'cash_on_delivery' => 9.99,
144+
]);
145+
146+
$array = $options->toArray();
147+
148+
$this->assertIsArray($array);
149+
150+
$this->assertEquals([
151+
[
152+
'key' => 'DOOR',
153+
],
154+
[
155+
'key' => 'COD_CASH',
156+
'input' => '9.99',
157+
],
158+
], $array);
159+
}
138160
}

0 commit comments

Comments
 (0)