backend-manager 2.5.77 → 2.5.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "2.5.77",
3
+ "version": "2.5.78",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -62,4 +62,4 @@
62
62
  "wonderful-fetch": "^0.0.17",
63
63
  "yargs": "^17.7.1"
64
64
  }
65
- }
65
+ }
@@ -116,14 +116,27 @@ SubscriptionResolver.prototype.resolve = function (options) {
116
116
  get(resource, 'billing_info.last_payment.time', 0)
117
117
  )
118
118
 
119
+ // Set last payment
120
+ if (get(resource, 'billing_info.last_payment')) {
121
+ resolved.lastPayment.amount = parseFloat(resource.billing_info.last_payment.amount.value);
122
+ resolved.lastPayment.date.timestamp = moment(resource.billing_info.last_payment.time);
123
+ }
124
+
119
125
  // Get trial
120
126
  const trialTenure = get(resource, 'plan.billing_cycles', []).find((cycle) => cycle.tenure_type === 'TRIAL');
121
127
  const regularTenure = get(resource, 'plan.billing_cycles', []).find((cycle) => cycle.tenure_type === 'REGULAR');
122
128
 
123
129
  // Resolve trial
130
+ /*
131
+ Special condition for PayPal
132
+ Because you cannot remove trial on a sub-level, you have to charge a prorated amount for the "trial".
133
+ Even if charged, it is still considered a trial period by paypal.
134
+ Thus, we must remove the trial indicator if the user has been charged.
135
+ */
124
136
  if (
125
137
  resolved.status === 'active'
126
138
  && (trialTenure && regularTenure && regularTenure.total_cycles === 0)
139
+ && resolved.lastPayment.amount === 0
127
140
  ) {
128
141
  resolved.trial.active = true;
129
142
 
@@ -154,12 +167,6 @@ SubscriptionResolver.prototype.resolve = function (options) {
154
167
  resolved.payment.completed = !['CREATED', 'SAVED', 'APPROVED', 'VOIDED', 'PAYER_ACTION_REQUIRED'].includes(resource.status);
155
168
  }
156
169
 
157
- // Set last payment
158
- if (resource.billing_info && resource.billing_info.last_payment) {
159
- resolved.lastPayment.amount = parseFloat(resource.billing_info.last_payment.amount.value);
160
- resolved.lastPayment.date.timestamp = moment(resource.billing_info.last_payment.time);
161
- }
162
-
163
170
  } else if (profile.processor === 'chargebee') {
164
171
  // Set status
165
172
  // subscription: https://apidocs.chargebee.com/docs/api/subscriptions?prod_cat_ver=2#subscription_status
@@ -187,6 +194,12 @@ SubscriptionResolver.prototype.resolve = function (options) {
187
194
  ) * 1000
188
195
  )
189
196
 
197
+ // Set last payment @@@ TODO
198
+ // if (resource.billing_info && resource.billing_info.last_payment) {
199
+ // resolved.lastPayment.amount = parseFloat(resource.billing_info.last_payment.amount.value);
200
+ // resolved.lastPayment.date.timestamp = moment(resource.billing_info.last_payment.time);
201
+ // }
202
+
190
203
  // Get trial
191
204
  if (resource.status === 'in_trial') {
192
205
  resolved.trial.active = true;
@@ -218,12 +231,6 @@ SubscriptionResolver.prototype.resolve = function (options) {
218
231
  resolved.payment.completed = !['future'].includes(resource.status);
219
232
  }
220
233
 
221
- // Set last payment @@@ TODO
222
- // if (resource.billing_info && resource.billing_info.last_payment) {
223
- // resolved.lastPayment.amount = parseFloat(resource.billing_info.last_payment.amount.value);
224
- // resolved.lastPayment.date.timestamp = moment(resource.billing_info.last_payment.time);
225
- // }
226
-
227
234
  } else if (profile.processor === 'stripe') {
228
235
  // Subscription: https://stripe.com/docs/api/subscriptions/object#subscription_object-status
229
236
  // incomplete, incomplete_expired, trialing, active, past_due, canceled, or unpaid
@@ -252,6 +259,14 @@ SubscriptionResolver.prototype.resolve = function (options) {
252
259
  get(resource, 'current_period_start', 0) * 1000
253
260
  );
254
261
 
262
+ // Set last payment
263
+ if (resource.latest_invoice) {
264
+ resolved.lastPayment.amount = resource.latest_invoice.amount_paid / 100;
265
+ resolved.lastPayment.date.timestamp = moment(
266
+ get(resource, 'latest_invoice.created', 0) * 1000
267
+ );
268
+ }
269
+
255
270
  // Get trial
256
271
  if (resource.status === 'trialing') {
257
272
  resolved.trial.active = true;
@@ -264,7 +279,7 @@ SubscriptionResolver.prototype.resolve = function (options) {
264
279
  )
265
280
  }
266
281
 
267
- // Resolve frequency @@@
282
+ // Resolve frequency
268
283
  const unit = resource.plan.interval;
269
284
  if (unit === 'year') {
270
285
  resolved.frequency = 'annually';
@@ -285,14 +300,6 @@ SubscriptionResolver.prototype.resolve = function (options) {
285
300
  resolved.payment.completed = !['requires_payment_method', 'requires_confirmation', 'requires_action', 'processing', 'requires_capture', 'canceled'].includes(resource.status);
286
301
  }
287
302
 
288
- // Set last payment
289
- if (resource.latest_invoice) {
290
- resolved.lastPayment.amount = resource.latest_invoice.amount_paid / 100;
291
- resolved.lastPayment.date.timestamp = moment(
292
- get(resource, 'latest_invoice.created', 0) * 1000
293
- );
294
- }
295
-
296
303
  } else if (profile.processor === 'coinbase') {
297
304
  // Set status
298
305
  resolved.status = 'cancelled';
@@ -310,6 +317,12 @@ SubscriptionResolver.prototype.resolve = function (options) {
310
317
  get(resource, 'created_at', 0)
311
318
  );
312
319
 
320
+ // Set last payment
321
+ if (lastPayment) {
322
+ resolved.lastPayment.amount = parseFloat(lastPayment.value.local.amount);
323
+ resolved.lastPayment.date.timestamp = moment(lastPayment.detected_at);
324
+ }
325
+
313
326
  // Get trial
314
327
  if (true) {
315
328
  resolved.trial.active = false;
@@ -329,12 +342,6 @@ SubscriptionResolver.prototype.resolve = function (options) {
329
342
  resolved.payment.completed = !!lastPayment;
330
343
  }
331
344
 
332
- // Set last payment
333
- if (lastPayment) {
334
- resolved.lastPayment.amount = parseFloat(lastPayment.value.local.amount);
335
- resolved.lastPayment.date.timestamp = moment(lastPayment.detected_at);
336
- }
337
-
338
345
  } else {
339
346
  throw new Error('Unknown processor');
340
347
  }