Skip to content

Commit

Permalink
feat(stripe): Don't initiate intent/subscription if campaign is compl…
Browse files Browse the repository at this point in the history
…eted
  • Loading branch information
sashko9807 committed Apr 11, 2024
1 parent 7367091 commit 2c9fee9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
46 changes: 45 additions & 1 deletion apps/api/src/stripe/stripe.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import { VaultService } from '../vault/vault.service'
import { ExportService } from '../export/export.service'
import { NotificationModule } from '../sockets/notifications/notification.module'
import { CACHE_MANAGER } from '@nestjs/cache-manager'

import { CreateSubscriptionPaymentDto } from './dto/create-subscription-payment.dto'
import { mockDeep } from 'jest-mock-extended'
import { Grant } from 'keycloak-connect'
import { KeycloakTokenParsed } from '../auth/keycloak'
describe('StripeController', () => {
let controller: StripeController

Expand Down Expand Up @@ -182,4 +185,45 @@ describe('StripeController', () => {
reason: 'requested_by_customer',
})
})
it(`should not call setupintents.update if campaign can't accept donations`, async () => {
prismaMock.campaign.findFirst.mockResolvedValue({
id: 'complete-campaign',
allowDonationOnComplete: false,
state: CampaignState.complete,
} as Campaign)

const payload = {
metadata: {
campaignId: 'complete-campaign',
},
}

await expect(controller.updateSetupIntent('123', payload)).rejects.toThrow(
new NotAcceptableException('Campaign cannot accept donations in state: complete'),
)
})
it(`should not call subscription.create if campaign can't accept donations`, async () => {
prismaMock.campaign.findFirst.mockResolvedValue({
id: 'complete-campaign',
allowDonationOnComplete: false,
state: CampaignState.complete,
} as Campaign)

const user: KeycloakTokenParsed = {
sub: '00000000-0000-0000-0000-000000000013',
'allowed-origins': [],
email: '[email protected]',
}
const subscribtionObj: CreateSubscriptionPaymentDto = {
type: 'donation',
email: '[email protected]',
campaignId: 'complete-campaign',
amount: 200,
currency: 'BGN',
}

await expect(controller.createSubscription(user, subscribtionObj)).rejects.toThrow(
new NotAcceptableException('Campaign cannot accept donations in state: complete'),
)
})
})
1 change: 0 additions & 1 deletion apps/api/src/stripe/stripe.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export class StripeController {
@Body()
updateSetupIntentDto: UpdateSetupIntentDto,
) {
console.log(updateSetupIntentDto)
return this.stripeService.updateSetupIntent(id, updateSetupIntentDto)
}

Expand Down
8 changes: 8 additions & 0 deletions apps/api/src/stripe/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export class StripeService {
id: string,
inputDto: UpdateSetupIntentDto,
): Promise<Stripe.Response<Stripe.SetupIntent>> {
if (!inputDto.metadata.campaignId)
throw new BadRequestException('campaignId is missing from metadata')
const campaign = await this.campaignService.validateCampaignId(
inputDto.metadata.campaignId as string,
)
return await this.stripeClient.setupIntents.update(id, inputDto)
}
/**
Expand Down Expand Up @@ -178,6 +183,9 @@ export class StripeService {
user: KeycloakTokenParsed,
subscriptionPaymentDto: CreateSubscriptionPaymentDto,
): Promise<Stripe.PaymentIntent> {
const campaign = await this.campaignService.validateCampaignId(
subscriptionPaymentDto.campaignId,
)
const customer = await this.createCustomer(user, subscriptionPaymentDto)
const product = await this.createProduct(subscriptionPaymentDto)
const subscription = await this.stripeClient.subscriptions.create({
Expand Down

0 comments on commit 2c9fee9

Please sign in to comment.