使用C#验证应用购买收据中的iOS

时间:2014-04-08 09:37:42

标签: c# ios objective-c in-app-purchase

我正在使用in app purchase网络服务

在我的服务器上验证我的ios C#收据

我通过在Xcode中执行以下操作获得收据作为字符串:

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    NSString* receiptString = [[NSString alloc] initWithString:transaction.payment.productIdentifier];

    NSLog(@"%@",receiptString);

    NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];

    NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];

    NSString *jsonObjectString = [receipt base64EncodedStringWithOptions:0];

}

我将该字符串(收据)作为参数发送到我的C#网络服务。 这是我的网络服务方法:

[WebMethod(Description = "Purchase Item Verify")]
public string PurchaseItem(string receiptData)
 {
    string returnmessage = "";

    try
    {
        var json = "{ 'receipt-data': '" + receiptData + "'}";

        ASCIIEncoding ascii = new ASCIIEncoding();
        byte[] postBytes = Encoding.UTF8.GetBytes(json);

        HttpWebRequest request;
        request = WebRequest.Create("https://sandbox.itunes.apple.com/verifyReceipt") as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "application/json";
        request.ContentLength = postBytes.Length;

        Stream postStream = request.GetRequestStream();
        postStream.Write(postBytes, 0, postBytes.Length);
        postStream.Close();

        var sendresponse = (HttpWebResponse)request.GetResponse();

        string sendresponsetext = "";
        using (var streamReader = new StreamReader(sendresponse.GetResponseStream()))
        {
            sendresponsetext = streamReader.ReadToEnd();
        }
        returnmessage = sendresponsetext;

    }
    catch (Exception ex)
    {
        ex.Message.ToString();
    }
    return returnmessage;
}

它始终返回{"status":21002}。 我一直在寻找两天,但仍无法找到解决方案。有人可以帮助我,我错了什么?

**我正在测试沙盒,这就是我使用沙盒URL的原因。我可以在我的应用中验证交易收据。

4 个答案:

答案 0 :(得分:15)

我得到了解决方案

对我有用的最终代码是:

 public string PurchaseItem(string receiptData)
{
    string returnmessage = "";
    try
    {
       // var json = "{ 'receipt-data': '" + receiptData + "'}";

        var json = new JObject(new JProperty("receipt-data", receiptData)).ToString();

        ASCIIEncoding ascii = new ASCIIEncoding();
        byte[] postBytes = Encoding.UTF8.GetBytes(json);

      //  HttpWebRequest request;
        var request = System.Net.HttpWebRequest.Create("https://sandbox.itunes.apple.com/verifyReceipt");
        request.Method = "POST";
        request.ContentType = "application/json";
        request.ContentLength = postBytes.Length;

        //Stream postStream = request.GetRequestStream();
        //postStream.Write(postBytes, 0, postBytes.Length);
        //postStream.Close();

        using (var stream = request.GetRequestStream())
        {
            stream.Write(postBytes, 0, postBytes.Length);
            stream.Flush();
        }

      //  var sendresponse = (HttpWebResponse)request.GetResponse();

        var sendresponse = request.GetResponse();

        string sendresponsetext = "";
        using (var streamReader = new StreamReader(sendresponse.GetResponseStream()))
        {
            sendresponsetext = streamReader.ReadToEnd().Trim();
        }
        returnmessage = sendresponsetext;

    }
    catch (Exception ex)
    {
        ex.Message.ToString();
    }
    return returnmessage;

花两天半时间来改变方法。谢谢上帝。

答案 1 :(得分:3)

这是使用HTTPClient的替代异步实现:

public static async Task<string> CheckReceiptWithAppStore()
    {
        string responseStr = null;

        string uri = "https://sandbox.itunes.apple.com/verifyReceipt";

        string receiptData = // Get your receipt from wherever you store it

        var json = new JObject(new JProperty("receipt-data", receiptData), 
            new JProperty("password", "paste-your-shared-secret-here")).ToString();

        using (var httpClient = new HttpClient())
        {        

            if (receiptData != null)
            {
                HttpContent content = new StringContent(json);

                try
                {
                    Task<HttpResponseMessage> getResponse = httpClient.PostAsync(uri, content);
                    HttpResponseMessage response = await getResponse;
                    responseStr = await response.Content.ReadAsStringAsync();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error verifying receipt: " + e.Message);
                }
            }
        }

        return responseStr;
    }

非基于订阅的购买不需要共享密码。

答案 2 :(得分:1)

为了管理订阅,@ Jerry Naing的回答还要求提供您的共享密钥(可以从iTunes Connect检索/生成)。包含此内容的最简单方法是在定义json var。

的行中添加一个附加属性
var json = new JObject(new JProperty("receipt-data", receiptData), new JProperty("password", "put_your_shared_secret_here")).ToString();

未能提供共享密钥将导致21004状态响应。

答案 3 :(得分:1)