Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Chargily/chargily-epay-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chargily ePay Gateway Js

Chargily ePay Gateway

Integrate ePayment gateway with Chargily easily.

  • Currently support payment by CIB / EDAHABIA cards and soon by Visa / Mastercard
  • This is a Js package, If you are using another programing language Browse here or look to API documentation

Requirements

  1. Node 8, 10 or higher.
  2. NPM
  3. Get your API Key/Secret from ePay by Chargily dashboard for free

Installation

  1. Via npm (Recomended)
npm i chargily-epay-gateway
# or
yarn add chargily-epay-gateway

Quick start

Add CHARGILY_APP_KEY and CHARGILY_APP_SECRET in .env file with the secret key and app key from ePay Dashboard

#Usage The package needs to be configured with your account's secret key, which is available in the ePay Dashboard. Require it with the key's value:

const chargily = require('chargily-epay-js');
const dotenv = require('dotenv');
const { Invoice, Mode } = require('chargily-epay-js/lib/configuration');

dotenv.config();

const order = new Invoice();
order.invoiceNumber = '100'; // must be integer or string
order.mode = Mode.EDAHABIA; // or Mode.CIB
order.backUrl = 'https://www.exemple.org/'; // must be a valid and active URL
order.amount = 5000; // must be integer , and more or equal 75
order.webhookUrl = 'https://www.exemple.org/webhook-validator'; // this URL where receive the response
order.client = 'Abderraouf Zine';
order.discount = 10; // by percentage between [0, 100]
order.clientEmail = 'client@example.com'; // email of customer where he will receive the Bill
order.appKey = process.env.CHARGILY_APP_KEY;

// createPayment is promise function (async, await ), so you will need to use then to receive the checkoutURL

const checkoutUrl = chargily.createPayment(order).then((res) => {
  return res.checkout_url; // redirect to this url to proccess the checkout
});

Configurations

  • Available Configurations
key description redirect url process url
CHARGILY_APP_KEY must be string given by organization required required
CHARGILY_APP_SECRET must be string given by organization required required
back_url must be string and valid url required not required
webhook_url must be string and valid url _ required required
mode must be in CIB,EDAHABIA required not required
invoice_number string or int required not required
client_name string required not required
clientEmail must be valid email This is where client receive payment receipt after confirmation required not required
amount must be numeric and greather or equal than 75 required not required
discount must be numeric and between 0 and 99 (discount in %) required not required
description must be string_ required not required

Testing Webhook signing

You can use DefaultSignatureValidator.isValid() to validate incoming webhook, as demonstrated in the src/examples/validate-signature.ts file.

import { DefaultSignatureValidator } from '../classes/Webhook';
import express from 'express';
import dotenv from 'dotenv';
import bodyParser from 'body-parser';
const PORT = 4000;
const app = express();
const sigHeaderName = 'Signature';

// load env variables
dotenv.config();

// format requests
app.use(
  bodyParser.json({
    verify: (req: any, _res, buf, encoding: any) => {
      if (buf && buf.length) {
        req.rawBody = buf.toString(encoding || 'utf8');
      }
    },
  })
);

app.post('/validator', (req: any, res) => {
  const signature = Buffer.from(req.get(sigHeaderName) || '', 'utf8');
  let rs = DefaultSignatureValidator.isValid(
    signature,
    process.env.CHARGILY_APP_SECRET!,
    req.rawBody
  );
  // returns true if the signature is valid, throws error if invalid

  res.send(rs);
});

app.listen(PORT, () => {
  console.log('🪝 Webhook live at http://localhost:' + PORT);
});