@ruraim/nestjs-midtrans v0.0.12
NestJS Midtrans
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:
- Star this repository
- Contribute to this repository
- Donate via Trakteer
Stay in touch
If you have any question, feel free to contact me on: