npm install tslib // also to work in javascript
npm install payme-merchant-integrator
import { PaymeIntegrator } from 'payme-merchant-integrator';
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
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
}
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 } }
);
}
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