Skip to content

Commit f684bd5

Browse files
committed
Add rough support for team account purchases in PayPro webhooks
1 parent ef6b5da commit f684bd5

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

api/src/functions/paypro-webhook.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import moment from 'moment';
66
import * as log from 'loglevel';
77
import { SubscriptionStatus } from '@httptoolkit/accounts';
88

9-
import { SKUs } from '../products';
9+
import { isProSubscription, isTeamSubscription, SKUs } from '../products';
1010
import { recordCancellation, recordSubscription } from '../accounting';
1111
import { PayingUserMetadata, getUsersByEmail } from '../auth0';
12-
import { parseCheckoutPassthrough, reportSuccessfulCheckout, updateProUserData } from '../webhook-handling';
12+
import { parseCheckoutPassthrough, reportSuccessfulCheckout, updateProUserData, updateTeamData } from '../webhook-handling';
1313
import {
1414
parsePayProCustomFields,
1515
PayProOrderDateFormat,
@@ -68,7 +68,7 @@ export const handler = catchErrors(async (event) => {
6868
const subscriptionId = eventData.SUBSCRIPTION_ID;
6969
if (!subscriptionId) throw new Error(`Received webhook with no subscription id`);
7070

71-
await updateProUserData(email, {
71+
const userData = {
7272
subscription_status: subState,
7373
subscription_sku: sku,
7474
subscription_quantity: quantity,
@@ -77,7 +77,19 @@ export const handler = catchErrors(async (event) => {
7777

7878
payment_provider: 'paypro',
7979
subscription_id: subscriptionId // Useful for API requests later
80-
});
80+
} as const;
81+
82+
if (isTeamSubscription(sku)) {
83+
log.info(`Updating Team user ${email} to ${JSON.stringify(userData)}`);
84+
await updateTeamData(email, userData);
85+
} else if (isProSubscription(sku)) {
86+
log.info(`Updating Pro user ${email} to ${JSON.stringify(userData)}`);
87+
await updateProUserData(email, userData);
88+
} else {
89+
throw new Error(`Webhook received for unknown subscription type: ${
90+
userData.subscription_sku
91+
}`);
92+
}
8193

8294
try {
8395
if (eventType === 'OrderCharged') {

0 commit comments

Comments
 (0)