🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

@ruraim/nestjs-midtrans

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ruraim/nestjs-midtrans

NestJS Midtrans SDK Module

0.0.12
latest
Source
npm
Version published
Maintainers
1
Created
Source

NestJS Midtrans

NPM Version Package License NPM Downloads

Description

Midtrans API Wrapper for Nest JS, with this package you can easily integrate your Nest JS application with Midtrans Payment Gateway. Using zod for schema validation so you can import the schema and use it for validation in your application.

Status

Under Development

This package is still under development, so there are still many features that are not available yet. Will be stable on version 1.0.0

Table of Contents

Setup & Installation

Installation

Using NPM

$ npm i --save @ruraim/nestjs-midtrans

Using Yarn

$ yarn add @ruraim/nestjs-midtrans

Module Import

import { Module } from '@nestjs/common';
import { MidtransModule } from '@ruraim/nestjs-midtrans';

// using register method
@Module({
  imports: [
      MidtransModule.register({
        clientKey: 'client-key',
        serverKey: 'server-key',
        merchantId: 'merchant-id',
        sandbox: true, // default: false,
        isGlobal: true // default: false, register module globally
      })
    ],
})

// using registerAsync with dependencies
@Module({
  imports: [
      MidtransModule.registerAsync({
          useFactory: (config: ConfigService) => ({
              clientKey: config.get<string>('MIDTRANS_CLIENT_KEY'),
              serverKey: config.get<string>('MIDTRANS_SERVER_KEY'),
              merchantId: config.get<string>('MIDTRANS_MERCHANT_ID'),
              sandbox: config.get<string>('MIDTRANS_MODE') === 'sandbox',
          }),
          // using ConfigService from @nestjs/config to get .env value
          inject: [ConfigService],
          imports: [ConfigModule],
          isGlobal: true // default: false, register module globally
      })
    ],
})
export class AppModule {}

Inject Service


import { MidtransService } from '@ruraim/nestjs-midtrans';

@Injectable()
export class AppService {
    constructor(
        private readonly midtransService: MidtransService
    ) {}
}

Transaction

Charge Transaction

You can refer to Midtrans - Charge Transaction for more information about the payload or see Charge Schema file in the source code.

  const result = await this.midtransService.charge({
      payment_type: 'bank_transfer',
      transaction_details: {
          order_id: 'ORDER-ID-123',
          gross_amount: 200000
      },
      customer_details: {
          email: 'customer@gmail.com',
          first_name: 'John Doe',
          phone: '081234567890'
      },
      item_details: [
        {
          id: 'Item1',
          price: 100000,
          quantity: 1,
          name: 'Item 1'
        },
        {
          id: 'Item2',
          price: 50000,
          quantity: 2,
          name: 'Item 2'
        }
      ],
      bank_transfer: {
        bank: 'bca'
      }
  })
  console.log(result)

Check Transaction Status

  const result = await this.midtransService.getStatus('ORDER-ID-123')
  console.log(result)

Expire Transaction

  const result = await this.midtransService.expireTransaction('ORDER-ID-123')
  console.log(result)

Subscription

Create Subscription

You can refer to Midtrans - Create Subscription for more information about the payload or see Create Subscription Schema file in the source code.

  const result = await this.midtransService.createSubscription({
    name: 'Langganan bulanan',
    amount: 10000,
    currency: 'IDR',
    payment_type: 'credit_card',
    schedule: {
      interval: 1,
      interval_unit: 'month',
      max_interval: 12,
      start_time: '2023-07-15 00:00:00'
    },
    token: 'dummy_credit_card_token',
  })
  console.log(result)

Get Subscription

  const result = await this.midtransService.getSubscription('SUBSCRIPTION-ID-123')
  console.log(result)

Disable Subscription

  const result = await this.midtransService.disableSubscription('SUBSCRIPTION-ID-123')
  console.log(result)

Enable Subscription

  const result = await this.midtransService.enableSubscription('SUBSCRIPTION-ID-123')
  console.log(result)

Update Subscription

  const result = await this.midtransService.updateSubscription('4b9273ae-8bfa-4400-94fe-ba4b2b4af70f', {
      name: 'Langganan bulanan',
      amount: 50000,
      currency: 'IDR',
      token: 'dummy_credit_card_token'
  })
  console.log(result)

References

Support

If you like this package, you can support me by:

Stay in touch

If you have any question, feel free to contact me on:

License

MIT licensed.

Keywords

nestjs

FAQs

Package last updated on 07 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts