From 2d8b5f3d627fcc295048303f8f88c76c3fb10cf3 Mon Sep 17 00:00:00 2001 From: "Junjie(Jack) Chen" Date: Mon, 8 Apr 2024 14:35:16 -0400 Subject: [PATCH] Add refund function in insurance service (#308) --- CHANGELOG.md | 4 + .../easypost/service/InsuranceService.java | 13 ++ src/test/cassettes/insurance/refund.json | 186 ++++++++++++++++++ src/test/java/com/easypost/InsuranceTest.java | 20 ++ 4 files changed, 223 insertions(+) create mode 100644 src/test/cassettes/insurance/refund.json diff --git a/CHANGELOG.md b/CHANGELOG.md index de9de9ea6..6a1248686 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/com/easypost/service/InsuranceService.java b/src/main/java/com/easypost/service/InsuranceService.java index 375980752..2c54b87bd 100644 --- a/src/main/java/com/easypost/service/InsuranceService.java +++ b/src/main/java/com/easypost/service/InsuranceService.java @@ -66,6 +66,19 @@ public InsuranceCollection all(final Map 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. * diff --git a/src/test/cassettes/insurance/refund.json b/src/test/cassettes/insurance/refund.json new file mode 100644 index 000000000..9081787ac --- /dev/null +++ b/src/test/cassettes/insurance/refund.json @@ -0,0 +1,186 @@ +[ + { + "recordedAt": 1712599563, + "request": { + "body": "{\n \"insurance\": {\n \"carrier\": \"USPS\",\n \"amount\": \"100\",\n \"to_address\": {\n \"zip\": \"90277\",\n \"country\": \"US\",\n \"city\": \"Redondo Beach\",\n \"phone\": \"REDACTED\",\n \"name\": \"Elizabeth Swan\",\n \"street1\": \"179 N Harbor Dr\",\n \"state\": \"CA\",\n \"email\": \"test@example.com\"\n },\n \"from_address\": {\n \"zip\": \"94107\",\n \"country\": \"US\",\n \"city\": \"San Francisco\",\n \"phone\": \"REDACTED\",\n \"name\": \"Jack Sparrow\",\n \"street1\": \"388 Townsend St\",\n \"street2\": \"Apt 20\",\n \"state\": \"CA\",\n \"email\": \"test@example.com\"\n },\n \"tracking_code\": \"EZ1000000001\"\n }\n}", + "method": "POST", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ], + "Content-Type": [ + "application/json" + ] + }, + "uri": "https://api.easypost.com/v2/insurances" + }, + "response": { + "body": "{\n \"amount\": \"100.00000\",\n \"fee\": {\n \"amount\": \"1.00000\",\n \"refunded\": false,\n \"type\": \"InsuranceFee\",\n \"charged\": true,\n \"object\": \"Fee\"\n },\n \"created_at\": \"2024-04-08T18:06:03Z\",\n \"to_address\": {\n \"zip\": \"90277-2506\",\n \"country\": \"US\",\n \"city\": \"REDONDO BEACH\",\n \"created_at\": \"2024-04-08T18:06:03+00:00\",\n \"verifications\": {\n \"delivery\": {\n \"success\": true,\n \"details\": {\n \"latitude\": 33.8436,\n \"time_zone\": \"America/Los_Angeles\",\n \"longitude\": -118.39177\n },\n \"errors\": []\n },\n \"zip4\": {\n \"success\": true,\n \"details\": null,\n \"errors\": []\n }\n },\n \"mode\": \"test\",\n \"federal_tax_id\": null,\n \"state_tax_id\": null,\n \"carrier_facility\": null,\n \"residential\": false,\n \"updated_at\": \"2024-04-08T18:06:03+00:00\",\n \"phone\": \"REDACTED\",\n \"name\": \"ELIZABETH SWAN\",\n \"company\": null,\n \"street1\": \"179 N HARBOR DR\",\n \"id\": \"adr_a9b786ddf5d211ee8b49ac1f6bc53342\",\n \"street2\": \"\",\n \"state\": \"CA\",\n \"email\": \"TEST@EXAMPLE.COM\",\n \"object\": \"Address\"\n },\n \"shipment_id\": null,\n \"tracking_code\": \"EZ1000000001\",\n \"mode\": \"test\",\n \"reference\": null,\n \"updated_at\": \"2024-04-08T18:06:03Z\",\n \"provider\": \"easypost\",\n \"tracker\": {\n \"fees\": [],\n \"carrier_detail\": {\n \"est_delivery_date_local\": null,\n \"origin_location\": \"HOUSTON TX, 77001\",\n \"destination_tracking_location\": null,\n \"guaranteed_delivery_date\": null,\n \"est_delivery_time_local\": null,\n \"service\": \"First-Class Package Service\",\n \"container_type\": null,\n \"initial_delivery_attempt\": null,\n \"origin_tracking_location\": {\n \"zip\": \"77063\",\n \"country\": null,\n \"city\": \"HOUSTON\",\n \"state\": \"TX\",\n \"object\": \"TrackingLocation\"\n },\n \"destination_location\": \"CHARLESTON SC, 29401\",\n \"alternate_identifier\": null,\n \"object\": \"CarrierDetail\"\n },\n \"created_at\": \"2024-04-08T18:04:39Z\",\n \"weight\": null,\n \"tracking_details\": [\n {\n \"tracking_location\": {\n \"zip\": null,\n \"country\": null,\n \"city\": null,\n \"state\": null,\n \"object\": \"TrackingLocation\"\n },\n \"datetime\": \"2024-03-08T18:04:39Z\",\n \"description\": \"\",\n \"source\": \"USPS\",\n \"message\": \"Pre-Shipment Info Sent to USPS\",\n \"object\": \"TrackingDetail\",\n \"status\": \"pre_transit\",\n \"status_detail\": \"status_update\",\n \"carrier_code\": \"\"\n },\n {\n \"tracking_location\": {\n \"zip\": \"77063\",\n \"country\": null,\n \"city\": \"HOUSTON\",\n \"state\": \"TX\",\n \"object\": \"TrackingLocation\"\n },\n \"datetime\": \"2024-03-09T06:41:39Z\",\n \"description\": \"\",\n \"source\": \"USPS\",\n \"message\": \"Shipping Label Created\",\n \"object\": \"TrackingDetail\",\n \"status\": \"pre_transit\",\n \"status_detail\": \"status_update\",\n \"carrier_code\": \"\"\n }\n ],\n \"shipment_id\": null,\n \"tracking_code\": \"EZ1000000001\",\n \"status_detail\": \"status_update\",\n \"mode\": \"test\",\n \"finalized\": true,\n \"public_url\": \"https://track.easypost.com/djE6dHJrXzlmZjQwOGUyZDE0YjQ0M2RiN2E4ZjZiMDQ5ZWU5MTNl\",\n \"est_delivery_date\": \"2024-04-08T18:04:39Z\",\n \"carrier\": \"USPS\",\n \"is_return\": false,\n \"updated_at\": \"2024-04-08T18:04:39Z\",\n \"signed_by\": null,\n \"id\": \"trk_9ff408e2d14b443db7a8f6b049ee913e\",\n \"object\": \"Tracker\",\n \"status\": \"pre_transit\"\n },\n \"provider_id\": null,\n \"messages\": [],\n \"id\": \"ins_ac83a8f204cc4ae3a8c1d0770accf73f\",\n \"from_address\": {\n \"zip\": \"94107-1670\",\n \"country\": \"US\",\n \"city\": \"SAN FRANCISCO\",\n \"created_at\": \"2024-04-08T18:06:03+00:00\",\n \"verifications\": {\n \"delivery\": {\n \"success\": true,\n \"details\": {\n \"latitude\": 37.77551,\n \"time_zone\": \"America/Los_Angeles\",\n \"longitude\": -122.39697\n },\n \"errors\": []\n },\n \"zip4\": {\n \"success\": true,\n \"details\": null,\n \"errors\": []\n }\n },\n \"mode\": \"test\",\n \"federal_tax_id\": null,\n \"state_tax_id\": null,\n \"carrier_facility\": null,\n \"residential\": true,\n \"updated_at\": \"2024-04-08T18:06:03+00:00\",\n \"phone\": \"REDACTED\",\n \"name\": \"JACK SPARROW\",\n \"company\": null,\n \"street1\": \"388 TOWNSEND ST APT 20\",\n \"id\": \"adr_a9bcda99f5d211eea0db3cecef1b359e\",\n \"street2\": \"\",\n \"state\": \"CA\",\n \"email\": \"TEST@EXAMPLE.COM\",\n \"object\": \"Address\"\n },\n \"object\": \"Insurance\",\n \"status\": \"pending\"\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 201 Created" + ], + "content-length": [ + "3366" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb32nuq" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-backend": [ + "easypost" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-download-options": [ + "noopen" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "pragma": [ + "no-cache" + ], + "x-canary": [ + "direct" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-ep-request-uuid": [ + "0f86fc776614320be799ed5f008b1324" + ], + "x-proxied": [ + "extlb1nuq 60566a9ec2", + "intlb4nuq 39c21b8207" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.179790" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "location": [ + "/api/v2/insurances/ins_ac83a8f204cc4ae3a8c1d0770accf73f" + ], + "x-version-label": [ + "easypost-202404081732-cdcb81b7f6-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 201, + "message": "Created" + }, + "uri": "https://api.easypost.com/v2/insurances" + }, + "duration": 582 + }, + { + "recordedAt": 1712599564, + "request": { + "body": "", + "method": "POST", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ] + }, + "uri": "https://api.easypost.com/v2/insurances/ins_ac83a8f204cc4ae3a8c1d0770accf73f/refund" + }, + "response": { + "body": "{\n \"amount\": \"100.00000\",\n \"fee\": {\n \"amount\": \"1.00000\",\n \"refunded\": false,\n \"type\": \"InsuranceFee\",\n \"charged\": true,\n \"object\": \"Fee\"\n },\n \"created_at\": \"2024-04-08T18:06:03Z\",\n \"to_address\": {\n \"zip\": \"90277-2506\",\n \"country\": \"US\",\n \"city\": \"REDONDO BEACH\",\n \"created_at\": \"2024-04-08T18:06:03+00:00\",\n \"verifications\": {\n \"delivery\": {\n \"success\": true,\n \"details\": {\n \"latitude\": 33.8436,\n \"time_zone\": \"America/Los_Angeles\",\n \"longitude\": -118.39177\n },\n \"errors\": []\n },\n \"zip4\": {\n \"success\": true,\n \"details\": null,\n \"errors\": []\n }\n },\n \"mode\": \"test\",\n \"federal_tax_id\": null,\n \"state_tax_id\": null,\n \"carrier_facility\": null,\n \"residential\": false,\n \"updated_at\": \"2024-04-08T18:06:03+00:00\",\n \"phone\": \"REDACTED\",\n \"name\": \"ELIZABETH SWAN\",\n \"company\": null,\n \"street1\": \"179 N HARBOR DR\",\n \"id\": \"adr_a9b786ddf5d211ee8b49ac1f6bc53342\",\n \"street2\": null,\n \"state\": \"CA\",\n \"email\": \"TEST@EXAMPLE.COM\",\n \"object\": \"Address\"\n },\n \"shipment_id\": null,\n \"tracking_code\": \"EZ1000000001\",\n \"mode\": \"test\",\n \"reference\": null,\n \"updated_at\": \"2024-04-08T18:06:04Z\",\n \"provider\": \"easypost\",\n \"tracker\": {\n \"fees\": [],\n \"carrier_detail\": {\n \"est_delivery_date_local\": null,\n \"origin_location\": \"HOUSTON TX, 77001\",\n \"destination_tracking_location\": null,\n \"guaranteed_delivery_date\": null,\n \"est_delivery_time_local\": null,\n \"service\": \"First-Class Package Service\",\n \"container_type\": null,\n \"initial_delivery_attempt\": null,\n \"origin_tracking_location\": {\n \"zip\": \"77063\",\n \"country\": null,\n \"city\": \"HOUSTON\",\n \"state\": \"TX\",\n \"object\": \"TrackingLocation\"\n },\n \"destination_location\": \"CHARLESTON SC, 29401\",\n \"alternate_identifier\": null,\n \"object\": \"CarrierDetail\"\n },\n \"created_at\": \"2024-04-08T18:06:03Z\",\n \"weight\": null,\n \"tracking_details\": [\n {\n \"tracking_location\": {\n \"zip\": null,\n \"country\": null,\n \"city\": null,\n \"state\": null,\n \"object\": \"TrackingLocation\"\n },\n \"datetime\": \"2024-03-08T18:06:03Z\",\n \"description\": \"\",\n \"source\": \"USPS\",\n \"message\": \"Pre-Shipment Info Sent to USPS\",\n \"object\": \"TrackingDetail\",\n \"status\": \"pre_transit\",\n \"status_detail\": \"status_update\",\n \"carrier_code\": \"\"\n },\n {\n \"tracking_location\": {\n \"zip\": \"77063\",\n \"country\": null,\n \"city\": \"HOUSTON\",\n \"state\": \"TX\",\n \"object\": \"TrackingLocation\"\n },\n \"datetime\": \"2024-03-09T06:43:03Z\",\n \"description\": \"\",\n \"source\": \"USPS\",\n \"message\": \"Shipping Label Created\",\n \"object\": \"TrackingDetail\",\n \"status\": \"pre_transit\",\n \"status_detail\": \"status_update\",\n \"carrier_code\": \"\"\n }\n ],\n \"shipment_id\": null,\n \"tracking_code\": \"EZ1000000001\",\n \"status_detail\": \"status_update\",\n \"mode\": \"test\",\n \"finalized\": true,\n \"public_url\": \"https://track.easypost.com/djE6dHJrX2YyYmZjMGVjMDEyNTRjOWI5YzI1ZGMwNzdmNTM1OTE4\",\n \"est_delivery_date\": \"2024-04-08T18:06:03Z\",\n \"carrier\": \"USPS\",\n \"is_return\": false,\n \"updated_at\": \"2024-04-08T18:06:03Z\",\n \"signed_by\": null,\n \"id\": \"trk_f2bfc0ec01254c9b9c25dc077f535918\",\n \"object\": \"Tracker\",\n \"status\": \"pre_transit\"\n },\n \"provider_id\": null,\n \"messages\": [\n \"Insurance was cancelled by the user.\"\n ],\n \"id\": \"ins_ac83a8f204cc4ae3a8c1d0770accf73f\",\n \"from_address\": {\n \"zip\": \"94107-1670\",\n \"country\": \"US\",\n \"city\": \"SAN FRANCISCO\",\n \"created_at\": \"2024-04-08T18:06:03+00:00\",\n \"verifications\": {\n \"delivery\": {\n \"success\": true,\n \"details\": {\n \"latitude\": 37.77551,\n \"time_zone\": \"America/Los_Angeles\",\n \"longitude\": -122.39697\n },\n \"errors\": []\n },\n \"zip4\": {\n \"success\": true,\n \"details\": null,\n \"errors\": []\n }\n },\n \"mode\": \"test\",\n \"federal_tax_id\": null,\n \"state_tax_id\": null,\n \"carrier_facility\": null,\n \"residential\": true,\n \"updated_at\": \"2024-04-08T18:06:03+00:00\",\n \"phone\": \"REDACTED\",\n \"name\": \"JACK SPARROW\",\n \"company\": null,\n \"street1\": \"388 TOWNSEND ST APT 20\",\n \"id\": \"adr_a9bcda99f5d211eea0db3cecef1b359e\",\n \"street2\": null,\n \"state\": \"CA\",\n \"email\": \"TEST@EXAMPLE.COM\",\n \"object\": \"Address\"\n },\n \"object\": \"Insurance\",\n \"status\": \"cancelled\"\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 200 OK" + ], + "content-length": [ + "3410" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb43nuq" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-backend": [ + "easypost" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-download-options": [ + "noopen" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "pragma": [ + "no-cache" + ], + "x-canary": [ + "direct" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-ep-request-uuid": [ + "0f86fc7a6614320ce799ed60008b13d7" + ], + "x-proxied": [ + "extlb1nuq 60566a9ec2", + "intlb3nuq 39c21b8207" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.107833" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202404081732-cdcb81b7f6-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 200, + "message": "OK" + }, + "uri": "https://api.easypost.com/v2/insurances/ins_ac83a8f204cc4ae3a8c1d0770accf73f/refund" + }, + "duration": 500 + } +] \ No newline at end of file diff --git a/src/test/java/com/easypost/InsuranceTest.java b/src/test/java/com/easypost/InsuranceTest.java index 469ae4d61..5f6d783e0 100644 --- a/src/test/java/com/easypost/InsuranceTest.java +++ b/src/test/java/com/easypost/InsuranceTest.java @@ -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 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)); + } }