Last active
July 24, 2021 16:04
-
-
Save ghaffaru/12e69deec8ed738d0aa7427b4c8b24ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const axios = require("axios"); | |
| const Event = require("../models/Event"); | |
| const Payment = require("../models/Payment"); | |
| var paystack = require("../utils/paystackCall.js"); | |
| // Initialize payment | |
| exports.initialize_payment = (req, res) => { | |
| if (!req.headers["authorization"]) { | |
| return res.status(401).json({ message: "Unauthenticated" }); | |
| } | |
| axios | |
| .post( | |
| `https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=${process.env.FIREBASE_KEY}`, | |
| { | |
| idToken: req.headers["authorization"].split(" ")[1], | |
| } | |
| ) | |
| .then((response) => { | |
| let localId = response.data.users[0].localId; | |
| let email = response.data.users[0].email; | |
| Event.findById(req.body.eventKey) | |
| .exec() | |
| .then((response2) => { | |
| Payment.find({ userKey: localId, eventKey: req.body.eventKey }) | |
| .exec() | |
| .then((data) => { | |
| if (data.length > 0) { | |
| if (data[0].paid) { | |
| return res | |
| .status(422) | |
| .json({ message: "You have already paid for this event" }); | |
| } else if (!data[0].paid) { | |
| data[0] | |
| .deleteOne() | |
| .then(() => { | |
| paystack.payInitialize(req, res, email, localId); | |
| }) | |
| .catch((err) => { | |
| return res | |
| .status(500) | |
| .json({ message: "Something went wrong" }); | |
| }); | |
| } | |
| } else { | |
| // console.log(email); | |
| // Initialize paystack API | |
| paystack.payInitialize(req, res, email, localId); | |
| // | |
| } | |
| }); | |
| }) | |
| .catch((err) => { | |
| return res.status(404).json({ message: "Event not found" }); | |
| }); | |
| }) | |
| .catch((err) => { | |
| res.status(422).json({ message: "Invalid Token" }); | |
| }); | |
| }; | |
| exports.verifyPayment = (req, res) => { | |
| // if (!req.headers["authorization"]) { | |
| // return res.status(401).json({ message: "Unauthenticated" }); | |
| // } | |
| Payment.find({reference: req.params.reference}).exec().then(data => { | |
| if (data.length > 0) { | |
| axios.get(`https://api.paystack.co/transaction/verify/${req.params.reference}`, { | |
| headers: { | |
| Authorization: `Bearer ${process.env.PAYSTACK_SECRET_KEY}`, | |
| }, | |
| }).then(res => { | |
| if (res.data.status === 'success') { | |
| data[0].updateOne({ | |
| paid: true | |
| }).then(() => { | |
| return res.status(200).json({success: true}) | |
| }).catch(err => { | |
| return res.status(500).json({message: 'Something went wrong'}) | |
| }) | |
| } else { | |
| return res.status(400).json({message: 'Payment not completed'}) | |
| } | |
| }) | |
| } else { | |
| return res.status(404).json({message: 'Invalid reference'}) | |
| } | |
| }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment