Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter stripe webhooks for correct server #15320

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion habitica-images
Submodule habitica-images updated 399 files
10 changes: 9 additions & 1 deletion test/api/unit/libs/payments/stripe/checkout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('Stripe - Checkout', () => {
gift: undefined,
sub: undefined,
gemsBlock: gemsBlockKey,
server_url: BASE_URL,
};

expect(gems.validateGiftMessage).to.not.be.called;
Expand Down Expand Up @@ -101,6 +102,7 @@ describe('Stripe - Checkout', () => {
gift: JSON.stringify(gift),
sub: undefined,
gemsBlock: undefined,
server_url: BASE_URL,
};

expect(gems.validateGiftMessage).to.be.calledOnce;
Expand Down Expand Up @@ -155,6 +157,7 @@ describe('Stripe - Checkout', () => {
gift: JSON.stringify(gift),
sub: undefined,
gemsBlock: undefined,
server_url: BASE_URL,
};

expect(oneTimePayments.getOneTimePaymentInfo).to.be.calledOnce;
Expand Down Expand Up @@ -192,6 +195,7 @@ describe('Stripe - Checkout', () => {
userId: user._id,
gift: undefined,
sub: JSON.stringify(sub),
server_url: BASE_URL,
};

expect(subscriptions.checkSubData).to.be.calledOnce;
Expand Down Expand Up @@ -258,6 +262,7 @@ describe('Stripe - Checkout', () => {
userId: user._id,
gift: undefined,
sub: JSON.stringify(sub),
server_url: BASE_URL,
groupId,
};

Expand Down Expand Up @@ -328,8 +333,9 @@ describe('Stripe - Checkout', () => {
user.purchased.plan.customerId = customerId;

const metadata = {
userId: user._id,
type: 'edit-card-user',
userId: user._id,
server_url: BASE_URL,
};

const res = await createEditCardCheckoutSession({ user }, stripe);
Expand Down Expand Up @@ -418,6 +424,7 @@ describe('Stripe - Checkout', () => {
const metadata = {
userId: user._id,
type: 'edit-card-group',
server_url: BASE_URL,
groupId,
};

Expand Down Expand Up @@ -455,6 +462,7 @@ describe('Stripe - Checkout', () => {
userId: anotherUser._id,
type: 'edit-card-group',
groupId,
server_url: BASE_URL,
};

const res = await createEditCardCheckoutSession({ user: anotherUser, groupId }, stripe);
Expand Down
5 changes: 4 additions & 1 deletion test/api/unit/libs/payments/stripe/webhooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as subscriptions from '../../../../../../website/server/libs/payments/s
const { i18n } = common;

describe('Stripe - Webhooks', () => {
const BASE_URL = nconf.get('BASE_URL');
const stripe = stripeModule('test');
const endpointSecret = nconf.get('STRIPE_WEBHOOKS_ENDPOINT_SECRET');
const headers = {};
Expand Down Expand Up @@ -284,7 +285,9 @@ describe('Stripe - Webhooks', () => {
const session = {};

beforeEach(() => {
session.metadata = {};
session.metadata = {
server_url: BASE_URL,
};
event = { type: eventType, data: { object: session } };
constructEventStub = sandbox.stub(stripe.webhooks, 'constructEvent');
constructEventStub.returns(event);
Expand Down
2 changes: 2 additions & 0 deletions website/server/libs/payments/stripe/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function createCheckoutSession (options, stripeInc) {
userId: user._id,
gift: gift ? JSON.stringify(gift) : undefined,
sub: sub ? JSON.stringify(sub) : undefined,
server_url: BASE_URL,
};

let lineItems;
Expand Down Expand Up @@ -141,6 +142,7 @@ export async function createEditCardCheckoutSession (options, stripeInc) {
const metadata = {
type,
userId: user._id,
server_url: BASE_URL,
};

let customerId;
Expand Down
6 changes: 6 additions & 0 deletions website/server/libs/payments/stripe/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { applySubscription, handlePaymentMethodChange } from './subscriptions';

const endpointSecret = nconf.get('STRIPE_WEBHOOKS_ENDPOINT_SECRET');

const BASE_URL = nconf.get('BASE_URL');

export async function handleWebhooks (options, stripeInc) {
const { body, headers } = options;

Expand Down Expand Up @@ -67,6 +69,10 @@ export async function handleWebhooks (options, stripeInc) {
const session = event.data.object;
const { metadata } = session;

if (metadata.server_url !== BASE_URL) {
break;
}

if (metadata.type === 'edit-card-group' || metadata.type === 'edit-card-user') {
await handlePaymentMethodChange(session);
} else if (metadata.type === 'subscription') {
Expand Down
Loading