package com.xx.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Locale;
import javapns.json.JSONException;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import com.ishehui.output.IphoneReceipt;
public class PhoneUtil {
/**
* 处理号码成标准的号码
* @param phone
* @return
*/
public static String process(String phone) {
return phone;
}
/****
* app store 测试服务器
*/
private static String SandboxUrl = "https://sandbox.itunes.apple.com/verifyReceipt";
/****
* app store 线上服务器
*/
private static String url = "https://buy.itunes.apple.com/verifyReceipt";
//请求app store 服务器进行 订单验证
public static String iapcheck(String receipt,String url){
String result = "";// 返回的结果
if(receipt != null){
try {
URL dataUrl = new URL(url);
HttpURLConnection con = (HttpURLConnection) dataUrl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("content-type", "text/json");
con.setRequestProperty("Proxy-Connection", "Keep-Alive");
//con.setRequestProperty("receipt-data", receipt);
con.setDoOutput(true);
con.setDoInput(true);
OutputStreamWriter out =new OutputStreamWriter(con.getOutputStream());
String str= String.format(Locale.CHINA,"{\"receipt-data\":\"" + receipt+"\"}");
out.write(str);
out.flush();
out.close();
InputStream is = con.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
String line = null;
while((line = reader.readLine()) != null){
result += line+"\r\n";
}
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
//支付凭证验证 入口
public static IphoneReceipt getCheck(String receipt){
IphoneReceipt ir = openCheckJosn(receipt,url,1);
return ir;
}
//解析 出订单信息
public static IphoneReceipt openCheckJosn(String receipt,String url,int from){//from 1:正常环境 2:沙盒环境
JSONObject responseJson2 = (JSONObject) JSONValue.parse(iapcheck(receipt,url));
Long status = (Long) responseJson2.get("status");
IphoneReceipt ir = null;
if(status.intValue() == 0){
JSONObject receipt_data = (JSONObject) responseJson2.get("receipt");
String product_id = (String) receipt_data.get("product_id");
String transaction_id = (String) receipt_data.get("transaction_id");
String app_item_id = (String) receipt_data.get("app_item_id");
ir = new IphoneReceipt();
ir.setTransaction_id(transaction_id);
ir.setProduct_id(product_id);
ir.setApp_item_id(app_item_id);
ir.setFrom(from);
return ir;
}else if(status.intValue() == 21007){
return openCheckJosn(receipt, SandboxUrl,2);
}
return null;
}
//得到客户端上传上来的支付凭证
public static String getReceipt(String requesJson) {
String s = "";
javapns.json.JSONObject j;
try {
j = new javapns.json.JSONObject(requesJson);
s = j.get("receipt-data").toString();
} catch (JSONException e) {
e.printStackTrace();
}
return s;
}
public static void main(String[] args) throws JSONException {
System.out.println(getCheck("这个凭证有好长。长。长..."));
}
}
<pre name="code" class="java">package com.xx.output;
import org.apache.commons.lang3.StringUtils;
public class IphoneReceipt {
private String product_id="";
private String transaction_id="";
private String app_item_id="ios";
// private int status; //0:成功 其余失败
private int from;//1:正常环境 2:沙盒环境
public String getTransaction_id() {
return transaction_id;
}
public void setTransaction_id(String transaction_id) {
this.transaction_id = transaction_id;
}
public String getProduct_id() {
return product_id;
}
public void setProduct_id(String product_id) {
this.product_id = product_id;
}
public String getApp_item_id() {
return app_item_id;
}
public void setApp_item_id(String app_item_id) {
if(!StringUtils.isBlank(app_item_id)){
this.app_item_id = app_item_id;
}
}
public IphoneReceipt() {
super();
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
}