Razorpay
Context: As a user I want to be able to see all payments and orders related to a customer from Razorpay.
Example: I have a collection users and a collection orders in the database. An order belongs to a customer through a field user. An order has a field order_reference and payment_reference that are ids of objects from Razorpay.

Models

models/users.js
1
const mongoose = require('mongoose');
2
3
const schema = mongoose.Schema({
4
'Age': Number,
5
'Interests': [String],
6
'Name': String,
7
'createdAt': Date,
8
'updatedAt': Date,
9
}, {
10
timestamps: false,
11
});
12
13
module.exports = mongoose.model('users', schema, 'users');
Copied!
models/orders.js
1
const mongoose = require('mongoose');
2
3
const schema = mongoose.Schema({
4
'reference': String,
5
'payment_ref': String,
6
'order_ref': String,
7
'user': { type: mongoose.Schema.Types.ObjectId, ref: 'users' },
8
9
}, {
10
timestamps: false,
11
});
12
13
module.exports = mongoose.model('orders', schema, 'orders');
Copied!

Implementation

Declare virtual collections

forest/razorpay-payments
1
const { collection } = require('forest-express-mongoose');
2
3
collection('razorpayPayments', {
4
actions: [],
5
fields: [{
6
field: 'amount',
7
type: 'Number',
8
},{
9
field: 'entity',
10
type: 'String',
11
},{
12
field: 'method',
13
type: 'String',
14
},{
15
field: 'international',
16
type: 'Boolean',
17
},{
18
field: 'currency',
19
type: 'String',
20
},{
21
field: 'method',
22
type: 'String',
23
},{
24
field: 'amount_refunded',
25
type: 'Number',
26
},{
27
field: 'refund_status',
28
type: 'String',
29
},{
30
field: 'captured',
31
type: 'Boolean',
32
},{
33
field: 'description',
34
type: 'String',
35
},{
36
field: 'card_id',
37
type: 'String',
38
},{
39
field: 'bank',
40
type: 'String',
41
},{
42
field: 'wallet',
43
type: 'String',
44
},{
45
field: 'vpa',
46
type: 'String',
47
},{
48
field: 'email',
49
type: 'String',
50
},{
51
field: 'contact',
52
type: 'String',
53
},{
54
field: 'fee',
55
type: 'Number',
56
},{
57
field: 'tax',
58
type: 'Number',
59
},{
60
field: 'error_code',
61
type: 'String',
62
},{
63
field: 'error_description',
64
type: 'String',
65
},{
66
field: 'status',
67
type: 'String',
68
},{
69
field: 'created_at',
70
type: 'Date',
71
}],
72
segments: [],
73
});
Copied!
forest/razorpay-orders.js
1
const { collection } = require('forest-express-mongoose');
2
3
collection('razorpayOrders', {
4
actions: [],
5
fields: [{
6
field: 'amount',
7
type: 'Number',
8
},{
9
field: 'entity',
10
type: 'String',
11
},{
12
field: 'amount_paid',
13
type: 'String',
14
},{
15
field: 'amount_due',
16
type: 'String',
17
},{
18
field: 'currency',
19
type: 'INR',
20
},{
21
field: 'receipt',
22
type: 'String',
23
},{
24
field: 'status',
25
type: 'String',
26
},{
27
field: 'created_at',
28
type: 'Date',
29
}],
30
segments: [],
31
});
Copied!

Add relationships to virtual collections

You need to declare a relationship between the users collection and the virtual razorpayPayments and razorpayOrders collections in the forest/users.js file.
1
const { collection } = require('forest-express-mongoose');
2
3
collection('users', {
4
actions: [],
5
fields: [{
6
field: 'razorpayPayments',
7
type: ['String'],
8
reference: 'razorpayPayments.id',
9
},],
10
segments: [],
11
});
Copied!

Define route logic for the relationship

You now have to implement the logic to be executed to retrieve and send the information from Razorpay to the UI when the corresponding route is called.
This is done in the file routes/users.js. Remember that you need to properly serialize the objects in order for the UI to correctly display them, using the RecordsSerializer.
1
const express = require('express');
2
const { PermissionMiddlewareCreator, RecordSerializer } = require('forest-express-mongoose');
3
const superagent = require('superagent');
4
const { orders } = require('../models');
5
6
7
const router = express.Router();
8
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('users');
9
10
router.get('/users/:recordId/relationships/razorpayPayments', permissionMiddlewareCreator.details(), async (request, response, next) => {
11
const razorpayPaymentSerializer = new RecordSerializer({modelName: 'razorpayPayments'});
12
const { recordId } = request.params;
13
const userOrders = await orders.find({ user: recordId });
14
const fetchPaymentsFromApi = [];
15
userOrders.forEach((record) => {
16
let fetchPayment = superagent.get(`https://${process.env.RAZORPAY_KEY_ID}:${process.env.RAZORPAY_KEY_SECRET}@api.razorpay.com/v1/payments/${record.payment_ref}`)
17
.then((res) => JSON.parse(res.text))
18
fetchPaymentsFromApi.push(fetchPayment);
19
});
20
return Promise.all(fetchPaymentsFromApi)
21
.then((res) => razorpayPaymentSerializer.serialize(res))
22
.then((recordsSerialized) => response.send({
23
...recordsSerialized,
24
meta: { count: recordsSerialized.data.length },
25
}))
26
.catch((e) => {
27
console.log(chalk.red('error with razorpay api call =>', e.response.res.text));
28
return response.send({});
29
});
30
});
31
32
router.get('/users/:recordId/relationships/razorpayOrders', permissionMiddlewareCreator.details(), async (request, response, next) => {
33
const razorpayOrderSerializer = new RecordSerializer({modelName: 'razorpayOrders'});
34
const { recordId } = request.params;
35
const userOrders = await orders.find({ user: recordId });
36
const fetchPaymentsFromApi = [];
37
userOrders.forEach((record) => {
38
let fetchPayment = superagent.get(`https://${process.env.RAZORPAY_KEY_ID}:${process.env.RAZORPAY_KEY_SECRET}@api.razorpay.com/v1/orders/${record.order_ref}`)
39
.then((res) => JSON.parse(res.text))
40
fetchPaymentsFromApi.push(fetchPayment);
41
});
42
return Promise.all(fetchPaymentsFromApi)
43
.then((res) => razorpayOrderSerializer.serialize(res))
44
.then((recordsSerialized) => response.send({
45
...recordsSerialized,
46
meta: { count: recordsSerialized.data.length },
47
}))
48
.catch((e) => {
49
console.log(chalk.red('error with razorpay api call =>', e.response.res.text));
50
return response.send({});
51
});
52
});
53
54
module.exports = router;
Copied!