Skip to content

Commit

Permalink
fix stripe default_payment_method removed
Browse files Browse the repository at this point in the history
  • Loading branch information
sspenst committed Mar 30, 2024
1 parent 8e562e6 commit aad72ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 5 additions & 5 deletions components/settings/settingsPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ export default function SettingsPro({ stripeCustomerPortalLink, stripePaymentLin
<div className='font-bold'>Received Gifted Pro:</div>
<div className='text-sm text-left'>
<div className='flex gap-1 items-center'>Gifted From: <FormattedUser id={'subscription-' + subscriptionData.subscriptionId} user={subscriptionData.giftFromUser} /></div>
<div>Status: <span className='font-bold'>{subscriptionData.cancel_at ? 'Ends ' + dayjs(new Date(subscriptionData.cancel_at * 1000)).format('MMMM Do, YYYY') : 'Active'}</span></div>
{!subscriptionData.cancel_at && subscriptionData.current_period_end && (<div>Renews: {dayjs(new Date(subscriptionData.current_period_end * 1000)).format('MMMM Do, YYYY')}</div>)}
<div>Status: <span className='font-bold'>{subscriptionData.cancel_at ? 'Ends ' + dayjs(new Date(subscriptionData.cancel_at * 1000)).format('MMMM DD, YYYY') : 'Active'}</span></div>
{!subscriptionData.cancel_at && subscriptionData.current_period_end && (<div>Renews: {dayjs(new Date(subscriptionData.current_period_end * 1000)).format('MMMM DD, YYYY')}</div>)}
</div>
</div>
)}
Expand Down Expand Up @@ -316,9 +316,9 @@ export default function SettingsPro({ stripeCustomerPortalLink, stripePaymentLin
<div className='flex gap-1 items-center'>
Gifted to: <FormattedUser id={'subscription-' + subscriptionData.subscriptionId} user={subscriptionData.giftToUser} /></div>
)}
{!subscriptionData.cancel_at_period_end && subscriptionData.current_period_end && (<div>Renews: <span className='font-bold'>{dayjs(new Date(subscriptionData.current_period_end * 1000)).format('MMMM Do, YYYY')}</span></div>)}
<div>Status: <span className='font-bold'>{subscriptionData.cancel_at ? 'Ends ' + dayjs(new Date(subscriptionData.cancel_at * 1000)).format('MMMM Do, YYYY') : 'Active'}</span></div>
<div>Card Used: {subscriptionData.paymentMethod?.card?.brand} ending in {subscriptionData.paymentMethod?.card?.last4}</div>
{!subscriptionData.cancel_at_period_end && subscriptionData.current_period_end && (<div>Renews: <span className='font-bold'>{dayjs(new Date(subscriptionData.current_period_end * 1000)).format('MMMM DD, YYYY')}</span></div>)}
<div>Status: <span className='font-bold'>{subscriptionData.cancel_at ? 'Ends ' + dayjs(new Date(subscriptionData.cancel_at * 1000)).format('MMMM DD, YYYY') : 'Active'}</span></div>
<div>Card Used: {subscriptionData.paymentMethod?.card ? `${subscriptionData.paymentMethod.card.brand} ending in ${subscriptionData.paymentMethod.card.last4}` : 'Not found'}</div>
{subscriptionData.cancel_at_period_end &&
<span className='font-bold'>
Subscription will cancel at period end
Expand Down
8 changes: 6 additions & 2 deletions pages/api/subscription/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface SubscriptionData {
current_period_end: number;
current_period_start: number;
giftToUser?: User | null;
paymentMethod: Stripe.PaymentMethod;
paymentMethod: Stripe.PaymentMethod | null;
plan: Stripe.Plan;
planName: string;
status: Stripe.Subscription.Status;
Expand Down Expand Up @@ -64,7 +64,11 @@ export async function getSubscriptions(req: NextApiRequestWithAuth): Promise<[nu
const product = await stripe.products.retrieve(plan.product as string);
const planName = product.name;

const paymentMethod = await stripe.paymentMethods.retrieve(subscription.default_payment_method as string);
let paymentMethod: Stripe.PaymentMethod | null = null;

if (subscription.default_payment_method) {
paymentMethod = await stripe.paymentMethods.retrieve(subscription.default_payment_method as string);
}

// if subscription has metadata... it is a gift and we should query the gift id
let giftToUser = undefined;
Expand Down

0 comments on commit aad72ee

Please sign in to comment.