I'm trying to integrate Angular 8 with Stripe. Reading the documentation, I did see that I need to expand the Customer retrieve to get the Sources data, but I didn't get yet:
customers.ts
// Read the stripe customer ID from firestore, or create a new one if missing
export const getOrCreateCustomer = async (uid: string) => {
const user = await getUser(uid);
const customerId = user && user.stripeCustomerId;
const listOptions = {limit: 100};
// If missing customerID, create it
if (!customerId) {
return createCustomer(uid);
} else {
const params = stripe.customers.listSources(customerId, listOptions); // -> this is my start approach, but I don't know how to include it into Customer Retrieve
const customer = stripe.customers.retrieve(customerId);
console.log(customer);
return customer;
}
};
sources.ts
// Attaches a payment source to a stripe customer account.
export const attachSource = async (uid: string, source: string) => {
const customer = await getOrCreateCustomer(uid);
const existingSource = customer.sources.data.filter((s) => s.id === source).pop();// -> here there is an error
if (existingSource) {
return existingSource;
} else {
await stripe.customers.createSource(customer.id, { source });
return await stripe.customers.update(customer.id, { default_source: source });
}
};
Since I couldn't expand the Customer retrieve, the sources.ts file displays the error:
Property 'sources' does not exist on type 'Response | (DeletedCustomer & { lastResponse: { headers: { [key: string]: string; }; requestId: string; statusCode: number; apiVersion?: string; idempotencyKey?: string; stripeAccount?: string; }; })'. Property 'sources' does not exist on type 'DeletedCustomer & { lastResponse: { headers: { [key: string]: string; }; requestId: string; statusCode: number; apiVersion?: string; idempotencyKey?: string; stripeAccount?: string; }; }'.ts(2339)
I found in a post something similar, but in PHP, that mentions about cards. But I didn't understand if it would be the same thing I'm looking for (furthermore, it doesn't explain how to get the data I need).
How can I do this?