Skip to content

Commit 0f59c2f

Browse files
committed
account for live/test-mode in stripeAppClient
1 parent cd6088e commit 0f59c2f

6 files changed

Lines changed: 31 additions & 22 deletions

File tree

apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
151151
const connectedCustomer = await getConnectedCustomer({
152152
stripeCustomerId,
153153
stripeAccountId,
154+
livemode: event.livemode,
154155
});
155156

156157
if (!connectedCustomer || !connectedCustomer.metadata.dubCustomerId) {

apps/web/app/api/stripe/integration/webhook/invoice-paid.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export async function invoicePaid(event: Stripe.Event) {
3131
const connectedCustomer = await getConnectedCustomer({
3232
stripeCustomerId,
3333
stripeAccountId,
34+
livemode: event.livemode,
3435
});
3536
const dubCustomerId = connectedCustomer?.metadata.dubCustomerId;
3637

apps/web/app/api/stripe/integration/webhook/utils.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,22 @@ export async function createNewCustomer(event: Stripe.Event) {
129129
export async function getConnectedCustomer({
130130
stripeCustomerId,
131131
stripeAccountId,
132+
livemode = true,
132133
}: {
133134
stripeCustomerId?: string | null;
134135
stripeAccountId?: string | null;
136+
livemode?: boolean;
135137
}) {
136138
// if stripeCustomerId or stripeAccountId is not provided, return null
137139
if (!stripeCustomerId || !stripeAccountId) {
138140
return null;
139141
}
140142

141-
const connectedCustomer = await stripeAppClient.customers.retrieve(
142-
stripeCustomerId,
143-
{
144-
stripeAccount: stripeAccountId,
145-
},
146-
);
143+
const connectedCustomer = await stripeAppClient({
144+
livemode,
145+
}).customers.retrieve(stripeCustomerId, {
146+
stripeAccount: stripeAccountId,
147+
});
147148

148149
if (connectedCustomer.deleted) {
149150
return null;

apps/web/lib/stripe/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ export const stripe = new Stripe(`${process.env.STRIPE_SECRET_KEY}`, {
99
});
1010

1111
// Stripe Integration App client
12-
export const stripeAppClient = new Stripe(
13-
`${process.env.STRIPE_APP_SECRET_KEY}`,
14-
{
15-
apiVersion: "2022-11-15",
16-
appInfo: {
17-
name: "Dub.co",
18-
version: "0.1.0",
12+
export const stripeAppClient = ({
13+
livemode,
14+
}: {
15+
livemode?: boolean;
16+
} = {}) =>
17+
new Stripe(
18+
`${!livemode ? process.env.STRIPE_APP_SECRET_KEY_TEST : process.env.STRIPE_APP_SECRET_KEY}`,
19+
{
20+
apiVersion: "2022-11-15",
21+
appInfo: {
22+
name: "Dub.co",
23+
version: "0.1.0",
24+
},
1925
},
20-
},
21-
);
26+
);

apps/web/scripts/stripe/backfill-stripe-webhook-events.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ async function main() {
2626
if (!customer.email) return;
2727
if (customer.stripeCustomerId) return;
2828

29-
const stripeCustomer = await stripeAppClient.customers.list(
29+
const stripeCustomer = await stripeAppClient({
30+
livemode: false,
31+
}).customers.list(
3032
{
3133
email: customer.email,
3234
},

apps/web/scripts/stripe/get-connected-customer.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import "dotenv-flow/config";
22
import { stripeAppClient } from "../../lib/stripe";
33

44
async function main() {
5-
const connectedCustomer = await stripeAppClient.customers.retrieve(
6-
"cus_xxx",
7-
{
8-
stripeAccount: "acct_xxx",
9-
},
10-
);
5+
const connectedCustomer = await stripeAppClient({
6+
livemode: false,
7+
}).customers.retrieve("cus_xxx", {
8+
stripeAccount: "acct_xxx",
9+
});
1110

1211
console.log(connectedCustomer);
1312
}

0 commit comments

Comments
 (0)