payme-integration
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

payme-integration

NestJS module for integrating Payme payment system into your NestJS applications.

Installation

npm install payme-integration

Features

  • Full Payme Merchant API implementation
  • Built-in authentication guard
  • MongoDB integration
  • TypeScript support
  • Error handling
  • Transaction state management

Usage

  1. Create PaymeStrategy:
import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model, Types } from 'mongoose'
import {
  IPaymeStrategy,
  Order,
  OrderStatus,
  PaymeTransaction,
} from 'payme-integration'

import {
  Donation,
  DonationDocument,
  PaymentProvider,
} from 'src/models/donation.schema'

@Injectable()
export class PaymeStrategy implements IPaymeStrategy {
  constructor(
    @InjectModel(Donation.name)
    private readonly donationModel: Model<DonationDocument>,
  ) {}

  async getOrderByAccount(account: { user_id: string }): Promise<Order | null> {
    const donation = await this.donationModel.findOne({
      user: account.user_id,
      status: OrderStatus.ReadyForPayment,
    })

    if (donation) {
      return {
        id: donation.user.toString(),
        status: donation.status,
        amount: donation.amount,
      }
    }
    return null
  }

  async successPayment(transaction: PaymeTransaction): Promise<void> {
    const account = transaction.account as {
      user_id: string
    }

    await this.donationModel.findOneAndUpdate(
      {
        user: new Types.ObjectId(account.user_id),
        status: OrderStatus.ReadyForPayment,
      },
      {
        status: transaction.status,
        paid_time: new Date(transaction.create_time),
        payment_provider: PaymentProvider.PAYME,
      },
    )
  }
}
  1. Configure Module:
import { Module } from '@nestjs/common';
import { PaymeModule } from '@globalmove/payme-integration';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';

@Module({
  imports: [
    ConfigModule.forRoot(),
    MongooseModule.forRoot('mongodb://localhost/your-db'),
    MongooseModule.forFeature([
      { name: YourModel.name, schema: YourModelSchema },
    ]),
    PaymeModule.forRootAsync({
      paymeStrategy: PaymeStrategy,
      imports: [
        ConfigModule,
        MongooseModule.forFeature([
          { name: YourModel.name, schema: YourModelSchema },
        ]),
      ],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        username: configService.get('PAYME_LOGIN'),
        password: configService.get('PAYME_PASSWORD'),
      }),
      isGlobal: true,
    }),
  ],
})
export class AppModule {}

Environment Variables

PAYME_LOGIN=Paycom
PAYME_PASSWORD=YOUR_MERCHANT_KEY

API Methods

  • CheckPerformTransaction
  • CreateTransaction
  • PerformTransaction
  • CancelTransaction
  • CheckTransaction
  • GetStatement

Transaction States

enum TransactionState {
  STATE_NEW = 1,
  STATE_IN_PROGRESS = 2,
  STATE_DONE = 3,
  STATE_CANCELLED = 4,
  STATE_POST_CANCELLED = 5
}

Author

Globalmove

License

MIT

Package Sidebar

Install

npm i payme-integration

Weekly Downloads

6

Version

0.0.2

License

MIT

Unpacked Size

94.3 kB

Total Files

93

Last publish

Collaborators

  • shohjahon0107