Skip to content

Commit cc64c57

Browse files
authored
Merge pull request #199 from EasyPost/carbon_offset
Add ability to create carbon offset shipment
2 parents 1eb8708 + 86eba85 commit cc64c57

40 files changed

+2317
-1702
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
## Next Release
44

55
- Adds `validateWebhook` function that returns your webhook or raises an error if there is a `webhookSecret` mismatch
6+
- Adds ability to create a shipment with carbon offset
7+
- Adds ability to buy a shipment with carbon offset
8+
- Adds ability to one-call-buy a shipment with carbon offset
9+
- Adds ability to rerate a shipment with carbon offset
610
- Fixes a bug that required the values of `verify` and `verify_strict` params on an Address creation call to be arrays since passing `true` as a boolean is also valid (passing an array will continue to work but is no longer required)
711

812
## v5.4.0 (2022-07-18)

lib/EasyPost/CarbonOffset.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace EasyPost;
4+
5+
/**
6+
* @package EasyPost
7+
* @property string $currency
8+
* @property int $grams
9+
* @property string $price
10+
* @property string $object
11+
*/
12+
class CarbonOffset extends EasypostResource
13+
{
14+
}

lib/EasyPost/Rate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @property int $delivery_days
2121
* @property string $delivery_date
2222
* @property bool $delivery_date_guaranteed
23+
* @property CarbonOffset $carbon_offset
2324
* @property string $created_at
2425
* @property string $updated_at
2526
*/

lib/EasyPost/Shipment.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,36 @@ public static function all($params = null, $apiKey = null)
6565
* Create a shipment.
6666
*
6767
* @param mixed $params
68-
* @param string $apiKey
68+
* @param string|null $apiKey
69+
* @param boolean $withCarbonOffset
6970
* @return mixed
7071
*/
71-
public static function create($params = null, $apiKey = null)
72+
public static function create($params = null, $apiKey = null, $withCarbonOffset = false)
7273
{
7374
if (!isset($params['shipment']) || !is_array($params['shipment'])) {
7475
$clone = $params;
7576
unset($params);
7677
$params['shipment'] = $clone;
7778
}
7879

80+
$params['carbon_offset'] = $withCarbonOffset;
81+
7982
return self::createResource(get_class(), $params, $apiKey);
8083
}
8184

8285
/**
8386
* Re-rate a shipment.
8487
*
8588
* @param mixed $params
89+
* @param boolean $withCarbonOffset
8690
* @return $this
8791
* @throws \EasyPost\Error
8892
*/
89-
public function regenerate_rates($params = null)
93+
public function regenerate_rates($params = null, $withCarbonOffset = false)
9094
{
9195
$requestor = new Requestor($this->_apiKey);
9296
$url = $this->instanceUrl() . '/rerate';
97+
$params['carbon_offset'] = $withCarbonOffset;
9398
list($response, $apiKey) = $requestor->request('post', $url, $params);
9499
$this->refreshFrom($response, $apiKey, true);
95100

@@ -115,10 +120,11 @@ public function get_smartrates()
115120
* Buy a shipment.
116121
*
117122
* @param mixed $params
123+
* @param boolean $withCarbonOffset
118124
* @return $this
119125
* @throws \EasyPost\Error
120126
*/
121-
public function buy($params = null)
127+
public function buy($params = null, $withCarbonOffset = false)
122128
{
123129
$requestor = new Requestor($this->_apiKey);
124130
$url = $this->instanceUrl() . '/buy';
@@ -129,6 +135,7 @@ public function buy($params = null)
129135
$params['rate'] = $clone;
130136
}
131137

138+
$params['carbon_offset'] = $withCarbonOffset;
132139
list($response, $apiKey) = $requestor->request('post', $url, $params);
133140
$this->refreshFrom($response, $apiKey, true);
134141

lib/easypost.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
require(dirname(__FILE__) . '/EasyPost/Address.php');
2525
require(dirname(__FILE__) . '/EasyPost/Batch.php');
2626
require(dirname(__FILE__) . '/EasyPost/Brand.php');
27+
require(dirname(__FILE__) . '/EasyPost/CarbonOffset.php');
2728
require(dirname(__FILE__) . '/EasyPost/CarrierAccount.php');
2829
require(dirname(__FILE__) . '/EasyPost/CarrierDetail.php');
2930
require(dirname(__FILE__) . '/EasyPost/CreditCard.php');

test/EasyPost/Fixture.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public static function one_call_buy_shipment()
180180
// USPS only does "next-day" pickups including Saturday but not Sunday or Holidays.
181181
public static function basic_pickup()
182182
{
183-
$pickupDate = '2022-05-16';
183+
$pickupDate = '2022-07-29';
184184

185185
return [
186186
'address' => self::basic_address(),
@@ -333,4 +333,47 @@ public static function webhookBody()
333333

334334
return utf8_encode(json_encode($data));
335335
}
336+
337+
public static function carbonOffsetShipment()
338+
{
339+
return [
340+
'to_address' => [
341+
'name' => 'Dr. Steve Brule',
342+
'street1' => '179 N Harbor Dr',
343+
'city' => 'Redondo Beach',
344+
'state' => 'CA',
345+
'zip' => '90277',
346+
'country' => 'US',
347+
'phone' => '8573875756',
348+
'email' => 'dr_steve_brule@gmail.com',
349+
],
350+
'from_address' => [
351+
'name' => 'EasyPost',
352+
'street1' => '417 Montgomery Street',
353+
'street2' => '5th Floor',
354+
'city' => 'San Francisco',
355+
'state' => 'CA',
356+
'zip' => '94104',
357+
'country' => 'US',
358+
'phone' => '4153334445',
359+
'email' => 'support@easypost.com',
360+
],
361+
'parcel' => [
362+
'length' => '20.2',
363+
'width' => '10.9',
364+
'height' => '5',
365+
'weight' => '65.9',
366+
],
367+
];
368+
}
369+
370+
public static function carbon_offset_shipment_one_call_buy()
371+
{
372+
$carbonOffsetShipment = Fixture::carbonOffsetShipment();
373+
$carbonOffsetShipment['service'] = 'Priority';
374+
$carbonOffsetShipment['carrier_accounts'] = Fixture::usps_carrier_account_id();
375+
$carbonOffsetShipment['carrier'] = Fixture::usps();
376+
377+
return $carbonOffsetShipment;
378+
}
336379
}

test/EasyPost/ShipmentTest.php

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use EasyPost\Test\Fixture;
1111
use VCR\VCR;
1212

13+
use function PHPUnit\Framework\assertTrue;
14+
1315
EasyPost::setApiKey(getenv('EASYPOST_TEST_API_KEY'));
1416

1517
class ShipmentTest extends \PHPUnit\Framework\TestCase
@@ -332,9 +334,9 @@ public function testLowestSmartrate()
332334
$shipment = Shipment::create(Fixture::full_shipment());
333335

334336
// Test lowest rate with no filters
335-
$lowestRate = $shipment->lowest_smartrate(1, 'percentile_90');
336-
$this->assertEquals('Priority', $lowestRate['service']);
337-
$this->assertEquals(7.37, $lowestRate['rate']);
337+
$lowestRate = $shipment->lowest_smartrate(2, 'percentile_90');
338+
$this->assertEquals('First', $lowestRate['service']);
339+
$this->assertEquals(5.49, $lowestRate['rate']);
338340
$this->assertEquals('USPS', $lowestRate['carrier']);
339341

340342
// Test lowest smartrate with invalid filters (should error due to strict delivery_days)
@@ -365,9 +367,9 @@ public function testGetLowestSmartrate()
365367
$smartrates = $shipment->get_smartrates();
366368

367369
// Test lowest smartrate with valid filters
368-
$lowestSmartrate = Shipment::get_lowest_smartrate($smartrates, 1, 'percentile_90');
369-
$this->assertEquals('Priority', $lowestSmartrate['service']);
370-
$this->assertEquals(7.37, $lowestSmartrate['rate']);
370+
$lowestSmartrate = Shipment::get_lowest_smartrate($smartrates, 2, 'percentile_90');
371+
$this->assertEquals('First', $lowestSmartrate['service']);
372+
$this->assertEquals(5.49, $lowestSmartrate['rate']);
371373
$this->assertEquals('USPS', $lowestSmartrate['carrier']);
372374

373375
// Test lowest smartrate with invalid filters (should error due to strict delivery_days)
@@ -409,4 +411,84 @@ public function testGenerateForm()
409411
$this->assertEquals($formType, $form->form_type);
410412
$this->assertNotNull($form->form_url);
411413
}
414+
415+
/**
416+
* Tests creating a carbon offset shipment.
417+
*
418+
*/
419+
public function testCreateCarbonOffsetShipment()
420+
{
421+
VCR::insertCassette('shipments/createCarbonOffsetShipment.yml');
422+
423+
$shipment = Shipment::create(Fixture::carbonOffsetShipment(), null, true);
424+
425+
$this->assertInstanceOf('\EasyPost\Shipment', $shipment);
426+
427+
foreach ($shipment->rates as $rate) {
428+
$this->assertNotNull($rate->carbon_offset);
429+
}
430+
}
431+
432+
/**
433+
* Tests buying a carbon offset shipment.
434+
*
435+
*/
436+
public function testBuyCarbonOffsetShipment()
437+
{
438+
VCR::insertCassette('shipments/buyCarbonOffsetShipment.yml');
439+
440+
$shipment = Shipment::create(Fixture::carbonOffsetShipment());
441+
442+
$shipment->buy(
443+
[
444+
'rate' => $shipment->lowest_rate(),
445+
],
446+
true,
447+
);
448+
449+
$this->assertInstanceOf('\EasyPost\Shipment', $shipment);
450+
451+
$foundCarbonOffset = false;
452+
453+
foreach ($shipment->fees as $fee) {
454+
if ($fee->type == 'CarbonOffsetFee') {
455+
$foundCarbonOffset = true;
456+
}
457+
}
458+
459+
$this->assertTrue($foundCarbonOffset);
460+
}
461+
462+
/**
463+
* Tests one call buy a carbon offset shipment.
464+
*
465+
*/
466+
public function testOneCallBuyCarbonOffsetShipment()
467+
{
468+
VCR::insertCassette('shipments/oneCallBuyCarbonOffsetShipment.yml');
469+
470+
$shipment = Shipment::create(Fixture::carbon_offset_shipment_one_call_buy(), null, true);
471+
472+
$this->assertInstanceOf('\EasyPost\Shipment', $shipment);
473+
474+
foreach ($shipment->rates as $rate) {
475+
$this->assertNotNull($rate->carbon_offset);
476+
}
477+
}
478+
479+
/**
480+
* Tests rerate a shipment with carbon offset.
481+
*
482+
*/
483+
public function testRerateShipmentWithCarbonOffset()
484+
{
485+
VCR::insertCassette('shipments/rerateCarbonOffsetShipment.yml');
486+
487+
$shipment = Shipment::create(Fixture::carbon_offset_shipment_one_call_buy());
488+
489+
$newCarbonOffset = $shipment->regenerate_rates(null, true);
490+
foreach ($newCarbonOffset->rates as $rate) {
491+
$this->assertNotNull($rate->carbon_offset);
492+
}
493+
}
412494
}

test/cassettes/batches/addRemoveShipment.yml

Lines changed: 103 additions & 103 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cassettes/errors/errors.yml

Lines changed: 27 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)