Skip to content

Commit bc181a4

Browse files
committed
functionality added
1 parent 2a66684 commit bc181a4

23 files changed

+12715
-37
lines changed

payment gateway/config/paystack.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
const request=require('request');
1+
var request = require('request');
22

3-
const paystack=function(request){
4-
const secretkey='sk_test_adf1b701bf31970a670478a66ca0b2902eb2cfd0';
3+
const paystack = (req) => {
4+
const MySecretKey = 'sk_test_adf1b701bf31970a670478a66ca0b2902eb2cfd0';
55

6-
const initializepayment=function(form, cb){
7-
const options={
6+
const initializePayment = (form, mycallback) => {
7+
const options = {
88
url : 'https://api.paystack.co/transaction/initialize',
99
headers : {
10-
authorization : secretkey,
11-
'content-type' : 'application/json',
12-
'cache-control' : 'no-cache'
10+
authorization: MySecretKey,
11+
'content-type': 'application/json',
12+
'cache-control': 'no-cache'
1313
},
1414
form
1515
}
16-
const callback=(error,response,body)=>{
17-
return cb(error,body);
16+
const callback = (error, response, body) => {
17+
return mycallback(error, body)
1818
}
19-
request.post(options,callback);
20-
};
21-
22-
23-
const verifypayment=function(ref,cb){
19+
request.post(options, callback)
20+
}
2421

22+
const verifyPayment = (ref, mycallback) => {
2523
const options = {
2624
url : 'https://api.paystack.co/transaction/verify/'+encodeURIComponent(ref),
2725
headers : {
28-
authorization: secretkey,
26+
authorization: MySecretKey,
2927
'content-type': 'application/json',
30-
'cache-control': 'no-cache'
31-
}
28+
'cache-control': 'no-cache'
29+
}
3230
}
33-
const callback=(err,response,body)=>{
34-
return cb(err,body);
35-
};
36-
request(options,callback);
37-
};
38-
return {initializepayment,verifypayment};
31+
const callback = (error, response, body) => {
32+
return mycallback(error, body)
33+
}
34+
request(options, callback)
35+
}
36+
37+
return {initializePayment, verifyPayment};
3938
}
40-
module.exports=paystack;
39+
40+
module.exports = paystack;

payment gateway/index.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const path=require('path');
77
const _=require('lodash');
88
const donor=require('./models/model');
99
const { request } = require('express');
10-
const {initializepayment, verifypayment}=require('./config/paystack')(request);
10+
const {initializePayment, verifyPayment}=require('./config/paystack')(request);
1111

1212
const app=express();
1313

@@ -27,12 +27,12 @@ app.get('/',function(req,res){
2727
})
2828

2929
app.post('/paystack/pay',function(req,res){
30-
const form=_.pick(req.body,['amount','email','name']);
30+
const form=_.pick(req.body,['amount','email','full_name']);
3131
form.metadata={
32-
name : form.name
32+
full_name : form.full_name
3333
}
3434
form.amount*=100;
35-
initializepayment(form,(error,body)=>{
35+
initializePayment(form,(error,body)=>{
3636
if(error) {
3737
console.log(error);
3838
return;
@@ -42,7 +42,41 @@ app.post('/paystack/pay',function(req,res){
4242
});
4343
});
4444

45-
45+
app.get('/paystack/callback', (req,res) => {
46+
const ref = req.query.reference;
47+
verifyPayment(ref, (error,body)=>{
48+
if(error){
49+
//handle errors appropriately
50+
console.log(error)
51+
return res.redirect('/error');
52+
}
53+
response = JSON.parse(body);
54+
const data = _.at(response.data, ['reference', 'amount','customer.email', 'metadata.full_name']);
55+
[reference, amount, email, full_name] = data;
56+
newDonor = {reference, amount, email, full_name}
57+
const donor = new Donor(newDonor)
58+
donor.save().then((donor)=>{
59+
if(!donor){
60+
res.redirect('/error');
61+
}
62+
res.redirect('/receipt/'+donor._id);
63+
}).catch((e)=>{
64+
res.redirect('/error');
65+
});
66+
});
67+
});
68+
app.get('/receipt/:id', (req, res)=>{
69+
const id = req.params.id;
70+
Donor.findById(id).then((donor)=>{
71+
if(!donor){
72+
//handle error when the donor is not found
73+
res.redirect('/error')
74+
}
75+
res.render('success.pug',{donor});
76+
}).catch((e)=>{
77+
res.redirect('/error')
78+
});
79+
});
4680
const port=process.env.PORT||3000;
4781
app.listen(port,()=>{
4882
console.log(`app is live at ${port}`);

payment gateway/models/model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const mongoose=require('mongoose');
22

33
const donorschema=mongoose.Schema({
4-
name:{
4+
full_name:{
55
type : String,
66
required : true
77
},

payment gateway/node_modules/request/package.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)