Integrate Zarinpal payment gateway

master
nicekid1 2 months ago
parent eecf60bef4
commit 3c216fa12a
  1. 2
      src/app.module.ts
  2. 25
      src/payment/payment.controller.ts
  3. 9
      src/payment/payment.module.ts
  4. 37
      src/payment/payment.service.ts

@ -10,6 +10,7 @@ import { CartModule } from './cart/cart.module';
import { WalletModule } from './wallet/wallet.module';
import { InvoiceModule } from './invoice/invoice.module';
import { AdminModule } from './admin/admin.module';
import { PaymentModule } from "./payment/payment.module";
@Module({
imports: [
@ -23,6 +24,7 @@ import { AdminModule } from './admin/admin.module';
WalletModule,
InvoiceModule,
AdminModule,
PaymentModule
],
controllers: [AppController],
providers: [AppService],

@ -0,0 +1,25 @@
import { Controller, Get, Query } from '@nestjs/common';
import { PaymentService } from './payment.service';
@Controller('payment')
export class PaymentController {
constructor(private readonly paymentService: PaymentService) {}
@Get('request')
async requestPayment(){
const amount = 10000;
const description = 'Test payment';
const callbackUrl = 'http://localhost:3000/payment/verify';
const paymentUrl = await this.paymentService.requestPayment(amount, description, callbackUrl);
return { paymentUrl };
}
@Get('verify')
async verifyPayment(@Query('Authority') authority: string, @Query('Status') status: string) {
if (status === 'OK') {
const amount = 10000;
const refId = await this.paymentService.verifyPayment(authority, amount);
return { success: true, refId };
} else {
return { success: false, message: 'Payment canceled' };
}
}
}

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { PaymentService } from './payment.service';
import { PaymentController } from './payment.controller';
@Module({
controllers: [PaymentController],
providers: [PaymentService],
})
export class PaymentModule {}

@ -0,0 +1,37 @@
import { Injectable } from '@nestjs/common';
const ZarinpalCheckout = require('zarinpal-checkout');
@Injectable()
export class PaymentService {
private zarinpal;
constructor() {
this.zarinpal = ZarinpalCheckout.create('00000000-0000-0000-0000-000000000000', true);
}
async requestPayment(amount: number, description: string, callbackUrl: string) {
const result = await this.zarinpal.PaymentRequest({
Amount: amount,
CallbackURL: callbackUrl,
Description: description,
});
if (result.status === 100) {
return result.url;
} else {
throw new Error(`Error in payment request: ${result.status}`);
}
}
async verifyPayment(authority: string, amount: number) {
const result = await this.zarinpal.PaymentVerification({
Amount: amount,
Authority: authority,
});
if (result.status === 100) {
return result.RefID;
} else {
throw new Error(`Payment verification failed: ${result.status}`);
}
}
}
Loading…
Cancel
Save