C# WebAPI完成iOS支付验证
后端使用C# WebApi框架,用户支付完成后请求API地址对支付结果进行后端检验并完成相关数据的修改。
API 方法
// API Post 请求
public string Post(IOSplay iosplay)
{
// 连接数据库
SqlConnection sqlConnection = new SqlConnection(@"");
try
{
// 入参校验
if (iosplay.transactionReceipt != "" && iosplay.productid != "" && iosplay.userid != "")
{
// IOS支付检验参数
string strJosn = string.Format("{{\"receipt-data\":\"{0}\",\"password\":\"\"}}", iosplay.transactionReceipt);
// 请求iOS检验地址
string fsjisa = CreatePostHttpResponse(strJosn);
JObject obj = JObject.Parse(fsjisa);
// 判断是否购买成功
if (obj["status"].ToString() == "0")
{
// 支付成功 进行数据库操作
sqlConnection.Open();
string sqlxz = "insert into iosplay() values()";
DataSet dataSetq = new DataSet();
SqlDataAdapter sqlDataAdapterq = new SqlDataAdapter(sqlxz, sqlConnection);
sqlDataAdapterq.Fill(dataSetq);
sqlConnection.Close();
return "success";
}
else
{
// 支付验证失败,数据库最好也进行一下记录
sqlConnection.Open();
string sqlxz2 = "insert into iosplay() values()";
DataSet dataSetq2 = new DataSet();
SqlDataAdapter sqlDataAdapterq2 = new SqlDataAdapter(sqlxz2, sqlConnection);
sqlDataAdapterq2.Fill(dataSetq2);
sqlConnection.Close();
return "IOS订单验证失败";
}
}
else
{
// 入参错误
sqlConnection.Open();
string sqlxz3 = "insert into iosplay(sdduihuan) values(@duihshif)";
DataSet dataSetq3 = new DataSet();
SqlDataAdapter sqlDataAdapterq3 = new SqlDataAdapter(sqlxz3, sqlConnection);
sqlDataAdapterq3.SelectCommand.Parameters.AddWithValue("@duihshif", "请求参数错误");
sqlDataAdapterq3.Fill(dataSetq3);
sqlConnection.Close();
return "请求参数错误";
}
}
catch (Exception ex)
{
// 处理异常
sqlConnection.Open();
string sqlxz4 = "insert into iosplay(sdduihuan) values(@duihshif)";
DataSet dataSetq4 = new DataSet();
SqlDataAdapter sqlDataAdapterq4 = new SqlDataAdapter(sqlxz4, sqlConnection);
sqlDataAdapterq4.SelectCommand.Parameters.AddWithValue("@duihshif", ex.ToString());
sqlDataAdapterq4.Fill(dataSetq4);
sqlConnection.Close();
return "error"+ ex.ToString();
}
finally
{
sqlConnection.Close();
}
}
CreatePostHttpResponse(请求IOS验证地址)
public string CreatePostHttpResponse(string datas, bool isSandbox = false)
{
string url_buy = "https://buy.itunes.apple.com/verifyReceipt"; // 正式购买地址
string url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt"; // 沙盒购买地址
string url = isSandbox ? 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();
}
苹果会返回的数据
{
"receipt":
{
"original_purchase_date_pst":"2015-06-22 20:56:34 America/Los_Angeles", //购买时间,太平洋标准时间
"purchase_date_ms":"1435031794826", //购买时间毫秒
"unique_identifier":"5bcc5503dbcc886d10d09bef079dc9ab08ac11bb",//唯一标识符
"original_transaction_id":"1000000160390314", //原始交易ID
"bvrs":"1.0",//iPhone程序的版本号
"transaction_id":"1000000160390314", //交易的标识
"quantity":"1", //购买商品的数量
"unique_vendor_identifier":"AEEC55C0-FA41-426A-B9FC-324128342652", //开发商交易ID
"item_id":"1008526677",//App Store用来标识程序的字符串
"product_id":"cosmosbox.strikehero.gems60",//商品的标识
"purchase_date":"2015-06-23 03:56:34 Etc/GMT",//购买时间
"original_purchase_date":"2015-06-23 03:56:34 Etc/GMT", //原始购买时间
"purchase_date_pst":"2015-06-22 20:56:34 America/Los_Angeles",//太平洋标准时间
"bid":"com.cosmosbox.StrikeHero",//iPhone程序的bundle标识
"original_purchase_date_ms":"1435031794826"//毫秒
},
"status":0 //状态码,0为成功
}
苹果会反馈的状态码;
21000App Store无法读取你提供的JSON数据
21002 收据数据不符合格式
21003 收据无法被验证
21004 你提供的共享密钥和账户的共享密钥不一致
21005 收据服务器当前不可用
21006 收据是有效的,但订阅服务已经过期。当收到这个信息时,解码后的收据信息也包含在返回内容中
21007 收据信息是测试用(sandbox),但却被发送到产品环境中验证
21008 收据信息是产品环境中使用,但却被发送到测试环境中验证