-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-usage.js
More file actions
110 lines (94 loc) · 2.92 KB
/
basic-usage.js
File metadata and controls
110 lines (94 loc) · 2.92 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Basic Usage Example - Manual Confirmation
* Start FREE with zero dependencies
*/
const PayByTransfer = require("pay-by-transfer");
// Initialize with manual provider (FREE!)
const payment = new PayByTransfer({
provider: "manual",
account: {
number: "70XXXXX311",
name: "Your Business",
bank: "Moniepoint",
},
});
// Listen for successful payments
payment.on("payment.confirmed", (data) => {
console.log("🎉 Payment confirmed!");
console.log("Reference:", data.reference);
console.log("Amount:", data.amount / 100, "NGN");
console.log("Paid at:", data.paidAt);
// Update your database here
// updateOrderStatus(data.reference, 'paid');
});
// Listen for errors
payment.on("error", (error) => {
console.error("❌ Error:", error.message);
});
// Create a payment session
async function createPayment() {
try {
const session = await payment.create({
amount: 7700, // Amount in kobo (₦77)
reference: "ORDER_12345",
customerEmail: "customer@example.com",
});
console.log("✅ Payment session created:");
console.log("Account Number:", session.accountNumber);
console.log("Account Name:", session.accountName);
console.log("Bank:", session.bankName);
console.log("Amount:", session.amount / 100, "NGN");
console.log("Reference:", session.reference);
console.log("Expires:", session.expiresAt);
console.log("\n📱 Customer should transfer money to the account above\n");
return session;
} catch (error) {
console.error("Failed to create payment:", error.message);
}
}
// Check payment status manually
async function checkPaymentStatus(reference) {
try {
const status = await payment.check(reference);
console.log("Payment status:", status);
return status;
} catch (error) {
console.error("Failed to check status:", error.message);
}
}
// Manually confirm payment (after checking bank alert)
async function confirmPayment(reference) {
try {
await payment.provider.confirmPayment(reference, {
transactionId: "BANK_TXN_123",
confirmedBy: "admin",
});
console.log("✅ Payment manually confirmed");
} catch (error) {
console.error("Failed to confirm:", error.message);
}
}
// Example usage
async function main() {
// 1. Create payment
const session = await createPayment();
// 2. Customer transfers money...
console.log("Waiting for customer to transfer...");
console.log(
"After they transfer, you check your bank alert and confirm manually\n"
);
// 3. Manually confirm (you do this after checking bank alert)
setTimeout(async () => {
console.log("Simulating manual confirmation...");
await confirmPayment("ORDER_12345");
}, 2000);
// 4. Check status
setTimeout(async () => {
await checkPaymentStatus("ORDER_12345");
}, 3000);
}
// Run if executed directly
if (require.main === module) {
main();
}
module.exports = { createPayment, checkPaymentStatus, confirmPayment };