Skip to content

Commit

Permalink
feat(console): Billing/payment notifications (#2446)
Browse files Browse the repository at this point in the history
  • Loading branch information
poolsar42 committed Jul 4, 2023
1 parent 06b099c commit 3004a75
Show file tree
Hide file tree
Showing 18 changed files with 454 additions and 116 deletions.
51 changes: 49 additions & 2 deletions apps/console/app/routes/__layout/gnillib/webhook.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import { ActionFunction } from '@remix-run/cloudflare'
import { type ActionFunction } from '@remix-run/cloudflare'

import Stripe from 'stripe'
import createAccountClient from '@proofzero/platform-clients/account'
import createStarbaseClient from '@proofzero/platform-clients/starbase'
import createAddressClient from '@proofzero/platform-clients/address'
import { getAuthzHeaderConditionallyFromToken } from '@proofzero/utils'
import { AccountURN } from '@proofzero/urns/account'
import { type AccountURN } from '@proofzero/urns/account'
import { ServicePlanType } from '@proofzero/types/account'
import { updateSubscriptionMetadata } from '~/services/billing/stripe'

Expand All @@ -18,6 +20,15 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
...traceHeader,
})

const starbaseClient = createStarbaseClient(Starbase, {
...getAuthzHeaderConditionallyFromToken(undefined),
...traceHeader,
})
const addressClient = createAddressClient(Address, {
...getAuthzHeaderConditionallyFromToken(undefined),
...traceHeader,
})

const stripeClient = new Stripe(STRIPE_API_SECRET, {
apiVersion: '2022-11-15',
})
Expand Down Expand Up @@ -120,6 +131,42 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
})
}

break
case 'customer.deleted':
case 'customer.subscription.deleted':
const {
customer: customerDel,
id: subIdDel,
metadata: metaDel,
} = event.data.object as {
customer: string
id: string
metadata: {
accountURN: AccountURN
}
}
const customerDataDel = await stripeClient.customers.retrieve(
customerDel
)
if (!customerDataDel.deleted && customerDataDel.email) {
const { email, name } = customerDataDel

await Promise.all([
addressClient.sendBillingNotification.mutate({
email,
name: name || 'Client',
}),
accountClient.cancelServicePlans.mutate({
account: metaDel.accountURN,
subscriptionID: subIdDel,
deletePaymentData: event.type === 'customer.deleted',
}),
starbaseClient.deleteSubscriptionPlans.mutate({
accountURN: metaDel.accountURN,
}),
])
}

break
}

Expand Down
6 changes: 3 additions & 3 deletions apps/console/app/routes/apps/$clientId/designer.beta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ import { Helmet } from 'react-helmet'
import { notificationHandlerType } from '~/types'
import InputTextarea from '@proofzero/design-system/src/atoms/form/InputTextarea'
import {
EmailTemplate,
EmailTemplateOTP,
darkModeStyles,
lightModeStyles,
} from '@proofzero/platform/email/emailOtpTemplate'
} from '@proofzero/platform/email/emailTemplate'
import { BadRequestError } from '@proofzero/errors'
import { GetEmailOTPThemeResult } from '@proofzero/platform/starbase/src/jsonrpc/methods/getEmailOTPTheme'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
Expand Down Expand Up @@ -1178,7 +1178,7 @@ const EmailPanel = ({
ref={iFrameRef}
className="w-full border rounded-lg"
srcDoc={
EmailTemplate('XXXXXX', {
EmailTemplateOTP('XXXXXX', {
appName: 'Designer',
logoURL:
logoURL ??
Expand Down
4 changes: 2 additions & 2 deletions apps/passport/app/routes/connect/email/otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getAddressClient, getStarbaseClient } from '~/platform.server'

import type { ActionFunction, LoaderFunction } from '@remix-run/cloudflare'
import { getAuthzCookieParams } from '~/session.server'
import type { SendOTPEmailThemeProps } from '@proofzero/platform/email/src/jsonrpc/methods/sendOTPEmail'
import type { EmailThemeProps } from '@proofzero/platform/email/src/emailFunctions'
import { BadRequestError, InternalServerError } from '@proofzero/errors'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'

Expand Down Expand Up @@ -62,7 +62,7 @@ export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
])
}

let themeProps: SendOTPEmailThemeProps | undefined
let themeProps: EmailThemeProps | undefined
if (appProps) {
themeProps = {
privacyURL: appProps.privacyURL as string,
Expand Down
29 changes: 29 additions & 0 deletions platform/account/src/jsonrpc/methods/cancelServicePlans.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { z } from 'zod'
import { Context } from '../../context'
import { inputValidators } from '@proofzero/platform-middleware'
import { initAccountNodeByName } from '../../nodes'
export const CancelServicePlansInput = z.object({
account: inputValidators.AccountURNInput,
subscriptionID: z.string(),
deletePaymentData: z.boolean().optional(),
})

export type CancelServicePlansParams = z.infer<typeof CancelServicePlansInput>

export const cancelServicePlans = async ({
input,
ctx,
}: {
input: CancelServicePlansParams
ctx: Context
}) => {
const servicePlansNode = await initAccountNodeByName(
input.account,
ctx.Account
)

await servicePlansNode.storage.delete('servicePlans')
if (input.deletePaymentData) {
await servicePlansNode.storage.delete('stripePaymentData')
}
}
9 changes: 9 additions & 0 deletions platform/account/src/jsonrpc/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ import {
getStripePaymentData,
setStripePaymentData,
} from './methods/stripePaymentData'
import {
CancelServicePlansInput,
cancelServicePlans,
} from './methods/cancelServicePlans'

const t = initTRPC.context<Context>().create({ errorFormatter })

Expand Down Expand Up @@ -171,4 +175,9 @@ export const appRouter = t.router({
.use(Analytics)
.input(SetStripePaymentDataInputSchema)
.mutation(setStripePaymentData),
cancelServicePlans: t.procedure
.use(LogUsage)
.use(Analytics)
.input(CancelServicePlansInput)
.mutation(cancelServicePlans),
})
6 changes: 3 additions & 3 deletions platform/address/src/jsonrpc/methods/generateEmailOTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { AddressNode } from '../../nodes'
import EmailAddress from '../../nodes/email'

import { EMAIL_VERIFICATION_OPTIONS } from '../../constants'
import { SendOTPEmailThemePropsSchema } from '../../../../email/src/jsonrpc/methods/sendOTPEmail'
import { EmailThemePropsSchema } from '../../../../email/src/emailFunctions'

export const GenerateEmailOTPInput = z.object({
email: z.string(),
themeProps: SendOTPEmailThemePropsSchema.optional(),
themeProps: EmailThemePropsSchema.optional(),
preview: z.boolean().optional(),
})

Expand All @@ -36,7 +36,7 @@ export const generateEmailOTPMethod = async ({
delayMiliseconds
)

await ctx.emailClient.sendEmailNotification.mutate({
await ctx.emailClient.sendOTP.mutate({
emailAddress: email,
name: email,
otpCode: code,
Expand Down
31 changes: 31 additions & 0 deletions platform/address/src/jsonrpc/methods/sendBillingNotification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { z } from 'zod'

import { Context } from '../../context'

import { EmailThemePropsSchema } from '../../../../email/src/emailFunctions'

export const SendBillingNotificationInput = z.object({
email: z.string(),
name: z.string(),
themeProps: EmailThemePropsSchema.optional(),
})

type SendBillingNotificationParams = z.infer<
typeof SendBillingNotificationInput
>

export const sendBillingNotificationMethod = async ({
input,
ctx,
}: {
input: SendBillingNotificationParams
ctx: Context
}) => {
const { email, name, themeProps } = input

await ctx.emailClient.sendBillingNotification.mutate({
emailAddress: email,
name: name,
themeProps,
})
}
9 changes: 9 additions & 0 deletions platform/address/src/jsonrpc/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ import {
revokeWalletSessionKeyMethod,
revokeWalletSessionKeyBatchMethod,
} from './methods/revokeWalletSessionKey'
import {
SendBillingNotificationInput,
sendBillingNotificationMethod,
} from './methods/sendBillingNotification'

const t = initTRPC.context<Context>().create({ errorFormatter })

Expand Down Expand Up @@ -264,4 +268,9 @@ export const appRouter = t.router({
.use(Analytics)
.input(RevokeWalletSessionKeyBatchInput)
.mutation(revokeWalletSessionKeyBatchMethod),
sendBillingNotification: t.procedure
.use(LogUsage)
.use(Analytics)
.input(SendBillingNotificationInput)
.mutation(sendBillingNotificationMethod),
})
Loading

0 comments on commit 3004a75

Please sign in to comment.