23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

If you’re using a custom commerce engine, the app will use its own services to retrieve the necessary<br />

product information and handle all the UI and license management for a transaction—the Store API will<br />

not play any role here. If the products are handled through the Store, on the other hand, the<br />

ListingInformation.productListings collection supplies localized product details and the<br />

CurrentApp.requestProductPurchaseAsync method handles the transaction, including the UI. This<br />

section focuses on these APIs.<br />

As noted before, in-app purchases can have expiration dates, after which time the user needs to<br />

repurchase the product to continue its use. This is appropriate for subscriptions or rentals where the<br />

user was fully aware at the time of purchase that the product would eventually expire. In general, you<br />

should always make it clear if a product purchase will expire. Don’t surprise the user or the user will<br />

likely surprise you with a less than favorable review in the Store!<br />

In-app purchases are demonstrated in Scenario 2 of the sample we’ve been using. In this case, the<br />

CurrentAppSimulator is initialized with data/in-app-purchase.xml, which defines two products<br />

(prosaically named Product 1 and Product 2), the first of which has an active license and the second of<br />

which is inactive. When you switch to this scenario (js/in-app-purchase.js), the sample loads the app’s<br />

ListingInformation, retrieves the product details from the productListings collection, and then<br />

displays those options (as shown in Figure 17-5):<br />

currentApp.loadListingInformationAsync().done(<br />

function (listing) {<br />

var product1 = listing.productListings.lookup("product1");<br />

var product2 = listing.productListings.lookup("product2");<br />

document.getElementById("product1SellMessage").innerText =<br />

"You can buy " + product1.name + " for: " + product1.formattedPrice + ".";<br />

document.getElementById("product2SellMessage").innerText =<br />

"You can buy " + product2.name + " for: " + product2.formattedPrice + ".";<br />

});<br />

FIGURE 17-5 Scenario 2 of the Trial apps and in-app purchases sample (cropped); product information obtained<br />

from the Store is displayed in blue text (circled here in red).<br />

777

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!