|
|
@@ -207,41 +207,53 @@ function subscriptionIsCanceledOrExpired(subscription) {
|
|
|
/**
|
|
|
* Build a RecurlySubscription from Recurly API data
|
|
|
*
|
|
|
- * @param {recurly.Subscription} subscription
|
|
|
+ * @param {recurly.Subscription} apiSubscription
|
|
|
* @return {RecurlySubscription}
|
|
|
*/
|
|
|
-function subscriptionFromApi(subscription) {
|
|
|
+function subscriptionFromApi(apiSubscription) {
|
|
|
if (
|
|
|
- subscription.uuid == null ||
|
|
|
- subscription.plan == null ||
|
|
|
- subscription.plan.code == null ||
|
|
|
- subscription.plan.name == null ||
|
|
|
- subscription.account == null ||
|
|
|
- subscription.account.code == null ||
|
|
|
- subscription.unitAmount == null ||
|
|
|
- subscription.subtotal == null ||
|
|
|
- subscription.total == null ||
|
|
|
- subscription.currency == null ||
|
|
|
- subscription.currentPeriodStartedAt == null ||
|
|
|
- subscription.currentPeriodEndsAt == null
|
|
|
+ apiSubscription.uuid == null ||
|
|
|
+ apiSubscription.plan == null ||
|
|
|
+ apiSubscription.plan.code == null ||
|
|
|
+ apiSubscription.plan.name == null ||
|
|
|
+ apiSubscription.account == null ||
|
|
|
+ apiSubscription.account.code == null ||
|
|
|
+ apiSubscription.unitAmount == null ||
|
|
|
+ apiSubscription.subtotal == null ||
|
|
|
+ apiSubscription.total == null ||
|
|
|
+ apiSubscription.currency == null ||
|
|
|
+ apiSubscription.currentPeriodStartedAt == null ||
|
|
|
+ apiSubscription.currentPeriodEndsAt == null
|
|
|
) {
|
|
|
- throw new OError('Invalid Recurly subscription', { subscription })
|
|
|
+ throw new OError('Invalid Recurly subscription', {
|
|
|
+ subscription: apiSubscription,
|
|
|
+ })
|
|
|
}
|
|
|
- return new RecurlySubscription({
|
|
|
- id: subscription.uuid,
|
|
|
- userId: subscription.account.code,
|
|
|
- planCode: subscription.plan.code,
|
|
|
- planName: subscription.plan.name,
|
|
|
- planPrice: subscription.unitAmount,
|
|
|
- addOns: (subscription.addOns ?? []).map(subscriptionAddOnFromApi),
|
|
|
- subtotal: subscription.subtotal,
|
|
|
- taxRate: subscription.taxInfo?.rate ?? 0,
|
|
|
- taxAmount: subscription.tax ?? 0,
|
|
|
- total: subscription.total,
|
|
|
- currency: subscription.currency,
|
|
|
- periodStart: subscription.currentPeriodStartedAt,
|
|
|
- periodEnd: subscription.currentPeriodEndsAt,
|
|
|
+
|
|
|
+ const subscription = new RecurlySubscription({
|
|
|
+ id: apiSubscription.uuid,
|
|
|
+ userId: apiSubscription.account.code,
|
|
|
+ planCode: apiSubscription.plan.code,
|
|
|
+ planName: apiSubscription.plan.name,
|
|
|
+ planPrice: apiSubscription.unitAmount,
|
|
|
+ addOns: (apiSubscription.addOns ?? []).map(subscriptionAddOnFromApi),
|
|
|
+ subtotal: apiSubscription.subtotal,
|
|
|
+ taxRate: apiSubscription.taxInfo?.rate ?? 0,
|
|
|
+ taxAmount: apiSubscription.tax ?? 0,
|
|
|
+ total: apiSubscription.total,
|
|
|
+ currency: apiSubscription.currency,
|
|
|
+ periodStart: apiSubscription.currentPeriodStartedAt,
|
|
|
+ periodEnd: apiSubscription.currentPeriodEndsAt,
|
|
|
})
|
|
|
+
|
|
|
+ if (apiSubscription.pendingChange != null) {
|
|
|
+ subscription.pendingChange = subscriptionChangeFromApi(
|
|
|
+ subscription,
|
|
|
+ apiSubscription.pendingChange
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ return subscription
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -289,14 +301,22 @@ function subscriptionChangeFromApi(subscription, subscriptionChange) {
|
|
|
const nextAddOns = (subscriptionChange.addOns ?? []).map(
|
|
|
subscriptionAddOnFromApi
|
|
|
)
|
|
|
+
|
|
|
+ let immediateCharge =
|
|
|
+ subscriptionChange.invoiceCollection?.chargeInvoice?.total ?? 0
|
|
|
+ for (const creditInvoice of subscriptionChange.invoiceCollection
|
|
|
+ ?.creditInvoices ?? []) {
|
|
|
+ // The credit invoice totals are already negative
|
|
|
+ immediateCharge += creditInvoice.total ?? 0
|
|
|
+ }
|
|
|
+
|
|
|
return new RecurlySubscriptionChange({
|
|
|
subscription,
|
|
|
nextPlanCode: subscriptionChange.plan.code,
|
|
|
nextPlanName: subscriptionChange.plan.name,
|
|
|
nextPlanPrice: subscriptionChange.unitAmount,
|
|
|
nextAddOns,
|
|
|
- immediateCharge:
|
|
|
- subscriptionChange.invoiceCollection?.chargeInvoice?.total ?? 0,
|
|
|
+ immediateCharge,
|
|
|
})
|
|
|
}
|
|
|
|