Skip to content

Commit

Permalink
Added error loging to stripe client (podkrepi-bg#470)
Browse files Browse the repository at this point in the history
* added error logging to stripe client

* added type for stripe config

* removed unneded import

---------

Co-authored-by: quantum-grit <[email protected]>
  • Loading branch information
2 people authored and dimitur2204 committed Mar 23, 2023
1 parent 234e7f9 commit 1e5a4b7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 33 deletions.
34 changes: 18 additions & 16 deletions apps/api/src/bank-transactions-file/helpers/use-factory-service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { StripeModuleConfig } from '@golevelup/nestjs-stripe'
import { ConfigService } from '@nestjs/config'
import { Public } from 'nest-keycloak-connect'

export const useFactoryService = {
useFactory: async (config: ConfigService) => ({
apiKey: config.get('stripe.secretKey', ''),
webhookConfig: {
stripeWebhookSecret: config.get('stripe.webhookSecret', ''),
requestBodyProperty: 'body',
decorators: [
/**
* Avoid Keycloak @AuthGuard and @RoleGuard on Webhook controller
**/
Public(),
],
stripeSecrets: {
connect: config.get('stripe.webhookSecret', ''),
account: config.get('stripe.webhookSecret', ''),
useFactory: async (config: ConfigService) =>
({
apiKey: config.get('stripe.secretKey', ''),
webhookConfig: {
stripeWebhookSecret: config.get('stripe.webhookSecret', ''),
requestBodyProperty: 'body',
decorators: [
/**
* Avoid Keycloak @AuthGuard and @RoleGuard on Webhook controller
**/
Public(),
],
stripeSecrets: {
connect: config.get('stripe.webhookSecret', ''),
account: config.get('stripe.webhookSecret', ''),
},
},
},
}),
} as StripeModuleConfig),
}
2 changes: 1 addition & 1 deletion apps/api/src/donations/donations.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('DonationsController', () => {
const stripeMock = {
checkout: { sessions: { create: jest.fn() } },
}
stripeMock.checkout.sessions.create.mockReturnValue({ payment_intent: 'unique-intent' })
stripeMock.checkout.sessions.create.mockResolvedValue({ payment_intent: 'unique-intent' })

const mockSession = {
mode: 'payment',
Expand Down
61 changes: 46 additions & 15 deletions apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,25 @@ export class DonationsService {
) {}

async listPrices(type?: Stripe.PriceListParams.Type, active?: boolean): Promise<Stripe.Price[]> {
const list = await this.stripeClient.prices.list({ active, type, limit: 100 })
return list.data.filter((price) => price.active)
const listResponse = await this.stripeClient.prices.list({ active, type, limit: 100 }).then(
function (list) {
Logger.debug('[Stripe] Prices received: ' + list.data.length)
return { list }
},
function (error) {
if (error instanceof Stripe.errors.StripeError)
Logger.error(
'[Stripe] Error while getting price list. Error type: ' +
error.type +
' message: ' +
error.message,
)
},
)

if (listResponse) {
return listResponse.list.data.filter((price) => price.active)
} else return new Array<Stripe.Price>()
}

/**
Expand Down Expand Up @@ -210,7 +227,7 @@ export class DonationsService {

async createCheckoutSession(
sessionDto: CreateSessionDto,
): Promise<{ session: Stripe.Checkout.Session }> {
): Promise<void | { session: Stripe.Checkout.Session }> {
const campaign = await this.campaignService.validateCampaignId(sessionDto.campaignId)
const { mode } = sessionDto
const appUrl = this.config.get<string>('APP_URL')
Expand All @@ -236,19 +253,33 @@ export class DonationsService {

Logger.debug('[ CreateCheckoutSession ]', createSessionRequest)

const session = await this.stripeClient.checkout.sessions.create(createSessionRequest)

Logger.log('[ CreateInitialDonation ]', {
session: session,
})

await this.createInitialDonationFromSession(
campaign,
sessionDto,
(session.payment_intent as string) ?? session.id,
)
const sessionResponse = await this.stripeClient.checkout.sessions
.create(createSessionRequest)
.then(
function (session) {
Logger.debug('[Stripe] Checkout session created.')
return { session }
},
function (error) {
if (error instanceof Stripe.errors.StripeError)
Logger.error(
'[Stripe] Error while creating checkout session. Error type: ' +
error.type +
' message: ' +
error.message,
)
},
)

if (sessionResponse) {
this.createInitialDonationFromSession(
campaign,
sessionDto,
(sessionResponse.session.payment_intent as string) ?? sessionResponse.session.id,
)
}

return { session }
return sessionResponse
}

private async prepareSessionItems(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { RecurringDonationService } from '../../recurring-donation/recurring-don
import { HttpService } from '@nestjs/axios'
import { mockDeep } from 'jest-mock-extended'
import { NotificationModule } from '../../sockets/notifications/notification.module'
import { PrismaService } from '../../prisma/prisma.service'

const defaultStripeWebhookEndpoint = '/stripe/webhook'
const stripeSecret = 'wh_123'
Expand Down

0 comments on commit 1e5a4b7

Please sign in to comment.