WP8中的应用内购买收据解析

本文关键字:应用 WP8 | 更新日期: 2023-09-27 18:19:22

我尝试写一个phonegap WP8在应用程序购买插件。在我的插件中,我想获得"订单id"。我知道这是可能的,所以收据有一个唯一的id。

当我尝试这个时,

string receipt = await CurrentApp.RequestProductPurchaseAsync(productListing.ProductId, false);

收据字符串是这样的:

<?xml version="1.0" ?> 
- <Receipt Version="1.0" CertificateId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" xmlns="http://schemas.microsoft.com/windows/2012/store/receipt">
  <ProductReceipt PurchasePrice="1.0" PurchaseDate="10:32:31 AM" Id="0fba8b37-95ed-4c57-b16a-00f9ac25d696" AppId="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ProductId="img.2" ProductType="Consumable" PublisherUserId="00000000000000000000000000000000000000000000" PublisherDeviceId="00000000-0000-0000-0000-000000000000" MicrosoftProductId="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" /> 
- <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
- <SignedInfo>
  <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-xxxx-00000000" /> 
  <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /> 
- <Reference URI="">
- <Transforms>
  <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> 
  </Transforms>
  <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> 
  <DigestValue>whocares</DigestValue> 
  </Reference>
  </SignedInfo>
  <SignatureValue>whocares</SignatureValue> 
  </Signature>
  </Receipt>

我怎么能得到Id(它的属性的ProductReceipt元素)从这个字符串?这个链接和任何其他类似的链接不为我工作

WP8中的应用内购买收据解析

检查您的收据,它不包含"order-id",但在"Productreceipt"中有"Id"。我想你需要那个Id。

这是我为我的项目实现的方法来解析xml收据。它将在响应中提供Dictionary,您可以很容易地在Dictionary中找到Id。

private string ParseReceiptData(string xmlReceipt)
    {
        Dictionary AttributeDict = new Dictionary();
        var xd = XDocument.Parse(xmlReceipt).Descendants();
        foreach (var node in xd)
        {
            if (node.Name.LocalName.Equals("ProductReceipt"))
            {
                var attribute = node.Attributes();
                foreach (var attrib in attribute)
                {
                    AttributeDict.Add(attrib.Name.LocalName, attrib.Value);
                }
            }
        }
        var purchaseDate = Convert.ToDateTime(AttributeDict["PurchaseDate"]);
        var Expdate = purchaseDate.Add(TimeSpan.FromDays(30)).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
        AttributeDict.Add("ExpiryDate", Expdate);
        return JsonConvert.SerializeObject(AttributeDict);
    }

我找到了。事实上,这很容易。

XDocument xml = XDocument.Parse(receipt);
XElement productReceipt = xml.Root.Elements().FirstOrDefault();
string orderId = productReceipt.Attribute("Id").Value;