Skip to content

Commit

Permalink
Add customer name
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 committed Sep 2, 2022
1 parent 36f16c4 commit a022138
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/pages/api/stripe/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ const webhookHandler = async (
return;
}

if (!invoice.customer_name) {
throw new Error(
'customer_name is empty in invoice'
);
}
const customerName = await getCustomerName(invoice);

// Generate PDF with the given info.
const stripeBuyDate = new Date(invoice.created * 1000);
Expand All @@ -153,7 +149,7 @@ const webhookHandler = async (
license_end_date: licenseEndDate,
number_devs: 8,
stripe_buy_date: stripeBuyDate,
stripe_buyer_name: invoice.customer_name,
stripe_buyer_name: customerName,
stripe_buyer_email: invoice.customer_email,
stripe_buyer_address: stripeAddressToString(
invoice.customer_address
Expand Down Expand Up @@ -243,4 +239,22 @@ function stripeAddressToString(addr: Stripe.Address | null): string {
.join(', ');
}

// / Stripe invoice object doesn't always include the customer name. If it's
// not present, we make an additional API call.
async function getCustomerName(invoice: Stripe.Invoice): Promise<string> {
if (invoice.customer_name) {
return invoice.customer_name;
}

try {
const c = (await stripe.customers.retrieve(
invoice.customer as string
)) as Stripe.Customer;

return c.name || 'Reacher customer';
} catch (e) {
return 'Reacher customer';
}
}

export default withSentry(webhookHandler);

0 comments on commit a022138

Please sign in to comment.