@jskit-ai/store-purchases-web 0.1.2 → 0.1.4

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 CHANGED
@@ -2,4 +2,5 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - A store sheet that fails or is closed, a failed restore and prices that don't load reject with the person's words (`jskit.storePurchases.device.purchaseUnfinished`, `restoreFailed`, `offersFailed`) instead of the store plugin's own text.
5
6
  - New package: the `client.store-purchases` capability (`StorePurchasesClientProvider`) and the `storePurchases.device` interface over `@capgo/native-purchases` ^9.0.0 (an optional peer): store prices, buying with the server's purchase intent as Apple's appAccountToken and Google's obfuscatedAccountId, restoring, and each transaction confirmed by `@jskit-ai/store-purchases-core` before it is finished.
package/locales/en.json CHANGED
@@ -2,7 +2,10 @@
2
2
  "jskit": {
3
3
  "storePurchases": {
4
4
  "device": {
5
- "unavailable": "Purchases are made in the app from the App Store or Google Play."
5
+ "unavailable": "Purchases are made in the app from the App Store or Google Play.",
6
+ "offersFailed": "The store's prices didn't load. Check the connection, then try again.",
7
+ "purchaseUnfinished": "The store didn't finish the purchase. Try again.",
8
+ "restoreFailed": "The store didn't restore your purchases. Check the connection, then try again."
6
9
  }
7
10
  }
8
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/store-purchases-web",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -53,12 +53,12 @@
53
53
  }
54
54
  },
55
55
  "dependencies": {
56
- "@jskit-ai/store-purchases-core": "0.1.2"
56
+ "@jskit-ai/store-purchases-core": "0.1.4"
57
57
  },
58
58
  "peerDependencies": {
59
59
  "@capgo/native-purchases": "^9.0.0",
60
- "@jskit-ai/http-web": "0.2.85",
61
- "@jskit-ai/kernel": "0.2.42",
60
+ "@jskit-ai/http-web": "0.2.87",
61
+ "@jskit-ai/kernel": "0.2.44",
62
62
  "vue": "^3.5.44"
63
63
  },
64
64
  "peerDependenciesMeta": {
@@ -42,6 +42,21 @@ function createStoreDevice({ plugin, platform }) {
42
42
  const storeIdOf = (product) => (platform === "ios" ? product.appStoreProductId : product.playProductId);
43
43
  const typeOf = (product) => (product.kind === "subscription" ? "subs" : "inapp");
44
44
 
45
+ /**
46
+ * A store call whose failure the person reads in their own words: a sheet they closed or a store that didn't answer
47
+ * rejects with the plugin's own text, which never reaches the screen.
48
+ * @template T
49
+ * @param {() => Promise<T>} call
50
+ * @param {string} key The message for its failure.
51
+ */
52
+ async function storeCall(call, key) {
53
+ try {
54
+ return await call();
55
+ } catch {
56
+ throw createTextError(key);
57
+ }
58
+ }
59
+
45
60
  /** What the server confirms: the signed transaction on iOS, the purchase token on Android. */
46
61
  function transactionOf(transaction) {
47
62
  return Object.freeze({
@@ -59,7 +74,10 @@ function createStoreDevice({ plugin, platform }) {
59
74
  for (const kind of ["inapp", "subs"]) {
60
75
  const wanted = products.filter((product) => typeOf(product) === kind && storeIdOf(product));
61
76
  if (!wanted.length) continue;
62
- const { products: details } = await plugin.getProducts({ productIdentifiers: wanted.map(storeIdOf), productType: kind });
77
+ const { products: details } = await storeCall(
78
+ () => plugin.getProducts({ productIdentifiers: wanted.map(storeIdOf), productType: kind }),
79
+ "jskit.storePurchases.device.offersFailed"
80
+ );
63
81
  for (const detail of details) {
64
82
  const product = wanted.find((item) => storeIdOf(item) === detail.identifier || storeIdOf(item) === detail.planIdentifier);
65
83
  if (product) found.push(Object.freeze({ productId: product.id, title: String(detail.title || ""), price: String(detail.priceString || ""), currency: String(detail.currencyCode || "") }));
@@ -70,14 +88,16 @@ function createStoreDevice({ plugin, platform }) {
70
88
 
71
89
  /** Buys a product with the server's intent as the account token; answers what the server confirms. */
72
90
  async function purchase(product, accountToken) {
73
- await plugin.configure({ autoFinishTransactions: false });
74
- const transaction = await plugin.purchaseProduct({
75
- productIdentifier: storeIdOf(product),
76
- productType: typeOf(product),
77
- appAccountToken: accountToken,
78
- isConsumable: false,
79
- autoAcknowledgePurchases: false
80
- });
91
+ const transaction = await storeCall(async () => {
92
+ await plugin.configure({ autoFinishTransactions: false });
93
+ return plugin.purchaseProduct({
94
+ productIdentifier: storeIdOf(product),
95
+ productType: typeOf(product),
96
+ appAccountToken: accountToken,
97
+ isConsumable: false,
98
+ autoAcknowledgePurchases: false
99
+ });
100
+ }, "jskit.storePurchases.device.purchaseUnfinished");
81
101
  return transactionOf(transaction);
82
102
  }
83
103
 
@@ -88,8 +108,10 @@ function createStoreDevice({ plugin, platform }) {
88
108
 
89
109
  /** Asks the store again for the person's purchases, then answers each one, for the server to confirm. */
90
110
  async function restore() {
91
- await plugin.restorePurchases();
92
- const { purchases } = await plugin.getPurchases({});
111
+ const { purchases } = await storeCall(async () => {
112
+ await plugin.restorePurchases();
113
+ return plugin.getPurchases({});
114
+ }, "jskit.storePurchases.device.restoreFailed");
93
115
  return Object.freeze(purchases.map(transactionOf));
94
116
  }
95
117
 
@@ -82,3 +82,16 @@ test("offers show the store's own prices; restoring confirms each purchase again
82
82
  assert.deepEqual(server.sent.at(-1), ["POST", "/api/store-purchases/apple/transactions", { signedTransaction: "jws-7" }]);
83
83
  assert.equal(createStoreDevice({ plugin: null, platform: "ios" }).available, false);
84
84
  });
85
+
86
+ test("a store sheet that fails or is closed, a failed restore and prices that don't load say so in the person's words", async () => {
87
+ const plugin = {
88
+ ...fakeNativePurchases({}),
89
+ async getProducts() { throw new Error("BillingClient: SERVICE_UNAVAILABLE"); },
90
+ async purchaseProduct() { throw new Error("USER_CANCELED (code 1)"); },
91
+ async restorePurchases() { throw new Error("SKErrorDomain error 0."); }
92
+ };
93
+ const device = createStoreDevice({ plugin, platform: "android" });
94
+ await assert.rejects(device.offers(PRODUCTS), { message: "The store's prices didn't load. Check the connection, then try again." });
95
+ await assert.rejects(device.purchase(PRODUCTS[0], "token"), { message: "The store didn't finish the purchase. Try again." });
96
+ await assert.rejects(device.restore(), { message: "The store didn't restore your purchases. Check the connection, then try again." });
97
+ });