Skip to content

Commit

Permalink
Added refund APIs, updated docs, code coverage 💯 🎉
Browse files Browse the repository at this point in the history
Refund API integration
  • Loading branch information
Tejas H authored Aug 18, 2020
2 parents 5e825c9 + cc34109 commit f3232ce
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 32 deletions.
153 changes: 123 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

**Promise based Instamojo payment gateway integrator for NodeJS based projects**

![Coverage](https://img.shields.io/badge/Coverage-75%25-brightgreen.svg)
- No callback hell ✅
- Clean, readable code with async-await 💖

![Coverage](https://img.shields.io/badge/Code_Coverage-100%25-brightgreen.svg)

> If you face any problems using this package, please create an issue on this repo OR even better, create a PR! 🙂
## Installation

Expand All @@ -17,6 +22,9 @@ npm i instamojo-payment-nodejs // OR yarn add instamojo-payment-nodejs
1. [Get single payment request details](#Get-single-payment-request-details)
1. [Get single payment details](#Get-single-payment-details)
1. [Get all payment requests](#Get-all-payment-requests)
1. [Initiate refund](#Initiate-refund)
1. [Get all refund requests](#Get-all-refund-requests)
1. [Get single refund details with `refundId`](#Get-single-refund-details-with-refundId)

### **Setup keys**

Expand Down Expand Up @@ -234,34 +242,115 @@ const response = await Instamojo.getAllPaymentRequests();

<details><summary>Response from <b>getAllPaymentRequests</b></summary>

```json
{}
```

</details>

<br />

### **Initiate refund**

```js
const refundInstance = new Instamojo.RefundRequestOptions({
payment_id: "MOJO0816705N15845280",
type: "RFD", // Refer below section
body: "Reason for refund",
});

refundInstance.setOptionalRefundAmount(99);

const refundResponse = await Instamojo.initiateRefund(
refundInstance.getObject()
);
```

**Valid values for type parameter:**

- `RFD`: Duplicate/delayed payment.
- `TNR`: Product/service no longer available.
- `QFL`: Customer not satisfied.
- `QNR`: Product lost/damaged.
- `EWN`: Digital download issue.
- `TAN`: Event was canceled/changed.
- `PTH`: Problem not described above.

For more, refer: https://docs.instamojo.com/docs/creating-a-refund,

<details><summary>Response from <b>initiateRefund</b></summary>

```json
{
"success": true,
// List of payment requests
"payment_requests": [
"refund": {
"id": "C5c0751269",
"payment_id": "MOJO5a06005J21512197",
"status": "Refunded",
"type": "QFL",
"body": "Customer isn't satisfied with the quality",
"refund_amount": "2500.00",
"total_amount": "2500.00",
"created_at": "2015-12-07T11:01:37.640Z"
},
"success": true
}
```

</details>

<br />

### **Get all refund requests**

```js
const response = await Instamojo.getAllRefunds();
```

<details><summary>Response from <b>getAllRefunds</b></summary>

```json
{
"refunds": [
{
"id": PAYMENT_REQUEST_ID,
"phone": null,
"email": "",
"buyer_name": "",
"amount": "20.00",
"purpose": "Product name",
"expires_at": null,
"status": "Pending",
"send_sms": false,
"send_email": false,
"sms_status": null,
"email_status": null,
"shorturl": null,
"longurl": "https://test.instamojo.com/@USER/PAYMENT_REQUEST_ID",
"redirect_url": "",
"webhook": "",
"allow_repeated_payments": false,
"customer_id": null,
"created_at": "2020-08-16T15:46:42.970983Z",
"modified_at": "2020-08-16T15:46:42.971001Z"
"id": "C5c0751269",
"payment_id": "MOJO5a06005J21512197",
"status": "Refunded",
"type": "QFL",
"body": "Customer isn't satisfied with the quality",
"refund_amount": "2500.00",
"total_amount": "2500.00",
"created_at": "2015-12-07T11:01:37.640Z"
}
]
],
"success": true
}
```

</details>

<br />

### **Get single refund details with refundId**

```js
const response = await Instamojo.getRefundDetails("C5c0751272");
```

<details><summary>Response from <b>getRefundDetails</b></summary>

```json
{
"refund": {
"id": "C5c0751272",
"payment_id": "MOJO5a06005J21512197",
"status": "Refunded",
"type": "QFL",
"body": "Customer isn't satisfied with the quality",
"refund_amount": "2500.00",
"total_amount": "2500.00",
"created_at": "2015-12-07T11:04:09.500Z"
},
"success": true
}
```

Expand All @@ -277,16 +366,20 @@ const response = await Instamojo.getAllPaymentRequests();

1. ✅ Implement Sandbox mode for developers
1. ✅ Create a payment request
1. 📈 Write easy-to-follow docs for package consumers
1. Write an easy-to-follow docs for package consumers
1. ✅ Get Single payment request details
1. ✅ Get Single payment details
1. ✅ Get all payment requests
1. Get all payments
1. Initiate refund
1. Get refund details
1. Get all refunds
1. ✅ Get all payments

1. Refund

1. ✅ Initiate refund
1. ✅ Get all refund requests
1. ✅ Get single refund details with refundId

1. ✅ Reach code test coverage threshold 60%
1. ✅ Reach code test coverage threshold 100%

## Far

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "instamojo-payment-nodejs",
"version": "2.2.0-rc",
"version": "2.3.0",
"description": "Promise based Instamojo payment gateway integration for NodeJS",
"main": "main.js",
"scripts": {
Expand Down
51 changes: 51 additions & 0 deletions src/Instamojo.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,53 @@ const getAllPaymentLinksCreatedOnInstamojo = async () => {
}
};

class RefundRequestOptions {
constructor({ payment_id, type, body }) {
this.payment_id = payment_id;
this.type = type;
this.body = body;
}
setOptionalRefundAmount(refundAmount) {
this.refund_amount = refundAmount;
}

getObject() {
return {
payment_id: this.payment_id,
type: this.type,
body: this.body,
refund_amount: this.refund_amount,
};
}
}

const initiateRefund = async (refundRequest) => {
try {
const response = await axios.post(ENDPOINT.refunds, refundRequest);
return response.data;
} catch (error) {
return error.response;
}
};

const getAllRefunds = async () => {
try {
const response = await axios.get(ENDPOINT.refunds);
return response.data;
} catch (error) {
return error.response;
}
};

const getRefundDetails = async (refundId) => {
try {
const response = await axios.get(`${ENDPOINT.refunds}/${refundId}`);
return response.data;
} catch (error) {
return error.response;
}
};

module.exports = {
isSandboxMode,
setKeys,
Expand All @@ -113,4 +160,8 @@ module.exports = {
getOnePayedPaymentDetails,
getAllPaymentRequests,
getAllPaymentLinksCreatedOnInstamojo,
RefundRequestOptions,
initiateRefund,
getAllRefunds,
getRefundDetails,
};
Loading

0 comments on commit f3232ce

Please sign in to comment.