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 Claims service #344

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Next Release

- Adds new `Claim` service for filing claims on EasyPost shipments and insurances

## v7.3.0 (2024-07-12)

- Adds new `shipment.recommendShipDate`, `smartrate.recommendShipDate`, and `smartrate.estimateDeliveryDate` functions
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"squizlabs/php_codesniffer": "^3.7",
"roave/security-advisories": "dev-latest",
"rregeer/phpunit-coverage-check": "^0.3.1",
"phpstan/phpstan": "^1.10"
"phpstan/phpstan": "^1.11"
},
"scripts": {
"coverage": "XDEBUG_MODE=coverage ./bin/phpunit --coverage-html clover.html --coverage-clover build/logs/clover.xml && ./bin/coverage-check build/logs/clover.xml 85 --only-percentage",
Expand Down
2 changes: 1 addition & 1 deletion examples
Submodule examples updated 225 files
29 changes: 29 additions & 0 deletions lib/EasyPost/Claim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace EasyPost;

/**
* @package EasyPost
* @property string $id
* @property string $approved_amount
* @property string[] $attachments
* @property string $check_delivery_address
* @property string $contact_email
* @property string $description
* @property object $history
* @property string $insurance_amount
* @property string $insurance_id
* @property string $payment_method
* @property string $recipient_name
* @property string $requested_amount
* @property string $salvage_value
* @property string $shipment_id
* @property string $status
* @property string $status_detail
* @property string $status_timestamp
* @property string $tracking_code
* @property string $type
*/
class Claim extends EasyPostObject
{
}
3 changes: 3 additions & 0 deletions lib/EasyPost/EasyPostClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use EasyPost\Service\BillingService;
use EasyPost\Service\CarrierAccountService;
use EasyPost\Service\CarrierMetadataService;
use EasyPost\Service\ClaimService;
use EasyPost\Service\CustomsInfoService;
use EasyPost\Service\CustomsItemService;
use EasyPost\Service\EndShipperService;
Expand Down Expand Up @@ -47,6 +48,7 @@
* @property BillingService $billing
* @property CarrierAccountService $carrierAccount
* @property CarrierMetadataService $carrierMetadata
* @property ClaimService $claim
* @property CustomsInfoService $customsInfo
* @property CustomsItemService $customsItem
* @property EndShipperService $endShipper
Expand Down Expand Up @@ -119,6 +121,7 @@ public function __get(string $serviceName)
'billing' => BillingService::class,
'carrierAccount' => CarrierAccountService::class,
'carrierMetadata' => CarrierMetadataService::class,
'claim' => ClaimService::class,
'customsInfo' => CustomsInfoService::class,
'customsItem' => CustomsItemService::class,
'endShipper' => EndShipperService::class,
Expand Down
70 changes: 70 additions & 0 deletions lib/EasyPost/Service/ClaimService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace EasyPost\Service;

use EasyPost\Http\Requestor;
use EasyPost\Util\InternalUtil;

/**
* Claim service containing all the logic to make API calls.
*/
class ClaimService extends BaseService
{
/**
* Create an claim object.
*
* @param mixed $params
* @return mixed
*/
public function create(mixed $params = null): mixed
{
return self::createResource(self::serviceModelClassName(self::class), $params);
}

/**
* Retrieve an claim object.
*
* @param string $id
* @return mixed
*/
public function retrieve(string $id): mixed
{
return self::retrieveResource(self::serviceModelClassName(self::class), $id);
}

/**
* Retrieve all claim objects.
*
* @param mixed $params
* @return mixed
*/
public function all(mixed $params = null): mixed
{
return self::allResources(self::serviceModelClassName(self::class), $params);
}

/**
* Retrieve the next page of claim collection.
*
* @param mixed $claims
* @param int|null $pageSize
* @return mixed
*/
public function getNextPage(mixed $claims, ?int $pageSize = null): mixed
{
return $this->getNextPageResources(self::serviceModelClassName(self::class), $claims, $pageSize);
}

/**
* Cancel a claim object.
*
* @param string $id
* @return mixed
*/
public function cancel(string $id): mixed
{
$response = Requestor::request($this->client, 'post', "/claims/{$id}/cancel");

return InternalUtil::convertToEasyPostObject($this->client, $response);
}
}
2 changes: 2 additions & 0 deletions lib/EasyPost/Util/InternalUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use EasyPost\Brand;
use EasyPost\CarrierAccount;
use EasyPost\CarrierDetail;
use EasyPost\Claim;
use EasyPost\Constant\Constants;
use EasyPost\CustomsInfo;
use EasyPost\CustomsItem;
Expand Down Expand Up @@ -43,6 +44,7 @@
'Brand' => Brand::class,
'CarrierAccount' => CarrierAccount::class,
'CarrierDetail' => CarrierDetail::class,
'Claim' => Claim::class,
'CustomsInfo' => CustomsInfo::class,
'CustomsItem' => CustomsItem::class,
'EndShipper' => EndShipper::class,
Expand Down
2 changes: 2 additions & 0 deletions lib/easypost.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
require_once(dirname(__FILE__) . '/EasyPost/Brand.php');
require_once(dirname(__FILE__) . '/EasyPost/CarrierAccount.php');
require_once(dirname(__FILE__) . '/EasyPost/CarrierDetail.php');
require_once(dirname(__FILE__) . '/EasyPost/Claim.php');
require_once(dirname(__FILE__) . '/EasyPost/CustomsInfo.php');
require_once(dirname(__FILE__) . '/EasyPost/CustomsItem.php');
require_once(dirname(__FILE__) . '/EasyPost/EndShipper.php');
Expand Down Expand Up @@ -96,6 +97,7 @@
require_once(dirname(__FILE__) . '/EasyPost/Service/BatchService.php');
require_once(dirname(__FILE__) . '/EasyPost/Service/BillingService.php');
require_once(dirname(__FILE__) . '/EasyPost/Service/CarrierAccountService.php');
require_once(dirname(__FILE__) . '/EasyPost/Service/ClaimService.php');
require_once(dirname(__FILE__) . '/EasyPost/Service/CustomsInfoService.php');
require_once(dirname(__FILE__) . '/EasyPost/Service/CustomsItemService.php');
require_once(dirname(__FILE__) . '/EasyPost/Service/EndShipperService.php');
Expand Down
154 changes: 154 additions & 0 deletions test/EasyPost/ClaimTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace EasyPost\Test;

use EasyPost\EasyPostClient;
use EasyPost\Exception\General\EndOfPaginationException;
use EasyPost\Claim;
use Exception;

class ClaimTest extends \PHPUnit\Framework\TestCase
{
private static EasyPostClient $client;

/**
* Setup the testing environment for this file.
*/
public static function setUpBeforeClass(): void
{
TestUtil::setupVcrTests();
self::$client = new EasyPostClient(getenv('EASYPOST_TEST_API_KEY'));
}

/**
* Cleanup the testing environment once finished.
*/
public static function tearDownAfterClass(): void
{
TestUtil::teardownVcrTests();
}

/**
* Helper method to create and purchase an insured shipment.
*
* @param string $amount The amount of insurance for the shipment.
* @return \EasyPost\Shipment The purchased shipment object.
*/
private static function createAndBuyShipment(string $amount): \EasyPost\Shipment
{
$shipment = self::$client->shipment->create(Fixture::fullShipment());
return self::$client->shipment->buy(
$shipment->id,
[
'rate' => $shipment->lowestRate(),
'insurance' => $amount,
]
);
}


/**
* Test creating an claim object.
*/
public function testCreate(): void
{
TestUtil::setupCassette('claim/create.yml');
$amount = '100';
$purchasedShipment = self::createAndBuyShipment($amount);

$claimData = Fixture::basicClaimData();
$claimData['tracking_code'] = $purchasedShipment->tracking_code;
$claimData['amount'] = $amount;

$claim = self::$client->claim->create($claimData);

$this->assertInstanceOf(Claim::class, $claim);
$this->assertStringMatchesFormat('clm_%s', $claim->id);
}

/**
* Test retrieving an claim object.
*/
public function testRetrieve(): void
{
TestUtil::setupCassette('claim/retrieve.yml');
$amount = '100';
$purchasedShipment = self::createAndBuyShipment($amount);

$claimData = Fixture::basicClaimData();
$claimData['tracking_code'] = $purchasedShipment->tracking_code;
$claimData['amount'] = $amount;

$claim = self::$client->claim->create($claimData);
$retrievedClaim = self::$client->claim->retrieve($claim->id);

$this->assertInstanceOf(Claim::class, $claim);
$this->assertStringMatchesFormat('clm_%s', $claim->id);
$this->assertEquals($claim->id, $retrievedClaim->id);
}

/**
* Test retrieving all claims.
*/
public function testAll(): void
{
TestUtil::setupCassette('claim/all.yml');

$claim = self::$client->claim->all([
'page_size' => Fixture::pageSize(),
]);

$claimArray = $claim['claims'];

$this->assertLessThanOrEqual($claimArray, Fixture::pageSize());
$this->assertNotNull($claim['has_more']);
$this->assertContainsOnlyInstancesOf(Claim::class, $claimArray);
}

/**
* Test retrieving next page.
*/
public function testGetNextPage(): void
{
TestUtil::setupCassette('claim/getNextPage.yml');

try {
$claims = self::$client->claim->all([
'page_size' => Fixture::pageSize(),
]);
$nextPage = self::$client->claim->getNextPage($claims, Fixture::pageSize());

$firstIdOfFirstPage = $claims['claims'][0]->id;
$secondIdOfSecondPage = $nextPage['claims'][0]->id;

$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage);
$this->assertNotNull($nextPage['_params']);
} catch (EndOfPaginationException $error) {
// There's no second page, that's not a failure
$this->assertTrue(true);
} catch (Exception $error) {
throw $error;
}
}

/**
* Test cancelling a filed claim.
*/
public function testRefund(): void
{
TestUtil::setupCassette('claim/cancel.yml');
$amount = '100';
$purchasedShipment = self::createAndBuyShipment($amount);

$claimData = Fixture::basicClaimData();
$claimData['tracking_code'] = $purchasedShipment->tracking_code;
$claimData['amount'] = $amount;

$claim = self::$client->claim->create($claimData);
$cancelledClaim = self::$client->claim->cancel($claim->id);

$this->assertInstanceOf(Claim::class, $cancelledClaim);
$this->assertStringMatchesFormat('clm_%s', $cancelledClaim->id);
$this->assertEquals('cancelled', $cancelledClaim->status);
}
}
14 changes: 14 additions & 0 deletions test/EasyPost/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,27 @@ public static function rmaFormOtions(): array
return self::readFixtureData()['form_options']['rma'];
}

/**
* @return string
*/
public static function plannedShipDate(): string
{
return '2024-07-16';
}

/**
* @return string
*/
public static function desiredDeliveryDate(): string
{
return '2024-07-16';
}

/**
* @return array<mixed>
*/
public static function basicClaimData(): array
{
return self::readFixtureData()['claims']['basic'];
}
}
Loading
Loading