Skip to content

Commit

Permalink
Add refund function in insurance service (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchen293 authored Apr 8, 2024
1 parent 274f207 commit 2d8b5f3
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 0 deletions.
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 `refund` function in Insurance service for requesting a refund for a standalone insurance

## v7.1.1 (2024-03-21)

- Fix `EasyPostTimeInTransitData` class and `easypostTimeInTransitData` property of `EstimatedDeliveryDate` class being publicly inaccessible
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/easypost/service/InsuranceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ public InsuranceCollection all(final Map<String, Object> params) throws EasyPost
return Requestor.request(RequestMethod.GET, endpoint, params, InsuranceCollection.class, client);
}

/**
* Refund an Insurance from the API.
*
* @param id The ID of the Insurance to refund.
* @return Insurance object
* @throws EasyPostException when the request fails.
*/
public Insurance refund(final String id) throws EasyPostException {
String endpoint = String.format("insurances/%s/refund", id);

return Requestor.request(RequestMethod.POST, endpoint, null, Insurance.class, client);
}

/**
* Get the next page of an InsuranceCollection.
*
Expand Down
186 changes: 186 additions & 0 deletions src/test/cassettes/insurance/refund.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/test/java/com/easypost/InsuranceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,24 @@ public void testGetNextPage() throws EasyPostException {
fail();
}
}

/**
* Test refunding an insurance.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testRefundInsurance() throws EasyPostException {
vcr.setUpTest("refund");

HashMap<String, Object> params = Fixtures.basicInsurance();
params.put("tracking_code", "EZ1000000001");
Insurance insurance = vcr.client.insurance.create(params);
Insurance cancelledInsurance = vcr.client.insurance.refund(insurance.getId());

assertInstanceOf(Insurance.class, cancelledInsurance);
assertTrue(insurance.getId().startsWith("ins_"));
assertEquals("cancelled", cancelledInsurance.getStatus());
assertEquals("Insurance was cancelled by the user.", cancelledInsurance.getMessages().get(0));
}
}

0 comments on commit 2d8b5f3

Please sign in to comment.