服务端判断苹果内容是否支付完成(二)
2016-03-31 09:30
369 查看
1、 从Transaction 的TransactionReceipt属性中得到接收的数据,并以base64编码;
2、创建JSON对象,字典格式,单键值对,键名为“receiptdata”,值为上一次编码的数据,效果:
{"receipt-data":"base64编码之后的数据"}
3、发送HTTP POST请求,将数据发送到App Store ,其地址为:https://buy.itunes.apple.com/verifyReceipt
4、App Store的返回值也是一个JSON格式对象,包括两个键值对:
{
"status" : 0,
"receipt" : { … }
}
说明:
如果status的值为0,就说明该receipt为有效的,否则就是无效的。
[java] view
plain copy
public int verifyReceipt( byte[] receipt) {
int status = -1;
//This is the URL of the REST webservice in iTunes App Store
URL url = new URL("https://buy.itunes.apple.com/verifyReceipt");
//make connection, use post mode
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setAllowUserInteraction(false);
//Encode the binary receipt data into Base 64
//Here I'm using org.apache.commons.codec.binary.Base64 as an encoder, since commons-codec is already in Grails classpath
Base64 encoder = new Base64();
String encodedReceipt = new String(encoder.encode(receipt));
//Create a JSON query object
//Here I'm using Grails' org.codehaus.groovy.grails.web.json.JSONObject
Map map = new HashMap();
map.put("receipt-data", encodedReceipt);
JSONObject jsonObject = new JSONObject(map);
//Write the JSON query object to the connection output stream
PrintStream ps = new PrintStream(connection.getOutputStream());
ps.print(jsonObject.toString());
ps.close();
//Call the service
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//Extract response
String str;
StringBuffer sb = new StringBuffer();
while ((str = br.readLine()) != null) {
sb.append(str);
sb.append("/n");
}
br.close();
String response = sb.toString();
//Deserialize response
JSONObject result = new JSONObject(response);
status = result.getInt("status");
if (status == 0) {
//provide content
} else {
//signal error, throw an exception, do your stuff honey!
}
return status ;
}
验证的方式也可以采用HTTP协议:HttpClient,下载httpcomponents-client,然后导入lib里的几个jar包。
这也是一种方式,不再进行多余的说明了。所谓条条道路通“苹果”。
2、创建JSON对象,字典格式,单键值对,键名为“receiptdata”,值为上一次编码的数据,效果:
{"receipt-data":"base64编码之后的数据"}
3、发送HTTP POST请求,将数据发送到App Store ,其地址为:https://buy.itunes.apple.com/verifyReceipt
4、App Store的返回值也是一个JSON格式对象,包括两个键值对:
{
"status" : 0,
"receipt" : { … }
}
说明:
如果status的值为0,就说明该receipt为有效的,否则就是无效的。
[java] view
plain copy
public int verifyReceipt( byte[] receipt) {
int status = -1;
//This is the URL of the REST webservice in iTunes App Store
URL url = new URL("https://buy.itunes.apple.com/verifyReceipt");
//make connection, use post mode
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setAllowUserInteraction(false);
//Encode the binary receipt data into Base 64
//Here I'm using org.apache.commons.codec.binary.Base64 as an encoder, since commons-codec is already in Grails classpath
Base64 encoder = new Base64();
String encodedReceipt = new String(encoder.encode(receipt));
//Create a JSON query object
//Here I'm using Grails' org.codehaus.groovy.grails.web.json.JSONObject
Map map = new HashMap();
map.put("receipt-data", encodedReceipt);
JSONObject jsonObject = new JSONObject(map);
//Write the JSON query object to the connection output stream
PrintStream ps = new PrintStream(connection.getOutputStream());
ps.print(jsonObject.toString());
ps.close();
//Call the service
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//Extract response
String str;
StringBuffer sb = new StringBuffer();
while ((str = br.readLine()) != null) {
sb.append(str);
sb.append("/n");
}
br.close();
String response = sb.toString();
//Deserialize response
JSONObject result = new JSONObject(response);
status = result.getInt("status");
if (status == 0) {
//provide content
} else {
//signal error, throw an exception, do your stuff honey!
}
return status ;
}
验证的方式也可以采用HTTP协议:HttpClient,下载httpcomponents-client,然后导入lib里的几个jar包。
这也是一种方式,不再进行多余的说明了。所谓条条道路通“苹果”。
相关文章推荐
- IOS编程学习:property、nonatomic, retain,weak,strong用法
- IOS编程学习:property、nonatomic, retain,weak,strong用法
- va_start和va_end使用详解
- 面试互联网企业Android开发工程师的一些面试题
- .NET 中的 async/await 异步编程
- Java内存区域和内存溢出异常(深入理解Java虚拟机)
- EditText常用属性【二】:为文本输入框指定软键盘类型和软键盘回车键图标设置
- webview的简单总结(最近面试遇到好多这方面的问题)
- ios9中 UIStackView的使用
- jQuery Validate 插件验证,,返回不同信息(json remote)自定义
- 进程间通信方式与Binder机制原理
- [POJ 3411]Paid Roads[BFS][状压]
- startActivityForResult用法详解
- 最详细的Codis集群扩容方法
- JavaScript位移运算符(无符号) >>> 三个大于号 的使用方法详解
- java 集合 collection内容总结
- java学习笔记(2)
- Mac OSX 的字体渲染
- poj3252 Round Numbers
- SAS Options