Skip to content

Commit

Permalink
Merge pull request #15 from razorpay/f/invoices
Browse files Browse the repository at this point in the history
F/invoices
  • Loading branch information
pronav authored Oct 12, 2017
2 parents 7aa567f + 09cb22e commit adb5c06
Show file tree
Hide file tree
Showing 9 changed files with 778 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ dist/

*.orig
*.log
*.swp
*.swo
*.swn
*.swm
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ instance.payments.all({
- [Transfers](https://github.com/razorpay/razorpay-node/wiki#transfers)

- [Virtual Accounts](https://github.com/razorpay/razorpay-node/wiki#virtual-accounts)

- [Invoices](https://github.com/razorpay/razorpay-node/wiki#invoices)
---


Expand Down
1 change: 1 addition & 0 deletions lib/razorpay.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Razorpay {
customers : require('./resources/customers')(this.api),
transfers : require('./resources/transfers')(this.api),
virtualAccounts: require('./resources/virtualAccounts')(this.api),
invoices : require('./resources/invoices')(this.api),
})
}
}
Expand Down
230 changes: 230 additions & 0 deletions lib/resources/invoices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
"use strict";

/*
* DOCS: https://razorpay.com/docs/invoices/
*/

const Promise = require("promise"),
{ normalizeDate, normalizeNotes } = require('../utils/razorpay-utils');

module.exports = function invoicesApi (api) {

const BASE_URL = "/invoices",
MISSING_ID_ERROR = "Invoice ID is mandatory";

/**
* Invoice entity gets used for both Payment Links and Invoices system.
* Few of the methods are only meaningful for Invoices system and
* calling those for against/for a Payment Link would throw
* Bad request error.
*/

return {

create (params={}, callback) {

/*
* Creates invoice of any type(invoice|link|ecod).
*
* @param {Object} params
* @param {Function} callback
*
* @return {Promise}
*/

let url = BASE_URL,
{ notes, ...rest } = params,
data = Object.assign(rest, normalizeNotes(notes));

return api.post({
url,
data
}, callback)
},

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

/*
* Patches given invoice with new attributes
*
* @param {String} invoiceId
* @param {Object} params
* @param {Function} callback
*
* @return {Promise}
*/

let url = `${BASE_URL}/${invoiceId}`,
{ notes, ...rest } = params,
data = Object.assign(rest, normalizeNotes(notes));

if (!invoiceId) {

return Promise.reject("Invoice ID is mandatory");
}

return api.patch({
url,
data
}, callback);
},

issue (invoiceId, callback) {

/*
* Issues drafted invoice
*
* @param {String} invoiceId
* @param {Function} callback
*
* @return {Promise}
*/

if (!invoiceId) {

return Promise.reject(MISSING_ID_ERROR);
}

let url = `${BASE_URL}/${invoiceId}/issue`;

return api.post({
url
}, callback);
},

delete (invoiceId, callback) {

/*
* Deletes drafted invoice
*
* @param {String} invoiceId
* @param {Function} callback
*
* @return {Promise}
*/

if (!invoiceId) {

return Promise.reject(MISSING_ID_ERROR);
}

let url = `${BASE_URL}/${invoiceId}`;

return api.delete({
url
}, callback);
},

cancel (invoiceId, callback) {

/*
* Cancels issued invoice
*
* @param {String} invoiceId
* @param {Function} callback
*
* @return {Promise}
*/

if (!invoiceId) {

return Promise.reject(MISSING_ID_ERROR);
}

let url = `${BASE_URL}/${invoiceId}/cancel`;

return api.post({
url
}, callback);
},

fetch (invoiceId, callback) {

/*
* Fetches invoice entity with given id
*
* @param {String} invoiceId
* @param {Function} callback
*
* @return {Promise}
*/

if (!invoiceId) {

return Promise.reject(MISSING_ID_ERROR);
}

let url = `${BASE_URL}/${invoiceId}`;

return api.get({
url
}, callback);
},

all (params={}, callback) {

/*
* Fetches multiple invoices with given query options
*
* @param {Object} invoiceId
* @param {Function} callback
*
* @return {Promise}
*/

let { from, to, count, skip } = params,
url = BASE_URL;

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

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

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

return api.get({
url,
data: {
...params,
from,
to,
count,
skip
}
}, callback);
},

notifyBy (invoiceId, medium, callback) {

/*
* Send/re-send notification for invoice by given medium
*
* @param {String} invoiceId
* @param {String} medium
* @param {Function} callback
*
* @return {Promise}
*/

if (!invoiceId) {

return Promise.reject(MISSING_ID_ERROR);
}

if (!medium) {

return Promise.reject("`medium` is required");
}

let url = `${BASE_URL}/${invoiceId}/notify_by/${medium}`;

return api.post({
url
}, callback);
}
};
};
Loading

0 comments on commit adb5c06

Please sign in to comment.