From 435192f2fdf028e59cd83580e3d1140408548a19 Mon Sep 17 00:00:00 2001 From: rzpamidi Date: Thu, 12 Oct 2017 18:21:29 +0530 Subject: [PATCH] Adds dist folder in source repo , to prevent accidental publishing to npm without dist folder --- .gitignore | 1 - dist/api.js | 78 ++++++++++ dist/razorpay.js | 61 ++++++++ dist/resources/customers.js | 53 +++++++ dist/resources/invoices.js | 245 ++++++++++++++++++++++++++++++ dist/resources/orders.js | 95 ++++++++++++ dist/resources/payments.js | 111 ++++++++++++++ dist/resources/refunds.js | 64 ++++++++ dist/resources/transfers.js | 110 ++++++++++++++ dist/resources/virtualAccounts.js | 85 +++++++++++ dist/utils/nodeify.js | 15 ++ dist/utils/predefined-tests.js | 142 +++++++++++++++++ dist/utils/razorpay-utils.js | 68 +++++++++ package.json | 2 +- 14 files changed, 1128 insertions(+), 2 deletions(-) create mode 100644 dist/api.js create mode 100644 dist/razorpay.js create mode 100644 dist/resources/customers.js create mode 100644 dist/resources/invoices.js create mode 100644 dist/resources/orders.js create mode 100644 dist/resources/payments.js create mode 100644 dist/resources/refunds.js create mode 100644 dist/resources/transfers.js create mode 100644 dist/resources/virtualAccounts.js create mode 100644 dist/utils/nodeify.js create mode 100644 dist/utils/predefined-tests.js create mode 100644 dist/utils/razorpay-utils.js diff --git a/.gitignore b/.gitignore index cc690bb2..c17b18d7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ node_modules/ -dist/ *.orig *.log diff --git a/dist/api.js b/dist/api.js new file mode 100644 index 00000000..c6a368d3 --- /dev/null +++ b/dist/api.js @@ -0,0 +1,78 @@ +'use strict'; + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var request = require('request-promise'); +var nodeify = require('./utils/nodeify'); + +function normalizeError(err) { + throw { + statusCode: err.statusCode, + error: err.error.error + }; +} + +var API = function () { + function API(options) { + _classCallCheck(this, API); + + this.rq = request.defaults({ + baseUrl: options.hostUrl, + json: true, + auth: { + user: options.key_id, + pass: options.key_secret + }, + headers: { + 'User-Agent': options.ua + } + }); + } + + _createClass(API, [{ + key: 'get', + value: function get(params, cb) { + return nodeify(this.rq.get({ + url: params.url, + qs: params.data + }).catch(normalizeError), cb); + } + }, { + key: 'post', + value: function post(params, cb) { + return nodeify(this.rq.post({ + url: params.url, + form: params.data + }).catch(normalizeError), cb); + } + }, { + key: 'put', + value: function put(params, cb) { + return nodeify(this.rq.put({ + url: params.url, + form: params.data + }).catch(normalizeError), cb); + } + }, { + key: 'patch', + value: function patch(params, cb) { + return nodeify(this.rq.patch({ + url: params.url, + form: params.data + }).catch(normalizeError), cb); + } + }, { + key: 'delete', + value: function _delete(params, cb) { + return nodeify(this.rq.delete({ + url: params.url + }).catch(normalizeError), cb); + } + }]); + + return API; +}(); + +module.exports = API; \ No newline at end of file diff --git a/dist/razorpay.js b/dist/razorpay.js new file mode 100644 index 00000000..8233fdfe --- /dev/null +++ b/dist/razorpay.js @@ -0,0 +1,61 @@ +'use strict'; + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var API = require('./api'); +var pkg = require('../package.json'); + +var Razorpay = function () { + function Razorpay() { + var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; + + _classCallCheck(this, Razorpay); + + var key_id = options.key_id, + key_secret = options.key_secret; + + + if (!key_id) { + throw new Error('`key_id` is mandatory'); + } + + if (!key_secret) { + throw new Error('`key_secret` is mandatory'); + } + + this.key_id = key_id; + this.key_secret = key_secret; + + this.api = new API({ + hostUrl: 'https://api.razorpay.com/v1/', + ua: 'razorpay-node@' + Razorpay.VERSION, + key_id: key_id, + key_secret: key_secret + }); + this.addResources(); + } + + _createClass(Razorpay, [{ + key: 'addResources', + value: function addResources() { + Object.assign(this, { + payments: require('./resources/payments')(this.api), + refunds: require('./resources/refunds')(this.api), + orders: require('./resources/orders')(this.api), + customers: require('./resources/customers')(this.api), + transfers: require('./resources/transfers')(this.api), + virtualAccounts: require('./resources/virtualAccounts')(this.api), + invoices: require('./resources/invoices')(this.api) + }); + } + }]); + + return Razorpay; +}(); + +Razorpay.VERSION = pkg.version; + + +module.exports = Razorpay; \ No newline at end of file diff --git a/dist/resources/customers.js b/dist/resources/customers.js new file mode 100644 index 00000000..1414a5b2 --- /dev/null +++ b/dist/resources/customers.js @@ -0,0 +1,53 @@ +'use strict'; + +function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } + +var _require = require('../utils/razorpay-utils'), + normalizeNotes = _require.normalizeNotes; + +module.exports = function (api) { + return { + create: function create(params, callback) { + var notes = params.notes, + rest = _objectWithoutProperties(params, ['notes']); + + var data = Object.assign(rest, normalizeNotes(notes)); + + return api.post({ + url: '/customers', + data: data + }, callback); + }, + edit: function edit(customerId, params, callback) { + var notes = params.notes, + rest = _objectWithoutProperties(params, ['notes']); + + var data = Object.assign(rest, normalizeNotes(notes)); + + return api.put({ + url: '/customers/' + customerId, + data: data + }, callback); + }, + fetch: function fetch(customerId, callback) { + return api.get({ + url: '/customers/' + customerId + }, callback); + }, + fetchTokens: function fetchTokens(customerId, callback) { + return api.get({ + url: '/customers/' + customerId + '/tokens' + }, callback); + }, + fetchToken: function fetchToken(customerId, tokenId, callback) { + return api.get({ + url: '/customers/' + customerId + '/tokens/' + tokenId + }, callback); + }, + deleteToken: function deleteToken(customerId, tokenId, callback) { + return api.delete({ + url: '/customers/' + customerId + '/tokens/' + tokenId + }, callback); + } + }; +}; \ No newline at end of file diff --git a/dist/resources/invoices.js b/dist/resources/invoices.js new file mode 100644 index 00000000..292ce42e --- /dev/null +++ b/dist/resources/invoices.js @@ -0,0 +1,245 @@ +"use strict"; + +/* + * DOCS: https://razorpay.com/docs/invoices/ + */ + +var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; + +function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } + +var Promise = require("promise"), + _require = require('../utils/razorpay-utils'), + normalizeDate = _require.normalizeDate, + normalizeNotes = _require.normalizeNotes; + + +module.exports = function invoicesApi(api) { + + var 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: function create() { + var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; + var callback = arguments[1]; + + + /* + * Creates invoice of any type(invoice|link|ecod). + * + * @param {Object} params + * @param {Function} callback + * + * @return {Promise} + */ + + var url = BASE_URL, + notes = params.notes, + rest = _objectWithoutProperties(params, ["notes"]), + data = Object.assign(rest, normalizeNotes(notes)); + + + return api.post({ + url: url, + data: data + }, callback); + }, + edit: function edit(invoiceId) { + var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + var callback = arguments[2]; + + + /* + * Patches given invoice with new attributes + * + * @param {String} invoiceId + * @param {Object} params + * @param {Function} callback + * + * @return {Promise} + */ + + var url = BASE_URL + "/" + invoiceId, + notes = params.notes, + rest = _objectWithoutProperties(params, ["notes"]), + data = Object.assign(rest, normalizeNotes(notes)); + + + if (!invoiceId) { + + return Promise.reject("Invoice ID is mandatory"); + } + + return api.patch({ + url: url, + data: data + }, callback); + }, + issue: function issue(invoiceId, callback) { + + /* + * Issues drafted invoice + * + * @param {String} invoiceId + * @param {Function} callback + * + * @return {Promise} + */ + + if (!invoiceId) { + + return Promise.reject(MISSING_ID_ERROR); + } + + var url = BASE_URL + "/" + invoiceId + "/issue"; + + return api.post({ + url: url + }, callback); + }, + delete: function _delete(invoiceId, callback) { + + /* + * Deletes drafted invoice + * + * @param {String} invoiceId + * @param {Function} callback + * + * @return {Promise} + */ + + if (!invoiceId) { + + return Promise.reject(MISSING_ID_ERROR); + } + + var url = BASE_URL + "/" + invoiceId; + + return api.delete({ + url: url + }, callback); + }, + cancel: function cancel(invoiceId, callback) { + + /* + * Cancels issued invoice + * + * @param {String} invoiceId + * @param {Function} callback + * + * @return {Promise} + */ + + if (!invoiceId) { + + return Promise.reject(MISSING_ID_ERROR); + } + + var url = BASE_URL + "/" + invoiceId + "/cancel"; + + return api.post({ + url: url + }, callback); + }, + fetch: function 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); + } + + var url = BASE_URL + "/" + invoiceId; + + return api.get({ + url: url + }, callback); + }, + all: function all() { + var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; + var callback = arguments[1]; + + + /* + * Fetches multiple invoices with given query options + * + * @param {Object} invoiceId + * @param {Function} callback + * + * @return {Promise} + */ + + var from = params.from, + to = params.to, + count = params.count, + skip = params.skip, + 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: url, + data: _extends({}, params, { + from: from, + to: to, + count: count, + skip: skip + }) + }, callback); + }, + notifyBy: function 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"); + } + + var url = BASE_URL + "/" + invoiceId + "/notify_by/" + medium; + + return api.post({ + url: url + }, callback); + } + }; +}; \ No newline at end of file diff --git a/dist/resources/orders.js b/dist/resources/orders.js new file mode 100644 index 00000000..d92d939f --- /dev/null +++ b/dist/resources/orders.js @@ -0,0 +1,95 @@ +'use strict'; + +var _require = require('../utils/razorpay-utils'), + normalizeDate = _require.normalizeDate, + normalizeBoolean = _require.normalizeBoolean, + normalizeNotes = _require.normalizeNotes; + +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: '/orders', + data: { + from: from, + to: to, + count: count, + skip: skip, + authorized: authorized, + receipt: receipt + } + }, callback); + }, + fetch: function fetch(orderId, callback) { + if (!orderId) { + throw new Error('`order_id` is mandatory'); + } + + return api.get({ + url: '/orders/' + orderId + }, 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, + receipt = params.receipt, + payment_capture = params.payment_capture, + notes = params.notes; + + currency = currency || 'INR'; + + if (!amount) { + throw new Error('`amount` is mandatory'); + } + + if (!receipt) { + throw new Error('`receipt` is mandatory'); + } + + var data = Object.assign({ + amount: amount, + currency: currency, + receipt: receipt, + payment_capture: normalizeBoolean(payment_capture) + }, normalizeNotes(notes)); + + return api.post({ + url: '/orders', + data: data + }, callback); + }, + fetchPayments: function fetchPayments(orderId, callback) { + if (!orderId) { + throw new Error('`order_id` is mandatory'); + } + + return api.get({ + url: '/orders/' + orderId + '/payments' + }, callback); + } + }; +}; \ No newline at end of file diff --git a/dist/resources/payments.js b/dist/resources/payments.js new file mode 100644 index 00000000..fcd91997 --- /dev/null +++ b/dist/resources/payments.js @@ -0,0 +1,111 @@ +'use strict'; + +function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } + +var _require = require('../utils/razorpay-utils'), + normalizeDate = _require.normalizeDate, + normalizeBoolean = _require.normalizeBoolean, + normalizeNotes = _require.normalizeNotes; + +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; + + + if (from) { + from = normalizeDate(from); + } + + if (to) { + to = normalizeDate(to); + } + + count = Number(count) || 10; + skip = Number(skip) || 0; + + return api.get({ + url: '/payments', + data: { + from: from, + to: to, + count: count, + skip: skip + } + }, callback); + }, + fetch: function fetch(paymentId, callback) { + if (!paymentId) { + throw new Error('`payment_id` is mandatory'); + } + + return api.get({ + url: '/payments/' + paymentId + }, callback); + }, + capture: function capture(paymentId, amount, callback) { + if (!paymentId) { + throw new Error('`payment_id` is mandatory'); + } + + if (!amount) { + throw new Error('`amount` is mandatory'); + } + + return api.post({ + url: '/payments/' + paymentId + '/capture', + data: { + amount: amount + } + }, callback); + }, + refund: function refund(paymentId) { + var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + var callback = arguments[2]; + + var notes = params.notes, + otherParams = _objectWithoutProperties(params, ['notes']); + + if (!paymentId) { + throw new Error('`payment_id` is mandatory'); + } + + var data = Object.assign(otherParams, normalizeNotes(notes)); + return api.post({ + url: '/payments/' + paymentId + '/refund', + data: data + }, callback); + }, + transfer: function transfer(paymentId) { + var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + var callback = arguments[2]; + + if (!paymentId) { + throw new Error('`payment_id` is mandatory'); + } + + var notes = params.notes, + otherParams = _objectWithoutProperties(params, ['notes']); + + var data = Object.assign(otherParams, normalizeNotes(notes)); + + if (data.transfers) { + var transfers = data.transfers; + transfers.forEach(function (transfer) { + if (transfer.on_hold) { + transfer.on_hold = normalizeBoolean(transfer.on_hold); + } + }); + } + return api.post({ + url: '/payments/' + paymentId + '/transfers', + data: data + }, callback); + } + }; +}; \ No newline at end of file diff --git a/dist/resources/refunds.js b/dist/resources/refunds.js new file mode 100644 index 00000000..c7d44f5b --- /dev/null +++ b/dist/resources/refunds.js @@ -0,0 +1,64 @@ +'use strict'; + +var _require = require('../utils/razorpay-utils'), + normalizeDate = _require.normalizeDate; + +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, + payment_id = params.payment_id; + + var url = '/refunds'; + + if (payment_id) { + url = '/payments/' + payment_id + '/refunds'; + } + + if (from) { + from = normalizeDate(from); + } + + if (to) { + to = normalizeDate(to); + } + + count = Number(count) || 10; + skip = Number(skip) || 0; + + return api.get({ + url: url, + data: { + from: from, + to: to, + count: count, + skip: skip + } + }, callback); + }, + fetch: function fetch(refundId) { + var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + var callback = arguments[2]; + var payment_id = params.payment_id; + + if (!refundId) { + throw new Error('`refund_id` is mandatory'); + } + + var url = '/refunds/' + refundId; + + if (payment_id) { + url = '/payments/' + payment_id + url; + } + + return api.get({ + url: url + }, callback); + } + }; +}; \ No newline at end of file diff --git a/dist/resources/transfers.js b/dist/resources/transfers.js new file mode 100644 index 00000000..d557fe08 --- /dev/null +++ b/dist/resources/transfers.js @@ -0,0 +1,110 @@ +"use strict"; + +function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } + +var _require = require('../utils/razorpay-utils'), + normalizeDate = _require.normalizeDate, + normalizeBoolean = _require.normalizeBoolean, + normalizeNotes = _require.normalizeNotes; + +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, + payment_id = params.payment_id; + + var url = '/transfers'; + + if (payment_id) { + url = '/payments/' + payment_id + '/transfers'; + } + + if (from) { + from = normalizeDate(from); + } + + if (to) { + to = normalizeDate(to); + } + + count = Number(count) || 10; + skip = Number(skip) || 0; + + return api.get({ + url: url, + data: { + from: from, + to: to, + count: count, + skip: skip + } + }, callback); + }, + fetch: function fetch(transferId) { + var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + var callback = arguments[2]; + var payment_id = params.payment_id; + + if (!transferId) { + throw new Error('`transfer_id` is mandatory'); + } + + var url = '/transfers/' + transferId; + + return api.get({ + url: url + }, callback); + }, + create: function create(params, callback) { + var notes = params.notes, + rest = _objectWithoutProperties(params, ['notes']); + + var data = Object.assign(rest, normalizeNotes(notes)); + + if (data.on_hold) { + data.on_hold = normalizeBoolean(data.on_hold); + } + + return api.post({ + url: '/transfers', + data: data + }, callback); + }, + edit: function edit(transferId, params, callback) { + var notes = params.notes, + rest = _objectWithoutProperties(params, ['notes']); + + var data = Object.assign(rest, normalizeNotes(notes)); + + if (data.on_hold) { + data.on_hold = normalizeBoolean(data.on_hold); + } + + return api.patch({ + url: '/transfers/' + transferId, + data: data + }, callback); + }, + reverse: function reverse(transferId, params, callback) { + if (!transferId) { + throw new Error('`transfer_id` is mandatory'); + } + + var notes = params.notes, + rest = _objectWithoutProperties(params, ['notes']); + + var data = Object.assign(rest, normalizeNotes(notes)); + var url = '/transfers/' + transferId + '/reversals'; + + return api.post({ + url: url, + data: data + }, callback); + } + }; +}; \ No newline at end of file diff --git a/dist/resources/virtualAccounts.js b/dist/resources/virtualAccounts.js new file mode 100644 index 00000000..175eba04 --- /dev/null +++ b/dist/resources/virtualAccounts.js @@ -0,0 +1,85 @@ +"use strict"; + +function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } + +var Promise = require("promise"); + +var _require = require('../utils/razorpay-utils'), + normalizeDate = _require.normalizeDate, + normalizeNotes = _require.normalizeNotes; + +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; + + var url = '/virtual_accounts'; + + if (from) { + from = normalizeDate(from); + } + + if (to) { + to = normalizeDate(to); + } + + count = Number(count) || 10; + skip = Number(skip) || 0; + + return api.get({ + url: url, + data: { + from: from, + to: to, + count: count, + skip: skip + } + }, callback); + }, + fetch: function fetch(virtualAccountId, callback) { + + if (!virtualAccountId) { + + return Promise.reject('`virtual_account_id` is mandatory'); + } + + var url = "/virtual_accounts/" + virtualAccountId; + + return api.get({ + url: url + }, callback); + }, + create: function create(params, callback) { + var notes = params.notes, + rest = _objectWithoutProperties(params, ["notes"]); + + var data = Object.assign(rest, normalizeNotes(notes)); + + return api.post({ + url: '/virtual_accounts', + data: data + }, callback); + }, + close: function close(virtualAccountId, callback) { + + if (!virtualAccountId) { + + return Promise.reject('`virtual_account_id` is mandatory'); + } + + var data = { + status: 'closed' + }; + + return api.patch({ + url: "/virtual_accounts/" + virtualAccountId, + data: data + }, callback); + } + }; +}; \ No newline at end of file diff --git a/dist/utils/nodeify.js b/dist/utils/nodeify.js new file mode 100644 index 00000000..c9b8568e --- /dev/null +++ b/dist/utils/nodeify.js @@ -0,0 +1,15 @@ +'use strict'; + +var nodeify = function nodeify(promise, cb) { + if (!cb) { + return promise; + } + + return promise.then(function (response) { + cb(null, response); + }).catch(function (error) { + cb(error, null); + }); +}; + +module.exports = nodeify; \ No newline at end of file diff --git a/dist/utils/predefined-tests.js b/dist/utils/predefined-tests.js new file mode 100644 index 00000000..ca5c817e --- /dev/null +++ b/dist/utils/predefined-tests.js @@ -0,0 +1,142 @@ +'use strict'; + +var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; + +function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } + +var mocker = require('../../test/mocker'), + equal = require('deep-equal'), + chai = require('chai'), + assert = chai.assert, + _require = require("../../dist/utils/razorpay-utils"), + prettify = _require.prettify, + getTestError = _require.getTestError; + + +var runCallbackCheckTest = function runCallbackCheckTest(params) { + var apiObj = params.apiObj, + methodName = params.methodName, + methodArgs = params.methodArgs, + mockerParams = params.mockerParams; + + + it("Checks if the passed api callback gets called", function (done) { + + mocker.mock(mockerParams); + + apiObj[methodName].apply(apiObj, _toConsumableArray(methodArgs).concat([function (err) { + done(); + }])); + }); + + it("Checks for error flow", function (done) { + + mocker.mock(_extends({}, mockerParams, { replyWithError: true })); + + apiObj[methodName].apply(apiObj, _toConsumableArray(methodArgs).concat([function (err) { + + assert.ok(!!err, "Error callback called with error"); + done(); + }])); + }); + + it("Checks if the api call returns a Promise", function (done) { + + mocker.mock(mockerParams); + + var retVal = apiObj[methodName].apply(apiObj, _toConsumableArray(methodArgs)); + + retVal && typeof retVal.then === "function" ? done() : done(getTestError("Invalid Return Value", String("Promise"), retVal)); + }); +}; + +var runURLCheckTest = function runURLCheckTest(params) { + var apiObj = params.apiObj, + methodName = params.methodName, + methodArgs = params.methodArgs, + expectedUrl = params.expectedUrl, + mockerParams = params.mockerParams; + + + it("Checks if the URL is formed correctly", function (done) { + + mocker.mock(mockerParams); + + apiObj[methodName].apply(apiObj, _toConsumableArray(methodArgs).concat([function (err, resp) { + + var respData = resp.__JUST_FOR_TESTS__; + + if (respData.url === expectedUrl) { + + assert.ok(true, "URL Matched"); + done(); + } else { + + done(getTestError("URL Mismatch", expectedUrl, respData.url)); + } + }])); + }); +}; + +var runParamsCheckTest = function runParamsCheckTest(params) { + var apiObj = params.apiObj, + methodName = params.methodName, + methodArgs = params.methodArgs, + expectedParams = params.expectedParams, + mockerParams = params.mockerParams, + testTitle = params.testTitle; + + + testTitle = testTitle || "Validates URL and Params"; + + it(testTitle, function (done) { + + mocker.mock(mockerParams); + + apiObj[methodName].apply(apiObj, _toConsumableArray(methodArgs)).then(function (resp) { + + var respData = resp.__JUST_FOR_TESTS__, + respParams = respData[respData.method === "GET" ? "requestQueryParams" : "requestBody"]; + + if (equal(respParams, expectedParams)) { + + assert.ok(true, "Params Matched"); + } else { + + return getTestError("Params Mismatch", expectedParams, respParams); + } + }, function (err) { + + return new Error(prettify(err)); + }).then(function (err) { + + done(err); + }); + }); +}; + +var runCommonTests = function runCommonTests(params) { + var apiObj = params.apiObj, + methodName = params.methodName, + methodArgs = params.methodArgs, + expectedUrl = params.expectedUrl, + expectedParams = params.expectedParams, + mockerParams = params.mockerParams; + + + runURLCheckTest(_extends({}, params)); + + if (expectedParams) { + + runParamsCheckTest(_extends({}, params)); + } + + runCallbackCheckTest(_extends({}, params)); +}; + +module.exports = { + runCallbackCheckTest: runCallbackCheckTest, + runParamsCheckTest: runParamsCheckTest, + runURLCheckTest: runURLCheckTest, + runCommonTests: runCommonTests +}; \ No newline at end of file diff --git a/dist/utils/razorpay-utils.js b/dist/utils/razorpay-utils.js new file mode 100644 index 00000000..11742d54 --- /dev/null +++ b/dist/utils/razorpay-utils.js @@ -0,0 +1,68 @@ +"use strict"; + +var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; + +function getDateInSecs(date) { + return +new Date(date) / 1000; +} + +function normalizeDate(date) { + return isNumber(date) ? date : getDateInSecs(date); +} + +function isNumber(num) { + return !isNaN(Number(num)); +} + +function normalizeBoolean(bool) { + if (bool === undefined) { + return bool; + } + + return bool ? 1 : 0; +} + +function normalizeNotes() { + var notes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; + + var normalizedNotes = {}; + for (var key in notes) { + normalizedNotes["notes[" + key + "]"] = notes[key]; + } + return normalizedNotes; +} + +function prettify(val) { + + /* + * given an object , returns prettified string + * + * @param {Object} val + * @return {String} + */ + + return JSON.stringify(val, null, 2); +} + +function getTestError(summary, expectedVal, gotVal) { + + /* + * @param {String} summary + * @param {*} expectedVal + * @param {*} gotVal + * + * @return {Error} + */ + + return new Error("\n" + summary + "\n" + ("Expected(" + (typeof expectedVal === "undefined" ? "undefined" : _typeof(expectedVal)) + ")\n" + prettify(expectedVal) + "\n\n") + ("Got(" + (typeof gotVal === "undefined" ? "undefined" : _typeof(gotVal)) + ")\n" + prettify(gotVal))); +} + +module.exports = { + normalizeNotes: normalizeNotes, + normalizeDate: normalizeDate, + normalizeBoolean: normalizeBoolean, + isNumber: isNumber, + getDateInSecs: getDateInSecs, + prettify: prettify, + getTestError: getTestError +}; \ No newline at end of file diff --git a/package.json b/package.json index 252d2b9b..347a5774 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "razorpay", - "version": "1.5.0", + "version": "1.5.1", "description": "Official Node SDK for Razorpay API", "main": "dist/razorpay.js", "scripts": {