Merge pull request #1014 from automatisch/handle-subscription-created
feat: Handle subscription created webhook event
This commit is contained in:
@@ -9,6 +9,9 @@ export default async (request: IRequest, response: Response) => {
|
|||||||
return response.sendStatus(401);
|
return response.sendStatus(401);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Handle Paddle webhooks
|
if (request.body.alert_name === 'subscription_created') {
|
||||||
|
await Billing.webhooks.handleSubscriptionCreated(request);
|
||||||
|
}
|
||||||
|
|
||||||
return response.sendStatus(200);
|
return response.sendStatus(200);
|
||||||
};
|
};
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import appConfig from '../../config/app';
|
import appConfig from '../../config/app';
|
||||||
import paddleClient from './paddle.ee';
|
import paddleClient from './paddle.ee';
|
||||||
import paddlePlans from './plans.ee';
|
import paddlePlans from './plans.ee';
|
||||||
|
import webhooks from './webhooks.ee';
|
||||||
|
|
||||||
const paddleInfo = {
|
const paddleInfo = {
|
||||||
sandbox: appConfig.isDev ? true : false,
|
sandbox: appConfig.isDev ? true : false,
|
||||||
@@ -11,6 +12,7 @@ const billing = {
|
|||||||
paddleClient,
|
paddleClient,
|
||||||
paddlePlans,
|
paddlePlans,
|
||||||
paddleInfo,
|
paddleInfo,
|
||||||
|
webhooks,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default billing;
|
export default billing;
|
||||||
|
25
packages/backend/src/helpers/billing/webhooks.ee.ts
Normal file
25
packages/backend/src/helpers/billing/webhooks.ee.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { IRequest } from '@automatisch/types';
|
||||||
|
import Subscription from '../../models/subscription.ee';
|
||||||
|
|
||||||
|
const handleSubscriptionCreated = async (request: IRequest) => {
|
||||||
|
await Subscription.query().insertAndFetch(formatSubscription(request));
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatSubscription = (request: IRequest) => {
|
||||||
|
return {
|
||||||
|
userId: JSON.parse(request.body.passthrough).id,
|
||||||
|
paddleSubscriptionId: request.body.subscription_id,
|
||||||
|
paddlePlanId: request.body.subscription_plan_id,
|
||||||
|
cancelUrl: request.body.cancel_url,
|
||||||
|
updateUrl: request.body.update_url,
|
||||||
|
status: request.body.status,
|
||||||
|
nextBillDate: request.body.next_bill_date,
|
||||||
|
nextBillAmount: request.body.next_bill_amount,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const webhooks = {
|
||||||
|
handleSubscriptionCreated,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default webhooks;
|
Reference in New Issue
Block a user