WP8中的应用程序内购买收据分析

时间:2014-08-25 08:53:06

标签: c# windows-phone-8 xml-parsing in-app-purchase receipt

我尝试在应用购买插件中编写一个phonegap WP8。在我的插件中,我想获得“订单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元素的属性)? This link以及任何其他类似链接对我不起作用。

2 个答案:

答案 0 :(得分:1)

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

这是我为我的项目实现的解析xml收据的方法。它将提供字典作为回应,您可以在字典中找到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);
    }

答案 1 :(得分:1)

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

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