Skip to content

Commit

Permalink
Support success and cancel URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
qianl15 committed Dec 5, 2024
1 parent dd04104 commit 9efa235
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ export class CloudSubscription {
const auth0UserID = ctxt.authenticatedUser;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const userEmail = ctxt.koaContext.state.user["https://dbos.dev/email"] as string;
const sessionURL = await ctxt.invokeWorkflow(Utils).createSubscription(auth0UserID, userEmail);
const body = ctxt.request.body as {success_url: string, cancel_url: string};
const successUrl = body.success_url as string ?? 'https://console.dbos.dev';
const cancelUrl = body.cancel_url as string ?? 'https://www.dbos.dev/pricing';
const sessionURL = await ctxt.invokeWorkflow(Utils).createSubscription(auth0UserID, userEmail, successUrl, cancelUrl);
if (!sessionURL) {
throw new DBOSResponseError("Failed to create a checkout session!", 500);
}
Expand Down
10 changes: 5 additions & 5 deletions src/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Utils {

// Workflow to create a Stripe checkout session
@Workflow()
static async createSubscription(ctxt: WorkflowContext, auth0UserID: string, userEmail: string): Promise<string|null> {
static async createSubscription(ctxt: WorkflowContext, auth0UserID: string, userEmail: string, successUrl: string, cancelUrl: string): Promise<string|null> {
// First, look up the customer from the accounts table
let stripeCustomerID = await ctxt.invoke(Utils).findStripeCustomerID(auth0UserID);

Expand All @@ -55,7 +55,7 @@ export class Utils {
}

// Finally, create a Stripe checkout session.
const res = await ctxt.invoke(Utils).createStripeCheckout(stripeCustomerID);
const res = await ctxt.invoke(Utils).createStripeCheckout(stripeCustomerID, successUrl, cancelUrl);
return res;
}

Expand Down Expand Up @@ -123,7 +123,7 @@ export class Utils {

// Create a Stripe checkout session for a customer
@Communicator({intervalSeconds: 10, maxAttempts: 2})
static async createStripeCheckout(_ctxt: CommunicatorContext, stripeCustomerID: string): Promise<string|null> {
static async createStripeCheckout(_ctxt: CommunicatorContext, stripeCustomerID: string, successUrl: string, cancelUrl: string): Promise<string|null> {
const session = await stripe.checkout.sessions.create({
customer: stripeCustomerID,
billing_address_collection: 'auto',
Expand All @@ -134,8 +134,8 @@ export class Utils {
},
],
mode: 'subscription',
success_url: `https://docs.dbos.dev`,
cancel_url: `https://www.dbos.dev/pricing`,
success_url: successUrl,
cancel_url: cancelUrl,
allow_promotion_codes: true,
});
return session.url;
Expand Down

0 comments on commit 9efa235

Please sign in to comment.