mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
33f26fbf32
refs https://github.com/TryGhost/Team/issues/616 We need a way to assign Products to Members via a Subscription, and we've followed the same pattern as the editSubscription method for the Members API controller, which acts upon Subscriptions as a nested resource. Subscriptions now are linked to products, and we've included those links by default in the Member Admin API as we already include subscriptions by default, and Products are now a core part of the Members feature-set.
77 lines
3.3 KiB
JavaScript
77 lines
3.3 KiB
JavaScript
const ghostBookshelf = require('./base');
|
|
|
|
const StripeCustomerSubscription = ghostBookshelf.Model.extend({
|
|
tableName: 'members_stripe_customers_subscriptions',
|
|
|
|
customer() {
|
|
return this.belongsTo('MemberStripeCustomer', 'customer_id', 'customer_id');
|
|
},
|
|
|
|
stripePrice() {
|
|
return this.hasOne('StripePrice', 'stripe_price_id', 'stripe_price_id');
|
|
},
|
|
|
|
serialize(options) {
|
|
const defaultSerializedObject = ghostBookshelf.Model.prototype.serialize.call(this, options);
|
|
|
|
const serialized = {
|
|
id: defaultSerializedObject.subscription_id,
|
|
customer: {
|
|
id: defaultSerializedObject.customer_id,
|
|
// TODO? The customer is not fetched by default so these sometimes won't exist
|
|
name: defaultSerializedObject.customer ? defaultSerializedObject.customer.name : null,
|
|
email: defaultSerializedObject.customer ? defaultSerializedObject.customer.email : null
|
|
},
|
|
plan: {
|
|
id: defaultSerializedObject.plan_id,
|
|
nickname: defaultSerializedObject.plan_nickname,
|
|
amount: defaultSerializedObject.plan_amount,
|
|
interval: defaultSerializedObject.plan_interval,
|
|
currency: String.prototype.toUpperCase.call(defaultSerializedObject.plan_currency)
|
|
},
|
|
status: defaultSerializedObject.status,
|
|
start_date: defaultSerializedObject.start_date,
|
|
default_payment_card_last4: defaultSerializedObject.default_payment_card_last4,
|
|
cancel_at_period_end: defaultSerializedObject.cancel_at_period_end,
|
|
cancellation_reason: defaultSerializedObject.cancellation_reason,
|
|
current_period_end: defaultSerializedObject.current_period_end
|
|
};
|
|
|
|
if (defaultSerializedObject.stripePrice) {
|
|
serialized.price = {
|
|
id: defaultSerializedObject.stripePrice.stripe_price_id,
|
|
nickname: defaultSerializedObject.stripePrice.nickname,
|
|
amount: defaultSerializedObject.stripePrice.amount,
|
|
interval: defaultSerializedObject.stripePrice.interval,
|
|
currency: String.prototype.toUpperCase.call(defaultSerializedObject.stripePrice.currency)
|
|
};
|
|
|
|
if (defaultSerializedObject.stripePrice.stripeProduct) {
|
|
serialized.price.product = {
|
|
id: defaultSerializedObject.stripePrice.stripeProduct.stripe_product_id,
|
|
name: defaultSerializedObject.stripePrice.stripeProduct.name,
|
|
product_id: defaultSerializedObject.stripePrice.stripeProduct.product_id
|
|
};
|
|
}
|
|
}
|
|
|
|
return serialized;
|
|
}
|
|
|
|
}, {
|
|
async upsert(data, unfilteredOptions) {
|
|
const subscriptionId = unfilteredOptions.subscription_id;
|
|
const model = await this.findOne({subscription_id: subscriptionId}, unfilteredOptions);
|
|
if (model) {
|
|
return this.edit(data, Object.assign({}, unfilteredOptions, {
|
|
id: model.id
|
|
}));
|
|
}
|
|
return this.add(data, unfilteredOptions);
|
|
}
|
|
});
|
|
|
|
module.exports = {
|
|
StripeCustomerSubscription: ghostBookshelf.model('StripeCustomerSubscription', StripeCustomerSubscription)
|
|
};
|