parent
eecf60bef4
commit
3c216fa12a
4 changed files with 73 additions and 0 deletions
@ -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…
Reference in new issue