Stripe cancel and duplicate key value violates unique constraint

Hi there, I'm trying to add Stripe to my website and I found something strange. I guess I forgot something, but can't find the info... so: I have that button which call
await auth.subscription.upgrade({
plan,
successUrl: `/${locale}/dashboard`,
cancelUrl: `/${locale}/pricing`,
annual,
})
await auth.subscription.upgrade({
plan,
successUrl: `/${locale}/dashboard`,
cancelUrl: `/${locale}/pricing`,
annual,
})
It opens the Stripe page. On that page, if you click the "back to website" link, and try again to subscribe, you got the following error
query: 'insert into "subscriptions" ("id", "plan", "reference_id", "stripe_customer_id", "stripe_subscription_id", "status", "period_start", "period_end", "cancel_at_period_end", "seats", "created_at", "updated_at") values ($1, $2, $3, $4, default, $5, default, default, default, $6, default, default) returning "id", "plan", "reference_id", "stripe_customer_id", "stripe_subscription_id", "status", "period_start", "period_end", "cancel_at_period_end", "seats", "created_at", "updated_at"',
params: [Array],
[cause]: [error: duplicate key value violates unique constraint "subscriptions_reference_id_unique"] {
length: 275,
severity: 'ERROR',
code: '23505',
detail: 'Key (reference_id)=(phDhsbH.....0bKoq9NTl) already exists.',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'subscriptions',
column: undefined,
dataType: undefined,
constraint: 'subscriptions_reference_id_unique',
file: 'nbtinsert.c',
line: '673',
routine: '_bt_check_unique'
}
}
query: 'insert into "subscriptions" ("id", "plan", "reference_id", "stripe_customer_id", "stripe_subscription_id", "status", "period_start", "period_end", "cancel_at_period_end", "seats", "created_at", "updated_at") values ($1, $2, $3, $4, default, $5, default, default, default, $6, default, default) returning "id", "plan", "reference_id", "stripe_customer_id", "stripe_subscription_id", "status", "period_start", "period_end", "cancel_at_period_end", "seats", "created_at", "updated_at"',
params: [Array],
[cause]: [error: duplicate key value violates unique constraint "subscriptions_reference_id_unique"] {
length: 275,
severity: 'ERROR',
code: '23505',
detail: 'Key (reference_id)=(phDhsbH.....0bKoq9NTl) already exists.',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'subscriptions',
column: undefined,
dataType: undefined,
constraint: 'subscriptions_reference_id_unique',
file: 'nbtinsert.c',
line: '673',
routine: '_bt_check_unique'
}
}
I'm using Drizzle and the screenshot is my better-auth config. I guess I'm missing some "cancelation" but I can't figure how/where
No description
2 Replies
Ping
Ping4mo ago
Hey @bmichotte do you still have this issue on latest version of Better-auth?
bmichotte
bmichotteOP4mo ago
Hello @Ping, I haven't tried the latest version, currently stick with the PR from https://github.com/better-auth/better-auth/issues/2929
GitHub
Can't upgrade an incomplete Stripe subscription · Issue #2929 · b...
Is this suited for github? Yes, this is suited for github To Reproduce Better-Auth configured with Drizzle adapter Stripe plugin configured User logged Click on the "subscribe button" and...

Did you find this page helpful?