-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-paystack.js
More file actions
45 lines (41 loc) · 1.3 KB
/
config-paystack.js
File metadata and controls
45 lines (41 loc) · 1.3 KB
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
const axios = require("axios");
require("dotenv").config();
const PAYSTACK_SECRET_KEY = process.env.PAYSTACK_SECRET_KEY;
const FRONTEND_URL = process.env.FRONTEND_URL;
const paystack = {
initializePayment: async (email, amount) => {
try {
const response = await axios.post(
"https://api.paystack.co/transaction/initialize",
{
email,
amount: amount * 100, // Convert Naira to Kobo
currency: "NGN",
callback_url: `${FRONTEND_URL}/payment-success`,
},
{
headers: { Authorization: `Bearer ${PAYSTACK_SECRET_KEY}` },
}
);
return response.data;
} catch (error) {
console.error("Error initializing payment:", error.response?.data || error.message);
throw new Error("Payment initialization failed");
}
},
verifyPayment: async (reference) => {
try {
const response = await axios.get(
`https://api.paystack.co/transaction/verify/${reference}`,
{
headers: { Authorization: `Bearer ${PAYSTACK_SECRET_KEY}` },
}
);
return response.data;
} catch (error) {
console.error("Error verifying payment:", error.response?.data || error.message);
throw new Error("Payment verification failed");
}
},
};
module.exports = paystack;