Unofficial Behpardakht Mellat Gateway implementation in Node.JS
Install the package from 'npm' or 'yarn'.
npm install mellat-checkout
yarn add mellat-checkout
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
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);
});
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);
});
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);
});
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);
}
});
- bpPayRequest
- bpVerifyRequest
- bpSettleRequest
- bpInquiryRequest
- bpReversalRequest
- bpDynamicPayRequest
- bpCumulativeDynamicPayRequest
- verifyAndSettle (Verify the payment and settle/reverse it)
- JSDocs and code comments
- Unit tests using
mocha
Contributions are welcome. Please submit PRs or just file an Issue if you see something broken or in need of improving.