-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
147 lines (143 loc) · 4.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var crypto = require('crypto');
var checkPostVariable = function(req){
if(!req.body.hasOwnProperty("buyer_name")){
return false;
}
if(!req.body.hasOwnProperty("offer_title"))
{
return false;
}
if(!req.body.hasOwnProperty("fees"))
{
return false;
}
if(!req.body.hasOwnProperty("payment_id"))
{
return false;
}
if(!req.body.hasOwnProperty("variants"))
{
return false;
}
if(!req.body.hasOwnProperty("custom_fields"))
{
return false;
}
if(!req.body.hasOwnProperty("currency"))
{
return false;
}
if(!req.body.hasOwnProperty("status"))
{
return false;
}
if(!req.body.hasOwnProperty("amount"))
{
return false;
}
if(!req.body.hasOwnProperty("buyer_phone"))
{
return false;
}
if(!req.body.hasOwnProperty("offer_slug"))
{
return false;
}
if(!req.body.hasOwnProperty("buyer"))
{
return false;
}
if(!req.body.hasOwnProperty("mac"))
{
return false;
}
if(!req.body.hasOwnProperty("quantity"))
{
return false;
}
if(!req.body.hasOwnProperty("unit_price"))
{
return false;
}
return true;
}
module.exports = function(options) {
return function(req, res, next) {
var secret = options.secretKey;
var generated_string = "";
var generated_symbol = "|";
//creating instance for instamojo with request object
req.instamojo = {
webhookStatus : false,
amount : "",
buyer_name : "",
buyer_phone : "",
currency : "",
custom_fields : "",
fees : "",
offer_slug : "",
offer_title : "",
payment_id : "",
quantity : "",
status : "",
unit_price : "",
variants : "",
reason : ""
};
if(req.hasOwnProperty("body"))
{
if(checkPostVariable(req))
{
generated_string = req.body.amount + generated_symbol;
generated_string = generated_string + req.body.buyer + generated_symbol;
generated_string = generated_string + req.body.buyer_name + generated_symbol;
generated_string = generated_string + req.body.buyer_phone + generated_symbol;
generated_string = generated_string + req.body.currency + generated_symbol;
generated_string = generated_string + req.body.custom_fields + generated_symbol;
generated_string = generated_string + req.body.fees + generated_symbol;
generated_string = generated_string + req.body.offer_slug + generated_symbol;
generated_string = generated_string + req.body.offer_title + generated_symbol;
generated_string = generated_string + req.body.payment_id + generated_symbol;
generated_string = generated_string + req.body.quantity + generated_symbol;
generated_string = generated_string + req.body.status + generated_symbol;
generated_string = generated_string + req.body.unit_price + generated_symbol;
generated_string = generated_string + req.body.variants;
var hash = crypto.createHmac('sha1', secret).update(generated_string).digest('hex');
if(hash === req.body.mac)
{
req.instamojo.webhookStatus = true;
req.instamojo.amount = req.body.amount;
req.instamojo.buyer_name = req.body.buyer_name;
req.instamojo.buyer_phone = req.body.buyer_phone;
req.instamojo.currency = req.body.currency;
req.instamojo.custom_fields = req.body.custom_fields;
req.instamojo.fees = req.body.fees;
req.instamojo.offer_slug = req.body.offer_slug;
req.instamojo.offer_title = req.body.offer_title;
req.instamojo.payment_id = req.body.payment_id;
req.instamojo.quantity = req.body.quantity;
req.instamojo.status = req.body.status;
req.instamojo.unit_price = req.body.unit_price;
req.instamojo.variants = req.body.variants;
req.instamojo.reason = "successful transactions";
}
else
{
req.instamojo.webhookStatus = false;
req.instamojo.reason = "HMAC-SHA1 authentication didnt succeed";
}
}
else
{
req.instamojo.webhookStatus = false;
req.instamojo.reason = "instamojo key pair post data missing";
}
}
else
{
req.instamojo.webhookStatus = false;
req.instamojo.reason = "couldnt retrieve post data or form data! Try using body-parser above instamojo webhook middleware";
}
next();
}
}