UNPKG

@tecafrik/africa-payment-sdk

Version:

A single SDK to integrate all african payment providers seamlessly

146 lines 7.56 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const payment_provider_interface_1 = require("../payment-provider.interface"); const payment_events_1 = require("../payment-events"); class BogusPaymentProvider { constructor(config) { this.config = config; } useEventEmitter(eventEmitter) { this.eventEmitter = eventEmitter; } checkout(options, isFailure) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { if (this.config.instantEvents) { (_a = this.eventEmitter) === null || _a === void 0 ? void 0 : _a.emit(payment_events_1.PaymentEventType.PAYMENT_INITIATED, { type: payment_events_1.PaymentEventType.PAYMENT_INITIATED, transactionAmount: options.amount, transactionCurrency: options.currency, transactionId: options.transactionId, transactionReference: `${options.paymentMethod.toLowerCase()}-transaction-reference`, paymentMethod: payment_provider_interface_1.PaymentMethod.WAVE, metadata: options.metadata, paymentProvider: BogusPaymentProvider.name, }); if (isFailure) { (_b = this.eventEmitter) === null || _b === void 0 ? void 0 : _b.emit(payment_events_1.PaymentEventType.PAYMENT_FAILED, { type: payment_events_1.PaymentEventType.PAYMENT_FAILED, transactionAmount: options.amount, transactionCurrency: options.currency, transactionId: options.transactionId, transactionReference: `${options.paymentMethod.toLowerCase()}-transaction-reference`, paymentMethod: payment_provider_interface_1.PaymentMethod.WAVE, metadata: options.metadata, reason: "Payment failed", paymentProvider: BogusPaymentProvider.name, }); } else { (_c = this.eventEmitter) === null || _c === void 0 ? void 0 : _c.emit(payment_events_1.PaymentEventType.PAYMENT_SUCCESSFUL, { type: payment_events_1.PaymentEventType.PAYMENT_SUCCESSFUL, transactionAmount: options.amount, transactionCurrency: options.currency, transactionId: options.transactionId, transactionReference: `${options.paymentMethod.toLowerCase()}-transaction-reference`, paymentMethod: payment_provider_interface_1.PaymentMethod.WAVE, metadata: options.metadata, paymentProvider: BogusPaymentProvider.name, }); } } return { transactionAmount: options.amount, transactionCurrency: options.currency, transactionId: options.transactionId, transactionReference: `${options.paymentMethod.toLowerCase()}-transaction-reference`, transactionStatus: payment_provider_interface_1.TransactionStatus.PENDING, redirectUrl: isFailure ? options.failureRedirectUrl : options.successRedirectUrl, }; }); } checkoutMobileMoney(options) { var _a; return __awaiter(this, void 0, void 0, function* () { let isFailure = false; if (options.paymentMethod === payment_provider_interface_1.PaymentMethod.WAVE) { isFailure = options.customer.phoneNumber.endsWith("13"); } else if (options.paymentMethod === payment_provider_interface_1.PaymentMethod.ORANGE_MONEY) { isFailure = ((_a = options.authorizationCode) === null || _a === void 0 ? void 0 : _a.endsWith("13")) || false; } return this.checkout(options, isFailure); }); } checkoutCreditCard(options) { var _a; return __awaiter(this, void 0, void 0, function* () { const isFailure = ((_a = options.cardNumber) === null || _a === void 0 ? void 0 : _a.endsWith("13")) || false; return this.checkout(options, isFailure); }); } checkoutRedirect(options) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const isFailure = (_b = (_a = options.customer.email) === null || _a === void 0 ? void 0 : _a.endsWith("failure.com")) !== null && _b !== void 0 ? _b : false; return this.checkout(options, isFailure); }); } refund(options) { return __awaiter(this, void 0, void 0, function* () { console.debug("Refunding transaction", options.transactionId, options); return { transactionAmount: options.refundedAmount || 0, transactionCurrency: payment_provider_interface_1.Currency.XOF, transactionId: options.transactionId, transactionReference: `refunded-transaction-reference`, transactionStatus: payment_provider_interface_1.TransactionStatus.PENDING, }; }); } handleWebhook(rawBody, options) { var _a; return __awaiter(this, void 0, void 0, function* () { const body = JSON.parse(rawBody.toString()); const isFailure = body.success === false; const eventType = isFailure ? payment_events_1.PaymentEventType.PAYMENT_FAILED : payment_events_1.PaymentEventType.PAYMENT_SUCCESSFUL; const event = { type: eventType, transactionAmount: body.amount, transactionCurrency: body.currency, transactionId: body.transactionId, transactionReference: body.transactionReference, paymentMethod: body.paymentMethod, metadata: body.metadata, paymentProvider: BogusPaymentProvider.name, }; (_a = this.eventEmitter) === null || _a === void 0 ? void 0 : _a.emit(eventType, event); return event; }); } payoutMobileMoney(options) { console.debug("Paying out mobile money", options); return Promise.resolve({ transactionAmount: options.amount, transactionCurrency: options.currency, transactionId: options.transactionId, transactionReference: `payout-transaction-reference`, transactionStatus: payment_provider_interface_1.TransactionStatus.SUCCESS, }); } } exports.default = BogusPaymentProvider; //# sourceMappingURL=bogus.js.map