From a05d79e92829a11e96399a8a1f3abe353ab8f471 Mon Sep 17 00:00:00 2001 From: ankitdas13 Date: Mon, 8 Nov 2021 09:45:01 +0530 Subject: [PATCH 1/4] added item module --- dist/razorpay.js | 3 +- dist/resources/items.js | 104 +++++++++++++++++++++++ documents/items.md | 155 +++++++++++++++++++++++++++++++++++ lib/razorpay.js | 3 +- lib/resources/items.js | 88 ++++++++++++++++++++ test/resources/items.spec.js | 150 +++++++++++++++++++++++++++++++++ 6 files changed, 501 insertions(+), 2 deletions(-) create mode 100644 dist/resources/items.js create mode 100644 documents/items.md create mode 100644 lib/resources/items.js create mode 100644 test/resources/items.spec.js diff --git a/dist/razorpay.js b/dist/razorpay.js index a3e10d09..2e9460f6 100644 --- a/dist/razorpay.js +++ b/dist/razorpay.js @@ -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) }); } }]); diff --git a/dist/resources/items.js b/dist/resources/items.js new file mode 100644 index 00000000..f1e0ee88 --- /dev/null +++ b/dist/resources/items.js @@ -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); + } + }; +}; \ No newline at end of file diff --git a/documents/items.md b/documents/items.md new file mode 100644 index 00000000..adeeea50 --- /dev/null +++ b/documents/items.md @@ -0,0 +1,155 @@ +## 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. | +| desciprition | 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:** +For create item response please click [here](https://razorpay.com/docs/api/items/#create-an-item) + +------------------------------------------------------------------------------------------------------- + +### 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) | + +**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. | +| desciprition | 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** +
+
+**For reference click [here](https://razorpay.com/docs/api/items)** \ No newline at end of file diff --git a/lib/razorpay.js b/lib/razorpay.js index 5de44f90..6880d21d 100644 --- a/lib/razorpay.js +++ b/lib/razorpay.js @@ -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) }) } } diff --git a/lib/resources/items.js b/lib/resources/items.js new file mode 100644 index 00000000..21498562 --- /dev/null +++ b/lib/resources/items.js @@ -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); + } + } +} diff --git a/test/resources/items.spec.js b/test/resources/items.spec.js new file mode 100644 index 00000000..74671543 --- /dev/null +++ b/test/resources/items.spec.js @@ -0,0 +1,150 @@ +'use strict'; + +const Promise = require('promise') +const chai = require('chai') +const { assert } = chai +const rzpInstance = require('../razorpay'); +const mocker = require('../mocker') +const { runCallbackCheckTest, + runParamsCheckTest, + runCommonTests } = require("../../dist/utils/predefined-tests.js"); + +const SUB_PATH = "/items", + FULL_PATH = `/v1${SUB_PATH}`, + TEST_ITEM_ID = "item_sometestid", + apiObj = rzpInstance.items; + +describe("ITEMS", () => { + + describe("Create Item", () => { + + let expectedUrl = `${FULL_PATH}`, + params = { + "name":"Book / English August", + "description":"An indian story, Booker prize winner.", + "amount": 20000, + "currency": "INR" + }, + methodArgs = [params], + methodName = "create", + mockerParams = { + url: `${SUB_PATH}`, + method: "POST" + }; + + runCommonTests({ + apiObj, + methodName, + methodArgs, + expectedUrl, + mockerParams + }); + }); + + describe("Edit item", () => { + + let expectedUrl = `${FULL_PATH}/${TEST_ITEM_ID}`, + methodName = "edit", + params = { + "name":"Book / Ignited Minds - Updated name!", + "description":"New descirption too. :)." + }, + methodArgs = [TEST_ITEM_ID, params], + mockerParams = { + url: `${SUB_PATH}/${TEST_ITEM_ID}`, + method: "PATCH" + }; + + runCommonTests({ + apiObj, + methodName, + methodArgs, + expectedUrl, + mockerParams + }); + }); + + describe('Delete item', () => { + + let expectedUrl = `${FULL_PATH}/${TEST_ITEM_ID}`, + methodName = "delete", + methodArgs = [TEST_ITEM_ID], + mockerParams = { + url: `${SUB_PATH}/${TEST_ITEM_ID}`, + method: "DELETE" + }; + + runCommonTests({ + apiObj, + methodName, + methodArgs, + mockerParams, + expectedUrl + }); + }); + + describe("Fetch item", () => { + + let expectedUrl = `${FULL_PATH}/${TEST_ITEM_ID}`, + methodName = "fetch", + methodArgs = [TEST_ITEM_ID], + mockerParams = { + url: `${SUB_PATH}/${TEST_ITEM_ID}` + }; + + runCommonTests({ + apiObj, + methodName, + methodArgs, + mockerParams, + expectedUrl + }); + }); + + describe("Fetch All items", () => { + + let methodName = "all", + params = { + "skip": 10, + "count": 10 + }, + methodArgs = [params], + expectedParams = { + ...params + }, + mockerParams = { + url: `${SUB_PATH}` + }; + + runParamsCheckTest({ + apiObj, + methodName, + methodArgs, + mockerParams, + expectedParams, + testTitle: "Check if all params passed are being sent" + }); + + params = {}; + methodArgs = [params]; + expectedParams = {"skip": 0, "count": 10}; + + runParamsCheckTest({ + apiObj, + methodName, + methodArgs, + mockerParams, + expectedParams, + testTitle: "Check if skip and count are automatically populated" + }); + + methodArgs = [{}]; + + runCallbackCheckTest({ + apiObj, + methodName, + mockerParams, + methodArgs + }); + }); + }); From 1ac65b5813623871894da7461bc1bd17793f9b14 Mon Sep 17 00:00:00 2001 From: ankitdas13 Date: Mon, 8 Nov 2021 13:14:43 +0530 Subject: [PATCH 2/4] update readme and spelling mistake correction --- documents/items.md | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/documents/items.md b/documents/items.md index adeeea50..876db150 100644 --- a/documents/items.md +++ b/documents/items.md @@ -16,12 +16,21 @@ instance.Items.create({ | Name | Type | Description | |-----------------|---------|------------------------------------------------------------------------------| | name* | string | Name of the item. | -| desciprition | string | A brief description 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:** -For create item response please click [here](https://razorpay.com/docs/api/items/#create-an-item) +```json +{ + "id": "item_7Oxp4hmm6T4SCn", + "active": true, + "name": "Book / English August", + "description": "An indian story, Booker prize winner.", + "amount": 20000, + "currency": "INR" +} +``` ------------------------------------------------------------------------------------------------------- @@ -38,6 +47,11 @@ instance.Items.all(options) | 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 @@ -115,7 +129,7 @@ instance.Items.edit({ |----------|--------|-------------------------------------| | itemId* | string | The id of the item to be fetched | | name | string | Name of the item. | -| desciprition | string | A brief description 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` | From 02178b44855bbf6b311626e112f9c0258a16158a Mon Sep 17 00:00:00 2001 From: ankitdas13 Date: Wed, 10 Nov 2021 12:04:36 +0530 Subject: [PATCH 3/4] travis.yml to git action --- .github/node.js.yml | 31 +++++++++++++++++++++++++++++++ .travis.yml | 9 --------- README.md | 1 - 3 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 .github/node.js.yml delete mode 100644 .travis.yml diff --git a/.github/node.js.yml b/.github/node.js.yml new file mode 100644 index 00000000..25fa4a86 --- /dev/null +++ b/.github/node.js.yml @@ -0,0 +1,31 @@ +# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions + +name: Node.js CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [12.x, 14.x, 16.x] + # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + cache: 'npm' + - run: npm install + - run: npm run build --if-present + - run: npm test \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 2bef325b..00000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -sudo: false -language: node_js -node_js: - - "4" - - "5" - - "6" - - stable -script: - - npm test diff --git a/README.md b/README.md index 5075712c..7d46f7be 100644 --- a/README.md +++ b/README.md @@ -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). From b4f9cd65a35af9ec84980be31a1f3788fdd3fdaf Mon Sep 17 00:00:00 2001 From: ankitdas13 Date: Wed, 10 Nov 2021 12:08:18 +0530 Subject: [PATCH 4/4] remove yml file from .github --- .github/node.js.yml | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 .github/node.js.yml diff --git a/.github/node.js.yml b/.github/node.js.yml deleted file mode 100644 index 25fa4a86..00000000 --- a/.github/node.js.yml +++ /dev/null @@ -1,31 +0,0 @@ -# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions - -name: Node.js CI - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - - runs-on: ubuntu-latest - - strategy: - matrix: - node-version: [12.x, 14.x, 16.x] - # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ - - steps: - - uses: actions/checkout@v2 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 - with: - node-version: ${{ matrix.node-version }} - cache: 'npm' - - run: npm install - - run: npm run build --if-present - - run: npm test \ No newline at end of file