payme-merchant-integrator
v1.2.13
Published
Integration with payme
Downloads
24
Maintainers
Readme
Payme merchant api integration with mongodb database based application
Author
Install
npm install tslib // also to work in javascript
npm install payme-merchant-integrator
Usage
Import PaymeIntegrator
class
import { PaymeIntegrator } from 'payme-merchant-integrator';
And initialize paymeIntegrator
object based class
const paymeIntegrator = new PaymeIntegrator({
db_str: process.env.MONGODB_URL,
collection: 'payme_transactions', // collection name to save transactions
type: 'cumulative', // enum ['one-time', 'cumulative'] one time fee or cumulative
password: process.env.PAYME_PASSWORD,
isAccountExist,
markAsPaid,
getPayingCost, // optional for 'cumulative' type
canCancel, // method to check cancellation
markAsCancel, // method to mark as cancel
})
There two types of paymeIntegrator
- to pay for balance increase
cumulative
fee - to pay for check means
one-time
fee
And add methods below for cumulative
fee
Check account if exists or not
argument account
is an object that created in payme cabinet
For example phone
added to account object like this:
then isAccountExist
method should be
const isAccountExist = async (account) => {
const is_user_exist = await UserModel.findOne({phone: account.phone});
if(!is_user_exist) return false;
return true;
}
To increase balance after payment
const markAsPaid = async (account, amount) => {
await UserModel.findOneAndUpdate(
{phone: account.phone},
{ $inc: { balance: amount } }
);
}
To check cancel after payment
const canCancel = async (account) => {
return false; // for no cancellation;
// or check and return true or false
}
To mark as cancel after cancellation
const markAsCancel = async (account) => {
// write function to update balance or cancel paid transaction
}
And add methods below for one-time
fee
then isAccountExist
method should be
const isAccountExist = async (account) => {
const is_user_exist = await CheckModel.findOne({check_id: account.check_id});
if(!is_user_exist) return false;
return true;
}
To get get paying cost
const getPayingCost = async (account) => {
const check = await CheckModel.findOne({check_id: account.check_id});
return check.cost;
}
mark as paid
const markAsPaid = async (account, amount) => {
await CheckModel.findOneAndUpdate(
{check_id: account.check_id},
{ $set: { paid: true } }
);
}
Then use from route and middleware
Fastify example
const authenticate = async (request, reply) => {
await paymeIntegrator.authenticate(request, reply);
}
const handler = async (request, reply) => {
return await paymeIntegrator.handler(request, reply);
}
fastify.post(
'/payme',
{ preValidation: [authenticate] },
handler
);
And test your endpoint with PaymeTester
Official documentation Payme Merchant