/// <summary>
/// IOS内购支付
/// </summary>
public class ApplePay
{
/// <summary>
/// 苹果验证支付
/// </summary>
/// <param name="appleReceipt">苹果内购的验证收据</param>
/// <param name="orderCode">订单号</param>
/// <returns></returns>
public string ValidateApplePay(string appleReceipt, string orderCode)
{
try
{
//客户端post过来的参数
//string appleReceipt = HttpContext.Current.Request.Form["appleReceipt"]; //苹果内购的验证收据
//string orderCode = HttpContext.Current.Request.Form["orderCode"]; //订单号
// 验证参数
if (appleReceipt.Length < 20)
{
return "false";
}
string strJosn = string.Format("{{\"receipt-data\":\"{0}\"}}", appleReceipt);
// 请求验证
string strResult = CreatePostHttpResponse(strJosn, System.Convert.ToBoolean(ConfigurationManager.AppSettings["PayEnvironment"]));
JObject obj = JObject.Parse(strResult);//using Newtonsoft.Json.Linq;
// 判断是否购买成功
#region 苹果内购服务器验证码
if (obj["status"].ToString() == "0")
{
return "true";
}
else if (obj["status"].ToString() == "1")
{
return "凭据不在白名单之内";
}
else if (obj["status"].ToString() == "2")
{
return "伪造充值";
}
else if (obj["status"].ToString() == "3")
{
return "凭据不存在伪造充值";
}
else if (obj["status"].ToString() == "4")
{
return "支付时间过期";
}
else if (obj["status"].ToString() == "6")
{
return "非法凭据";
}
else if (obj["status"].ToString() == "21000")
{
return "App Store无法读取你提供的JSON数据";
}
else if (obj["status"].ToString() == "21002")
{
return "收据数据不符合格式";
}
else if (obj["status"].ToString() == "21003")
{
return "收据无法被验证";
}
else if (obj["status"].ToString() == "21004")
{
return "你提供的共享密钥和账户的共享密钥不一致";
}
else if (obj["status"].ToString() == "21005")
{
return "收据服务器当前不可用";
}
else if (obj["status"].ToString() == "21006")
{
return "收据是有效的,但订阅服务已经过期。当收到这个信息时,解码后的收据信息也包含在返回内容中";
}
else if (obj["status"].ToString() == "21007")
{
return "收据信息是测试用(sandbox),但却被发送到产品环境中验证";
}
else if (obj["status"].ToString() == "21008")
{
return "收据信息是产品环境中使用,但却被发送到测试环境中验证";
}
else
{
return "false";
}
#endregion
}
catch (System.Exception)
{
return null;
}
}
/// <summary>
/// 请求苹果支付
/// </summary>
/// <param name="datas">strJosn 支付单据</param>
/// <param name="isUrl">是否开启正式模式(默认false)</param>
/// <returns></returns>
public string CreatePostHttpResponse(string datas, bool isUrl = false)
{
try
{
//正式购买地址 沙盒购买地址
string url_buy = "https://buy.itunes.apple.com/verifyReceipt";
string url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
string url = isUrl == false ? url_sandbox : url_buy;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(datas.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream(); //获取响应的字符串流
StreamReader sr = new StreamReader(responseStream); //创建一个stream读取流
var str = sr.ReadToEnd();
sr.Close();
responseStream.Close();
return str.ToString();
}
catch (System.Exception)
{
return string.Empty;
}
}
}
.NET C# IOS内购支付功能
于 2021-03-23 16:08:27 首次发布