Created
September 14, 2020 16:56
-
-
Save cptiwari20/1f5b332eb6c75e995cc1f6f468b4cebe to your computer and use it in GitHub Desktop.
Mange Pabbly payment in nodejs using pabbly Checkout Page
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 url = "https://payments.pabbly.com/api/v1"; | |
| const auth = { | |
| auth: { | |
| username: process.env.PABBLY_API_KEY, | |
| password: process.env.PABBLY_API_SECRET | |
| } | |
| } | |
| // VerifyToken and create subscription | |
| // Pabbly returns a hostedpage token after successful redirection, we can fetch that and use it for subscribing users. | |
| const verifyHostedPage = async (req, res, next) => { | |
| try { | |
| if (!req.body.hostedpage) return res.status(400).json({ status: false, message: 'Hosted Page Token is required.' }) | |
| const data = { | |
| hostedpage: req.body.hostedpage | |
| } | |
| // check if the payment token already exists | |
| const checkToken = await Subscriptions.find({ hostedpage: req.body.hostedpage }); | |
| if (checkToken.length !== 0) return res.status(400).json({ status: false, message: 'Subscription Already exists.' }) | |
| // Check the hosted data | |
| const response = await Axios.post(`${url}/verifyhosted`, data, auth); | |
| if (response.data.status !== 'success') return res.status(400).json({ status: false, message: response.data.message }) | |
| // check token //its optional, I had used this for verifying the existing users. | |
| const authorization = req.headers['authorization'] | |
| if (!authorization || typeof authorization === 'undefined' ) { // if no token | |
| const subscribedUser = await Users.findOne({email: response.data.data.email_id}); | |
| // check the email address | |
| if(!subscribedUser) { // if not already exists | |
| return await createUserAndSubscribe({subscriptionData:response.data, hostedpage: req.body.hostedpage}, res) | |
| }else{ // if already exists | |
| return await findUserAndSubscribe({ | |
| user: subscribedUser, | |
| subscriptionData:response.data, | |
| hostedpage: req.body.hostedpage | |
| }, res) | |
| } | |
| } else{ // if token, verify it | |
| req.token = authorization | |
| jwt.verify(req.token, process.env.SECRET, async (err, profile) => { | |
| // if there is no user | |
| if (err) await createUserAndSubscribe({subscriptionData:response.data, hostedpage: req.body.hostedpage}, res) | |
| }); | |
| // If there is current USer Id | |
| const profile = jwt.decode(req.token) | |
| let currentUserId = profile.id | |
| const user = await Users.findById(currentUserId); | |
| return await findUserAndSubscribe({user, subscriptionData:response.data, hostedpage: req.body.hostedpage}, res) | |
| } | |
| } catch (error) { | |
| res.status(500).json({ status: false, message: error.message }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment