#hearse_best-practices
1 messages ¡ Page 1 of 1 (latest)
đ Welcome to your new thread!
â˛ď¸ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
âąď¸ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
đ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1357165279792070759
đ Have more to share? Add more details, code, screenshots, videos, etc. below.
For example, this is what i am doing in test mode:
// test
const stripeClient = getStripeClient(c.env.STRIPE_SECRET_KEY);
assert.ok(stripeClient, 'Stripe client is not defined');
// Create a payment method
const paymentMethod =
await stripeClient.paymentMethods.retrieve('pm_card_visa');
assert.ok(paymentMethod, 'Payment method is not defined');
// Create a customer
const email = 'test@gmail.com';
const customer = await stripeClient.customers.create({
email: email,
payment_method: paymentMethod.id,
description: 'Created by testing environment',
});
// ... create subscription to invoke the webhook for invoice payment succeeded
// Inside my webhook ->
// => in my webhook i also use things like:
const product = await stripeClient.products.retrieve(productId);
// and then eventually use information to populate the database
dbManager.createUser(...);
i can't really create a unit test for this since, vitest would not receive the webhook event
Hi there, you can use stripe-mock to mock some API responese data for your unit testing
stripe-mock is a mock HTTP server that responds like the real Stripe API. It can be used instead of Stripe's testmode to make test suites integrating with Stripe faster and less brittle. - ...
got it, i saw that but i was still confused since in my webhook i use the stripe client still. Would that work?
What does getStripeClient return ? are you using Stripe Node.js SDK?
i use things like inside of my webhook:
const event = await stripeClient.webhooks.constructEventAsync(
(await c.req.text()) as string,
sig as string,
c.env.STRIPE_WEBHOOK_ENDPOINT_SECRET,
);
const stripeSubscription =
await stripeClient.subscriptions.retrieve(subscriptionId);
const productId = invoice.lines.data[0].price?.product as string;
if (!productId) {
throw new Error('Could not retrieve product id from invoice');
}
const product = await stripeClient.products.retrieve(productId);
yea
inside of cloudflare workers but i doubt that matters
Ok you can run stripe-mock in your machine, and use your machine's URL and port when initializing Stripe SDK (https://github.com/stripe/stripe-node?tab=readme-ov-file#initialize-with-config-object)
So that Stripe Node.js SDK will make request to your stripe-mock endpoint for mocked responses.
oh that makes perfect sense.
So basically i could start stripe-mock, setup the stripe sdk to use stripe-mock endpoint as the server.
And in my testing environment, create a mock request to my webhook (since i cannot receive http requests in my testing environment)
thank you