Skip to content

Commit

Permalink
feat: Pick elements_configuration from the checkout start response
Browse files Browse the repository at this point in the history
  • Loading branch information
vicfergar committed Feb 6, 2025
1 parent 8ab822c commit f9ecb2e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/networking/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ export class Backend {
type CheckoutStartRequestBody = {
app_user_id: string;
product_id: string;
email?: string;
presented_offering_identifier: string;
price_id: string;
presented_placement_identifier?: string;
offer_id?: string;
price_id: string;
applied_targeting_rule?: {
rule_id: string;
revision: number;
};
email?: string;
};

const requestBody: CheckoutStartRequestBody = {
Expand Down
19 changes: 19 additions & 0 deletions src/networking/responses/checkout-start-response.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
export enum StripeElementsMode {
Payment = "payment",
Setup = "setup",
}

export enum StripeElementsSetupFutureUsage {
OffSession = "off_session",
OnSession = "on_session",
}

interface StripeElementsConfiguration {
mode: StripeElementsMode;
payment_method_types: string[];
setup_future_usage: StripeElementsSetupFutureUsage;
amount?: number;
currency?: string;
}

export interface CheckoutStartResponse {
operation_session_id: string;
data: {
stripe_account_id?: string;
publishable_api_key?: string;
elements_configuration?: StripeElementsConfiguration;
};
}
9 changes: 8 additions & 1 deletion src/stories/utils/purchase-response-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CheckoutStartResponse } from "src/networking/responses/checkout-start-response";
import { StripeElementsMode, StripeElementsSetupFutureUsage, type CheckoutStartResponse } from "src/networking/responses/checkout-start-response";

const publishableApiKey = import.meta.env.VITE_STORYBOOK_PUBLISHABLE_API_KEY;
const accountId = import.meta.env.VITE_STORYBOOK_ACCOUNT_ID;
Expand All @@ -21,6 +21,13 @@ export const buildCheckoutStartResponse = (): CheckoutStartResponse => {
data: {
publishable_api_key: publishableApiKey,
stripe_account_id: accountId,
elements_configuration: {
mode: StripeElementsMode.Payment,
payment_method_types: ["card"],
setup_future_usage: StripeElementsSetupFutureUsage.OffSession,
amount: 1000,
currency: "usd",
},
},
};

Expand Down
21 changes: 11 additions & 10 deletions src/ui/states/state-needs-payment-info.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@
}
onMount(async () => {
const stripePk = paymentInfoCollectionMetadata.data.publishable_api_key;
const stripeAcctId = paymentInfoCollectionMetadata.data.stripe_account_id;
if (!stripePk || !stripeAcctId) {
throw new Error("Stripe publishable key or account ID not found");
const configurationData = paymentInfoCollectionMetadata.data;
const stripePk = configurationData.publishable_api_key;
const stripeAcctId = configurationData.stripe_account_id;
const elementsConfiguration = configurationData.elements_configuration;
if (!stripePk || !stripeAcctId || !elementsConfiguration) {
throw new Error("Stripe configuration is missing");
}
stripe = await loadStripe(stripePk, {
Expand All @@ -148,11 +149,11 @@
elements = stripe.elements({
loader: "always",
locale: localeToUse,
mode: "payment",
paymentMethodTypes: ["card"],
setup_future_usage: "off_session",
amount: productDetails.currentPrice.amountMicros / 10000,
currency: productDetails.currentPrice.currency.toLowerCase(),
mode: elementsConfiguration.mode,
paymentMethodTypes: elementsConfiguration.payment_method_types,
setupFutureUsage: elementsConfiguration.setup_future_usage,
amount: elementsConfiguration.amount,
currency: elementsConfiguration.currency,
appearance: {
theme: "stripe",
labels: "floating",
Expand Down

0 comments on commit f9ecb2e

Please sign in to comment.