Skip to content

Commit c53a76c

Browse files
Merge pull request #3324 from MicrosoftDocs/user/mikehoffms/dig-goods
Clarify "Provide in-app purchases with the Digital Goods API"
2 parents ee73441 + e7192dc commit c53a76c

File tree

1 file changed

+117
-40
lines changed

1 file changed

+117
-40
lines changed

microsoft-edge/progressive-web-apps-chromium/how-to/digital-goods-api.md

Lines changed: 117 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.author: msedgedevrel
66
ms.topic: conceptual
77
ms.service: microsoft-edge
88
ms.subservice: pwa
9-
ms.date: 11/25/2024
9+
ms.date: 12/19/2024
1010
---
1111
# Provide in-app purchases with the Digital Goods API
1212

@@ -24,62 +24,100 @@ The Digital Goods API is an interface between your PWA app and the Microsoft Sto
2424

2525
See:
2626
* [Digital Goods API For Microsoft Store PWA Explainer](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/PwaDigitalGoods/explainer.md)
27-
* [Digital Goods API: Draft Community Group Report](https://wicg.github.io/digital-goods/)
27+
* [Digital Goods API specification](https://wicg.github.io/digital-goods/)
2828

2929

3030
<!-- ====================================================================== -->
3131
## Payment Request API
3232

3333
The Payment Request API⁠⁠ handles the actual payment transaction when a purchase is made by a user. The Payment Request API uses the item details that the Digital Goods API provides, to make the in-app purchase by using whichever billing payment method the user has set up at the Microsoft Store.
3434

35-
See [Payment Request API](https://developer.mozilla.org/docs/Web/API/Payment_Request_API) at MDN.
35+
See:
36+
* [Payment Request API](https://developer.mozilla.org/docs/Web/API/Payment_Request_API) at MDN.
37+
38+
39+
<!-- ====================================================================== -->
40+
## Enable the Digital Goods API
41+
42+
The Digital Goods API is currently available for testing in Microsoft Edge. To test the API, use either of the following ways:
43+
* To test your code locally: [Use a supported preview channel of Microsoft Edge on your dev machine](#use-a-supported-preview-channel-of-microsoft-edge-on-your-dev-machine).
44+
* To test your code in your Microsoft Store PWA: [Register for the origin trial, then use your token at your website](#register-for-the-origin-trial-then-use-your-token-at-your-website).
45+
46+
Details are below.
47+
48+
49+
<!-- ------------------------------ -->
50+
#### Use a supported preview channel of Microsoft Edge on your dev machine
51+
52+
To test the Digital Goods API locally, before deploying your app to production, run Edge Dev or Edge Canary. These preview versions of Edge have the API enabled, as part of a running experiment.
53+
54+
To download a preview (insider) channel of Microsoft Edge, see [Become a Microsoft Edge Insider](https://aka.ms/microsoftedge).
55+
56+
57+
<!-- ------------------------------ -->
58+
#### Register for the origin trial, then use your token at your website
59+
60+
To test the Digital Goods API in production, with your users, use an origin trial token. With this approach, when your users download your PWA from the Microsoft Store, they will have the API enabled.
61+
62+
See:
63+
* [Use origin trials in Microsoft Edge](../../origin-trials/index.md)
64+
* [Digital Goods API](https://developer.microsoft.com/microsoft-edge/origin-trials/trials/4b4a9ead-d912-4349-87b3-25e5e50b4f13) at _Origin Trials_.
3665

3766

3867
<!-- ====================================================================== -->
3968
## Checking whether the Digital Goods API is available
4069

41-
To detect whether you've correctly enabled the API on your website, check for the `getDigitalGoodsService` method in the window object:
70+
To detect whether you've correctly enabled the API on your website by using your origin trial token, check whether the `getDigitalGoodsService` method exists on the `window` object:
4271

4372
```javascript
4473
if ('getDigitalGoodsService' in window) {
45-
// Digital Goods API is supported!
74+
// The Digital Goods API is supported.
4675
} else {
4776
console.log('DigitalGoodsService is not available.');
48-
// Use other payment method
77+
// Use another payment method.
4978
}
5079
```
5180

81+
See also:
82+
* [getDigitalGoodsService() method](https://wicg.github.io/digital-goods/#getdigitalgoodsservice-method) of the `Window` interface.
83+
5284

5385
<!-- ====================================================================== -->
54-
## Connecting to the Microsoft Store Billing service (`getDigitalGoodsService` method)
86+
## Connecting to the Microsoft Store Billing service (`window.getDigitalGoodsService` method)
5587

56-
Use the `getDigitalGoodsService` method to connect to the Microsoft Store Billing service.
88+
Use the `getDigitalGoodsService` method of the `window` object to connect to the Microsoft Store Billing service. A [DigitalGoodsService interface](https://wicg.github.io/digital-goods/#digitalgoodsservice) is returned.
5789

5890
The Digital Goods API was designed to be compatible with various browsers and digital stores, similar to how the Payment Request API is browser-agnostic and can be used with different payment providers. To retrieve an instance of the service for Microsoft Store Billing, pass the string `"https://store.microsoft.com/billing"` as the payment method to the `getDigitalGoodsService` method.
5991

6092
If the method throws an error, the Microsoft Store Billing payment method is not available (such as when the user is accessing your PWA through the browser). Alternatively, consider providing a different payment method for transactions.
6193

6294
```javascript
6395
if (window.getDigitalGoodsService === undefined) {
64-
// Digital Goods API is not supported in this context.
96+
// The Digital Goods API isn't supported in this context.
6597
return;
6698
}
6799
try {
68100
const digitalGoodsService = await window.getDigitalGoodsService("https://store.microsoft.com/billing");
69101
// Use the service here.
70102
...
71103
} catch (error) {
72-
// Our preferred service provider is not available.
104+
// The preferred service provider is not available.
73105
// Use a web-based payment flow instead.
74106
return;
75107
}
76108
```
77109

110+
This payment method `getDigitalGoodsService("https://store.microsoft.com/billing")` is available only for a PWA that's installed from the Microsoft Store, on Windows. No other settings are needed.
111+
112+
See also:
113+
* [getDigitalGoodsService() method](https://wicg.github.io/digital-goods/#getdigitalgoodsservice-method) of the `Window` interface.
114+
* [DigitalGoodsService interface](https://wicg.github.io/digital-goods/#digitalgoodsservice)
115+
78116

79117
<!-- ====================================================================== -->
80118
## Querying item details (`getDetails` method)
81119

82-
Use the `getDetails` method to query item details.
120+
Use the `getDetails` method of the `DigitalGoodsService` interface to query item details.
83121

84122
After connecting the Digital Goods service to Microsoft Store, you can use the API to access product and purchase information. The `getDetails` method lets you get information about the items you’ve set up in the Partner Center. Display information such as the product title, description, and price in your app UI, so the user knows what's available for purchase.
85123

@@ -105,20 +143,31 @@ The item ID is a string that represents the primary key of the items. In the Mi
105143

106144
The item's `price` is a `PaymentCurrencyAmount` that contains the current price of the item in the user's current region and currency. The `price` is designed to be formatted for the user's current locale by using `Intl.NumberFormat`, as shown above.
107145

108-
See also:
146+
147+
<!-- ------------------------------ -->
148+
#### See also
149+
150+
Digital Goods API specification
151+
* [getDetails() method](https://wicg.github.io/digital-goods/#getDetails-method)
152+
153+
Windows App Development:
109154
* [StoreProduct.InAppOfferToken Property](/uwp/api/windows.services.store.storeproduct.inappoffertoken)
110-
* [PaymentCurrencyAmount dictionary](https://www.w3.org/TR/payment-request/#dom-paymentcurrencyamount) in _Payment Request API_ at W3C.
111-
* [Intl.NumberFormat](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) at MDN.
112155

156+
W3C:
157+
* [PaymentCurrencyAmount dictionary](https://www.w3.org/TR/payment-request/#dom-paymentcurrencyamount)
113158

114-
<!-- ====================================================================== -->
115-
## Purchasing an item (`show` method)
159+
MDN:
160+
* [Intl.NumberFormat](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat)
116161

117-
Use the `show` method to purchase an item, after you construct a request that contains the item details.
118162

119-
Once your products and details are displayed to the user, you can implement the purchase flow by using the Payment Request API. When combined with the Digital Goods API, the only required input parameter is `methodData`.
163+
<!-- ====================================================================== -->
164+
## Purchasing an item (`PaymentRequest` constructor and `show` method)
165+
166+
After your products and details are displayed to the user, implement the purchase flow by using the Payment Request API. To purchase an item, first construct a request that contains the item details by using the `PaymentRequest` constructor, and then use the `show` method of the `PaymentRequest` object to start the payment flow.
120167

121-
Use the `supportedMethods` member of the `methodData`⁠⁠ parameter in the `PaymentRequest` to identify Microsoft Store Billing as the payment method with the string `"https://store.microsoft.com/billing"`. Then in the `data` member, pass along the item ID as the `sku`.
168+
When combined with the Digital Goods API, the only required input parameter for the `PaymentRequest` constructor is `methodData`. In the constructor's parameter:
169+
* In the `supportedMethods` member, specify Microsoft Store Billing as the payment method, as the string `'https://store.microsoft.com/billing'`.
170+
* In the `data` member, pass along the `itemId` as the `sku`.
122171

123172
```javascript
124173
const details = await digitalGoodsService.getDetails(['monthly_subscription']);
@@ -132,33 +181,30 @@ const request = new PaymentRequest([
132181
]);
133182
```
134183

135-
Then call the `show` method to start the payment flow:
184+
Then call the `show` method of the `PaymentRequest` object, to start the payment flow:
136185

137186
```javascript
138187
const response = await request.show();
139188
```
140189

141-
This will display the Store purchase UI to the user, where the user can view details about the product that they're trying to purchase. During this process, the current browser session is temporarily disabled until the purchase flow is complete. The user can either cancel the transaction, or proceed with the payment:
190+
This displays the Store purchase UI to the user, where the user can view details about the product that they're trying to purchase. During this process, the current browser session is temporarily disabled until the purchase flow is complete. The user can either cancel the transaction, or proceed with the payment:
142191

143192
* If the user cancels the payment, the Promise that's returned by the `show` method will be rejected with an error.
144193

145-
* If the user successfully pays and completes the purchase, the Promise will resolve with a `PaymentResponse`.
194+
* If the user successfully pays and completes the purchase, the Promise will resolve with a `PaymentResponse`. In the `details` property of the payment response, a purchase token is returned.
146195

147-
In the `details` property of the payment response, a purchase token is returned.
148-
149-
150-
<!-- ====================================================================== -->
151-
## Acknowledging a purchase
152-
153-
The payment response returns a _purchase token_ string, which can be used for direct communication between your server and the service provider beyond the Digital Goods API. Such communication can allow you to independently verify information about the purchase before granting entitlements.
154-
155-
Some stores might require that you (the developer) acknowledge a purchase after the purchase has succeeded, to confirm that the purchase has been recorded.
196+
See also:
197+
* [Payment Request API](https://developer.mozilla.org/docs/Web/API/Payment_Request_API)
198+
* [PaymentRequest: PaymentRequest() constructor](https://developer.mozilla.org/docs/Web/API/PaymentRequest/PaymentRequest)
199+
* [PaymentRequest: show() method](https://developer.mozilla.org/docs/Web/API/PaymentRequest/show)
200+
* [PaymentResponse](https://developer.mozilla.org/docs/Web/API/PaymentResponse/)
201+
* [PaymentResponse: details property](https://developer.mozilla.org/docs/Web/API/PaymentResponse/details)
156202

157203

158204
<!-- ====================================================================== -->
159205
## Consuming a purchase (`consume` method)
160206

161-
Use the `consume` method to consume a purchase.
207+
Use the `consume` method of the `DigitalGoodsService` interface to consume a purchase.
162208

163209
A _consumable purchase_ is a purchase that's designed to be purchased multiple times. A consumable purchase usually needs to be marked as "consumed" before the purchase can be purchased again by the user. An example of a consumable purchase is an in-game powerup that makes the player stronger for a short period of time.
164210

@@ -168,11 +214,14 @@ To mark a purchase as "consumed", use the `consume` method:
168214
digitalGoodsService.consume(purchaseToken);
169215
```
170216

217+
See also:
218+
* [consume() method](https://wicg.github.io/digital-goods/#consume-method) of the `DigitalGoodsService` interface.
219+
171220

172221
<!-- ====================================================================== -->
173222
## Checking existing purchases (`listPurchases` method)
174223

175-
Use the `listPurchases` method to check existing purchases. This method returns information about the user's existing purchases. This method allows a client to get a list of items that are currently owned or purchased by the user. This may be necessary, to do either of the following:
224+
Use the `listPurchases` method of the `DigitalGoodsService` interface to check existing purchases. This method returns information about the user's existing purchases. This method allows a client to get a list of items that are currently owned or purchased by the user. This may be necessary, to do either of the following:
176225

177226
* Check for entitlements, such as whether a subscription, promotional code, or permanent upgrade is active.
178227

@@ -191,11 +240,16 @@ for (const purchase of purchaseList) {
191240

192241
The `listPurchases` method doesn't return consumed products or expired subscriptions.
193242

243+
See also:
244+
* [listPurchases() method](https://wicg.github.io/digital-goods/#listPurchases-method) of the `DigitalGoodsService` interface.
245+
194246

195247
<!-- ====================================================================== -->
196248
## Getting the purchase history (`listPurchaseHistory` method)
197249

198-
Use the `listPurchaseHistory` method to get the purchase history. This method returns a list that shows the most recent purchase made by the user for each item, regardless of whether the purchase is expired, canceled, or consumed. This method returns a list of `PurchaseDetails` containing the `itemId` and `purchaseToken` for each purchase.
250+
Use the `listPurchaseHistory` method of the `DigitalGoodsService` interface to get the purchase history.
251+
252+
This method returns a list of `PurchaseDetails` containing the `itemId` and `purchaseToken` for each purchase. The list includes the most recent purchase made by the user for each item, regardless of whether the purchase is expired, canceled, or consumed.
199253

200254
```javascript
201255
const purchaseList = await digitalGoodsService.listPurchaseHistory();
@@ -206,17 +260,40 @@ for (const purchase of purchaseList) {
206260
}
207261
```
208262

263+
See also:
264+
* [listPurchaseHistory() method](https://wicg.github.io/digital-goods/#listPurchaseHistory-method) of the `DigitalGoodsService` interface.
265+
* [PurchaseDetails dictionary](https://wicg.github.io/digital-goods/#purchaseDetails-dictionary)
266+
209267

210268
<!-- ====================================================================== -->
211269
## See also
270+
<!-- all links in article: -->
212271

213-
* [Digital Goods API For Microsoft Store PWA Explainer](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/PwaDigitalGoods/explainer.md)
214-
* [StoreProduct.InAppOfferToken Property](/uwp/api/windows.services.store.storeproduct.inappoffertoken)
272+
Origin trials:
273+
* [Use origin trials in Microsoft Edge](../../origin-trials/index.md)
274+
* [Digital Goods API](https://developer.microsoft.com/microsoft-edge/origin-trials/trials/4b4a9ead-d912-4349-87b3-25e5e50b4f13) at _Origin Trials_.
215275

216-
W3C:
217-
* [Digital Goods API: Draft Community Group Report](https://wicg.github.io/digital-goods/)
218-
* [Payment Request API](https://www.w3.org/TR/payment-request/)
219-
* [PaymentCurrencyAmount dictionary](https://www.w3.org/TR/payment-request/#dom-paymentcurrencyamount) in _Payment Request API_.
276+
GitHub:
277+
* [Digital Goods API For Microsoft Store PWA Explainer](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/PwaDigitalGoods/explainer.md)
278+
* [Digital Goods API specification](https://wicg.github.io/digital-goods/)
279+
* [getDigitalGoodsService() method](https://wicg.github.io/digital-goods/#getdigitalgoodsservice-method) of the `Window` interface.
280+
* [DigitalGoodsService interface](https://wicg.github.io/digital-goods/#digitalgoodsservice)
281+
* [getDetails() method](https://wicg.github.io/digital-goods/#getDetails-method)
282+
* [listPurchases() method](https://wicg.github.io/digital-goods/#listPurchases-method)
283+
* [listPurchaseHistory() method](https://wicg.github.io/digital-goods/#listPurchaseHistory-method)
284+
* [consume() method](https://wicg.github.io/digital-goods/#consume-method)
285+
* [PurchaseDetails dictionary](https://wicg.github.io/digital-goods/#purchaseDetails-dictionary)
220286

221287
MDN:
288+
* [Payment Request API](https://developer.mozilla.org/docs/Web/API/Payment_Request_API)
289+
* [PaymentRequest: PaymentRequest() constructor](https://developer.mozilla.org/docs/Web/API/PaymentRequest/PaymentRequest)
290+
* [PaymentRequest: show() method](https://developer.mozilla.org/docs/Web/API/PaymentRequest/show)
291+
* [PaymentResponse](https://developer.mozilla.org/docs/Web/API/PaymentResponse/)
292+
* [PaymentResponse: details property](https://developer.mozilla.org/docs/Web/API/PaymentResponse/details)
222293
* [Intl.NumberFormat](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat)
294+
295+
W3C:
296+
* [PaymentCurrencyAmount dictionary](https://www.w3.org/TR/payment-request/#dom-paymentcurrencyamount)
297+
298+
Windows App Development:
299+
* [StoreProduct.InAppOfferToken Property](/uwp/api/windows.services.store.storeproduct.inappoffertoken)

0 commit comments

Comments
 (0)