@haatlabs-core/templates 0.11.2 → 0.11.3
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 +3 -3
- package/starter/apps/api/src/billing/shopify-billing.int.test.ts +11 -1
- package/starter/apps/api/src/security-sweep.int.test.ts +36 -13
- package/starter/apps/api/src/shopify/store-only.int.test.ts +23 -8
- package/starter/apps/worker/src/billing/store-billing-sync.int.test.ts +15 -0
- package/starter/apps/worker/src/billing/store-billing-sync.ts +6 -1
- package/starter/dot-env.example +4 -3
- package/starter/packages/web-ui/src/App.tsx +2 -1
- package/starter/packages/web-ui/src/pages/UsagePage.test.tsx +15 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@haatlabs-core/templates",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.3",
|
|
4
4
|
"description": "The starter files and shared project files that @haatlabs-core/cli writes.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"haatlabs"
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@haatlabs-core/tsconfig": "0.11.
|
|
39
|
-
"@haatlabs-core/vitest-config": "0.11.
|
|
38
|
+
"@haatlabs-core/tsconfig": "0.11.3",
|
|
39
|
+
"@haatlabs-core/vitest-config": "0.11.3"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
@@ -517,7 +517,7 @@ describe('Shopify billing API', () => {
|
|
|
517
517
|
});
|
|
518
518
|
|
|
519
519
|
describe('Shopify billing with test charges off', () => {
|
|
520
|
-
it('does not count a test charge as paid', async () => {
|
|
520
|
+
it('does not count a test charge as paid, but in a development store', async () => {
|
|
521
521
|
const h = await startHarness({ env: { SHOPIFY_BILLING_TEST: 'false' } });
|
|
522
522
|
try {
|
|
523
523
|
const shop = 'billing-live.myshopify.com';
|
|
@@ -527,11 +527,17 @@ describe('Shopify billing with test charges off', () => {
|
|
|
527
527
|
amountMinor: 4900,
|
|
528
528
|
});
|
|
529
529
|
let test = true;
|
|
530
|
+
let developmentStore = false;
|
|
530
531
|
h.fetch.mockImplementation(async (input: string | URL, init?: RequestInit) => {
|
|
531
532
|
if (new URL(String(input)).pathname.endsWith('/oauth/access_token')) {
|
|
532
533
|
return Response.json({ access_token: 'shpat_live', scope: 'read_products' });
|
|
533
534
|
}
|
|
534
535
|
const body = JSON.parse(String(init?.body)) as { query: string };
|
|
536
|
+
if (body.query.includes('partnerDevelopment')) {
|
|
537
|
+
return Response.json({
|
|
538
|
+
data: { shop: { plan: { partnerDevelopment: developmentStore } } },
|
|
539
|
+
});
|
|
540
|
+
}
|
|
535
541
|
if (!body.query.includes('activeSubscriptions')) throw new Error('unexpected');
|
|
536
542
|
return Response.json({
|
|
537
543
|
data: {
|
|
@@ -558,6 +564,10 @@ describe('Shopify billing with test charges off', () => {
|
|
|
558
564
|
headers: { authorization: `Bearer ${token}` },
|
|
559
565
|
});
|
|
560
566
|
expect((await confirm()).json()).toEqual({ plan: null });
|
|
567
|
+
// A development store pays with test charges: Shopify says which kind of store it is.
|
|
568
|
+
developmentStore = true;
|
|
569
|
+
expect((await confirm()).json()).toEqual({ plan: { code: 'pro', name: 'Pro' } });
|
|
570
|
+
developmentStore = false;
|
|
561
571
|
test = false;
|
|
562
572
|
expect((await confirm()).json()).toEqual({ plan: { code: 'pro', name: 'Pro' } });
|
|
563
573
|
} finally {
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { RequestMethod } from '@nestjs/common';
|
|
3
3
|
import { ModulesContainer } from '@nestjs/core';
|
|
4
4
|
import { SystemStoreRepository } from '@@@name@@/database';
|
|
5
|
+
import { PLAN_CHANGES_MESSAGE } from '@haatlabs-core/nest-billing';
|
|
5
6
|
import { SignJWT } from 'jose';
|
|
6
7
|
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
7
8
|
import { registerUser, startHarness } from './testing/harness';
|
|
@@ -56,9 +57,10 @@ const ROUTES: Record<
|
|
|
56
57
|
embeddedOnly?: true;
|
|
57
58
|
/**
|
|
58
59
|
* The organization's own (its owner's sign-in, say): refused to the Shopify staff of a
|
|
59
|
-
* store a dashboard account connected, who reach only their store.
|
|
60
|
+
* store a dashboard account connected, who reach only their store. `'plan'`: buying,
|
|
61
|
+
* changing or cancelling its plan, refused in nest-billing's words.
|
|
60
62
|
*/
|
|
61
|
-
wholeOrganization?: true;
|
|
63
|
+
wholeOrganization?: true | 'plan';
|
|
62
64
|
}
|
|
63
65
|
> = {
|
|
64
66
|
'GET /health/live': { access: 'public' },
|
|
@@ -102,17 +104,34 @@ const ROUTES: Record<
|
|
|
102
104
|
'GET /api/v1/auth/extension-keys': { access: 'authenticated', standaloneOnly: true },
|
|
103
105
|
'POST /api/v1/auth/extension-keys': { access: 'authenticated', standaloneOnly: true },
|
|
104
106
|
'POST /api/v1/settings/api-keys': { access: 'authenticated', standaloneOnly: true },
|
|
105
|
-
'POST /api/v1/billing/checkout': {
|
|
106
|
-
|
|
107
|
+
'POST /api/v1/billing/checkout': {
|
|
108
|
+
access: 'authenticated',
|
|
109
|
+
standaloneOnly: true,
|
|
110
|
+
wholeOrganization: 'plan',
|
|
111
|
+
},
|
|
112
|
+
'POST /api/v1/billing/cancel': {
|
|
113
|
+
access: 'authenticated',
|
|
114
|
+
standaloneOnly: true,
|
|
115
|
+
wholeOrganization: 'plan',
|
|
116
|
+
},
|
|
107
117
|
// Shopify's billing, for the store the embedded app was opened from; no request names a
|
|
108
|
-
// store (shopify-billing.int.test.ts).
|
|
118
|
+
// store (shopify-billing.int.test.ts). The plan is the whole organization's.
|
|
109
119
|
'POST /api/v1/billing/shopify/checkout': {
|
|
110
120
|
access: 'authenticated',
|
|
111
121
|
embeddedOnly: true,
|
|
122
|
+
wholeOrganization: 'plan',
|
|
112
123
|
body: ['planCode'],
|
|
113
124
|
},
|
|
114
|
-
'POST /api/v1/billing/shopify/confirm': {
|
|
115
|
-
|
|
125
|
+
'POST /api/v1/billing/shopify/confirm': {
|
|
126
|
+
access: 'authenticated',
|
|
127
|
+
embeddedOnly: true,
|
|
128
|
+
wholeOrganization: 'plan',
|
|
129
|
+
},
|
|
130
|
+
'POST /api/v1/billing/shopify/cancel': {
|
|
131
|
+
access: 'authenticated',
|
|
132
|
+
embeddedOnly: true,
|
|
133
|
+
wholeOrganization: 'plan',
|
|
134
|
+
},
|
|
116
135
|
|
|
117
136
|
'DELETE /api/v1/settings/api-keys/:apiKeyId': { access: 'tenant', standaloneOnly: true },
|
|
118
137
|
'DELETE /api/v1/auth/extension-keys/:extensionKeyId': { access: 'tenant', standaloneOnly: true },
|
|
@@ -195,7 +214,7 @@ describe('security sweep', () => {
|
|
|
195
214
|
.map(([route]) => route);
|
|
196
215
|
const standaloneOnly = (route: string): boolean => ROUTES[route]?.standaloneOnly === true;
|
|
197
216
|
const embeddedOnly = (route: string): boolean => ROUTES[route]?.embeddedOnly === true;
|
|
198
|
-
const wholeOrganization = (route: string): boolean => ROUTES[route]?.wholeOrganization
|
|
217
|
+
const wholeOrganization = (route: string): boolean => !!ROUTES[route]?.wholeOrganization;
|
|
199
218
|
const sessionToken = (shop: string, secret: string): Promise<string> => {
|
|
200
219
|
const apiKey = h.config.shopify?.apiKey ?? '';
|
|
201
220
|
return new SignJWT({
|
|
@@ -367,14 +386,17 @@ describe('security sweep', () => {
|
|
|
367
386
|
|
|
368
387
|
it("refuses the organization's own routes to the staff of a store a dashboard account connected, and only those", async () => {
|
|
369
388
|
const routes = [...routesWith('authenticated'), ...routesWith('tenant')];
|
|
370
|
-
expect(routes.filter(wholeOrganization).length).toBe(
|
|
389
|
+
expect(routes.filter(wholeOrganization).length).toBe(8);
|
|
371
390
|
for (const route of routes.filter(wholeOrganization)) {
|
|
372
391
|
const res = await send(route, aliceEmbedded);
|
|
373
392
|
expect(res.statusCode, route).toBe(403);
|
|
374
393
|
expect(res.json<{ error: { code: string } }>().error.code, route).toBe('FORBIDDEN');
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
394
|
+
const message = res.json<{ error: { message: string } }>().error.message;
|
|
395
|
+
if (ROUTES[route]?.wholeOrganization === 'plan') {
|
|
396
|
+
expect(message, route).toBe(PLAN_CHANGES_MESSAGE);
|
|
397
|
+
} else {
|
|
398
|
+
expect(message, route).toMatch(WHOLE_ORGANIZATION_MESSAGE);
|
|
399
|
+
}
|
|
378
400
|
expect(res.body, route).not.toContain('alice@example.com');
|
|
379
401
|
}
|
|
380
402
|
// What it may read answers it as anyone of the organization's; the organization's own
|
|
@@ -390,7 +412,8 @@ describe('security sweep', () => {
|
|
|
390
412
|
expect(res.statusCode, route).not.toBe(403);
|
|
391
413
|
}
|
|
392
414
|
}
|
|
393
|
-
// Nothing is asked of Shopify on the way: the email sign-in
|
|
415
|
+
// Nothing is asked of Shopify on the way: the email sign-in and Shopify's billing are
|
|
416
|
+
// refused before they would be.
|
|
394
417
|
expect(h.fetch).not.toHaveBeenCalled();
|
|
395
418
|
});
|
|
396
419
|
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
stores,
|
|
6
6
|
} from '@@@name@@/database';
|
|
7
7
|
import { MeResponseSchema } from '@@@name@@/contracts';
|
|
8
|
+
import { PLAN_CHANGES_MESSAGE } from '@haatlabs-core/nest-billing';
|
|
8
9
|
import { TokenCipher, sealTokens } from '@@@name@@/shopify';
|
|
9
10
|
import { eq } from 'drizzle-orm';
|
|
10
11
|
import { SignJWT } from 'jose';
|
|
@@ -179,7 +180,7 @@ describe('Shopify staff of a store, by where its organization came from', () =>
|
|
|
179
180
|
expect(h.fetch).not.toHaveBeenCalled();
|
|
180
181
|
});
|
|
181
182
|
|
|
182
|
-
it('keeps its own store and its plan', async () => {
|
|
183
|
+
it('keeps its own store, and sees its plan', async () => {
|
|
183
184
|
const seen: string[] = [];
|
|
184
185
|
const ok = async (route: { method: Method; url: string; payload?: object }) => {
|
|
185
186
|
const res = await send(DASHBOARD_SHOP, route);
|
|
@@ -191,19 +192,33 @@ describe('Shopify staff of a store, by where its organization came from', () =>
|
|
|
191
192
|
expect(listed.json().stores.map((s: { id: string }) => s.id)).toEqual([dashboardStoreId]);
|
|
192
193
|
await ok({ method: 'GET', url: '/api/v1/usage' });
|
|
193
194
|
await ok({ method: 'GET', url: '/api/v1/plans' });
|
|
195
|
+
for (const body of seen) expect(body).not.toContain(OWNER_EMAIL);
|
|
196
|
+
});
|
|
194
197
|
|
|
195
|
-
|
|
198
|
+
it("leaves the plan to the account's owner: no checkout, confirmation or cancel", async () => {
|
|
196
199
|
await new SystemBillingRepository(h.system.db).setPrice('starter', 'shopify', {
|
|
197
200
|
priceRef: 'shopify_starter_monthly',
|
|
198
201
|
currency: 'USD',
|
|
199
202
|
amountMinor: 1900,
|
|
200
203
|
});
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
204
|
+
for (const route of [
|
|
205
|
+
{
|
|
206
|
+
method: 'POST',
|
|
207
|
+
url: '/api/v1/billing/shopify/checkout',
|
|
208
|
+
payload: { planCode: 'starter' },
|
|
209
|
+
},
|
|
210
|
+
{ method: 'POST', url: '/api/v1/billing/shopify/confirm' },
|
|
211
|
+
{ method: 'POST', url: '/api/v1/billing/shopify/cancel' },
|
|
212
|
+
] as const) {
|
|
213
|
+
const res = await send(DASHBOARD_SHOP, route);
|
|
214
|
+
expect(res.statusCode, route.url).toBe(403);
|
|
215
|
+
expect(res.json().error, route.url).toMatchObject({
|
|
216
|
+
code: 'FORBIDDEN',
|
|
217
|
+
message: PLAN_CHANGES_MESSAGE,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
// Nothing was asked of Shopify on the organization's behalf.
|
|
221
|
+
expect(h.fetch).not.toHaveBeenCalled();
|
|
207
222
|
});
|
|
208
223
|
});
|
|
209
224
|
});
|
|
@@ -86,12 +86,16 @@ describe('StoreBillingSync', () => {
|
|
|
86
86
|
fail?: Error;
|
|
87
87
|
subscriptions?: () => unknown[];
|
|
88
88
|
acceptTestCharges?: boolean;
|
|
89
|
+
/** What Shopify says of the shop's plan: a partner development store or not. */
|
|
90
|
+
developmentStore?: boolean;
|
|
89
91
|
tokens?: StoreAccessTokens;
|
|
90
92
|
} = {},
|
|
91
93
|
) {
|
|
92
94
|
const cancelled: unknown[] = [];
|
|
93
95
|
const request = vi.fn(async (query: string, variables?: Record<string, unknown>) => {
|
|
94
96
|
if (options.fail) throw options.fail;
|
|
97
|
+
if (query.includes('partnerDevelopment'))
|
|
98
|
+
return { shop: { plan: { partnerDevelopment: options.developmentStore ?? false } } };
|
|
95
99
|
if (query.includes('activeSubscriptions'))
|
|
96
100
|
return {
|
|
97
101
|
currentAppInstallation: { activeSubscriptions: options.subscriptions?.() ?? [] },
|
|
@@ -300,6 +304,17 @@ describe('StoreBillingSync', () => {
|
|
|
300
304
|
expect(await planOf(ctx)).toBe('free');
|
|
301
305
|
});
|
|
302
306
|
|
|
307
|
+
it('counts a development store’s test charge as paid, with test charges off', async () => {
|
|
308
|
+
const { storeId, ctx } = await install('billing-sync-dev.myshopify.com');
|
|
309
|
+
const { service } = shopify({
|
|
310
|
+
acceptTestCharges: false,
|
|
311
|
+
developmentStore: true,
|
|
312
|
+
subscriptions: () => [subscription(62, '@@Name@@ Pro')],
|
|
313
|
+
});
|
|
314
|
+
expect(await service.syncBilling(storeId)).toMatchObject({ state: 'active', planCode: 'pro' });
|
|
315
|
+
expect(await planOf(ctx)).toBe('pro');
|
|
316
|
+
});
|
|
317
|
+
|
|
303
318
|
describe('an expiring token', () => {
|
|
304
319
|
const expire = async (storeId: string, refreshable = true): Promise<void> => {
|
|
305
320
|
await system.db
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
ShopifyReconnectRequiredError,
|
|
6
6
|
ShopifyTokenRejectedError,
|
|
7
7
|
cancelAppSubscription,
|
|
8
|
+
isDevelopmentStore,
|
|
8
9
|
listActiveAppSubscriptions,
|
|
9
10
|
} from '@@@name@@/shopify';
|
|
10
11
|
import type { GraphQLRequester, StoreAccessTokens } from '@@@name@@/shopify';
|
|
@@ -18,7 +19,10 @@ export interface StoreBillingSyncDeps {
|
|
|
18
19
|
SystemBillingRepository,
|
|
19
20
|
'subscriptionOnRecord' | 'pricesOf' | 'applySubscriptionEvent'
|
|
20
21
|
>;
|
|
21
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Whether a Shopify test charge counts as paid: `SHOPIFY_BILLING_TEST`. A development
|
|
24
|
+
* store's test charges count whatever it says (Shopify is asked).
|
|
25
|
+
*/
|
|
22
26
|
acceptTestCharges: boolean;
|
|
23
27
|
clientFactory: (shopDomain: string, accessToken: string) => GraphQLRequester;
|
|
24
28
|
logger: Logger;
|
|
@@ -36,6 +40,7 @@ export class StoreBillingSync extends SharedStoreBillingSync<GraphQLRequester> {
|
|
|
36
40
|
shopify: {
|
|
37
41
|
listActiveAppSubscriptions,
|
|
38
42
|
cancelAppSubscription,
|
|
43
|
+
isDevelopmentStore,
|
|
39
44
|
isTokenRejected: (error) => error instanceof ShopifyTokenRejectedError,
|
|
40
45
|
isReconnectRequired: (error) => error instanceof ShopifyReconnectRequiredError,
|
|
41
46
|
},
|
package/starter/dot-env.example
CHANGED
|
@@ -61,9 +61,10 @@ SHOPIFY_API_SECRET=
|
|
|
61
61
|
# What the app may do in a store, separated by commas: widen it as the app needs.
|
|
62
62
|
SHOPIFY_SCOPES=read_products
|
|
63
63
|
SHOPIFY_API_VERSION=2026-07
|
|
64
|
-
# Shopify billing: whether
|
|
65
|
-
# development store
|
|
66
|
-
#
|
|
64
|
+
# Shopify billing: whether every plan bought inside Shopify admin is a test charge. A
|
|
65
|
+
# development store (an App Store reviewer's, say) is always charged test charges, whatever
|
|
66
|
+
# this says; true makes every store's charges test charges. Leave false in production: with
|
|
67
|
+
# true nobody is ever charged.
|
|
67
68
|
SHOPIFY_BILLING_TEST=false
|
|
68
69
|
# The app's Shopify App Store listing (https://apps.shopify.com/<handle>), on the Stores page.
|
|
69
70
|
SHOPIFY_LISTING_URL=
|
|
@@ -138,7 +138,8 @@ function Shell({
|
|
|
138
138
|
<UsagePage
|
|
139
139
|
client={client}
|
|
140
140
|
canManagePlan={!embedded && me.data.role === 'OWNER'}
|
|
141
|
-
|
|
141
|
+
// The plan is the whole organization's: one store's staff only see it.
|
|
142
|
+
shopifyBilling={embedded && isAdmin && !me.data.storeOnly}
|
|
142
143
|
embedded={embedded}
|
|
143
144
|
/>
|
|
144
145
|
}
|
|
@@ -363,6 +363,21 @@ describe('plan and usage inside Shopify admin', () => {
|
|
|
363
363
|
});
|
|
364
364
|
});
|
|
365
365
|
|
|
366
|
+
it('shows the staff of a store a dashboard account connected the plan, and nothing to change it with', async () => {
|
|
367
|
+
// The plan is the whole organization's: the API refuses them every change to it.
|
|
368
|
+
const client = embedded(
|
|
369
|
+
{
|
|
370
|
+
me: vi.fn().mockResolvedValue({ ...embeddedMe, storeOnly: true }),
|
|
371
|
+
getUsage: vi.fn().mockResolvedValue({ ...free, managedBy: 'shopify' }),
|
|
372
|
+
},
|
|
373
|
+
'/usage?billing=return',
|
|
374
|
+
);
|
|
375
|
+
expect(await screen.findByText('$19.00 per month')).toBeTruthy();
|
|
376
|
+
expect(screen.getByText(/Only the organization.s owner can change the plan/)).toBeTruthy();
|
|
377
|
+
expect(screen.queryByRole('button', { name: /Upgrade|Switch|Cancel plan/ })).toBeNull();
|
|
378
|
+
expect(client.confirmShopifyPlan).not.toHaveBeenCalled();
|
|
379
|
+
});
|
|
380
|
+
|
|
366
381
|
it('offers each paid plan with its price and sends the whole window to Shopify’s approval page', async () => {
|
|
367
382
|
const client = embedded({
|
|
368
383
|
startShopifyCheckout: vi
|