苹果支付验单java

苹果也需要请求苹果服务器去验单。前端会带回支付信息,包括加密数据

// 0 校验成功
	// 21000 App Store不能读取你提供的JSON对象
	// 21002 receipt-data域的数据有问题
	// 21003 receipt无法通过验证
	// 21004 提供的shared secret不匹配你账号中的shared secret
	// 21005 receipt服务器当前不可用
	// 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
	// 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
	// 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
	private String validate(GameCallbackInfo info, boolean isSand) {
		String receipt = info.getIosInfo().getReceiptData();
		String url = null;
		if (isSand) {
			url = appleSandboxUrl;
		} else {
			url = appleValidateUrl;
		}
		// 发送的数据
		String receiptJson = "{\"receipt-data\":\"" + receipt + "\"}";
		// 执行三次
		int flag = 0;
		String transaction_id = null;
		String product_id = null;
		while (flag++ < 3) {
			String callbackInfo=null;
			try {
				 callbackInfo= DoPostUtils.doPost2(url,receiptJson);
			}catch (Exception e){

			}
			//			String 	callbackInfo=HttpUtils.postJSON(url, receiptJson);
				/*如果出现连接断开把订单记录*/
			try {
				if (null==callbackInfo){
					String sql="INSERT INTO pay_erro( platformOrder, paymoney, rmoney, errmsg, code, creatime) VALUES (?, ?, ?, ?, '2', NOW())";
					dao.update(sql,new Object[]{info.getPlatformOrder(),"","","连接苹果出现异常,可进行查单补发"});}
				}catch (Exception e){
					e.printStackTrace();
				}

			JSONObject obj = JSONUtils.parseObject(callbackInfo);
			if (null != obj) {
				int status = (int) obj.get("status");
				flag = 3;
				if (status == 0) {
					JSONObject content = obj.getJSONObject("receipt");
					System.out.println("苹果返回"+obj);
					// 返回是一个商品列表
					JSONArray arr = content.getJSONArray("in_app");
					for (int i = 0; i <arr.size() ; i++) {
						content = arr.getJSONObject(i);
						transaction_id = content.getString("transaction_id");
						if(info.getIosInfo().getTransactionId().equals(transaction_id)){
							product_id = content.getString("product_id");
							break;
						}
					}

					break;
				}
				log.error("支付错误码:" + status);
				break;
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		// 商品ID
		if(null == product_id){
			log.error("商品Id为空"+info.getPlatformOrder());

			try {
				String sql="INSERT INTO pay_erro( platformOrder, paymoney, rmoney, errmsg, code, creatime) VALUES (?, ?, ?, ?, '0', NOW())";
                dao.update(sql,new Object[]{info.getPlatformOrder(),"","","商品Id为空"});
			}catch (Exception e){
				e.printStackTrace();
			}
			return null;
		}
		Game game = gamePropertiesService.getGame(info.getGameId());
		Map<String, Integer> products = game.getIos().getProducts();
		Integer money = products.get(product_id);
		if(null == money){
			System.out.println("订单号[%s]不存在的商品ID[%s]"+info.getPlatformOrder()+product_id);
			log.error("订单号[%s]不存在的商品ID[%s]"+info.getPlatformOrder()+product_id);
			try {
				String sql="INSERT INTO pay_erro( platformOrder, paymoney, rmoney, errmsg, code, creatime) VALUES (?, ?, ?, ?, '0', NOW())";
				dao.update(sql,new Object[]{info.getPlatformOrder(),"","","订单号不存在的商品ID"});
			}catch (Exception e){
				e.printStackTrace();
			}
			return null;
		}
		// 单位 分
		if(money != info.getMoney()){
			log.error("价格不一致"+info.getPlatformOrder()+"商品Id"+product_id+"苹果支付金额"+money+"平台支付金额"+info.getMoney());
			try {
				String sql="INSERT INTO pay_erro( platformOrder, paymoney, rmoney, errmsg, code, creatime) VALUES (?, ?, ?, ?, '0', NOW())";
				dao.update(sql,new Object[]{info.getPlatformOrder(),info.getMoney(),money,"价格不一致"});
			}catch (Exception e){
				e.printStackTrace();
			}
			return null;
		}
		
		return transaction_id;
	}

需要注意点。苹果刷单严重,所以苹果订单,自己的平台需要验证唯一性

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tiny(泰尼)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值