Skip to content

Commit

Permalink
Add refund function to insurance service (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchen293 authored Apr 8, 2024
1 parent 614e4ca commit 0a05ddf
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next Release

- Fix payment method funding and deletion failures due to undetermined payment method type
- Adds `refund` function in Insurance service for requesting a refund of standalone insurance.

## v7.2.0 (2024-01-22)

Expand Down
13 changes: 13 additions & 0 deletions src/services/insurance_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,17 @@ export default (easypostClient) =>

return this._retrieve(url);
}

/**
* Refund an {@link Insurance insurance} record by its ID.
* See {@link https://www.easypost.com/docs/api/node#refund-an-insurance EasyPost API Documentation} for more information.
* @param {string} id - The ID of the insurance to be refunded.
* @returns {Insurance} - The refunded insurance.
*/
static async refund(id) {
const url = `insurances/${id}/refund`;
const response = await easypostClient._post(url);

return this._convertToEasyPostObject(response.body);
}
};

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

13 changes: 13 additions & 0 deletions test/services/insurance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,17 @@ describe('Insurance Service', function () {
}
}
});

it('refunds a standalone insurance', async function () {
const insuranceData = Fixture.basicInsurance();
insuranceData.tracking_code = 'EZ1000000001';

const insurance = await this.client.Insurance.create(insuranceData);
const cancelledInsurance = await this.client.Insurance.refund(insurance.id);

expect(cancelledInsurance).to.be.an.instanceOf(Insurance);
expect(cancelledInsurance.id).to.match(/^ins_/);
expect(cancelledInsurance.status).to.equal('cancelled');
expect(cancelledInsurance.messages[0]).to.equal('Insurance was cancelled by the user.');
});
});
10 changes: 10 additions & 0 deletions types/Insurance/Insurance.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,14 @@ export declare class Insurance implements IInsurance {
insurances: Object,
pageSize?: number,
): Promise<{ insurances: Insurance[]; has_more: boolean }>;

/**
* Refund an Insurance by id.
*
* @see https://www.easypost.com/docs/api/node#refund-an-insurance
*
* @param insuranceId Unique, starts with "ins_"
* @returns {Promise<Insurance>} The refunded {@link Insurance}.
*/
static refund(insuranceId: string): Promise<Insurance>;
}

0 comments on commit 0a05ddf

Please sign in to comment.