mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 23:00:34 +00:00
feat: sweep up lemon squeezy webhooks.
This commit is contained in:
@@ -31,5 +31,4 @@ export default ({ agenda }: { agenda: Agenda }) => {
|
|||||||
agenda.start().then(() => {
|
agenda.start().then(() => {
|
||||||
agenda.every('1 hours', 'delete-expired-imported-files', {});
|
agenda.every('1 hours', 'delete-expired-imported-files', {});
|
||||||
});
|
});
|
||||||
agenda.start();
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { getPrice } from '@lemonsqueezy/lemonsqueezy.js';
|
import { getPrice } from '@lemonsqueezy/lemonsqueezy.js';
|
||||||
import { ServiceError } from '@/exceptions';
|
import config from '@/config';
|
||||||
import { Inject, Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import {
|
import {
|
||||||
compareSignatures,
|
compareSignatures,
|
||||||
@@ -29,10 +29,10 @@ export class LemonSqueezyWebhooks {
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
configureLemonSqueezy();
|
configureLemonSqueezy();
|
||||||
|
|
||||||
if (!process.env.LEMONSQUEEZY_WEBHOOK_SECRET) {
|
if (!config.lemonSqueezy.webhookSecret) {
|
||||||
throw new ServiceError('Lemon Squeezy Webhook Secret not set in .env');
|
throw new Error('Lemon Squeezy Webhook Secret not set in .env');
|
||||||
}
|
}
|
||||||
const secret = process.env.LEMONSQUEEZY_WEBHOOK_SECRET;
|
const secret = config.lemonSqueezy.webhookSecret;
|
||||||
const hmacSignature = createHmacSignature(secret, rawData);
|
const hmacSignature = createHmacSignature(secret, rawData);
|
||||||
|
|
||||||
if (!compareSignatures(hmacSignature, signature)) {
|
if (!compareSignatures(hmacSignature, signature)) {
|
||||||
@@ -42,14 +42,15 @@ export class LemonSqueezyWebhooks {
|
|||||||
if (webhookHasMeta(data)) {
|
if (webhookHasMeta(data)) {
|
||||||
// Non-blocking call to process the webhook event.
|
// Non-blocking call to process the webhook event.
|
||||||
void this.processWebhookEvent(data);
|
void this.processWebhookEvent(data);
|
||||||
}
|
} else {
|
||||||
throw new Error('Data invalid');
|
throw new Error('Data invalid');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This action will process a webhook event in the database.
|
* This action will process a webhook event in the database.
|
||||||
*/
|
*/
|
||||||
async processWebhookEvent(eventBody) {
|
private async processWebhookEvent(eventBody) {
|
||||||
let processingError = '';
|
let processingError = '';
|
||||||
const webhookEvent = eventBody.meta.event_name;
|
const webhookEvent = eventBody.meta.event_name;
|
||||||
|
|
||||||
@@ -88,6 +89,7 @@ export class LemonSqueezyWebhooks {
|
|||||||
? priceData.data?.data.attributes.unit_price_decimal
|
? priceData.data?.data.attributes.unit_price_decimal
|
||||||
: priceData.data?.data.attributes.unit_price;
|
: priceData.data?.data.attributes.unit_price;
|
||||||
|
|
||||||
|
// Create a new subscription of the tenant.
|
||||||
if (webhookEvent === 'subscription_created') {
|
if (webhookEvent === 'subscription_created') {
|
||||||
await this.subscriptionService.newSubscribtion(
|
await this.subscriptionService.newSubscribtion(
|
||||||
tenantId,
|
tenantId,
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import Plan from './Subscriptions/Plan';
|
import Plan from './Subscriptions/Plan';
|
||||||
import PlanSubscription from './Subscriptions/PlanSubscription';
|
import PlanSubscription from './Subscriptions/PlanSubscription';
|
||||||
import License from './Subscriptions/License';
|
|
||||||
import Tenant from './Tenant';
|
import Tenant from './Tenant';
|
||||||
import TenantMetadata from './TenantMetadata';
|
import TenantMetadata from './TenantMetadata';
|
||||||
import SystemUser from './SystemUser';
|
import SystemUser from './SystemUser';
|
||||||
@@ -12,7 +11,6 @@ import { Import } from './Import';
|
|||||||
export {
|
export {
|
||||||
Plan,
|
Plan,
|
||||||
PlanSubscription,
|
PlanSubscription,
|
||||||
License,
|
|
||||||
Tenant,
|
Tenant,
|
||||||
TenantMetadata,
|
TenantMetadata,
|
||||||
SystemUser,
|
SystemUser,
|
||||||
|
|||||||
Reference in New Issue
Block a user