lumepay-sdk
Version:
SDK for Payment Gateway - Easily verify bank transfer payments
56 lines (44 loc) • 1.68 kB
JavaScript
// src/index.js
const HttpClient = require('./httpClient');
const { PaymentGatewayError } = require('./errors');
class PaymentGateway {
constructor({ apiKey, baseUrl = 'https://lumepay.pyrrho.dev/' }) {
if (!apiKey) throw new PaymentGatewayError('API Key is required.');
this.apiKey = apiKey;
this.baseUrl = baseUrl;
this.http = new HttpClient({ apiKey, baseUrl });
}
async createIntent({ amount, customerEmail, metadata = {} }) {
if (!amount || typeof amount !== 'number') {
throw new PaymentGatewayError('Amount must be a valid number.');
}
const response = await this.http.post('api/payment/intent/create', {
amount,
customerEmail,
metadata,
});
return response.intent;
}
async getIntent(intentId) {
if (!intentId) throw new PaymentGatewayError('Intent ID is required.');
const response = await this.http.get(`api/payment/intent/${intentId}`);
return response.intent;
}
async submitPayment({ intentId, transactionId }) {
if (!intentId || !transactionId) {
throw new PaymentGatewayError('Intent ID and Transaction ID are required.');
}
const response = await this.http.post(`api/payment/intent/${intentId}/pay`, {
transactionId,
});
return response;
}
async getAccountDetails() {
const response = await this.http.get('api/user/settings');
if (!response.settings?.bankDetails) {
throw new PaymentGatewayError('Bank details not found. Please set up your bank details in the dashboard.');
}
return response.settings.bankDetails;
}
}
module.exports = PaymentGateway;