diff --git a/src/app.module.ts b/src/app.module.ts index d925cc0..5bea356 100644 --- a/src/app.module.ts +++ b/src/app.module.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], diff --git a/src/payment/payment.controller.ts b/src/payment/payment.controller.ts new file mode 100644 index 0000000..330d5f3 --- /dev/null +++ b/src/payment/payment.controller.ts @@ -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' }; + } + } +} diff --git a/src/payment/payment.module.ts b/src/payment/payment.module.ts new file mode 100644 index 0000000..572f994 --- /dev/null +++ b/src/payment/payment.module.ts @@ -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 {} diff --git a/src/payment/payment.service.ts b/src/payment/payment.service.ts new file mode 100644 index 0000000..32266d6 --- /dev/null +++ b/src/payment/payment.service.ts @@ -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}`); + } + } +}