Skip to content

Commit

Permalink
Merge pull request #235 from razorpay/item_sdk
Browse files Browse the repository at this point in the history
Node-sdk Item
  • Loading branch information
ankitdas13 authored Nov 10, 2021
2 parents bc09f7d + b4f9cd6 commit e199f65
Show file tree
Hide file tree
Showing 7 changed files with 515 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Razorpay Node SDK

[![npm](https://img.shields.io/npm/v/razorpay.svg?maxAge=2592000?style=flat-square)](https://www.npmjs.com/package/razorpay)
[![Build Status](https://travis-ci.org/razorpay/razorpay-node.svg?branch=master)](https://travis-ci.org/razorpay/razorpay-node)

Official nodejs library for [Razorpay API](https://docs.razorpay.com/docs/payments).

Expand Down
3 changes: 2 additions & 1 deletion dist/razorpay.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ var Razorpay = function () {
paymentLink: require('./resources/paymentLink')(this.api),
plans: require('./resources/plans')(this.api),
subscriptions: require('./resources/subscriptions')(this.api),
addons: require('./resources/addons')(this.api)
addons: require('./resources/addons')(this.api),
items: require('./resources/items')(this.api)
});
}
}]);
Expand Down
104 changes: 104 additions & 0 deletions dist/resources/items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
'use strict';

var _require = require('../utils/razorpay-utils'),
normalizeDate = _require.normalizeDate,
normalizeBoolean = _require.normalizeBoolean;

module.exports = function (api) {
return {
all: function all() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var callback = arguments[1];
var from = params.from,
to = params.to,
count = params.count,
skip = params.skip,
authorized = params.authorized,
receipt = params.receipt;


if (from) {
from = normalizeDate(from);
}

if (to) {
to = normalizeDate(to);
}

count = Number(count) || 10;
skip = Number(skip) || 0;
authorized = normalizeBoolean(authorized);

return api.get({
url: '/items',
data: {
from: from,
to: to,
count: count,
skip: skip,
authorized: authorized,
receipt: receipt
}
}, callback);
},
fetch: function fetch(itemId, callback) {
if (!itemId) {
throw new Error('`item_id` is mandatory');
}

return api.get({
url: '/items/' + itemId
}, callback);
},
create: function create() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var callback = arguments[1];
var amount = params.amount,
currency = params.currency,
description = params.description;

currency = currency || 'INR';

if (!amount) {
throw new Error('`amount` is mandatory');
}

var data = Object.assign({
amount: amount,
currency: currency,
description: description
});

return api.post({
url: '/items',
data: data
}, callback);
},
edit: function edit(itemId) {
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var callback = arguments[2];


if (!itemId) {
throw new Error('`item_id` is mandatory');
}

return api.patch({
url: '/items/' + itemId,
data: params
}, callback);
},


delete: function _delete(itemId, callback) {

if (!itemId) {
throw new Error('`item_id` is mandatory');
}

return api.delete({
url: '/items/' + itemId
}, callback);
}
};
};
169 changes: 169 additions & 0 deletions documents/items.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
## items

### Create item

```js
instance.Items.create({
"name": "Book / English August",
"description": "An indian story, Booker prize winner.",
"amount": 20000,
"currency": "INR"
});
```

**Parameters:**

| Name | Type | Description |
|-----------------|---------|------------------------------------------------------------------------------|
| name* | string | Name of the item. |
| description | string | A brief description of the item. |
| amount | integer | Amount of the order to be paid |
| currency | string | Currency of the order. Currently only `INR` is supported. |

**Response:**
```json
{
"id": "item_7Oxp4hmm6T4SCn",
"active": true,
"name": "Book / English August",
"description": "An indian story, Booker prize winner.",
"amount": 20000,
"currency": "INR"
}
```

-------------------------------------------------------------------------------------------------------

### Fetch all items

```js
instance.Items.all(options)
```
**Parameters:**

| Name | Type | Description |
|-------|-----------|--------------------------------------------------|
| from | timestamp | timestamp after which the item were created |
| to | timestamp | timestamp before which the item were created |
| count | integer | number of item to fetch (default: 10) |
| skip | integer | number of item to be skipped (default: 0) |
| name | string | Name of the item. |
| description | string | A brief description of the item. |
| amount | integer | Amount of the order to be paid |
| currency | string | Currency of the order. Currently only `INR` is supported. |
| active | boolean | Possible values is `0` or `1` |

**Response:**
```json
{
"entity": "collection",
"count": 3,
"items": [
{
"id": "item_7Oy8OMV6BdEAac",
"active": true,
"name": "Book / Ignited Minds",
"description": null,
"amount": 15000,
"currency": "INR"
},
{
"id": "item_7Oxp4hmm6T4SCn",
"active": true,
"name": "Book / English August",
"description": "An indian story, Booker prize winner.",
"amount": 20000,
"currency": "INR"
},
{
"id": "item_7OxoGnoxCuUKbo",
"active": true,
"name": "Book / English August",
"description": null,
"amount": 20000,
"currency": "INR"
}
]
}
```
-------------------------------------------------------------------------------------------------------
### Fetch particular item

```js
instance.Items.fetch(itemId)
```
**Parameters**

| Name | Type | Description |
|----------|--------|-------------------------------------|
| itemId* | string | The id of the item to be fetched |

**Response:**
```json
{
"id": "item_7Oxp4hmm6T4SCn",
"active": true,
"name": "Book / English August",
"description": "An indian story, Booker prize winner.",
"amount": 20000,
"currency": "INR"
}
```

-------------------------------------------------------------------------------------------------------

### Update item

```js
instance.Items.edit({
"name": "Book / Ignited Minds - Updated name!",
"description": "New descirption too. :).",
"amount": 20000,
"currency": "INR",
"active": true
})
```
**Parameters**

| Name | Type | Description |
|----------|--------|-------------------------------------|
| itemId* | string | The id of the item to be fetched |
| name | string | Name of the item. |
| description | string | A brief description of the item. |
| amount | integer | Amount of the order to be paid |
| currency | string | Currency of the order. Currently only `INR` is supported. |
| active | boolean | Possible values is `0` or `1` |

**Response:**
```json
{
"id": "item_7Oy8OMV6BdEAac",
"active": true,
"name": "Book / Ignited Minds - Updated name!",
"description": "New descirption too. :)",
"amount": 15000,
"currency": "INR"
}
```
-------------------------------------------------------------------------------------------------------
### Delete item

```js
instance.Items.delete(itemId)
```
**Parameters**

| Name | Type | Description |
|----------|--------|-------------------------------------|
| itemId* | string | The id of the item to be fetched |

**Response:**
```json
[]
```
-------------------------------------------------------------------------------------------------------

**PN: * indicates mandatory fields**
<br>
<br>
**For reference click [here](https://razorpay.com/docs/api/items)**
3 changes: 2 additions & 1 deletion lib/razorpay.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class Razorpay {
paymentLink : require('./resources/paymentLink')(this.api),
plans : require('./resources/plans')(this.api),
subscriptions : require('./resources/subscriptions')(this.api),
addons : require('./resources/addons')(this.api)
addons : require('./resources/addons')(this.api),
items : require('./resources/items')(this.api)
})
}
}
Expand Down
88 changes: 88 additions & 0 deletions lib/resources/items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'use strict'

const { normalizeDate, normalizeBoolean } = require('../utils/razorpay-utils')

module.exports = function (api) {
return {
all(params = {}, callback) {
let { from, to, count, skip, authorized, receipt } = params

if (from) {
from = normalizeDate(from)
}

if (to) {
to = normalizeDate(to)
}

count = Number(count) || 10
skip = Number(skip) || 0
authorized = normalizeBoolean(authorized)

return api.get({
url: '/items',
data: {
from,
to,
count,
skip,
authorized,
receipt
}
}, callback)
},

fetch(itemId, callback) {
if (!itemId) {
throw new Error('`item_id` is mandatory')
}

return api.get({
url: `/items/${itemId}`
}, callback)
},

create(params = {}, callback) {
let { amount, currency, description } = params
currency = currency || 'INR'

if (!amount) {
throw new Error('`amount` is mandatory')
}

let data = Object.assign({
amount,
currency,
description
})

return api.post({
url: '/items',
data
}, callback)
},

edit(itemId, params = {}, callback) {

if (!itemId) {
throw new Error('`item_id` is mandatory')
}

return api.patch({
url: `/items/${itemId}`,
data : params
}, callback)
},

delete: function _delete(itemId, callback) {

if (!itemId) {
throw new Error('`item_id` is mandatory');
}

return api.delete({
url: '/items/' + itemId
}, callback);
}
}
}
Loading

0 comments on commit e199f65

Please sign in to comment.