Deepak Tewatia

Deepak Tewatia

  • 130
  • 14.2k
  • 1.5m

Verifying iOS in app purchase receipt with C#

Sep 15 2022 12:08 PM

I am verifying my iOS in-app purchase receipt on my server using C# web services and I am sending that string to my C# web services as a parameter.

public int PurchaseItem(string receiptData)
{

    int proUserStatus = 1;
    try
    {
        // var json = "{ 'receipt-data': '" + receiptData + "'}";
        var json = new JObject(new JProperty("exclude-old-transactions", true), new JProperty("receipt-data", receiptData), new JProperty("password", secretKey)).ToString();
        ASCIIEncoding ascii = new ASCIIEncoding();
        byte[] postBytes = Encoding.UTF8.GetBytes(json);
        //  HttpWebRequest request;
        var request = System.Net.HttpWebRequest.Create("sandbox  api url");
        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();
        }
        ValidateResponse vres = JsonConvert.DeserializeObject(sendresponsetext);
        proUserStatus = vres.status;
    }
    catch (Exception ex)
    {
        ex.Message.ToString();
    }
    return proUserStatus;
}

It always return {status:21002}. Why is this so? I am testing on a sandbox which is why I use the sandbox URL.