diff --git a/documents/documents.md b/documents/documents.md new file mode 100644 index 0000000..c223db9 --- /dev/null +++ b/documents/documents.md @@ -0,0 +1,73 @@ +## Document + +### Create a Document + +```js +var formData = { + 'file': { + 'value': fs.createReadStream('/Users/your_name/Downloads/sample_uploaded.pdf'), + 'options': { + 'filename': 'sample_uploaded.pdf', + 'contentType': null + } + }, + 'purpose': 'dispute_evidence' +}; + +instance.documents.create(formData); +``` + +**Parameters:** + +| Name | Type | Description | +|-------|-----------|--------------------------------------------------| +| file* | string | The URL generated once the business proof document is uploaded. | +| purpose | string | Possible value is `dispute_evidence` | + +**Response:** +```json +{ + "id": "doc_EsyWjHrfzb59Re", + "entity": "document", + "purpose": "dispute_evidence", + "name": "doc_19_12_2020.jpg", + "mime_type": "image/png", + "size": 2863, + "created_at": 1590604200 +} +``` +------------------------------------------------------------------------------------------------------- + +### Fetch Document Information + +```js +var documentId = "doc_EsyWjHrfzb59Re"; + +instance.documents.fetch(documentId); +``` + +**Parameters:** + +| Name | Type | Description | +|-------|-----------|--------------------------------------------------| +| Id* | string | The unique identifier of the document. | + +**Response:** +```json +{ + "entity": "document", + "id": "doc_00000000000000", + "purpose": "dispute_evidence", + "created_at": 1701701378, + "mime_type": "application/pdf", + "display_name": "ppm_00000000000000", + "size": 404678, + "url": "" +} +``` +------------------------------------------------------------------------------------------------------- + +**PN: * indicates mandatory fields** +
+
+**For reference click [here](https://razorpay.com/docs/api/documents)** \ No newline at end of file diff --git a/lib/razorpay.d.ts b/lib/razorpay.d.ts index 5698d1c..f0e5ed1 100644 --- a/lib/razorpay.d.ts +++ b/lib/razorpay.d.ts @@ -22,6 +22,7 @@ import webhooks from './types/webhooks' import products from './types/products' import tokens from './types/tokens' import iins from './types/iins' +import documents from './types/documents' import disputes from './types/disputes' interface IRazorpayConfig { @@ -146,6 +147,11 @@ declare class Razorpay { */ iins: ReturnType /** + * Documents Entity + * @see https://razorpay.com/docs/api/documents + */ + documents: ReturnType + /** * Dispute Entity * @see https://razorpay.com/docs/api/disputes */ diff --git a/lib/razorpay.js b/lib/razorpay.js index 3738bd1..ccfbca0 100644 --- a/lib/razorpay.js +++ b/lib/razorpay.js @@ -58,6 +58,7 @@ class Razorpay { items : require('./resources/items')(this.api), cards : require('./resources/cards')(this.api), webhooks : require('./resources/webhooks')(this.api), + documents : require('./resources/documents')(this.api), disputes : require('./resources/disputes')(this.api) }) } diff --git a/lib/resources/documents.js b/lib/resources/documents.js new file mode 100644 index 0000000..efb93e1 --- /dev/null +++ b/lib/resources/documents.js @@ -0,0 +1,21 @@ +'use strict'; + +module.exports = function (api) { + + const BASE_URL = "/documents"; + + return { + create(params, callback) { + return api.postFormData({ + url: `${BASE_URL}`, + formData: params + }, callback); + }, + + fetch(documentId, callback) { + return api.get({ + url: `${BASE_URL}/${documentId}`, + }, callback); + }, + } +} \ No newline at end of file diff --git a/lib/types/documents.d.ts b/lib/types/documents.d.ts new file mode 100644 index 0000000..83d6a50 --- /dev/null +++ b/lib/types/documents.d.ts @@ -0,0 +1,67 @@ +import { INormalizeError } from "./api"; +import * as fs from "fs" + +export declare namespace Documents { + + interface FileCreateParams { + file: { + value: fs.ReadStream; + options?: { + filename?: string; + contentType?: string | null; + }; + }; + purpose: string; + } + + interface RazorpayDocument { + /** + * The unique identifier of the document. + */ + id: string + /** + * Indicates the type of entity. + */ + entity: string + /** + * The reason you are uploading this document. possible value is `dispute_evidence`. + */ + purpose: string + name: string + /** + * Indicates the nature and format in which the document is uploaded. + * possible value is `image/jpg`, `image/jpeg`, `image/png`, `application/pdf` + * + */ + mime_type: string + /** + * Indicates the size of the document in bytes. + */ + size: number + /** + * Unix timestamp at which the document was uploaded. + */ + created_at: number + } + +} + +declare function documents(api: any): { + /** + * Create a Document + * + * @param params - Check [doc](https://razorpay.com/docs/api/documents/create/) for required params + * + */ + create(params: Documents.FileCreateParams): Promise + create(params: Documents.FileCreateParams, callback: (err: INormalizeError | null, data: Documents.RazorpayDocument) => void): void; + /** + * Fetch document by id + * + * @param documentId - The unique identifier of the document + * + */ + fetch(documentId: string): Promise +} + +export default documents \ No newline at end of file diff --git a/test/resources/documents.spec.js b/test/resources/documents.spec.js new file mode 100644 index 0000000..a00804f --- /dev/null +++ b/test/resources/documents.spec.js @@ -0,0 +1,56 @@ +'use strict' + +const chai = require('chai') +const { assert } = chai +const rzpInstance = require('../razorpay') +const mocker = require('../mocker') +const equal = require('deep-equal') + +const BASE_URL = '/documents', + TEST_DOCUMENT_ID = 'disp_AHfqOvkldwsbqt'; + +describe('DOCUMENTS', () => { + it('Create an document', (done) => { + + var formData = { + 'file': { + 'value': '/Users/your_name/Downloads/sample_uploaded.pdf', + 'options': { + 'filename': 'README.md', + 'contentType': null + } + }, + 'purpose': 'dispute_evidence' + }; + + mocker.mock({ + url: `${BASE_URL}`, + method: 'POST' + }) + + rzpInstance.documents.create(formData).then((response) => { + assert.equal( + response.__JUST_FOR_TESTS__.url, + `/v1/documents`, + 'Create document request url formed' + ) + done() + }) + }) + + it('Fetch document detail', (done) => { + mocker.mock({ + url: `/${BASE_URL}/${TEST_DOCUMENT_ID}`, + method: 'GET' + }) + + rzpInstance.documents.fetch(TEST_DOCUMENT_ID).then((response) => { + assert.equal( + response.__JUST_FOR_TESTS__.url, + `/v1/documents/${TEST_DOCUMENT_ID}`, + 'fetch document detail request url formed' + ) + done() + }) + }) +})