-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
95 lines (82 loc) · 3.39 KB
/
app.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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const md5 = require('md5');
const dotenv = require('dotenv');
dotenv.config();
app.set('view engine', 'ejs');
app.use(express.static(__dirname+'/public'))
app.use(
express.urlencoded({
extended: true
})
);
app.use(express.json());
app.get('/',(req,res)=>{
res.render('index')
})
var isVerified; // return value for md5 verification check
app.post('/notify',async(req,res)=>{
//* Post parameters
isVerified = false;
var merchant_id = req.body.merchant_id; // PayHere Merchant ID of the merchant
var order_id = req.body.order_id; // Order ID sent by Merchant to Checkout page
var payment_id = req.body.payment_id; // Unique Payment ID generated by PayHere for the processed payment
var payhere_amount = req.body.payhere_amount; // Total Amount of the payment
var payhere_currency = req.body.payhere_currency; // Currency code of the payment (LKR/USD/GBP/EUR/AUD)
var method = req.body.method; // Payment method selected by the customer. (VISA, MASTER, AMEX, EZCASH, MCASH, GENIE, VISHWA, PAYAPP, HNB, FRIMI)
var status_code = req.body.status_code;
var status_message = req.body.status_message; // Message received from payment gateway which the customer tried to pay If the customer made the payment by VISA or MASTER credit/debit card, following parameters will also be available.
// ** for card payments
if(req.body.card_holder_name){
var card_holder_name = req.body.card_holder_name;// Card Holder Name
var card_no = req.body.card_no; // Masked card number (Ex: ************4564)
var card_expiry = req.body.card_expiry; // Card expiry in format MMYY (Ex: 0122) Payment Status Codes
}
//other custom parameters if available....
// handle transaction status here
switch(status_code){
case "0" :
console.log('Transaction is Pending');
break;
case "2" :
console.log("Transaction is Successful");
// *** verification of payment status ***
//generate local md5 signature
var localMd5Sig = (md5(merchant_id+order_id+payhere_amount+payhere_currency+status_code+md5(process.env.PAYHERE_SECRET).toUpperCase())).toUpperCase();
if(localMd5Sig===req.body.md5sig){
console.log('Verified')
isVerified=true;
// update the database with successfull transation details here
} else {
console.log("Unverified")
isVerified=false;
// something is wrong
}
break;
case "-1" :
console.log("Transaction Canceled");
break;
case "-2" :
console.log("Transaction Failed");
break;
case "-3":
console.log("Charged Back");
break;
default:
console.log("Unknown Error");
}
res.end();
})
app.get('/check',(req,res)=>{
res.send({"Verified":isVerified})
})
app.get('/return',(req,res)=>{ // redirect url after the transaction..(provided in the html form )
res.send('success');
})
app.get('/cancel',(req,res)=>{
res.send('cancel');
})
app.listen(3000 || process.env.PORT,()=>{
console.log("Listening on port 3000");
})