backend-manager 5.0.108 → 5.0.110
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/CHANGELOG.md +9 -0
- package/package.json +1 -1
- package/src/manager/events/firestore/payments-webhooks/on-write.js +47 -2
- package/src/manager/routes/payments/cancel/post.js +27 -1
- package/src/manager/routes/payments/cancel/processors/stripe.js +18 -5
- package/src/manager/routes/payments/cancel/processors/test.js +27 -14
- package/src/manager/routes/payments/refund/post.js +28 -0
- package/src/test/test-accounts.js +9 -0
- package/test/events/payments/journey-payments-cancel-endpoint.js +15 -0
- package/test/events/payments/journey-payments-cancel.js +26 -0
- package/test/events/payments/journey-payments-failure.js +26 -0
- package/test/events/payments/journey-payments-one-time-failure.js +3 -0
- package/test/events/payments/journey-payments-one-time.js +6 -1
- package/test/events/payments/journey-payments-plan-change.js +15 -0
- package/test/events/payments/journey-payments-suspend.js +26 -0
- package/test/events/payments/journey-payments-trial-cancel.js +124 -0
- package/test/events/payments/journey-payments-trial.js +29 -0
- package/test/events/payments/journey-payments-upgrade.js +6 -2
package/CHANGELOG.md
CHANGED
|
@@ -14,6 +14,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
|
14
14
|
- `Fixed` for any bug fixes.
|
|
15
15
|
- `Security` in case of vulnerabilities.
|
|
16
16
|
|
|
17
|
+
# [5.0.109] - 2026-03-04
|
|
18
|
+
### Added
|
|
19
|
+
- Immediate trial cancellation: cancelling during a free trial now terminates the subscription instantly instead of scheduling cancel at period end, preventing free premium access for the remainder of the trial.
|
|
20
|
+
- Intent status tracking: `payments-intents/{orderId}` is now updated with `status: completed/failed` and completion timestamp after webhook processing.
|
|
21
|
+
- `journey-payments-trial-cancel` test suite covering the full trial → cancel → immediate cancellation flow.
|
|
22
|
+
|
|
23
|
+
### Changed
|
|
24
|
+
- Stripe and test cancel processors now detect trialing state and dispatch immediate cancel (`customer.subscription.deleted`) vs period-end cancel (`customer.subscription.updated`).
|
|
25
|
+
|
|
17
26
|
# [5.0.106] - 2026-03-04
|
|
18
27
|
### Added
|
|
19
28
|
- `GET /payments/trial-eligibility`: returns whether the authenticated user is eligible for a free trial (checks for any previous subscription orders in `payments-orders`).
|
package/package.json
CHANGED
|
@@ -31,6 +31,9 @@ module.exports = async ({ assistant, change, context }) => {
|
|
|
31
31
|
// Set status to processing
|
|
32
32
|
await webhookRef.set({ status: 'processing' }, { merge: true });
|
|
33
33
|
|
|
34
|
+
// Hoisted so orderId is available in catch block for audit trail
|
|
35
|
+
let orderId = null;
|
|
36
|
+
|
|
34
37
|
try {
|
|
35
38
|
const processor = dataAfter.processor;
|
|
36
39
|
const uid = dataAfter.owner;
|
|
@@ -73,7 +76,7 @@ module.exports = async ({ assistant, change, context }) => {
|
|
|
73
76
|
const webhookReceivedUNIX = dataAfter.metadata?.received?.timestampUNIX || nowUNIX;
|
|
74
77
|
|
|
75
78
|
// Extract orderId from resource (processor-agnostic)
|
|
76
|
-
|
|
79
|
+
orderId = library.getOrderId(resource);
|
|
77
80
|
|
|
78
81
|
// Process the payment event (subscription or one-time)
|
|
79
82
|
if (category !== 'subscription' && category !== 'one-time') {
|
|
@@ -100,11 +103,28 @@ module.exports = async ({ assistant, change, context }) => {
|
|
|
100
103
|
} catch (e) {
|
|
101
104
|
assistant.error(`Webhook ${eventId} failed: ${e.message}`, e);
|
|
102
105
|
|
|
106
|
+
const now = powertools.timestamp(new Date(), { output: 'string' });
|
|
107
|
+
const nowUNIX = powertools.timestamp(now, { output: 'unix' });
|
|
108
|
+
|
|
103
109
|
// Mark as failed with error message
|
|
104
110
|
await webhookRef.set({
|
|
105
111
|
status: 'failed',
|
|
106
112
|
error: e.message || String(e),
|
|
107
113
|
}, { merge: true });
|
|
114
|
+
|
|
115
|
+
// Mark intent as failed if we resolved the orderId before the error
|
|
116
|
+
if (orderId) {
|
|
117
|
+
await admin.firestore().doc(`payments-intents/${orderId}`).set({
|
|
118
|
+
status: 'failed',
|
|
119
|
+
error: e.message || String(e),
|
|
120
|
+
metadata: {
|
|
121
|
+
completed: {
|
|
122
|
+
timestamp: now,
|
|
123
|
+
timestampUNIX: nowUNIX,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
}, { merge: true });
|
|
127
|
+
}
|
|
108
128
|
}
|
|
109
129
|
};
|
|
110
130
|
|
|
@@ -216,10 +236,35 @@ async function processPaymentEvent({ category, library, resource, resourceType,
|
|
|
216
236
|
|
|
217
237
|
// Write to payments-orders/{orderId}
|
|
218
238
|
if (orderId) {
|
|
219
|
-
|
|
239
|
+
const orderRef = admin.firestore().doc(`payments-orders/${orderId}`);
|
|
240
|
+
const orderSnap = await orderRef.get();
|
|
241
|
+
|
|
242
|
+
// Initialize requests on first creation only (avoid overwriting cancel/refund data set by endpoints)
|
|
243
|
+
if (!orderSnap.exists) {
|
|
244
|
+
order.requests = {
|
|
245
|
+
cancellation: null,
|
|
246
|
+
refund: null,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
await orderRef.set(order, { merge: true });
|
|
220
251
|
assistant.log(`Updated payments-orders/${orderId}: type=${category}, uid=${uid}, eventType=${eventType}`);
|
|
221
252
|
}
|
|
222
253
|
|
|
254
|
+
// Update payments-intents/{orderId} status to match webhook outcome
|
|
255
|
+
if (orderId) {
|
|
256
|
+
await admin.firestore().doc(`payments-intents/${orderId}`).set({
|
|
257
|
+
status: 'completed',
|
|
258
|
+
metadata: {
|
|
259
|
+
completed: {
|
|
260
|
+
timestamp: now,
|
|
261
|
+
timestampUNIX: nowUNIX,
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
}, { merge: true });
|
|
265
|
+
assistant.log(`Updated payments-intents/${orderId}: status=completed`);
|
|
266
|
+
}
|
|
267
|
+
|
|
223
268
|
return transitionName;
|
|
224
269
|
}
|
|
225
270
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
|
+
const powertools = require('node-powertools');
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* POST /payments/cancel
|
|
@@ -6,6 +7,7 @@ const path = require('path');
|
|
|
6
7
|
* Delegates to the processor (e.g., Stripe) to set cancel_at_period_end=true.
|
|
7
8
|
* The resulting webhook triggers the Firestore pipeline which updates subscription state
|
|
8
9
|
* and fires the cancellation-requested transition handler.
|
|
10
|
+
* Stores the cancellation reason/feedback on payments-orders/{orderId}.requests.cancellation.
|
|
9
11
|
* Requires authentication.
|
|
10
12
|
*/
|
|
11
13
|
module.exports = async ({ assistant, user, settings }) => {
|
|
@@ -60,7 +62,31 @@ module.exports = async ({ assistant, user, settings }) => {
|
|
|
60
62
|
return assistant.respond(`Failed to cancel subscription: ${e.message}`, { code: 500, sentry: true });
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
// Store cancellation reason/feedback on the order doc
|
|
66
|
+
const orderId = subscription.payment?.orderId;
|
|
67
|
+
|
|
68
|
+
if (orderId) {
|
|
69
|
+
const admin = assistant.Manager.libraries.admin;
|
|
70
|
+
const now = powertools.timestamp(new Date(), { output: 'string' });
|
|
71
|
+
const nowUNIX = powertools.timestamp(now, { output: 'unix' });
|
|
72
|
+
|
|
73
|
+
await admin.firestore().doc(`payments-orders/${orderId}`).set({
|
|
74
|
+
requests: {
|
|
75
|
+
cancellation: {
|
|
76
|
+
reason: settings.reason || null,
|
|
77
|
+
feedback: settings.feedback || null,
|
|
78
|
+
date: {
|
|
79
|
+
timestamp: now,
|
|
80
|
+
timestampUNIX: nowUNIX,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
}, { merge: true });
|
|
85
|
+
|
|
86
|
+
assistant.log(`Stored cancellation request on payments-orders/${orderId}: reason=${settings.reason}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
assistant.log(`Cancel scheduled: uid=${uid}, processor=${processor}, sub=${resourceId}, reason=${settings.reason}`);
|
|
64
90
|
|
|
65
91
|
return assistant.respond({ success: true });
|
|
66
92
|
};
|
|
@@ -1,22 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Stripe cancel processor
|
|
3
|
-
*
|
|
3
|
+
* Cancels a subscription — immediately if trialing, at period end otherwise.
|
|
4
4
|
*/
|
|
5
5
|
module.exports = {
|
|
6
6
|
/**
|
|
7
|
-
* Cancel a Stripe subscription
|
|
7
|
+
* Cancel a Stripe subscription
|
|
8
|
+
*
|
|
9
|
+
* If the subscription is currently trialing, cancel immediately to avoid
|
|
10
|
+
* giving free premium access for the remainder of the trial.
|
|
11
|
+
* Otherwise, cancel at the end of the current billing period.
|
|
8
12
|
*
|
|
9
13
|
* @param {object} options
|
|
10
14
|
* @param {string} options.resourceId - Stripe subscription ID (e.g., 'sub_xxx')
|
|
11
15
|
* @param {string} options.uid - User's UID (for logging)
|
|
16
|
+
* @param {object} options.subscription - User's current subscription object
|
|
12
17
|
* @param {object} options.assistant - Assistant instance for logging
|
|
13
18
|
*/
|
|
14
|
-
async cancelAtPeriodEnd({ resourceId, uid, assistant }) {
|
|
19
|
+
async cancelAtPeriodEnd({ resourceId, uid, subscription, assistant }) {
|
|
15
20
|
const StripeLib = require('../../../../libraries/payment/processors/stripe.js');
|
|
16
21
|
const stripe = StripeLib.init();
|
|
17
22
|
|
|
18
|
-
|
|
23
|
+
const isTrialing = subscription?.trial?.claimed
|
|
24
|
+
&& subscription?.status === 'active'
|
|
25
|
+
&& subscription?.trial?.expires?.timestampUNIX === subscription?.expires?.timestampUNIX;
|
|
19
26
|
|
|
20
|
-
|
|
27
|
+
if (isTrialing) {
|
|
28
|
+
await stripe.subscriptions.cancel(resourceId);
|
|
29
|
+
assistant.log(`Stripe cancel immediate (trialing): sub=${resourceId}, uid=${uid}`);
|
|
30
|
+
} else {
|
|
31
|
+
await stripe.subscriptions.update(resourceId, { cancel_at_period_end: true });
|
|
32
|
+
assistant.log(`Stripe cancel at period end: sub=${resourceId}, uid=${uid}`);
|
|
33
|
+
}
|
|
21
34
|
},
|
|
22
35
|
};
|
|
@@ -2,9 +2,13 @@ const powertools = require('node-powertools');
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Test cancel processor
|
|
5
|
-
* Simulates the Stripe webhook that results from
|
|
5
|
+
* Simulates the Stripe webhook that results from cancellation
|
|
6
6
|
* by writing directly to payments-webhooks/{eventId} with status=pending.
|
|
7
7
|
* The on-write trigger picks it up and runs the full pipeline.
|
|
8
|
+
*
|
|
9
|
+
* If the user is trialing, simulates immediate cancellation (customer.subscription.deleted).
|
|
10
|
+
* Otherwise, simulates cancel at period end (customer.subscription.updated with cancel_at_period_end=true).
|
|
11
|
+
*
|
|
8
12
|
* Only available in non-production environments.
|
|
9
13
|
*/
|
|
10
14
|
module.exports = {
|
|
@@ -16,7 +20,6 @@ module.exports = {
|
|
|
16
20
|
const admin = assistant.Manager.libraries.admin;
|
|
17
21
|
|
|
18
22
|
const timestamp = Date.now();
|
|
19
|
-
const eventId = `_test-evt-cancel-${timestamp}`;
|
|
20
23
|
const now = Math.floor(timestamp / 1000);
|
|
21
24
|
const periodEnd = now + (30 * 86400);
|
|
22
25
|
|
|
@@ -35,21 +38,31 @@ module.exports = {
|
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
//
|
|
39
|
-
|
|
41
|
+
// Detect if user is on a trial
|
|
42
|
+
const isTrialing = subscription?.trial?.claimed
|
|
43
|
+
&& subscription?.status === 'active'
|
|
44
|
+
&& subscription?.trial?.expires?.timestampUNIX === subscription?.expires?.timestampUNIX;
|
|
45
|
+
|
|
46
|
+
// Trialing: immediate cancel (customer.subscription.deleted)
|
|
47
|
+
// Non-trialing: cancel at period end (customer.subscription.updated)
|
|
48
|
+
const eventType = isTrialing
|
|
49
|
+
? 'customer.subscription.deleted'
|
|
50
|
+
: 'customer.subscription.updated';
|
|
51
|
+
const eventId = `_test-evt-cancel-${timestamp}`;
|
|
52
|
+
|
|
40
53
|
const subscriptionObj = {
|
|
41
54
|
id: resourceId,
|
|
42
55
|
object: 'subscription',
|
|
43
|
-
status: 'active',
|
|
56
|
+
status: isTrialing ? 'canceled' : 'active',
|
|
44
57
|
metadata: { uid, orderId },
|
|
45
|
-
cancel_at_period_end:
|
|
46
|
-
cancel_at: periodEnd,
|
|
47
|
-
canceled_at: null,
|
|
48
|
-
current_period_end: periodEnd,
|
|
58
|
+
cancel_at_period_end: !isTrialing,
|
|
59
|
+
cancel_at: isTrialing ? now : periodEnd,
|
|
60
|
+
canceled_at: isTrialing ? now : null,
|
|
61
|
+
current_period_end: isTrialing ? now : periodEnd,
|
|
49
62
|
current_period_start: now - (30 * 86400),
|
|
50
63
|
start_date: now - (30 * 86400),
|
|
51
|
-
trial_start: null,
|
|
52
|
-
trial_end: null,
|
|
64
|
+
trial_start: isTrialing ? (now - 86400) : null,
|
|
65
|
+
trial_end: isTrialing ? now : null,
|
|
53
66
|
plan: { product: stripeProductId, interval: 'month' },
|
|
54
67
|
};
|
|
55
68
|
|
|
@@ -64,11 +77,11 @@ module.exports = {
|
|
|
64
77
|
owner: uid,
|
|
65
78
|
raw: {
|
|
66
79
|
id: eventId,
|
|
67
|
-
type:
|
|
80
|
+
type: eventType,
|
|
68
81
|
data: { object: subscriptionObj },
|
|
69
82
|
},
|
|
70
83
|
event: {
|
|
71
|
-
type:
|
|
84
|
+
type: eventType,
|
|
72
85
|
category: 'subscription',
|
|
73
86
|
resourceType: 'subscription',
|
|
74
87
|
resourceId: resourceId,
|
|
@@ -86,6 +99,6 @@ module.exports = {
|
|
|
86
99
|
},
|
|
87
100
|
});
|
|
88
101
|
|
|
89
|
-
assistant.log(`Test cancel processor: wrote payments-webhooks/${eventId} for sub=${resourceId}, uid=${uid}`);
|
|
102
|
+
assistant.log(`Test cancel processor: wrote payments-webhooks/${eventId} (${eventType}) for sub=${resourceId}, uid=${uid}, trialing=${isTrialing}`);
|
|
90
103
|
},
|
|
91
104
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
|
+
const powertools = require('node-powertools');
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* POST /payments/refund
|
|
@@ -8,6 +9,7 @@ const path = require('path');
|
|
|
8
9
|
* Delegates to the processor (e.g., Stripe) to issue the refund and cancel.
|
|
9
10
|
* The resulting webhook triggers the Firestore pipeline which updates subscription state
|
|
10
11
|
* and fires the subscription-cancelled transition handler.
|
|
12
|
+
* Stores the refund reason/feedback on payments-orders/{orderId}.requests.refund.
|
|
11
13
|
* Requires authentication.
|
|
12
14
|
*/
|
|
13
15
|
module.exports = async ({ assistant, user, settings }) => {
|
|
@@ -79,6 +81,32 @@ module.exports = async ({ assistant, user, settings }) => {
|
|
|
79
81
|
return assistant.respond(`Failed to process refund: ${e.message}`, { code: 500, sentry: true });
|
|
80
82
|
}
|
|
81
83
|
|
|
84
|
+
// Store refund reason/feedback on the order doc
|
|
85
|
+
const orderId = subscription.payment?.orderId;
|
|
86
|
+
|
|
87
|
+
if (orderId) {
|
|
88
|
+
const admin = assistant.Manager.libraries.admin;
|
|
89
|
+
const now = powertools.timestamp(new Date(), { output: 'string' });
|
|
90
|
+
const nowUNIX = powertools.timestamp(now, { output: 'unix' });
|
|
91
|
+
|
|
92
|
+
await admin.firestore().doc(`payments-orders/${orderId}`).set({
|
|
93
|
+
requests: {
|
|
94
|
+
refund: {
|
|
95
|
+
reason: settings.reason || null,
|
|
96
|
+
feedback: settings.feedback || null,
|
|
97
|
+
amount: refund.amount,
|
|
98
|
+
full: refund.full,
|
|
99
|
+
date: {
|
|
100
|
+
timestamp: now,
|
|
101
|
+
timestampUNIX: nowUNIX,
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
}, { merge: true });
|
|
106
|
+
|
|
107
|
+
assistant.log(`Stored refund request on payments-orders/${orderId}: reason=${settings.reason}, amount=${refund.amount}`);
|
|
108
|
+
}
|
|
109
|
+
|
|
82
110
|
assistant.log(`Refund processed: uid=${uid}, processor=${processor}, sub=${resourceId}, amount=${refund.amount}, full=${refund.full}, reason=${settings.reason}`);
|
|
83
111
|
|
|
84
112
|
return assistant.respond({ success: true, refund });
|
|
@@ -166,6 +166,15 @@ const JOURNEY_ACCOUNTS = {
|
|
|
166
166
|
subscription: { product: { id: 'basic' }, status: 'active' }, // Starts as basic, upgraded via trial webhook
|
|
167
167
|
},
|
|
168
168
|
},
|
|
169
|
+
'journey-payments-trial-cancel': {
|
|
170
|
+
id: 'journey-payments-trial-cancel',
|
|
171
|
+
uid: '_test-journey-payments-trial-cancel',
|
|
172
|
+
email: '_test.journey-payments-trial-cancel@{domain}',
|
|
173
|
+
properties: {
|
|
174
|
+
roles: {},
|
|
175
|
+
subscription: { product: { id: 'basic' }, status: 'active' }, // Starts as basic, trial then immediate cancel
|
|
176
|
+
},
|
|
177
|
+
},
|
|
169
178
|
'journey-payments-failure': {
|
|
170
179
|
id: 'journey-payments-failure',
|
|
171
180
|
uid: '_test-journey-payments-failure',
|
|
@@ -75,5 +75,20 @@ module.exports = {
|
|
|
75
75
|
assert.ok(userDoc.subscription.cancellation.date.timestampUNIX > 0, 'Cancellation date should be set');
|
|
76
76
|
},
|
|
77
77
|
},
|
|
78
|
+
|
|
79
|
+
{
|
|
80
|
+
name: 'cancellation-request-stored',
|
|
81
|
+
async run({ firestore, assert, state }) {
|
|
82
|
+
const orderDoc = await firestore.get(`payments-orders/${state.orderId}`);
|
|
83
|
+
|
|
84
|
+
assert.ok(orderDoc, 'Order doc should exist');
|
|
85
|
+
assert.ok(orderDoc.requests, 'requests field should exist');
|
|
86
|
+
assert.ok(orderDoc.requests.cancellation, 'requests.cancellation should be populated');
|
|
87
|
+
assert.equal(orderDoc.requests.cancellation.reason, 'Too expensive', 'Cancellation reason should match');
|
|
88
|
+
assert.equal(orderDoc.requests.cancellation.feedback, 'Would return at a lower price', 'Cancellation feedback should match');
|
|
89
|
+
assert.ok(orderDoc.requests.cancellation.date.timestampUNIX > 0, 'Cancellation date should be set');
|
|
90
|
+
assert.equal(orderDoc.requests.refund, null, 'requests.refund should still be null');
|
|
91
|
+
},
|
|
92
|
+
},
|
|
78
93
|
],
|
|
79
94
|
};
|
|
@@ -148,5 +148,31 @@ module.exports = {
|
|
|
148
148
|
assert.equal(userDoc.subscription.cancellation.pending, false, 'Cancellation should not be pending');
|
|
149
149
|
},
|
|
150
150
|
},
|
|
151
|
+
|
|
152
|
+
{
|
|
153
|
+
name: 'order-doc-verified',
|
|
154
|
+
async run({ firestore, assert, state }) {
|
|
155
|
+
const orderDoc = await firestore.get(`payments-orders/${state.orderId}`);
|
|
156
|
+
|
|
157
|
+
assert.ok(orderDoc, 'Order doc should exist');
|
|
158
|
+
assert.equal(orderDoc.type, 'subscription', 'Type should be subscription');
|
|
159
|
+
assert.equal(orderDoc.owner, state.uid, 'Owner should match');
|
|
160
|
+
assert.ok(orderDoc.requests !== undefined, 'requests field should exist');
|
|
161
|
+
assert.equal(orderDoc.requests.cancellation, null, 'requests.cancellation should be null (cancel was via webhook, not endpoint)');
|
|
162
|
+
assert.equal(orderDoc.requests.refund, null, 'requests.refund should be null');
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
{
|
|
167
|
+
name: 'intent-doc-completed',
|
|
168
|
+
async run({ firestore, assert, state }) {
|
|
169
|
+
const intentDoc = await firestore.get(`payments-intents/${state.orderId}`);
|
|
170
|
+
|
|
171
|
+
assert.ok(intentDoc, 'Intent doc should exist');
|
|
172
|
+
assert.equal(intentDoc.id, state.orderId, 'ID should match orderId');
|
|
173
|
+
assert.equal(intentDoc.status, 'completed', 'Intent status should be completed after webhook processing');
|
|
174
|
+
assert.ok(intentDoc.metadata?.completed?.timestampUNIX > 0, 'Completed timestamp should be set');
|
|
175
|
+
},
|
|
176
|
+
},
|
|
151
177
|
],
|
|
152
178
|
};
|
|
@@ -112,5 +112,31 @@ module.exports = {
|
|
|
112
112
|
assert.equal(userDoc.subscription.product.id, state.paidProductId, `Product should still be ${state.paidProductId}`);
|
|
113
113
|
},
|
|
114
114
|
},
|
|
115
|
+
|
|
116
|
+
{
|
|
117
|
+
name: 'order-doc-verified',
|
|
118
|
+
async run({ firestore, assert, state }) {
|
|
119
|
+
const orderDoc = await firestore.get(`payments-orders/${state.orderId}`);
|
|
120
|
+
|
|
121
|
+
assert.ok(orderDoc, 'Order doc should exist');
|
|
122
|
+
assert.equal(orderDoc.type, 'subscription', 'Type should be subscription');
|
|
123
|
+
assert.equal(orderDoc.owner, state.uid, 'Owner should match');
|
|
124
|
+
assert.ok(orderDoc.requests !== undefined, 'requests field should exist');
|
|
125
|
+
assert.equal(orderDoc.requests.cancellation, null, 'requests.cancellation should be null');
|
|
126
|
+
assert.equal(orderDoc.requests.refund, null, 'requests.refund should be null');
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
{
|
|
131
|
+
name: 'intent-doc-completed',
|
|
132
|
+
async run({ firestore, assert, state }) {
|
|
133
|
+
const intentDoc = await firestore.get(`payments-intents/${state.orderId}`);
|
|
134
|
+
|
|
135
|
+
assert.ok(intentDoc, 'Intent doc should exist');
|
|
136
|
+
assert.equal(intentDoc.id, state.orderId, 'ID should match orderId');
|
|
137
|
+
assert.equal(intentDoc.status, 'completed', 'Intent status should be completed after initial webhook processing');
|
|
138
|
+
assert.ok(intentDoc.metadata?.completed?.timestampUNIX > 0, 'Completed timestamp should be set');
|
|
139
|
+
},
|
|
140
|
+
},
|
|
115
141
|
],
|
|
116
142
|
};
|
|
@@ -85,6 +85,9 @@ module.exports = {
|
|
|
85
85
|
assert.equal(orderDoc.type, 'one-time', 'Type should be one-time');
|
|
86
86
|
assert.equal(orderDoc.owner, state.uid, 'Owner should match');
|
|
87
87
|
assert.equal(orderDoc.processor, 'test', 'Processor should be test');
|
|
88
|
+
assert.ok(orderDoc.requests !== undefined, 'requests field should exist');
|
|
89
|
+
assert.equal(orderDoc.requests.cancellation, null, 'requests.cancellation should be null');
|
|
90
|
+
assert.equal(orderDoc.requests.refund, null, 'requests.refund should be null');
|
|
88
91
|
},
|
|
89
92
|
},
|
|
90
93
|
|
|
@@ -88,6 +88,9 @@ module.exports = {
|
|
|
88
88
|
assert.equal(orderDoc.unified.product.id, state.productId, `Product should be ${state.productId}`);
|
|
89
89
|
assert.equal(orderDoc.unified.payment.processor, 'test', 'Unified processor should be test');
|
|
90
90
|
assert.equal(orderDoc.unified.payment.orderId, state.orderId, 'Unified orderId should match');
|
|
91
|
+
assert.ok(orderDoc.requests !== undefined, 'requests field should exist');
|
|
92
|
+
assert.equal(orderDoc.requests.cancellation, null, 'requests.cancellation should be null');
|
|
93
|
+
assert.equal(orderDoc.requests.refund, null, 'requests.refund should be null');
|
|
91
94
|
},
|
|
92
95
|
},
|
|
93
96
|
|
|
@@ -112,7 +115,7 @@ module.exports = {
|
|
|
112
115
|
},
|
|
113
116
|
|
|
114
117
|
{
|
|
115
|
-
name: 'intent-doc-
|
|
118
|
+
name: 'intent-doc-completed',
|
|
116
119
|
async run({ firestore, assert, state }) {
|
|
117
120
|
const intentDoc = await firestore.get(`payments-intents/${state.orderId}`);
|
|
118
121
|
|
|
@@ -121,7 +124,9 @@ module.exports = {
|
|
|
121
124
|
assert.equal(intentDoc.intentId, state.intentId, 'Intent ID should match processor session ID');
|
|
122
125
|
assert.equal(intentDoc.owner, state.uid, 'Owner should match');
|
|
123
126
|
assert.equal(intentDoc.processor, 'test', 'Processor should be test');
|
|
127
|
+
assert.equal(intentDoc.status, 'completed', 'Intent status should be completed after webhook processing');
|
|
124
128
|
assert.equal(intentDoc.productId, state.productId, `Product should be ${state.productId}`);
|
|
129
|
+
assert.ok(intentDoc.metadata?.completed?.timestampUNIX > 0, 'Completed timestamp should be set');
|
|
125
130
|
},
|
|
126
131
|
},
|
|
127
132
|
],
|
|
@@ -120,6 +120,21 @@ module.exports = {
|
|
|
120
120
|
assert.ok(orderDoc, 'Order doc should exist');
|
|
121
121
|
assert.equal(orderDoc.unified.product.id, state.productB.id, `Order product should be ${state.productB.id}`);
|
|
122
122
|
assert.equal(orderDoc.unified.status, 'active', 'Order status should be active');
|
|
123
|
+
assert.ok(orderDoc.requests !== undefined, 'requests field should exist');
|
|
124
|
+
assert.equal(orderDoc.requests.cancellation, null, 'requests.cancellation should be null');
|
|
125
|
+
assert.equal(orderDoc.requests.refund, null, 'requests.refund should be null');
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
{
|
|
130
|
+
name: 'intent-doc-completed',
|
|
131
|
+
async run({ firestore, assert, state }) {
|
|
132
|
+
const intentDoc = await firestore.get(`payments-intents/${state.orderId}`);
|
|
133
|
+
|
|
134
|
+
assert.ok(intentDoc, 'Intent doc should exist');
|
|
135
|
+
assert.equal(intentDoc.id, state.orderId, 'ID should match orderId');
|
|
136
|
+
assert.equal(intentDoc.status, 'completed', 'Intent status should be completed after webhook processing');
|
|
137
|
+
assert.ok(intentDoc.metadata?.completed?.timestampUNIX > 0, 'Completed timestamp should be set');
|
|
123
138
|
},
|
|
124
139
|
},
|
|
125
140
|
],
|
|
@@ -147,5 +147,31 @@ module.exports = {
|
|
|
147
147
|
assert.equal(userDoc.subscription.product.id, state.paidProductId, `Product should still be ${state.paidProductId}`);
|
|
148
148
|
},
|
|
149
149
|
},
|
|
150
|
+
|
|
151
|
+
{
|
|
152
|
+
name: 'order-doc-verified',
|
|
153
|
+
async run({ firestore, assert, state }) {
|
|
154
|
+
const orderDoc = await firestore.get(`payments-orders/${state.orderId}`);
|
|
155
|
+
|
|
156
|
+
assert.ok(orderDoc, 'Order doc should exist');
|
|
157
|
+
assert.equal(orderDoc.type, 'subscription', 'Type should be subscription');
|
|
158
|
+
assert.equal(orderDoc.owner, state.uid, 'Owner should match');
|
|
159
|
+
assert.ok(orderDoc.requests !== undefined, 'requests field should exist');
|
|
160
|
+
assert.equal(orderDoc.requests.cancellation, null, 'requests.cancellation should be null');
|
|
161
|
+
assert.equal(orderDoc.requests.refund, null, 'requests.refund should be null');
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
{
|
|
166
|
+
name: 'intent-doc-completed',
|
|
167
|
+
async run({ firestore, assert, state }) {
|
|
168
|
+
const intentDoc = await firestore.get(`payments-intents/${state.orderId}`);
|
|
169
|
+
|
|
170
|
+
assert.ok(intentDoc, 'Intent doc should exist');
|
|
171
|
+
assert.equal(intentDoc.id, state.orderId, 'ID should match orderId');
|
|
172
|
+
assert.equal(intentDoc.status, 'completed', 'Intent status should be completed after webhook processing');
|
|
173
|
+
assert.ok(intentDoc.metadata?.completed?.timestampUNIX > 0, 'Completed timestamp should be set');
|
|
174
|
+
},
|
|
175
|
+
},
|
|
150
176
|
],
|
|
151
177
|
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test: Payment Journey - Trial Cancel
|
|
3
|
+
* Simulates: basic user → trial activation → cancel during trial → immediate cancellation
|
|
4
|
+
*
|
|
5
|
+
* When a user cancels during a free trial, the subscription should be cancelled immediately
|
|
6
|
+
* (not scheduled for period end) to avoid giving free premium access for the remainder of the trial.
|
|
7
|
+
*
|
|
8
|
+
* Uses the test processor for initial trial, then cancel endpoint for cancellation.
|
|
9
|
+
* Product-agnostic: resolves the first paid product from config.payment.products
|
|
10
|
+
*/
|
|
11
|
+
module.exports = {
|
|
12
|
+
description: 'Payment journey: trial → cancel during trial → immediate cancellation',
|
|
13
|
+
type: 'suite',
|
|
14
|
+
timeout: 30000,
|
|
15
|
+
|
|
16
|
+
tests: [
|
|
17
|
+
{
|
|
18
|
+
name: 'verify-starts-as-basic',
|
|
19
|
+
async run({ accounts, firestore, assert, state, config }) {
|
|
20
|
+
const uid = accounts['journey-payments-trial-cancel'].uid;
|
|
21
|
+
const userDoc = await firestore.get(`users/${uid}`);
|
|
22
|
+
|
|
23
|
+
assert.ok(userDoc, 'User doc should exist');
|
|
24
|
+
assert.equal(userDoc.subscription?.product?.id, 'basic', 'Should start as basic');
|
|
25
|
+
assert.equal(userDoc.subscription?.trial?.claimed, false, 'Trial should not be claimed');
|
|
26
|
+
|
|
27
|
+
// Resolve first paid product with trial from config
|
|
28
|
+
const trialProduct = config.payment.products.find(p => p.id !== 'basic' && p.prices && p.trial?.days);
|
|
29
|
+
assert.ok(trialProduct, 'Config should have at least one paid product with trial');
|
|
30
|
+
|
|
31
|
+
state.uid = uid;
|
|
32
|
+
state.paidProductId = trialProduct.id;
|
|
33
|
+
state.paidProductName = trialProduct.name;
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
{
|
|
38
|
+
name: 'create-trial-intent',
|
|
39
|
+
async run({ http, assert, state }) {
|
|
40
|
+
const response = await http.as('journey-payments-trial-cancel').post('payments/intent', {
|
|
41
|
+
processor: 'test',
|
|
42
|
+
productId: state.paidProductId,
|
|
43
|
+
frequency: 'monthly',
|
|
44
|
+
trial: true,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
assert.isSuccess(response, 'Intent should succeed');
|
|
48
|
+
assert.ok(response.data.id, 'Should return intent ID');
|
|
49
|
+
assert.ok(response.data.orderId, 'Should return orderId');
|
|
50
|
+
|
|
51
|
+
state.intentId = response.data.id;
|
|
52
|
+
state.orderId = response.data.orderId;
|
|
53
|
+
state.eventId = response.data.id.replace('_test-cs-', '_test-evt-');
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
{
|
|
58
|
+
name: 'trial-activated',
|
|
59
|
+
async run({ firestore, assert, state, waitFor }) {
|
|
60
|
+
await waitFor(async () => {
|
|
61
|
+
const userDoc = await firestore.get(`users/${state.uid}`);
|
|
62
|
+
return userDoc?.subscription?.trial?.claimed === true;
|
|
63
|
+
}, 15000, 500);
|
|
64
|
+
|
|
65
|
+
const userDoc = await firestore.get(`users/${state.uid}`);
|
|
66
|
+
|
|
67
|
+
assert.equal(userDoc.subscription.product.id, state.paidProductId, `Product should be ${state.paidProductId}`);
|
|
68
|
+
assert.equal(userDoc.subscription.status, 'active', 'Status should be active');
|
|
69
|
+
assert.equal(userDoc.subscription.trial.claimed, true, 'Trial should be claimed');
|
|
70
|
+
assert.equal(userDoc.subscription.trial.expires.timestampUNIX, userDoc.subscription.expires.timestampUNIX, 'Trial expires should match subscription expires (still in trial period)');
|
|
71
|
+
|
|
72
|
+
state.subscriptionId = userDoc.subscription.payment.resourceId;
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
{
|
|
77
|
+
name: 'cancel-during-trial',
|
|
78
|
+
async run({ http, assert }) {
|
|
79
|
+
// Cancel via endpoint — test processor should detect trial and simulate immediate cancel
|
|
80
|
+
const response = await http.as('journey-payments-trial-cancel').post('payments/cancel', {
|
|
81
|
+
confirmed: true,
|
|
82
|
+
reason: 'Changed my mind during trial',
|
|
83
|
+
feedback: 'Testing trial cancellation',
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
assert.isSuccess(response, 'Cancel endpoint should succeed');
|
|
87
|
+
assert.equal(response.data.success, true, 'Should return { success: true }');
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
{
|
|
92
|
+
name: 'verify-immediate-cancellation',
|
|
93
|
+
async run({ firestore, assert, state, waitFor }) {
|
|
94
|
+
// Poll until subscription is cancelled (NOT just pending)
|
|
95
|
+
await waitFor(async () => {
|
|
96
|
+
const userDoc = await firestore.get(`users/${state.uid}`);
|
|
97
|
+
return userDoc?.subscription?.status === 'cancelled';
|
|
98
|
+
}, 15000, 500);
|
|
99
|
+
|
|
100
|
+
const userDoc = await firestore.get(`users/${state.uid}`);
|
|
101
|
+
|
|
102
|
+
// During trial cancel: subscription should be immediately cancelled, not pending
|
|
103
|
+
assert.equal(userDoc.subscription.status, 'cancelled', 'Status should be cancelled (not active with pending)');
|
|
104
|
+
assert.equal(userDoc.subscription.cancellation.pending, false, 'Should NOT be pending — should be fully cancelled');
|
|
105
|
+
assert.equal(userDoc.subscription.product.id, state.paidProductId, `Product should still be ${state.paidProductId}`);
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
{
|
|
110
|
+
name: 'cancellation-request-stored',
|
|
111
|
+
async run({ firestore, assert, state }) {
|
|
112
|
+
const orderDoc = await firestore.get(`payments-orders/${state.orderId}`);
|
|
113
|
+
|
|
114
|
+
assert.ok(orderDoc, 'Order doc should exist');
|
|
115
|
+
assert.ok(orderDoc.requests, 'requests field should exist');
|
|
116
|
+
assert.ok(orderDoc.requests.cancellation, 'requests.cancellation should be populated');
|
|
117
|
+
assert.equal(orderDoc.requests.cancellation.reason, 'Changed my mind during trial', 'Cancellation reason should match');
|
|
118
|
+
assert.equal(orderDoc.requests.cancellation.feedback, 'Testing trial cancellation', 'Cancellation feedback should match');
|
|
119
|
+
assert.ok(orderDoc.requests.cancellation.date.timestampUNIX > 0, 'Cancellation date should be set');
|
|
120
|
+
assert.equal(orderDoc.requests.refund, null, 'requests.refund should still be null');
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
};
|
|
@@ -79,6 +79,35 @@ module.exports = {
|
|
|
79
79
|
},
|
|
80
80
|
},
|
|
81
81
|
|
|
82
|
+
{
|
|
83
|
+
name: 'intent-doc-completed',
|
|
84
|
+
async run({ firestore, assert, state }) {
|
|
85
|
+
const intentDoc = await firestore.get(`payments-intents/${state.orderId}`);
|
|
86
|
+
|
|
87
|
+
assert.ok(intentDoc, 'Intent doc should exist');
|
|
88
|
+
assert.equal(intentDoc.id, state.orderId, 'ID should match orderId');
|
|
89
|
+
assert.equal(intentDoc.status, 'completed', 'Intent status should be completed after webhook processing');
|
|
90
|
+
assert.ok(intentDoc.metadata?.completed?.timestampUNIX > 0, 'Completed timestamp should be set');
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
{
|
|
95
|
+
name: 'order-doc-created',
|
|
96
|
+
async run({ firestore, assert, state }) {
|
|
97
|
+
const orderDoc = await firestore.get(`payments-orders/${state.orderId}`);
|
|
98
|
+
|
|
99
|
+
assert.ok(orderDoc, 'Order doc should exist');
|
|
100
|
+
assert.equal(orderDoc.id, state.orderId, 'ID should match orderId');
|
|
101
|
+
assert.equal(orderDoc.type, 'subscription', 'Type should be subscription');
|
|
102
|
+
assert.equal(orderDoc.owner, state.uid, 'Owner should match');
|
|
103
|
+
assert.equal(orderDoc.unified.product.id, state.paidProductId, `Product should be ${state.paidProductId}`);
|
|
104
|
+
assert.equal(orderDoc.unified.trial.claimed, true, 'Trial should be claimed');
|
|
105
|
+
assert.ok(orderDoc.requests !== undefined, 'requests field should exist');
|
|
106
|
+
assert.equal(orderDoc.requests.cancellation, null, 'requests.cancellation should be null');
|
|
107
|
+
assert.equal(orderDoc.requests.refund, null, 'requests.refund should be null');
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
|
|
82
111
|
{
|
|
83
112
|
name: 'send-trial-to-active-webhook',
|
|
84
113
|
async run({ http, assert, state, config }) {
|
|
@@ -90,6 +90,9 @@ module.exports = {
|
|
|
90
90
|
assert.equal(orderDoc.resourceId, state.subscriptionId, 'Resource ID should match');
|
|
91
91
|
assert.equal(orderDoc.unified.product.id, state.paidProductId, `Product should be ${state.paidProductId}`);
|
|
92
92
|
assert.equal(orderDoc.unified.status, 'active', 'Status should be active');
|
|
93
|
+
assert.ok(orderDoc.requests !== undefined, 'requests field should exist');
|
|
94
|
+
assert.equal(orderDoc.requests.cancellation, null, 'requests.cancellation should be null initially');
|
|
95
|
+
assert.equal(orderDoc.requests.refund, null, 'requests.refund should be null initially');
|
|
93
96
|
},
|
|
94
97
|
},
|
|
95
98
|
|
|
@@ -109,7 +112,7 @@ module.exports = {
|
|
|
109
112
|
},
|
|
110
113
|
|
|
111
114
|
{
|
|
112
|
-
name: 'intent-doc-
|
|
115
|
+
name: 'intent-doc-completed',
|
|
113
116
|
async run({ firestore, assert, state }) {
|
|
114
117
|
const intentDoc = await firestore.get(`payments-intents/${state.orderId}`);
|
|
115
118
|
|
|
@@ -118,8 +121,9 @@ module.exports = {
|
|
|
118
121
|
assert.equal(intentDoc.intentId, state.intentId, 'Intent ID should match processor session ID');
|
|
119
122
|
assert.equal(intentDoc.owner, state.uid, 'Owner should match');
|
|
120
123
|
assert.equal(intentDoc.processor, 'test', 'Processor should be test');
|
|
121
|
-
assert.equal(intentDoc.status, '
|
|
124
|
+
assert.equal(intentDoc.status, 'completed', 'Intent status should be completed after webhook processing');
|
|
122
125
|
assert.equal(intentDoc.productId, state.paidProductId, `Product should be ${state.paidProductId}`);
|
|
126
|
+
assert.ok(intentDoc.metadata?.completed?.timestampUNIX > 0, 'Completed timestamp should be set');
|
|
123
127
|
},
|
|
124
128
|
},
|
|
125
129
|
],
|