Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payment intent endpoint #410

Merged
merged 8 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions apps/api/src/donations/donations.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { RealmViewSupporters, ViewSupporters } from '@podkrepi-bg/podkrepi-types'
import { AuthenticatedUser, Public, RoleMatchingMode, Roles } from 'nest-keycloak-connect'
import { Response } from 'express'
import {
Body,
Controller,
Expand All @@ -11,18 +10,19 @@ import {
Query,
Res,
} from '@nestjs/common'
import { ApiQuery } from '@nestjs/swagger'
import { Response } from 'express'
import { ApiQuery, ApiTags } from '@nestjs/swagger'
import { DonationStatus } from '@prisma/client'
import { AuthenticatedUser, Public, RoleMatchingMode, Roles } from 'nest-keycloak-connect'

import { RealmViewSupporters, ViewSupporters } from '@podkrepi-bg/podkrepi-types'
import { isAdmin, KeycloakTokenParsed } from '../auth/keycloak'
import { DonationsService } from './donations.service'
import { CreateSessionDto } from './dto/create-session.dto'
import { CreatePaymentDto } from './dto/create-payment.dto'
import { UpdatePaymentDto } from './dto/update-payment.dto'
import { CreateBankPaymentDto } from './dto/create-bank-payment.dto'
import { DonationStatus } from '@prisma/client'
import { CreatePaymentIntentDto } from './dto/create-payment-intent.dto'
import { PagingQueryDto } from '../common/dto/paging-query-dto'
import { ApiTags } from '@nestjs/swagger'

@ApiTags('donation')
@Controller('donation')
Expand Down Expand Up @@ -135,6 +135,15 @@ export class DonationsController {
return this.donationsService.create(createPaymentDto, user)
}

@Post('create-payment-intent')
@Public()
createPaymentIntent(
@Body()
createPaymentIntentDto: CreatePaymentIntentDto,
) {
return this.donationsService.createPaymentIntent(createPaymentIntentDto)
}

@Post('create-bank-payment')
@Roles({
roles: [RealmViewSupporters.role, ViewSupporters.role],
Expand Down
32 changes: 32 additions & 0 deletions apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ export class DonationsService {
return result
}

/**
* Get donation by id
* @param id Donation id
* @returns {Promise<Donation>} Donation
* @throws NotFoundException if no donation is found
*/
async getDonationById(id: string): Promise<Donation> {
try {
const donation = await this.prisma.donation.findFirst({
Expand All @@ -251,6 +257,12 @@ export class DonationsService {
}
}

/**
* Get donation by id with person data attached
* @param id Donation id
* @param keycloakId Keycloak id of the user
* @returns {Promise<Donation & { person: Person | null }>} Donation
*/
async getUserDonationById(
id: string,
keycloakId: string,
Expand Down Expand Up @@ -281,6 +293,12 @@ export class DonationsService {
})
}

/** Describe the function below
* @param inputDto
* @param user
* @returns {Promise<Donation>}
*
*/
async create(inputDto: CreatePaymentDto, user: KeycloakTokenParsed): Promise<Donation> {
const donation = await this.prisma.donation.create({ data: inputDto.toEntity(user) })

Expand All @@ -291,6 +309,17 @@ export class DonationsService {
return donation
}

/**
* Create a payment intent for a donation
* @param inputDto Payment intent create params
* @returns {Promise<Stripe.Response<Stripe.PaymentIntent>>}
*/
async createPaymentIntent(
inputDto: Stripe.PaymentIntentCreateParams,
): Promise<Stripe.Response<Stripe.PaymentIntent>> {
return this.stripeClient.paymentIntents.create(inputDto)
}

/**
* Used by the administrators to manually add donations executed by bank payments to a campaign.
*/
Expand Down Expand Up @@ -435,6 +464,9 @@ export class DonationsService {
})
}

/**
* @param res - Response object to be used for the export to excel file
*/
async exportToExcel(res: Response) {
const { items } = await this.listDonations()
const donationsMappedForExport = items.map((donation) => ({
Expand Down
16 changes: 16 additions & 0 deletions apps/api/src/donations/dto/create-payment-intent.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Stripe from 'stripe'
import { ApiProperty } from '@nestjs/swagger'
import { Currency } from '@prisma/client'
import { Expose } from 'class-transformer'
import { IsNumber } from 'class-validator'

export class CreatePaymentIntentDto implements Stripe.PaymentIntentCreateParams {
@ApiProperty()
@Expose()
@IsNumber()
amount: number

@ApiProperty()
@Expose()
currency: Currency
}