You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

225 lines
7.9 KiB

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using Sog;
using LitJson;
namespace HttpProxyWorld
{
/*
* 苹果返回格式
{
"status": 0,
"environment": "Sandbox",
"receipt": {
"receipt_type": "ProductionSandbox",
"adam_id": 0,
"app_item_id": 0,
"bundle_id": "com.elex.rummyfun101",
"application_version": "58",
"download_id": 0,
"version_external_identifier": 0,
"receipt_creation_date": "2017-06-13 06:27:06 Etc/GMT",
"receipt_creation_date_ms": "1497335226000",
"receipt_creation_date_pst": "2017-06-12 23:27:06 America/Los_Angeles",
"request_date": "2017-06-13 06:30:05 Etc/GMT",
"request_date_ms": "1497335405817",
"request_date_pst": "2017-06-12 23:30:05 America/Los_Angeles",
"original_purchase_date": "2013-08-01 07:00:00 Etc/GMT",
"original_purchase_date_ms": "1375340400000",
"original_purchase_date_pst": "2013-08-01 00:00:00 America/Los_Angeles",
"original_application_version": "1.0",
"in_app": [
{
"quantity": "1",
"product_id": "iap_30",
"transaction_id": "1000000306394868",
"original_transaction_id": "1000000306394868",
"purchase_date": "2017-06-12 12:56:05 Etc/GMT",
"purchase_date_ms": "1497272165000",
"purchase_date_pst": "2017-06-12 05:56:05 America/Los_Angeles",
"original_purchase_date": "2017-06-12 12:56:05 Etc/GMT",
"original_purchase_date_ms": "1497272165000",
"original_purchase_date_pst": "2017-06-12 05:56:05 America/Los_Angeles",
"is_trial_period": "false"
}
]
}
}
*/
public class IAPReceiptCheck
{
private const string urlSandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
private const string urlProduction = "https://buy.itunes.apple.com/verifyReceipt";
private static int CheckByUrl(string urlUse, string receipt, out string item_id, out string transaction_id)
{
item_id = "";
transaction_id = "";
try
{
//客户端base64了,这里不再需要
string base64receiptdata = receipt/*Base64Encode(receipt)*/;
JsonData requestJson = new JsonData(JsonType.Object);
requestJson["receipt-data"] = base64receiptdata;
if (string.IsNullOrEmpty(HttpProxyWorldServerUtils.GetServerConfig().iap_share_key) == false)
{
requestJson["password"] = HttpProxyWorldServerUtils.GetServerConfig().iap_share_key;//"10e033adc8584913a3b61ef0af1c9cfc";
}
string strRequest = requestJson.ToJson();
TraceLog.Debug("IAPReceiptCheck.CheckFromApple urlUse {0} ", urlUse);
HttpContent content = new StringContent(strRequest); ;
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
bool exception;
string strRet = HttpUtils.HttpPost(urlUse, content, out exception);
if (strRet == null)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple ,HttpPost return null query failed");
return -2;
}
TraceLog.Debug("IAPReceiptCheck.CheckFromApple result {0}", strRet);
JsonData result = JsonMapper.ToObject(strRet);
if (result == null)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple result not valid");
return -3;
}
JsonData status = result["status"];
if (status == null)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple result status error");
return -4;
}
int iStatus = -999;
bool bSuccess = int.TryParse(status.ToString(), out iStatus);
if(bSuccess == false)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple result status error 2");
return -4;
}
if(iStatus != 0)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple result status {0}", iStatus);
return iStatus;
}
JsonData r_receipt = result["receipt"];
if (r_receipt == null)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple result receipt error");
return -5;
}
JsonData r_bundle_id = r_receipt["bundle_id"];
if(r_bundle_id == null)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple result receipt bundle_id error");
return -6;
}
string config_bundle_id = HttpProxyWorldServerUtils.GetServerConfig().iap_bundle_id;
if(r_bundle_id.ToString() != config_bundle_id)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple result receipt bundle_id {0} != config {1}", r_bundle_id.ToString(), config_bundle_id);
return -7;
}
r_receipt = r_receipt["in_app"];
if (r_receipt == null)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple result in_app error");
return -8;
}
r_receipt = r_receipt[0];
if (r_receipt == null)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple result in_app error");
return -9;
}
//产品ID
JsonData r_product_id = r_receipt["product_id"];
JsonData r_transaction_id = r_receipt["transaction_id"];
if (r_product_id == null || r_transaction_id == null)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple result receipt product_id transaction_id error");
return -10;
}
item_id = r_product_id.ToString();
transaction_id = r_transaction_id.ToString();
return 0;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
return -999;
}
}
public static bool CheckFromApple(string receipt, out string item_id, out string transaction_id)
{
item_id = "";
transaction_id = "";
try
{
int result = -1;
result = CheckByUrl(urlProduction, receipt, out item_id, out transaction_id);
if (result == 21007
&& HttpProxyWorldServerUtils.GetServerConfig().iapCheckSandbox == 1)
{
TraceLog.Error("IAPReceiptCheck.CheckFromApple receipt is sandbox , so recheck use sandbox ");
result = CheckByUrl(urlSandbox, receipt, out item_id, out transaction_id);
}
if (result == 0)
{
TraceLog.Debug("IAPReceiptCheck.CheckFromApple receipt check success ");
return true;
}
TraceLog.Error("IAPReceiptCheck.CheckFromApple receipt check result {0} ", result);
return false;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
return false;
}
}
private static string Base64Encode(string str)
{
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(str);
return Convert.ToBase64String(encbuff);
}
}
}