mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
Ensured that we do not insert orphaned rows (#190)
no-issue Previously we would blindly put subscriptions into the database when we received a webhook, which could result in orphaned rows that were not linked to a customer (and by extension a member) This updates the logic so that we will only add subscriptions if we have a record of their customer. Customers are only added during a checkout.session.completed webhook, at which point a member is guarunteed, but for formailty and safety against changes in the flow, the logic has been applied to inserting customers too.
This commit is contained in:
parent
20e3b6cc8a
commit
e7484638e3
2 changed files with 17 additions and 4 deletions
|
@ -41,6 +41,7 @@ module.exports = function MembersApi({
|
||||||
|
|
||||||
const {encodeIdentityToken, decodeToken} = Tokens({privateKey, publicKey, issuer});
|
const {encodeIdentityToken, decodeToken} = Tokens({privateKey, publicKey, issuer});
|
||||||
const metadata = Metadata({
|
const metadata = Metadata({
|
||||||
|
Member,
|
||||||
StripeWebhook,
|
StripeWebhook,
|
||||||
StripeCustomer,
|
StripeCustomer,
|
||||||
StripeCustomerSubscription
|
StripeCustomerSubscription
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
module.exports = function ({
|
module.exports = function ({
|
||||||
|
Member,
|
||||||
StripeWebhook,
|
StripeWebhook,
|
||||||
StripeCustomer,
|
StripeCustomer,
|
||||||
StripeCustomerSubscription
|
StripeCustomerSubscription
|
||||||
|
@ -9,16 +10,27 @@ module.exports = function ({
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metadata.customer) {
|
if (metadata.customer) {
|
||||||
|
const member = await Member.findOne({
|
||||||
|
id: metadata.customer.member_id
|
||||||
|
});
|
||||||
|
|
||||||
|
if (member) {
|
||||||
await StripeCustomer.upsert(metadata.customer, {
|
await StripeCustomer.upsert(metadata.customer, {
|
||||||
customer_id: metadata.customer.customer_id
|
customer_id: metadata.customer.customer_id
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (metadata.subscription) {
|
if (metadata.subscription) {
|
||||||
|
const customer = await StripeCustomer.findOne({
|
||||||
|
customer_id: metadata.subscription.customer_id
|
||||||
|
});
|
||||||
|
if (customer) {
|
||||||
await StripeCustomerSubscription.upsert(metadata.subscription, {
|
await StripeCustomerSubscription.upsert(metadata.subscription, {
|
||||||
subscription_id: metadata.subscription.subscription_id
|
subscription_id: metadata.subscription.subscription_id
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (metadata.webhook) {
|
if (metadata.webhook) {
|
||||||
await StripeWebhook.upsert(metadata.webhook, {
|
await StripeWebhook.upsert(metadata.webhook, {
|
||||||
|
|
Loading…
Add table
Reference in a new issue