Created
July 10, 2019 16:04
-
-
Save iamnasirudeen/9a98d62d8ab71f30657611a81776a439 to your computer and use it in GitHub Desktop.
Paystack recurring payment
This file contains 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
import axios from 'axios'; | |
const createCustomer = async form => { | |
const options = { | |
url: "https://api.paystack.co/customer", | |
headers: { | |
'authorization': `Bearer ${process.env.PAYSTACK_SECRET_KEY}`, | |
'content-type': 'application/json', | |
'cache-control': 'no-cache' | |
}, | |
method: 'POST', | |
data: form | |
}; | |
return new Promise(async (resolve, reject) => { | |
try { | |
let customerInfo = await axios.request(options); | |
resolve(customerInfo); | |
} catch (error) { | |
reject(error); | |
}; | |
}); | |
}; | |
const initializePayment = async form => { | |
const options = { | |
url: 'https://api.paystack.co/transaction/initialize', | |
headers: { | |
'authorization': `Bearer ${process.env.PAYSTACK_SECRET_KEY}`, | |
'content-type': 'application/json', | |
'cache-control': 'no-cache' | |
}, | |
method: 'POST', | |
data: form | |
}; | |
return new Promise(async (resolve, reject) => { | |
try { | |
const response = await axios.request(options); | |
resolve(response.data); | |
} catch (error) { | |
reject(error); | |
}; | |
}); | |
}; | |
const verifyPayment = async ref => { | |
const options = { | |
url: 'https://api.paystack.co/transaction/verify/' + encodeURIComponent(ref), | |
headers: { | |
'authorization': `Bearer ${process.env.PAYSTACK_SECRET_KEY}`, | |
'content-type': 'application/json', | |
'cache-control': 'no-cache' | |
}, | |
method: 'GET' | |
}; | |
return new Promise(async (resolve, reject) => { | |
try { | |
const data = await axios.request(options) | |
resolve(data); | |
} catch (error) { | |
reject(error); | |
}; | |
}); | |
}; | |
const createPlan = async planInfo => { | |
const options = { | |
url: 'https://api.paystack.co/plan', | |
headers: { | |
'authorization': `Bearer ${process.env.PAYSTACK_SECRET_KEY}`, | |
'content-type': 'application/json', | |
'cache-control': 'no-cache' | |
}, | |
method: 'POST', | |
data: planInfo // params are : plan name, interval, amount | |
}; | |
return new Promise(async (resolve, reject) => { | |
try { | |
const response = await axios.request(options); | |
resolve(response.data); | |
} catch (error) { | |
reject(error); | |
}; | |
}); | |
}; | |
const updatePlan = async (planInfo, planId) => { | |
const options = { | |
url: `https://api.paystack.co/plan/${encodeURIComponent(planId)}`, | |
headers: { | |
'authorization': `Bearer ${process.env.PAYSTACK_SECRET_KEY}`, | |
'content-type': 'application/json', | |
'cache-control': 'no-cache' | |
}, | |
method: 'POST', | |
data: planInfo // params are : plan name, interval, amount | |
}; | |
}; | |
const subscriberUser = async form => { | |
const options = { | |
url: 'https://api.paystack.co/subscription', | |
headers: { | |
'authorization': `Bearer ${process.env.PAYSTACK_SECRET_KEY}`, | |
'content-type': 'application/json', | |
'cache-control': 'no-cache' | |
}, | |
method: 'POST', | |
data: form // params are : customerId, planId | |
}; | |
return new Promise(async (resolve, reject) => { | |
try { | |
const response = await axios.request(options); | |
resolve(response.data); | |
} catch (error) { | |
reject(error); | |
}; | |
}); | |
}; | |
module.exports = { createCustomer, initializePayment, verifyPayment, createPlan, updatePlan, subscriberUser }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment