When retrieving customer information from Stripe, there are three possible object types that can be returned. Typically, I am interested in obtaining the customer ID.
The code snippet below might not be the most elegant solution to handle this scenario.
Is there a more efficient and cleaner way to achieve this?
const stripeCustomer: string | Stripe.Customer | Stripe.DeletedCustomer = checkoutSession.customer;
let stripeCustomerId: string;
if (stripeCustomer instanceof Object && "email" in stripeCustomer) {
// The customer is of type 'Stripe.Customer'
stripeCustomerId = stripeCustomer.id;
} else if (typeof stripeCustomer === 'string') {
// The customer is an ID
stripeCustomerId = stripeCustomer;
} else {
// The customer is of type 'Stripe.DeletedCustomer'
stripeCustomerId = null;
}