Skip to content

Unofficial NodeJS implementation of Behpardakht Mellat Gateway API.

License

Notifications You must be signed in to change notification settings

pouriaMaleki/mellat-checkout

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mellat Ckeckout:

Unofficial Behpardakht Mellat Gateway implementation in Node.JS

Installation

Install the package from 'npm' or 'yarn'.

NPM

npm install mellat-checkout

Yarn

yarn add mellat-checkout

Usage:

Create An Instance

Import the package:

const MellatCheckout = require('mellat-checkout');
// or (ES6):
import MellatCheckout from 'mellat-checkout';

Then create an instance:

const mellat = new MellatCheckout({
  terminalId: 'xxxxxxx',
  username: 'xxxxxxx',
  password: 'xxxxxxx',
  timeout: 10000 // int in millisecond, not required (defaults to 10 sec)
  apiUrl: 'https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl' // exists (and may updated) in bank documentation, not required (defaults to this)
});

mellat.initialize! // this is promise and will create client async
.then( () => console.log("Mellat client ready") )
.catch( (error) => console.log("Mellat client ends with error", error) ) // you can retry here

API

Payment Request:

mellat.paymentRequest({
  amount: 1000, // Payment Amount In Rials
  orderId: '12345678912', // OrderID Generated By You
  callbackUrl: 'https://call.back/mellat', // Payment Callback URL
  payerId: '0' // Optional
}).then(function (response) {
  if (response.resCode === '0') {
    console.log(response.refId);
  } else {
    console.warn('Gateway Error: ', response.resCode);
  }
}).catch(function (error) {
  console.error(error);
});

Payment Verification:

mellat.verifyPayment({
  orderId: '12345678912', // OrderID Used In Payment Request
  saleOrderId: '12345678912', // Get From Payment Callback Post Params
  saleReferenceId: '5142510', // Get From Payment Callback Post Params
}).then(function (response) {
  if (response.resCode === '0') {
    console.log("Verified, Calling settlePayment");
  } else {
    console.warn('Gateway Error: ', response.resCode);
  }
}).catch(function (error) {
  console.error(error);
});

Payment Settlement (Payment Finalization):

mellat.settlePayment({
  orderId: '12345678912', // OrderID Used In Payment Request
  saleOrderId: '12345678912', // Get From Payment Callback Post Params
  saleReferenceId: '5142510', // Get From Payment Callback Post Params
}).then(function (response) {
  if (response.resCode === '0') {
    console.log("Payment Is Done.");
  } else if (response.resCode === '45') {
    console.log("Payment Already Done(Settled Before).");    
  } else {
    console.warn('Gateway Error: ', response.resCode);
  }
}).catch(function (error) {
  console.error(error);
});

Callbacks Instead Of Promises

All methods can be called by using Callbacks instead of Promises, lets take paymentRequest as an example:

mellat.paymentRequest({
  amount: 1000, // Payment Amount In Rials
  orderId: '12345678912', // OrderID Generated By You
  callbackUrl: 'https://call.back/mellat', // Payment Callback URL
  payerId: '0' // Optional
}, function (error, response) {
  if (error) {
    console.error(error);
  } else if (response.resCode === '0') {
    console.log(response.refId);
  } else {
    console.warn('Gateway Error: ', response.resCode);
  }
});

TODO

API

  • bpPayRequest
  • bpVerifyRequest
  • bpSettleRequest
  • bpInquiryRequest
  • bpReversalRequest
  • bpDynamicPayRequest
  • bpCumulativeDynamicPayRequest

Helper Methods

  • verifyAndSettle (Verify the payment and settle/reverse it)

Code

  • JSDocs and code comments
  • Unit tests using mocha

Contributing

Contributions are welcome. Please submit PRs or just file an Issue if you see something broken or in need of improving.

About

Unofficial NodeJS implementation of Behpardakht Mellat Gateway API.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%