苹果也需要请求苹果服务器去验单。前端会带回支付信息,包括加密数据
// 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;
}
需要注意点。苹果刷单严重,所以苹果订单,自己的平台需要验证唯一性