Skip to content

Commit

Permalink
feat: Added document entity (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitdas13 authored May 7, 2024
1 parent f341db4 commit 5bc90ef
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
73 changes: 73 additions & 0 deletions documents/documents.md
Original file line number Diff line number Diff line change
@@ -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**
<br>
<br>
**For reference click [here](https://razorpay.com/docs/api/documents)**
6 changes: 6 additions & 0 deletions lib/razorpay.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -146,6 +147,11 @@ declare class Razorpay {
*/
iins: ReturnType<typeof iins>
/**
* Documents Entity
* @see https://razorpay.com/docs/api/documents
*/
documents: ReturnType<typeof documents>
/**
* Dispute Entity
* @see https://razorpay.com/docs/api/disputes
*/
Expand Down
1 change: 1 addition & 0 deletions lib/razorpay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
21 changes: 21 additions & 0 deletions lib/resources/documents.js
Original file line number Diff line number Diff line change
@@ -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);
},
}
}
67 changes: 67 additions & 0 deletions lib/types/documents.d.ts
Original file line number Diff line number Diff line change
@@ -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<Documents.RazorpayDocument>
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<Documents.RazorpayDocument>
}

export default documents
56 changes: 56 additions & 0 deletions test/resources/documents.spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})

0 comments on commit 5bc90ef

Please sign in to comment.