这个估计是我踩过的最大的坑,当时做微信支付的时候也没这么坑爹,当然他俩也半斤八两。。。
苹果官方明确表示:验证支付时,可能会有一定的延迟。第一次处理的时间就专注的解决这个问题了,忽略了掉单的问题(稍后再说),让我多次更新支付代码才降低了掉单率。
常识:
1,返回状态码含义:
![](https://www.codeprj.com/image/aHR0cHM6Ly9pbWFnZXMyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTA0NDUwNS8yMDE4MDcvMTA0NDUwNS0yMDE4MDcxNzE3Mjg0NTA1My04ODk3NTg3OTcuanBn.png)
2、正常返回结果格式:
{
"environment": "Sandbox",
"receipt": {
"in_app": [
{
"transaction_id": "10000004111119001",
"original_purchase_date": "2018-07-06 03:16:41 Etc/GMT",
"quantity": "1",
"original_transaction_id": "1000000414619001",
"purchase_date_pst": "2018-07-05 20:16:41 America/Los_Angeles",
"original_purchase_date_ms": "1530847001000",
"purchase_date_ms": "1530847001000",
"product_id": "com.Beixxxxxxxxxon.fourc",
"original_purchase_date_pst": "2018-07-05 20:16:41 America/Los_Angeles",
"is_trial_period": "false",
"purchase_date": "2018-07-06 03:16:41 Etc/GMT"
}
],
"adam_id": 0,
"receipt_creation_date": "2018-07-06 03:16:41 Etc/GMT",
"original_application_version": "1.0",
"app_item_id": 0,
"original_purchase_date_ms": "1375340400000",
"request_date_ms": "1530847558058",
"original_purchase_date_pst": "2013-08-01 00:00:00 America/Los_Angeles",
"original_purchase_date": "2013-08-01 07:00:00 Etc/GMT",
"receipt_creation_date_pst": "2018-07-05 20:16:41 America/Los_Angeles",
"receipt_type": "ProductionSandbox",
"bundle_id": "com.jiaxxxxxmei.www.Gxxxxxxxrooms",
"receipt_creation_date_ms": "1530847001000",
"request_date": "2018-07-06 03:25:58 Etc/GMT",
"version_external_identifier": 0,
"request_date_pst": "2018-07-05 20:25:58 America/Los_Angeles",
"download_id": 0,
"application_version": "3"
},
"status": 0
}
View Code
3、凭证(此处提供的是测试环境的成功支付凭证)
MIIT8wYJKoZIhvcNAQc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V0aG9yaXR5AggO61eH554JjTAJBgUrDgMCGgUAMA0GCSqGSIb3DQEBAQUABIIBAADRfAmamDGPG8TWXjxk3sFojS040elvMyO1NG+CBhv0DNANi/7seUNYVXTPSB7xmw3TuFcUpfVrBJjAMv6/jbXicAXrqBkGvJMEP+zHGzNvSPtfzbH0sc3n7f3A9Ydd1xjFWAN5Z9avFp+dns7EAAKwi7d3uYw0ykXVl2PwZb/IXDOEF8fNmcS3NrwInc6/GjTBdKputy2zUjQFirrVm8Ofhza119ZfZrEipu05p12AwNFFBWBza3pMjGl43R5BAtAcZHtEm6wcrZAfXhHpzPVF5TO+1oZ9AbV8fdGaeeRCS7qZo9sv78aPe4dwjbWT/8nDbXauOp93ELiva45Fd5k=
View Code
4、测试|正式验证地址
//购买凭证验证地址 private static final String certificateUrl = "https://buy.itunes.apple.com/verifyReceipt"; //测试的购买凭证验证地址 private static final String certificateUrlTest = "https://sandbox.itunes.apple.com/verifyReceipt";
开始JAVA开发接口验证:
初步代码,第一版,实现正常的支付功能(其中充值金额最好从返回结果中获取,不过我是让前端传过来的):
/**
* 接收iOS端发过来的购买凭证
* @param
*/
@RequestMapping("/setIapCertificate")
@ResponseBody
public R setIapCertificate(HttpServletRequest req){
String certificateCode = req.getParameter("certificateCode");
String money = req.getParameter("money");//充值金额
String type = req.getParameter("type");//1、正式 2、测试
String url = null;
url = type.equals("1")?certificateUrl : certificateUrlTest; //
if(StringUtils.isNotEmpty(certificateCode)){
String r = sendHttpsCoon(url, certificateCode); //向苹果服务器发送凭证并接受结果
JSONObject params = JSONObject.parseObject(r);
System.out.println("*****************"+params);
String code = "";
String environment = "";
String transaction_id = "";
final String orderNum = DateUtil.getRechargeOrderNum();//根据当前时间生成订单号,yyMMddHHmmssSSS + 6位随机数
if(params != null) {
code = String.valueOf(params.get("status"));
if("0".equals(code)) {//成功
environment = String.valueOf(params.get("environment"));
String r_receipt = params.getString("receipt");
JSONObject returnJson = JSONObject.parseObject(r_receipt);
System.out.println("*****************"+returnJson);
String in_app = returnJson.getString("in_app");
if (in_app.endsWith("]")) {
JSONObject in_appJson = JSONObject.parseObject(in_app.substring(1, in_app.length()-1));
transaction_id = in_appJson.getString("transaction_id"); // 订单号
}
//保存充值记录
saveRecharge(orderNum, money, environment, transaction_id);
return R.ok();
}
}else{
return R.error();
}
}else{
return R.error();
}
}
View Code
后来发现大部分可以支付成功,但部分充值不成功,考虑可能是网络的问题,然后利用定时任务实现多次请求。若第一次请求结果不是成功状态则保存第一次请求的结果,并且每隔10秒请求一次,最多请求6次(一分钟)。
就有了优化后的第二版:
/**
* 接收iOS端发过来的购买凭证(方法已过时,被替代)
* @param
*/
@RequestMapping("/setIapCertificate1")
@ResponseBody
public R setIapCertificate1(HttpServletRequest req){
String certificateCode = req.getParameter("certificateCode");
String money = req.getParameter("money");//充值金额
String type = req.getParameter("type");//1、正式 2、测试
String url = null;
url = type.equals("1")?certificateUrl : certificateUrlTest; //
if(StringUtils.isNotEmpty(certificateCode)){
String r = sendHttpsCoon(url, certificateCode);
JSONObject params = JSONObject.parseObject(r);
System.out.println("*****************"+params);
String code = "";
String environment = "";
String transaction_id = "";
final String orderNum = DateUtil.getRechargeOrderNum();//根据当前时间生成订单号,yyMMddHHmmssSSS + 0~1000000随机数
if(params != null) {
code = String.valueOf(params.get("status"));
payError = new WfIospayErrorEntity();
payError.setCertificatecode(certificateCode);
payError.setMoney(money);
payError.setRebate(rebate);
payError.setResultcode(code);
payError.setType(type);
payError.setUpdatetime(new Date());
payError.setRemark(params.toString());
iospayErrorService.save(payError);
if("0".equals(code)) {//成功
environment = String.valueOf(params.get("environment"));
String r_receipt = params.getString("receipt");
JSONObject returnJson = JSONObject.parseObject(r_receipt);
System.out.println("*****************"+returnJson);
String in_app = returnJson.getString("in_app");
if (in_app.endsWith("]")) {
JSONObject in_appJson = JSONObject.parseObject(in_app.substring(1, in_app.length()-1));
transaction_id = in_appJson.getString("transaction_id"); // 订单号
}
//保存充值记录
saveRecharge(rebate, orderNum, uid, money, environment, transaction_id);
return R.ok();
}else{//失败
final String url1 = url;
final String certificateCode1 = certificateCode;
final String environment1 = environment;
final String transaction_id1 = transaction_id;
final String money1 = money;//充值金额
final Timer timer = new Timer();
timer.schedule(new TimerTask() {//若失败,则每10请求一次
@Override
public void run() {
if (map.get(certificateCode1) != null) {
map.put(certificateCode1, map.get(certificateCode1) + 1);
} else {
map.put(certificateCode1, 1);
}
if (map.get(certificateCode1) >= 60) {
map.remove(certificateCode1);
timer.cancel();
}
String r1 = sendHttpsCoon(url1, certificateCode1);
if (JSONObject.parseObject(r1).getString("status").equals("0")) {
saveRecharge( orderNum, money1, environment1, transaction_id1);//当成功时处理充值业务
map.remove(certificateCode1);
timer.cancel();
}
//System.out.println("设定指定任务task在指定延迟delay后执行");
}
},1000, 10000);
return R.error("68");
}
}else{//保存失败凭证
payError = new WfIospayErrorEntity();
payError.setCertificatecode(certificateCode);
payError.setMoney(money);
payError.setRebate(rebate);
payError.setResultcode(code);
payError.setType(type);
payError.setUpdatetime(new Date());
if(params != null){
payError.setRemark("params为空");
}else {
payError.setRemark(params.toString());
}
iospayErrorService.save(payError);
return R.error();
}
}else{
return R.error();
}
}
View Code
but,还是有掉单的问题,此时我崩溃了,想不明白怎么回事。直到我查了苹果返回的结果发现,根据一个凭证查出了两次支付的订单信息:
{
"environment": "Production",
"receipt": {
"in_app": [
{
"transaction_id": "470000347480489",
"original_purchase_date": "2018-07-13 06:01:00 Etc/GMT",
"quantity": "1",
"original_transaction_id": "470000347480489",
"purchase_date_pst": "2018-07-12 23:01:00 America/Los_Angeles",
"original_purchase_date_ms": "1531461660000",
"purchase_date_ms": "1531461660000",
"product_id": "com.Beijxxxxxxxxation.fourc",
"original_purchase_date_pst": "2018-07-12 23:01:00 America/Los_Angeles",
"is_trial_period": "false",
"purchase_date": "2018-07-13 06:01:00 Etc/GMT"
},
{
"transaction_id": "470000347479507",
"original_purchase_date": "2018-07-13 05:56:51 Etc/GMT",
"quantity": "1",
"original_transaction_id": "470000347479507",
"purchase_date_pst": "2018-07-12 22:56:51 America/Los_Angeles",
"original_purchase_date_ms": "1531461411000",
"purchase_date_ms": "1531461411000",
"product_id": "com.Beixxxxxxxxxxxcation.fourb",
"original_purchase_date_pst": "2018-07-12 22:56:51 America/Los_Angeles",
"is_trial_period": "false",
"purchase_date": "2018-07-13 05:56:51 Etc/GMT"
}
],
"adam_id": 1375992347,
"receipt_creation_date": "2018-07-13 06:01:00 Etc/GMT",
"original_application_version": "3",
"app_item_id": 1375992347,
"original_purchase_date_ms": "1531016326000",
"request_date_ms": "1531461663282",
"original_purchase_date_pst": "2018-07-07 19:18:46 America/Los_Angeles",
"original_purchase_date": "2018-07-08 02:18:46 Etc/GMT",
"receipt_creation_date_pst": "2018-07-12 23:01:00 America/Los_Angeles",
"receipt_type": "Production",
"bundle_id": "com.jxxxxxxxnmei.www.Goxxxxxxsrooms",
"receipt_creation_date_ms": "1531461660000",
"request_date": "2018-07-13 06:01:03 Etc/GMT",
"version_external_identifier": 827702473,
"request_date_pst": "2018-07-12 23:01:03 America/Los_Angeles",
"download_id": 87033802813938,
"application_version": "3"
},
"status": 0
}
View Code
知道原因后,想解决办法。读取苹果服务器返回的订单数组,循环充值。但这样的话,需要避免重复充值,所以现在需要用缓存给每次充值做个记录(我选择的redis),在充值之前判断苹果服务器返回的订单中有没有已经充过值的。
然后有了第三版:
/**
* 接收iOS端发过来的购买凭证
*
* @param
*/
@RequestMapping("/setIapCertificate")
@ResponseBody
public R setIapCertificate(HttpServletRequest req) {
final Integer uid = WebUtils.getUid(req);
String certificateCode = req.getParameter("certificateCode");
String money = req.getParameter("money");//充值金额
String type = req.getParameter("type");//1、正式 2、测试
String url = null;
url = type.equals("1") ? certificateUrl : certificateUrlTest; //
WfIospayErrorEntity payError = new WfIospayErrorEntity();
payError.setCertificatecode(certificateCode);
payError.setMoney(money);
payError.setRebate(rebate);
payError.setResultcode("110");
payError.setType(type);
payError.setUid(String.valueOf(uid));
payError.setUpdatetime(new Date());
payError.setRemark("params为空");
iospayErrorService.save(payError);
if (StringUtils.isNotEmpty(certificateCode)) {
String r = sendHttpsCoon(url, certificateCode);
JSONObject params = JSONObject.parseObject(r);
System.out.println("*****************" + params);
String code = "";
String environment = "";
String transaction_id = "";
final String orderNum = DateUtil.getRechargeOrderNum();//根据当前时间生成订单号,yyMMddHHmmssSSS + 0~1000000随机数
if (params != null) {
code = String.valueOf(params.get("status"));
payError = new WfIospayErrorEntity();
payError.setCertificatecode(certificateCode);
payError.setMoney(money);
payError.setRebate(rebate);
payError.setResultcode(code);
payError.setType(type);
payError.setUid(String.valueOf(uid));
payError.setUpdatetime(new Date());
payError.setRemark(params.toString());
iospayErrorService.save(payError);
if ("0".equals(code)) {//成功
environment = String.valueOf(params.get("environment"));
String r_receipt = params.getString("receipt");
JSONObject returnJson = JSONObject.parseObject(r_receipt);
System.out.println("*****************" + returnJson);
//String in_app = returnJson.getString("in_app");
JSONArray jsonArray = (JSONArray) returnJson.get("in_app");// 获取返回结果中的订单列表
JSONObject targetOrder = null;
String product_id = null;
for (int i = 0; i < jsonArray.size(); i++) {
targetOrder = jsonArray.getJSONObject(i);//获取订单信息对象
product_id = targetOrder.getString("product_id");//获取产品信息
transaction_id = targetOrder.getString("transaction_id");// transaction_id交易号
if (!ISsoLoginHelper.confirmPay(uid, transaction_id)) {//redis验证订单是否已经充值
//保存充值记录
ISsoLoginHelper.savePay(uid, transaction_id, money);//用redis保存记录
//进行充值
saveRecharge(rebate, orderNum, uid, money, environment, transaction_id);
}
}
return R.ok();
} else {//失败
final String url1 = url;
final String certificateCode1 = certificateCode;
final String environment1 = environment;
final String transaction_id1 = transaction_id;
final String money1 = money;//充值金额
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (map.get(certificateCode1) != null) {
map.put(certificateCode1, map.get(certificateCode1) + 1);
} else {
map.put(certificateCode1, 1);
}
if (map.get(certificateCode1) >= 60) {
map.remove(certificateCode1);
timer.cancel();
}
String r1 = sendHttpsCoon(url1, certificateCode1);
if (JSONObject.parseObject(r1).getString("status").equals("0")) {
if (!ISsoLoginHelper.confirmPay(uid, transaction_id1)) {//redis验证订单是否已经充值
//保存充值记录
ISsoLoginHelper.savePay(uid, transaction_id1, money1);
//进行充值
saveRecharge(rebate, orderNum, uid, money1, environment1, transaction_id1);
}
map.remove(certificateCode1);
timer.cancel();
}
//System.out.println("设定指定任务task在指定延迟delay后执行");
}
}, 1000, 10000);
return R.error("68");
}
} else {
payError = new WfIospayErrorEntity();
payError.setCertificatecode(certificateCode);
payError.setMoney(money);
payError.setRebate(rebate);
payError.setResultcode("111");
payError.setType(type);
payError.setUid(String.valueOf(uid));
payError.setUpdatetime(new Date());
payError.setRemark("params再次为空");
iospayErrorService.save(payError);
return R.error();
}
} else {
return R.error();
}
}
View Code
其中涉及的工具或方法:
发送请求:
/**
* 重写X509TrustManager
*/
private static TrustManager myX509TrustManager = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
};
/**
* 发送请求
* @param url
* @param
* @return
*/
private String sendHttpsCoon(String url, String code){
if(url.isEmpty()){
return null;
}
try {
//设置SSLContext
SSLContext ssl = SSLContext.getInstance("SSL");
ssl.init(null, new TrustManager[]{myX509TrustManager}, null);
//打开连接
HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
//设置套接工厂
conn.setSSLSocketFactory(ssl.getSocketFactory());
//加入数据
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-type","application/json");
JSONObject obj = new JSONObject();
obj.put("receipt-data", code);
BufferedOutputStream buffOutStr = new BufferedOutputStream(conn.getOutputStream());
buffOutStr.write(obj.toString().getBytes());
buffOutStr.flush();
buffOutStr.close();
//获取输入流
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
StringBuffer sb = new StringBuffer();
while((line = reader.readLine())!= null){
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
return null;
}
}
View Code
redis保存|判断充值记录(安装配置我就不多说了,不懂的请自行百度)
/**
* 保存ios订单支付信息
*
* @param userid
* @return
*/
public static void savePay(Integer userid,String transaction_id,String money) {
Jedis jedis = myJedisPool.getResource();
try {
jedis.setnx(userid +"_"+ transaction_id , money);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally {
jedis.close();
}
}
/**
* 判断订单是否已充值成功
*
* @param userid
* @return
*/
public static boolean confirmPay(Integer userid,String transaction_id) {
Jedis jedis = myJedisPool.getResource();
boolean b = false;
try {
b = jedis.exists(userid +"_"+ transaction_id);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally {
jedis.close();
}
return b;
}
View Code
|