码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-03-03 18:41:42      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:苹果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 {
             		//账单无效 
        	}
	}





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

标签:苹果appstore   in-app purchase   内付费   服务器验证   

原文地址:http://blog.csdn.net/hjun01/article/details/44039939

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!