Skip to content

Commit

Permalink
Fix stripe client ref id to be alphanumeric
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhjtan committed Nov 20, 2024
1 parent 4ecfc5a commit b46ff74
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ export async function handleCheckoutSessionCompleted(
const matrixUserName = event.data.object.client_reference_id;

if (matrixUserName) {
const base64UserId = matrixUserName.replace(/-/g, '+').replace(/_/g, '/');
const decodedMatrixUserName = Buffer.from(
base64UserId,
'base64',
).toString('utf8');
await updateUserStripeCustomerId(
dbAdapter,
matrixUserName,
decodedMatrixUserName,
stripeCustomerId,
);
}
Expand Down
10 changes: 9 additions & 1 deletion packages/host/app/services/billing-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ import ENV from '@cardstack/host/config/environment';
const { stripePaymentLink } = ENV;

export default class BillingService extends Service {
encodeToAlphanumeric(matrixUserId: string) {
return Buffer.from(matrixUserId)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}

getStripePaymentLink(matrixUserId: string): string {
const clientReferenceId = Buffer.from(matrixUserId).toString('base64');
const clientReferenceId = this.encodeToAlphanumeric(matrixUserId);
return `${stripePaymentLink}?client_reference_id=${clientReferenceId}`;
}
}
17 changes: 15 additions & 2 deletions packages/matrix/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ export async function setupPayment(
page?: Page,
) {
// decode the username from base64
const decodedUsername = Buffer.from(username, 'base64').toString('utf8');
const decodedUsername = decodeFromAlphanumeric(username);

// mock trigger stripe webhook 'checkout.session.completed'
let freePlan = await realmServer.executeSQL(
Expand Down Expand Up @@ -689,7 +689,7 @@ export async function setupUserSubscribed(
username: string,
realmServer: IsolatedRealmServer,
) {
const matrixUserId = Buffer.from(username).toString('base64');
const matrixUserId = encodeToAlphanumeric(username);
await setupUser(username, realmServer);
await setupPayment(matrixUserId, realmServer);
}
Expand Down Expand Up @@ -760,3 +760,16 @@ export async function waitUntil<T>(
}
throw new Error('Timeout waiting for condition');
}

export function encodeToAlphanumeric(string: string) {
return Buffer.from(string)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, ''); // Remove padding
}

export function decodeFromAlphanumeric(encodedString: string) {
const base64 = encodedString.replace(/-/g, '+').replace(/_/g, '/');
return Buffer.from(base64, 'base64').toString('utf8');
}
6 changes: 3 additions & 3 deletions packages/matrix/tests/registration-with-token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
enterWorkspace,
showAllCards,
setupUser,
encodeToAlphanumeric,
} from '../helpers';
import { registerUser, createRegistrationToken } from '../docker/synapse';

Expand Down Expand Up @@ -109,7 +110,7 @@ test.describe('User Registration w/ Token - isolated realm server', () => {
});

// base 64 encode the matrix user id
const matrixUserId = Buffer.from('@user1:localhost').toString('base64');
const matrixUserId = encodeToAlphanumeric('@user1:localhost');

await assertPaymentSetup(page, matrixUserId);
await setupPayment(matrixUserId, realmServer, page);
Expand Down Expand Up @@ -201,8 +202,7 @@ test.describe('User Registration w/ Token - isolated realm server', () => {
page.locator('[data-test-setup-payment-message]'),
).toContainText('Setup your payment method now to enjoy Boxel');

const user2MatrixUserId =
Buffer.from('@user2:localhost').toString('base64');
const user2MatrixUserId = encodeToAlphanumeric('@user2:localhost');

await assertPaymentSetup(page, user2MatrixUserId);
await setupPayment(user2MatrixUserId, realmServer, page);
Expand Down
3 changes: 2 additions & 1 deletion packages/matrix/tests/registration-without-token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
assertPaymentSetup,
setupPayment,
registerRealmUsers,
encodeToAlphanumeric,
} from '../helpers';

test.describe('User Registration w/o Token', () => {
Expand Down Expand Up @@ -69,7 +70,7 @@ test.describe('User Registration w/o Token', () => {
);

// base 64 encode the matrix user id
const matrixUserId = Buffer.from('@user1:localhost').toString('base64');
const matrixUserId = encodeToAlphanumeric('@user1:localhost');

await assertPaymentSetup(page, matrixUserId);
await setupPayment(matrixUserId, realmServer, page);
Expand Down

0 comments on commit b46ff74

Please sign in to comment.