create-wirejs-app 2.0.129-payments → 2.0.131-payments

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": "create-wirejs-app",
3
- "version": "2.0.129-payments",
3
+ "version": "2.0.131-payments",
4
4
  "description": "Initializes a wirejs package.",
5
5
  "author": "Jon Wire",
6
6
  "license": "MIT",
@@ -100,6 +100,18 @@ export const Store = (auth: AuthenticationApi) => withContext(context => ({
100
100
  },
101
101
  async listSubscriptions() {
102
102
  const user = await auth.requireCurrentUser(context);
103
- return payments.listSubscriptions(user.id);
103
+ const subs = await payments.listSubscriptions(user.id);
104
+ return subs.filter(s => s.status === 'active').map(s => ({
105
+ ...s,
106
+ name: plans.find(p => p.id === s.productId)!.name
107
+ }));
108
+ },
109
+ async cancelSubscription(subscriptionLineId: string) {
110
+ const user = await auth.requireCurrentUser(context);
111
+ const existing = await payments.getSubscriptionLine(subscriptionLineId);
112
+ if (existing?.customerId !== user.id) {
113
+ throw new Error("Subscription doesn't exist for this user.");
114
+ }
115
+ await payments.cancelSubscriptionLine(subscriptionLineId)
104
116
  }
105
117
  }));
@@ -12,13 +12,13 @@
12
12
  "dompurify": "^3.2.3",
13
13
  "marked": "^15.0.6",
14
14
  "wirejs-dom": "^1.0.42",
15
- "wirejs-resources": "^0.1.124-payments",
16
- "wirejs-components": "^0.1.67-payments",
17
- "wirejs-module-payments-stripe": "^0.1.18-payments",
18
- "wirejs-web-worker": "^1.0.21-payments"
15
+ "wirejs-resources": "^0.1.126-payments",
16
+ "wirejs-components": "^0.1.69-payments",
17
+ "wirejs-module-payments-stripe": "^0.1.20-payments",
18
+ "wirejs-web-worker": "^1.0.23-payments"
19
19
  },
20
20
  "devDependencies": {
21
- "wirejs-scripts": "^3.0.122-payments",
21
+ "wirejs-scripts": "^3.0.124-payments",
22
22
  "typescript": "^5.7.3"
23
23
  },
24
24
  "scripts": {
@@ -1,6 +1,6 @@
1
1
  import { html, list, attribute, hydrate } from 'wirejs-dom/v2';
2
2
  import { AuthenticatedContent } from 'wirejs-components';
3
- import { store, Product, Transaction, SubscriptionLine } from 'internal-api';
3
+ import { store, Product, Transaction } from 'internal-api';
4
4
  import { Main } from '../layouts/main.js';
5
5
 
6
6
  type LineItem = {
@@ -10,6 +10,14 @@ type LineItem = {
10
10
  quantity: number;
11
11
  }
12
12
 
13
+ type SubscriptionLine = Awaited<ReturnType<
14
+ typeof store.listSubscriptions>
15
+ >[number];
16
+
17
+ function renderAmount(inCents: number) {
18
+ return `$${(inCents/100).toFixed(2)}`;
19
+ }
20
+
13
21
  function Storefront() {
14
22
  const self = html`<div>
15
23
 
@@ -29,7 +37,7 @@ function Storefront() {
29
37
  self.data.cart.push({
30
38
  productId: p.id,
31
39
  productName: p.name,
32
- price: `\$${(p.unitAmount/100).toFixed(2)}`,
40
+ price: `${renderAmount(p.unitAmount)}`,
33
41
  quantity: 1
34
42
  });
35
43
  }
@@ -67,17 +75,17 @@ function Storefront() {
67
75
  <table>
68
76
  <tr>
69
77
  <th>date</th>
70
- <th>amount<th>
78
+ <th>amount</th>
71
79
  <th>items</th>
72
80
  </tr>
73
81
  ${list('transactions', (t: Transaction) => html`<tr>
74
82
  <td>${new Date(t.createdAt).toLocaleDateString()}</td>
75
- <td>\$${(t.amount/100).toFixed(2)}</td>
83
+ <td>${renderAmount(t.amount)}</td>
76
84
  <td><table>
77
85
  ${(t.items || []).map(li => html`<tr>
78
86
  <td>${li.description}</td>
79
87
  <td>x ${li.quantity}</td>
80
- <td>= \$${(li.amount/100).toFixed(2)}</td>
88
+ <td>= ${renderAmount(li.amount)}</td>
81
89
  </tr>`)}
82
90
  </table></td>
83
91
  </tr>`)}
@@ -87,12 +95,13 @@ function Storefront() {
87
95
  <table>
88
96
  <tr>
89
97
  <th>Name</th>
90
- <th>Price</th>
98
+ <th colspan='2'>Price</th>
91
99
  <th></th>
92
100
  </tr>
93
101
  ${list('plans', (p: Product) => html`<tr>
94
102
  <td>${p.name}</td>
95
- <td>${(p.unitAmount/100).toFixed(2)}/${p.interval}</td>
103
+ <td>${renderAmount(p.unitAmount)}</td>
104
+ <td>per ${p.interval}</td>
96
105
  <td
97
106
  style='color: darkgreen; font-weight: bold; cursor: pointer;'
98
107
  onclick=${() => {
@@ -107,12 +116,13 @@ function Storefront() {
107
116
  <table>
108
117
  <tr>
109
118
  <th>Name</th>
110
- <th>Price</th>
119
+ <th colspan='2'>Price</th>
111
120
  <th></th>
112
121
  </tr>
113
122
  ${list('planCart', (p: Product) => html`<tr>
114
123
  <td>${p.name}</td>
115
- <td>${(p.unitAmount/100).toFixed(2)}/${p.interval}</td>
124
+ <td>${renderAmount(p.unitAmount)}</td>
125
+ <td>per ${p.interval}</td>
116
126
  <td><span
117
127
  style='color: darkred; font-weight: bold; cursor: pointer;'
118
128
  onclick=${() => {
@@ -136,10 +146,37 @@ function Storefront() {
136
146
  </form>
137
147
  </div>
138
148
 
139
- <h4>Subscriptions</h4>
140
- <table>${list('subscriptions', (s: SubscriptionLine) => html`<li>
141
- ${JSON.stringify(s)}
142
- </li>`)}</table>
149
+ <h4>Active Subscriptions</h4>
150
+ <table>
151
+ <tr>
152
+ <th>Plan</th>
153
+ <th colspan='2'>Quantity</th>
154
+ <th colspan='2'>Amount</th>
155
+ <th></th>
156
+ </tr>
157
+ ${list('subscriptions', (s: SubscriptionLine) => html`<tr>
158
+ <td>${s.name}</td>
159
+ <td>${s.quantity}</td>
160
+ <td>x</td>
161
+ <td>${renderAmount(s.amount)}</td>
162
+ <td>per ${s.interval}</td>
163
+ <td><span
164
+ style='color: darkred; font-weight: bold; cursor: pointer;'
165
+ onclick=${async () => {
166
+ const yes = confirm("Are you sure?");
167
+ if (!yes) return;
168
+ try {
169
+ await store.cancelSubscription(null, s.id);
170
+ self.data.subscriptions.splice(
171
+ self.data.subscriptions.indexOf(s), 1
172
+ );
173
+ } catch (error) {
174
+ alert(error);
175
+ }
176
+ }}
177
+ >cancel</span></td>
178
+ </tr>`)}
179
+ </table>
143
180
 
144
181
  <div>`.onadd(async self => {
145
182
  const products = await store.listProducts(null);