Skip to content

Commit

Permalink
feat: add stripe feed integration
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Oct 25, 2024
1 parent 59d32e0 commit 09d0f02
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/client/components/feed/FeedIntegration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover';
import { CodeBlock } from '../CodeBlock';
import { useTranslation } from '@i18next-toolkit/react';
import { SiSentry } from 'react-icons/si';
import { FaStripe } from 'react-icons/fa6';

export const FeedIntegration: React.FC<{
feedId: string;
Expand Down Expand Up @@ -57,6 +58,22 @@ export const FeedIntegration: React.FC<{
}
/>

<FeedIntegrationItem
icon={<FaStripe size={32} />}
label="Stripe"
content={
<div>
<div className="text-lg font-bold">{t('Receive Webhooks')}</div>

<div>{t('Add sentry webhook with url')}:</div>

<CodeBlock
code={`${window.location.origin}/open/feed/${props.feedId}/stripe`}
/>
</div>
}
/>

<div onClick={() => window.open('/feed/playground', '_blank')}>
<FeedIntegrationItemTrigger
icon={<LuTestTube2 size={32} />}
Expand Down
115 changes: 115 additions & 0 deletions src/server/trpc/routers/feed/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,121 @@ export const feedIntegrationRouter = router({

logUnknownIntegration('github', input);

return 'Not supported yet';
}),
stripe: publicProcedure
.meta(
buildFeedPublicOpenapi({
method: 'POST',
path: '/{channelId}/stripe',
summary: 'integrate with stripe webhook',
})
)
.input(
z
.object({
channelId: z.string(),
})
.passthrough()
)
.output(z.string())
.mutation(async ({ input, ctx }) => {
console.log(input);
const { channelId, ...data } = input;
const type = data.type;

const workspaceId = await prisma.feedChannel
.findFirst({
where: {
id: channelId,
},
select: {
workspaceId: true,
},
})
.then((res) => res?.workspaceId);

if (!workspaceId) {
throw new Error('Not found Workspace');
}

if (type === 'payment_intent.succeeded') {
const amount = get(data, 'data.object.amount_received');
const currency = String(
get(data, 'data.object.currency')
).toUpperCase();
const eventId = String(get(data, 'data.id')).toUpperCase();

await createFeedEvent(workspaceId, {
channelId: channelId,
eventName: type,
eventContent: `You receive a payment of ${currency} **${amount}**`,
tags: [],
source: 'stripe',
senderId: eventId,
important: true,
payload: data,
});
return 'ok';
}

if (type === 'payment_intent.canceled') {
const amount = get(data, 'data.object.amount');
const currency = String(
get(data, 'data.object.currency')
).toUpperCase();
const eventId = String(get(data, 'data.id')).toUpperCase();

await createFeedEvent(workspaceId, {
channelId: channelId,
eventName: type,
eventContent: `A payment has been canceled of ${currency} **${amount}**`,
tags: [],
source: 'stripe',
senderId: eventId,
important: false,
payload: data,
});
return 'ok';
}

if (type === 'customer.subscription.created') {
const eventId = String(get(data, 'data.id')).toUpperCase();
const planName = get(data, 'data.object.plan.nickname');

await createFeedEvent(workspaceId, {
channelId: channelId,
eventName: type,
eventContent: `A customer has been subscribed service: **${planName}**`,
tags: [],
source: 'stripe',
senderId: eventId,
important: true,
payload: data,
});
return 'ok';
}

if (type === 'customer.subscription.deleted') {
const eventId = String(get(data, 'data.id')).toUpperCase();
const planName = get(data, 'data.object.plan.nickname');

await createFeedEvent(workspaceId, {
channelId: channelId,
eventName: type,
eventContent: `A customer cancel subscription service: ${planName}`,
tags: [],
source: 'stripe',
senderId: eventId,
important: true,
payload: data,
});

return 'ok';
}

logUnknownIntegration('stripe', input);

return 'Not supported yet';
}),
tencentCloudAlarm: publicProcedure
Expand Down

0 comments on commit 09d0f02

Please sign in to comment.