📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
fixed webhook related problems
Blue Mouse4518cc158c7a
diff --git a/.eslintrc.js b/.eslintrc.jsindex bd398b813..be672cfd2 100644--- a/.eslintrc.js+++ b/.eslintrc.js@@ -28,6 +28,8 @@ module.exports = {'@typescript-eslint/no-explicit-any': 'off','@typescript-eslint/no-namespace': 'off','@typescript-eslint/ban-ts-comment': 'off',+ 'no-restricted-syntax': 'off',+ 'class-methods-use-this': 'off','arrow-body-style': 'off','arrow-parens': 'off','import/no-cycle': 'off',diff --git a/src/webhook/webhook.controller.ts b/src/webhook/webhook.controller.tsindex 1d4868443..4db403cca 100644--- a/src/webhook/webhook.controller.ts+++ b/src/webhook/webhook.controller.ts@@ -53,11 +53,11 @@ export class WebhookController {const {passthrough,email,- subscription_id,- subscription_plan_id,- cancel_url,- update_url,- next_bill_date,+ subscription_id: subID,+ subscription_plan_id: subscriptionPlanId,+ cancel_url: subCancelURL,+ update_url: subUpdateURL,+ next_bill_date: nextBillDate,} = bodylet uid@@ -72,31 +72,25 @@ export class WebhookController {}let monthlyBilling = true- let plan = _find(- ACCOUNT_PLANS,- ({ pid }) => pid === subscription_plan_id,- )+ let plan = _find(ACCOUNT_PLANS, ({ pid }) => pid === subscriptionPlanId)if (!plan) {monthlyBilling = false- plan = _find(- ACCOUNT_PLANS,- ({ ypid }) => ypid === subscription_plan_id,- )+ plan = _find(ACCOUNT_PLANS, ({ ypid }) => ypid === subscriptionPlanId)}if (!plan) {throw new NotFoundException(- `The selected account plan (${subscription_plan_id}) is not available`,+ `The selected account plan (${subscriptionPlanId}) is not available`,)}const updateParams = {planCode: plan.id,- subID: subscription_id,- subUpdateURL: update_url,- subCancelURL: cancel_url,- nextBillDate: next_bill_date,+ subID,+ subUpdateURL,+ subCancelURL,+ nextBillDate,billingFrequency: monthlyBilling? BillingFrequency.Monthly: BillingFrequency.Yearly,@@ -114,21 +108,24 @@ export class WebhookController {}case 'subscription_cancelled': {- const { subscription_id, cancellation_effective_date } = body+ const {+ subscription_id: subID,+ cancellation_effective_date: cancellationEffectiveDate,+ } = body- await this.userService.updateBySubID(subscription_id, {+ await this.userService.updateBySubID(subID, {billingFrequency: BillingFrequency.Monthly,nextBillDate: null,- cancellationEffectiveDate: cancellation_effective_date,+ cancellationEffectiveDate,})break}case 'subscription_payment_refunded': {- const { subscription_id } = body+ const { subscription_id: subID } = body- await this.userService.updateBySubID(subscription_id, {+ await this.userService.updateBySubID(subID, {planCode: PlanCode.none,billingFrequency: BillingFrequency.Monthly,nextBillDate: null,@@ -138,10 +135,10 @@ export class WebhookController {}case 'subscription_payment_failed': {- const { subscription_id, attempt_number } = body+ const { subscription_id: subID, attempt_number: attemptNumber } = body- if (parseInt(attempt_number) >= MAX_PAYMENT_ATTEMPTS) {- await this.userService.updateBySubID(subscription_id, {+ if (parseInt(attemptNumber, 10) >= MAX_PAYMENT_ATTEMPTS) {+ await this.userService.updateBySubID(subID, {planCode: PlanCode.none,billingFrequency: BillingFrequency.Monthly,nextBillDate: null,diff --git a/src/webhook/webhook.service.spec.ts b/src/webhook/webhook.service.spec.tsindex 30dfd96d6..7f9a913cb 100644--- a/src/webhook/webhook.service.spec.ts+++ b/src/webhook/webhook.service.spec.ts@@ -1,15 +1,9 @@import { Test, TestingModule } from '@nestjs/testing'-import { getRepositoryToken } from '@nestjs/typeorm'import { AppLoggerService } from 'src/logger/logger.service'-import { User } from 'src/user/entities/user.entity'-import { UserService } from 'src/user/user.service'-import { Util } from 'src/Util/Util'-import { WebhookController } from './webhook.controller'import { WebhookService } from './webhook.service'describe('WebhookService', () => {let service: WebhookService- const util = new Util()beforeEach(async () => {const module: TestingModule = await Test.createTestingModule({diff --git a/src/webhook/webhook.service.ts b/src/webhook/webhook.service.tsindex 08211c9d1..859560823 100644--- a/src/webhook/webhook.service.ts+++ b/src/webhook/webhook.service.ts@@ -1,3 +1,4 @@+/* eslint-disable no-param-reassign, no-prototype-builtins */import {Injectable,BadRequestException,@@ -6,6 +7,8 @@ import {import * as crypto from 'crypto'import * as Serialize from 'php-serialize'import * as _includes from 'lodash/includes'+import * as _keys from 'lodash/keys'+import * as _isArray from 'lodash/isArray'import { AppLoggerService } from '../logger/logger.service'const PADDLE_PUB_KEY = `-----BEGIN PUBLIC KEY-----@@ -38,16 +41,18 @@ const paddleWhitelistIPs = [export class WebhookService {constructor(private readonly logger: AppLoggerService) {}- ksort(obj) {- const keys = Object.keys(obj).sort()- const sortedObj = {}- for (const i in keys) {- sortedObj[keys[i]] = obj[keys[i]]+ public ksort(obj: Record<string, unknown>): Record<string, unknown> {+ const keys = _keys(obj).sort()+ const sortedObj: Record<string, unknown> = {}++ for (const key of keys) {+ sortedObj[key] = obj[key]}+return sortedObj}- validateWebhook(data) {+ public validateWebhook(data: any) {// Grab p_signatureconst mySig = Buffer.from(data.p_signature, 'base64')// Remove p_signature from object - not included in array of fields used in verification.@@ -56,7 +61,7 @@ export class WebhookService {data = this.ksort(data)for (const property in data) {if (data.hasOwnProperty(property) && typeof data[property] !== 'string') {- if (Array.isArray(data[property])) {+ if (_isArray(data[property])) {// is it an arraydata[property] = data[property].toString()} else {@@ -80,7 +85,7 @@ export class WebhookService {}}- verifyIP(reqIP: string) {+ public verifyIP(reqIP: string) {if (!_includes(paddleWhitelistIPs, reqIP)) {throw new ForbiddenException('You have no access to this endpoint')}