Beispiel #1
0
        //</ClaimOfferOnWindows1607AndLater>

        // This method uses the Windows.ApplicationModel.Store APIs to purchase and claim a targeted offer.
        // This method can be used on any version of Windows 10.
        //<ClaimOfferOnAnyVersionWindows10>
        private async Task ClaimOfferOnAnyVersionWindows10Async(
            string productId, TargetedOfferData offerData, string msaToken)
        {
            if (string.IsNullOrEmpty(productId) || string.IsNullOrEmpty(msaToken))
            {
                System.Diagnostics.Debug.WriteLine("Product ID or Microsoft Account token is null or empty.");
                return;
            }

            // Purchase the add-on for the current user and report it to the Store as fulfilled
            // if the purchase was successful. Typically, a game or app would first show
            // a UI that prompts the user to buy the add-on; for simplicity, this example
            // simply purchases the add-on.
            var purchaseResult = await CurrentApp.RequestProductPurchaseAsync(productId);

            if (purchaseResult.Status == ProductPurchaseStatus.Succeeded)
            {
                CurrentApp.ReportProductFulfillment(productId);
                var claim = new TargetedOfferClaim
                {
                    StoreOffer = offerData,
                    Receipt    = purchaseResult.ReceiptXml
                };

                // Submit a request to claim the offer by sending a POST message to the
                // Store endpoint for targeted offers.
                using (var httpClientClaimOffer = new HttpClient())
                {
                    var uri = new Uri(storeOffersUri, UriKind.Absolute);

                    using (var request = new HttpRequestMessage(HttpMethod.Post, uri))
                    {
                        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", msaToken);

                        request.Content = new StringContent(
                            JsonConvert.SerializeObject(claim),
                            Encoding.UTF8,
                            jsonMediaType);

                        using (var response = await httpClientClaimOffer.SendAsync(request))
                        {
                            response.EnsureSuccessStatusCode();
                        }
                    }
                }
            }
        }
Beispiel #2
0
        //</GetTargetedOffers>

        // This method uses the Windows.Services.Store APIs to purchase and claim a targeted offer.
        // Only call this method if the app is running on Windows 10, version 1607, or later.
        //<ClaimOfferOnWindows1607AndLater>
        private async Task ClaimOfferOnWindows1607AndLaterAsync(
            string productId, TargetedOfferData offerData, string msaToken)
        {
            if (string.IsNullOrEmpty(productId) || string.IsNullOrEmpty(msaToken))
            {
                System.Diagnostics.Debug.WriteLine("Product ID or Microsoft Account token is null or empty.");
                return;
            }

            // Purchase the add-on for the current user. Typically, a game or app would first show
            // a UI that prompts the user to buy the add-on; for simplicity, this example
            // simply purchases the add-on.
            StorePurchaseResult result = await storeContext.RequestPurchaseAsync(productId);

            if (result.Status == StorePurchaseStatus.Succeeded)
            {
                // Get the StoreProduct in the user's collection that matches the targeted offer.
                List <String>           filterList  = new List <string>(productKinds);
                StoreProductQueryResult queryResult = await storeContext.GetUserCollectionAsync(filterList);

                KeyValuePair <string, StoreProduct> offer =
                    queryResult.Products.Where(p => p.Key == productId).SingleOrDefault();

                if (offer == null)
                {
                    System.Diagnostics.Debug.WriteLine("No StoreProduct with the specified product ID could be found.");
                    return;
                }

                StoreProduct product = offer.Value;

                // Parse the JSON string returned by StoreProduct.ExtendedJsonData to get the order ID.
                string extendedJsonData = product.ExtendedJsonData;
                string skuAvailability  =
                    JObject.Parse(extendedJsonData)["DisplaySkuAvailabilities"].FirstOrDefault().ToString();
                string sku            = JObject.Parse(skuAvailability)["Sku"].ToString();
                string collectionData = JObject.Parse(sku)["CollectionData"].ToString();
                string orderId        = JObject.Parse(collectionData)["orderId"].ToString();

                var claim = new TargetedOfferClaim
                {
                    StoreOffer = offerData,
                    Receipt    = orderId
                };

                // Submit a request to claim the offer by sending a POST message to the
                // Store endpoint for targeted offers.
                using (var httpClientClaimOffer = new HttpClient())
                {
                    var uri = new Uri(storeOffersUri, UriKind.Absolute);

                    using (var request = new HttpRequestMessage(HttpMethod.Post, uri))
                    {
                        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", msaToken);

                        request.Content = new StringContent(
                            JsonConvert.SerializeObject(claim),
                            Encoding.UTF8,
                            jsonMediaType);

                        using (var response = await httpClientClaimOffer.SendAsync(request))
                        {
                            response.EnsureSuccessStatusCode();
                        }
                    }
                }
            }
        }