2013-04-09 81 views
3

我正在使用Windows Phone 8應用。我的應用將包含應用內購買。我試圖理解收據概念。根據我的理解,在用戶在我的應用程序內購買產品後,會生成收據。驗證Windows Phone應用內購買收據

<?xml version="1.0"?> 
<Receipt Version="1.0" CertificateId="{Identifier1}" xmlns="http://schemas.microsoft.com/windows/2012/store/receipt"> 
    <ProductReceipt PurchasePrice="${PurchaseAmount}" PurchaseDate="{DateTime}" Id="{Guid1}" AppId="{Guid2}" ProductId="{ProductName}" ProductType="Consumable" PublisherUserId="{Identifier2}" PublisherDeviceId="{Identifier3}" MicrosoftProductId="{Guid3}" /> 
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 
    <SignedInfo> 
     <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /> 
     <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /> 
     <Reference URI=""> 
     <Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms> 
    <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /> 
    <DigestValue>{Identifier4}</DigestValue> 
     </Reference> 
    </SignedInfo> 

    <SignatureValue>{HashedValue}</SignatureValue> 
    </Signature> 
</Receipt> 

太好了!我不確定如何判斷這個收據是否來自微軟的服務器。有人可以向我解釋如何驗證?我看到這個:http://code.msdn.microsoft.com/wpapps/In-app-purchase-receipt-c3e0bce4但是,它對我沒有意義。我不明白例子中的證書。 「IapReceiptProduction.cer」是一套嗎?或者只是爲了這個樣本?

對不起,如果這是一個愚蠢的問題。

+0

http://blogs.msdn.com/b/lighthouse/archive/2013/08/28/faqs-about-in-app-purchase-for-windows-phone-8.aspx FYI – 2014-05-29 02:56:47

回答

6

'Receipt'XML元素中的'CertificateId'屬性確定使用哪個證書來簽署Windows應用商店收據。在您的示例中具有CertificateID(「{Identifier1}」)後,您可以從以下代碼示例中指定的URL下載所需的證書作爲'certificateUrl'。這是你如何將程序下載證書:

public static X509Certificate2 RetrieveCertificate(string certificateId) 
{ 
    const int MaxCertificateSize = 10000; 

    // We are attempting to retrieve the following url. The getAppReceiptAsync website at 
    // http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.store.currentapp.getappreceiptasync.aspx 
    // lists the following format for the certificate url. 
    String certificateUrl = String.Format("https://go.microsoft.com/fwlink/?LinkId=246509&cid={0}", certificateId); 

    // Make an HTTP GET request for the certificate 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(certificateUrl); 
    request.Method = "GET"; 

    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

    // Retrieve the certificate out of the response stream 
    byte[] responseBuffer = new byte[MaxCertificateSize]; 
    Stream resStream = response.GetResponseStream(); 
    int bytesRead = ReadResponseBytes(responseBuffer, resStream); 

    if (bytesRead < 1) 
    { 
     //TODO: Handle error here 
    } 

    return new X509Certificate2(responseBuffer); 
} 

你可以看到更多這樣的代碼示例here的。該示例中包含「IapReceiptProduction.cer」,僅顯示接收驗證如何工作,無需通過代碼下載證書。獲得證書後,您可以使用System.Security.Cryptography.Xml.SignedXml API驗證收據,如鏈接的代碼示例所示。