payme-sdk
Version: 
A Node.js SDK to integrate with PAYME API
185 lines (184 loc) • 6.58 kB
JavaScript
"use strict";
// src/payme-sdk.ts
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const dotenv = __importStar(require("dotenv"));
const _ = __importStar(require("lodash"));
const external_data_service_1 = require("./services/external-data.service");
class PayMeSDK {
    constructor(email, password, apiKey) {
        // Set up dotenv
        dotenv.config({ path: '.env' });
        this.email = email;
        this.password = password;
        this.apiKey = apiKey;
        this.token = '';
        this.externalDataService = new external_data_service_1.ExternalDataService();
    }
    /**
     * This function check if a merchant with a specific key exists
     * @param key apiKey of the merchant
     */
    async init() {
        const resp = await this.externalDataService.postDataFromOnboarding(`users/developer/authenticate`, {
            email: this.email,
            password: this.password,
            subscription_key: this.apiKey,
        });
        if (!resp || resp.length == 0) {
            throw new Error('Unable to fetch account related to the key provided');
        }
        this.merchant = resp.data.user;
        this.account = this.merchant.individualProfiles[0];
        this.token = resp.data.token;
        console.log(this.account, this.token);
        this.externalDataService.setToken(this.token);
    }
    /**
     * This function calculate the applicable fees of a payment
     * @param amount amount of the payment
     * @param country country where the payment will be done
     */
    async getFees(amount, country) {
        const resp = await this.externalDataService.getDataFromBilling(`fees?filter={"where":{"min_amount":{"lte": ${amount}},"max_amount":{"gt": ${amount}}}}`);
        if (!resp || resp.length == 0) {
            throw new Error('Fees has not yet been define for this amount, please contact support');
        }
        const fees = _.pick(resp[0], [
            'operation_type',
            'corridor_tag',
            'operand',
            'min_amount',
            'max_amount',
            'value',
        ]);
        return fees;
    }
    /**
     * This function register a payment / simple / partial / grouped
     * @param param payment parameters
     */
    async postPayment(param) {
        let data = {
            ...param,
            account_id: this.account.id,
        };
        const resp = this.externalDataService.postDataFromPayment('transactions', data);
        return resp;
    }
    /**
     * This function check the status of a payment
     * @param reference uniq reference of the transaction
     */
    async getPaymentStatus(reference) {
        const resp = await this.externalDataService.getDataFromPayment(`transactions?filter={"where":{"reference":"${reference}"}}`);
        if (!resp || resp.length == 0) {
            throw new Error('Fees has not yet been define for this amount, please contact support');
        }
        const payment = _.pick(resp[0], [
            'reference',
            'account_id',
            'amount',
            'fees',
            'tva',
            'description',
            'status',
            'created_at',
            'updated_at',
        ]);
        return payment;
    }
    /**
     * This function init the payment of a customer
     * @param param parameters of the item payment
     */
    async postPaymentItem(param) {
        const resp = this.externalDataService.postDataFromPayment('payment-items', param);
        return resp;
    }
    /**
     * This function check the status of a payment item
     * @param reference uniq reference of the payment item
     */
    async getPaymentItemStatus(reference) {
        const resp = await this.externalDataService.getDataFromPayment(`payment-items?filter={"where":{"reference":"${reference}"}}`);
        if (!resp || resp.length == 0) {
            throw new Error('Fees has not yet been define for this amount, please contact support');
        }
        const payment_item = _.pick(resp[0], [
            'reference',
            'payment_id',
            'customer_id',
            'amount',
            'fees',
            'phone',
            'payment_method',
            'payment_proof',
            'status',
            'created_at',
            'updated_at',
        ]);
        return payment_item;
    }
    /**
     * This function return the payment and his associated payment items
     * @param reference uniq reference of the payment
     */
    async getPaymentWithItems(reference) {
        const resp = await this.externalDataService.getDataFromPayment(`transactions?filter={"where":{"reference":"${reference}"}, "include":["paymentItems"]}`);
        if (!resp || resp.length == 0) {
            throw new Error('');
        }
        const payment = _.pick(resp[0], [
            'reference',
            'account_id',
            'payment_type',
            'amount',
            'fees',
            'tva',
            'description',
            'status',
            'created_at',
            'updated_at',
            'paymentItems',
        ]);
        const items = resp[0].paymentItems.map((item) => _.pick(item, [
            'reference',
            'payment_id',
            'customer_id',
            'amount',
            'fees',
            'phone',
            'payment_method',
            'payment_proof',
            'status',
            'created_at',
            'updated_at',
        ]));
        payment.paymentItems = items;
        return payment;
    }
}
exports.default = PayMeSDK;