苹果AppStore In-app purchase 内付费验证代码

当在app内支付购买后,appstore服务器不会主动通知app应用服务器购买的结果,此时需要我们主动向appstore请求查询订单信息状态,以处理购买后的相应发货逻辑等。


苹果响应数据实体类(带上getter,setter):

public class AppStoreBuyEntity {
	private String original_purchase_date_pst;
	private String purchase_date_ms;
	private String unique_identifier;
	private String original_transaction_id;
	private String bvrs;
	private String transaction_id;
	private String quantity;
	private String unique_vendor_identifier;
	private String item_id;
	private String product_id;
	private String purchase_date;
	private String original_purchase_date;
	private String purchase_date_pst;
	private String bid;
	private String original_purchase_date_ms;
	private int status;
}

苹果服务器验证订单代码:

/**
	 * 苹果服务器验证
	 * @param receipt 账单
	 */
	public static String buyAppVerify(String receipt) {
     //测试url
     private static final String url_verify = "https://sandbox.itunes.apple.com/verifyReceipt";
     //正式url
     //private static final String url_verify="https://buy.itunes.apple.com/verifyReceipt";
		BufferedOutputStream hurlBufOus = null;
		InputStream is = null;
		BufferedReader reader = null;
		HttpsURLConnection conn = null;
		try {
			SSLContext sc = SSLContext.getInstance("SSL");
			sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
					new java.security.SecureRandom());
			URL console = new URL(url_verify);
			conn = (HttpsURLConnection) console.openConnection();
			conn.setSSLSocketFactory(sc.getSocketFactory());
			conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
			conn.setRequestMethod("POST");
			conn.setRequestProperty("content-type", "text/json");
			conn.setRequestProperty("Proxy-Connection", "Keep-Alive");
			conn.setDoInput(true);
			conn.setDoOutput(true);
			hurlBufOus = new BufferedOutputStream(conn.getOutputStream());

			String str = String.format(Locale.CHINA, "{\"receipt-data\":\"" + receipt+ "\"}");
			hurlBufOus.write(str.getBytes());
			hurlBufOus.flush();

			is = conn.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is));
			String line = null;
			StringBuffer sb = new StringBuffer();
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}

			return sb.toString();
		} catch (Exception ex) {
			logger.error("-----> post to appstore error", ex);
		} finally {
			try {
				if(null != is) {
					is.close();
				}
				if(null != hurlBufOus) {
					hurlBufOus.close();
				}
				if(null != reader) {
					reader.close();
				}
			} catch (Exception e) {
				logger.error(e);
			}
		}
		return null;
	}

TEST:

public static void main(String[] args) {
		// 即receipt
		String buyCode = "";
		String result = AppStorePayVerify.buyAppVerify(buyCode);
		JSONObject job = JSONObject.fromObject(result);
       		String states=job.getString("status");
       		if(states.equals("0")) {
        		//验证成功 
		       	String json = job.getString("receipt");
        		System.out.println(json);
	        	try {
				AppStoreBuyEntity vo = (AppStoreBuyEntity) Util.decodeJson(json, AppStoreBuyEntity.class);
				System.out.println(vo.getBid());
				System.out.println(vo.getBvrs());
				System.out.println(vo.getItem_id());
				System.out.println(vo.getOriginal_purchase_date());
				System.out.println(vo.getOriginal_purchase_date_ms());
				System.out.println(vo.getOriginal_purchase_date_pst());
				System.out.println(vo.getOriginal_transaction_id());
				System.out.println(vo.getProduct_id());
				System.out.println(vo.getPurchase_date());
				System.out.println(vo.getPurchase_date_ms());
				System.out.println(vo.getPurchase_date_pst());
				System.out.println(vo.getQuantity());
				System.out.println(vo.getTransaction_id());
				System.out.println(vo.getUnique_identifier());
				System.out.println(vo.getUnique_vendor_identifier());
			} catch (Exception e) {
				e.printStackTrace();
			}// 反序列化
       		} else {
             		//账单无效 
        	}
	}





版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(内付费,purchase,in-app,苹果appstore,服务器验证)