1、配置苹果内购项目
https://help.apple.com/app-store-connect/#/devb57be10e7
-
APP store 配置APP项目
-
项目下添加内购项目
-
添加沙盒测试账号
2、返回数据 前端调起苹果支付
log.info("=========================苹果支付-添加交易记录=======================");
//根据自己的业务 生成订单
insertPayRecord(orderSn, appUser.getAppUserId(), appUser.getNickName(), PayRecordPayType.PAY_TYPE3.getCode(),
payMode, PayRecordPayStatus.PAY_STATUS1.getCode(),
money, null, null);
//将苹果配置的productId传给前端
String productId = "";
switch(buyPhotoAlbumDTO.getAlbumNumber()){
case 1:
productId = AppleProductId.BUY_ALBUM_1.getProductId();
break;
case 2:
productId = AppleProductId.BUY_ALBUM_2.getProductId();
break;
}
//*****可以自行添加参数*****
ApplePayVO applePayVO = new ApplePayVO();
applePayVO.setOrderSn(orderSn); //添加订单id
applePayVO.setProductid(productId);
return Result.ok(applePayVO);
3、苹果支付回调
前端根据productId调用苹果支付,支付成功后前端得到payload,前端将订单id和payload作为入参调用苹果支付回调接口。
payload:
{
"status":0,
"environment":"Sandbox",
"payment":{
"username":"admin"
},
"receipt":{
"receipt_type":"ProductionSandbox",
"adam_id":0,
"app_item_id":0,
"bundle_id":"com.jiuying.twelveAnimal",
"application_version":"0",
"download_id":0,
"version_external_identifier":0,
"receipt_creation_date":"2018-01-05 10:06:12 Etc/GMT",
"receipt_creation_date_ms":"1515146772000",
"receipt_creation_date_pst":"2018-01-05 02:06:12 America/Los_Angeles",
"request_date":"2018-01-05 10:06:14 Etc/GMT",
"request_date_ms":"1515146774645",
"request_date_pst":"2018-01-05 02:06:14 America/Los_Angeles",
"original_purchase_date":"2013-08-01 07:00:00 Etc/GMT",
"original_purchase_date_ms":"1375340400000",
"original_purchase_date_pst":"2013-08-01 00:00:00 America/Los_Angeles",
"original_application_version":"1.0",
"in_app":[
{
"quantity":"1",
"product_id":"com.jiuying.twelveAnimal.6",
"transaction_id":"1000000364151484",
"original_transaction_id":"1000000364151484",
"purchase_date":"2018-01-05 10:06:11 Etc/GMT",
"purchase_date_ms":"1515146771000",
"purchase_date_pst":"2018-01-05 02:06:11 America/Los_Angeles",
"original_purchase_date":"2018-01-05 10:06:11 Etc/GMT",
"original_purchase_date_ms":"1515146771000",
"original_purchase_date_pst":"2018-01-05 02:06:11 America/Los_Angeles",
"is_trial_period":"false"
}
]
}
}
前端将订单id和payload作为入参调用苹果支付回调接口
回调接口
public Result applePayOrderNotifyCallBack(String orderSn, String payload) {
try {
// 支付凭证
JSONObject job1 = JSONObject.parseObject(payload);
String receipt = job1.getString("transactionReceipt");
// 读取自己添加的参数 订单号orderSn
String payment = job1.getString("payment");
JSONObject paymentJob = JSONObject.parseObject(payment);
String transaction_id = paymentJob.getString("orderSn");
String verifyResult = IosVerifyUtil.buyAppVerify(receipt, 1); //1.先线上测试 发送平台验证
if (verifyResult == null) { // 苹果服务器没有返回验证结果
log.info("无订单信息!");
return Result.error(ServiceErrCode.ORDER_NOT_FOUND);
} else { // 苹果验证有返回结果
log.info("线上,苹果平台返回JSON:" + verifyResult);
JSONObject job = JSONObject.parseObject(verifyResult);
String states = job.getString("status");
//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环境的验证服务
if("21007".equals(states)){ //是沙盒环境,应沙盒测试,否则执行下面
verifyResult = IosVerifyUtil.buyAppVerify(receipt, 0); //2.再沙盒测试 发送平台验证
log.info("沙盒环境,苹果平台返回JSON:" + verifyResult);
job = JSONObject.parseObject(verifyResult);
states = job.getString("status");
}
log.info("苹果平台返回值:job" + job);
if (states.equals("0")){ // 前端所提供的收据是有效的 验证成功
String r_receipt = job.getString("receipt");
JSONObject returnJson = JSONObject.parseObject(r_receipt);
String product_id = returnJson.getString("product_id");
// String transaction_id = returnJson.getString("transaction_id"); // 订单号
/*****************************************自己的业务逻辑开始*********************************************/
//如果单号一致 则开始处理逻辑
if(orderSn.equals(transaction_id)){
String[] moneys = product_id.split("\\."); //实际支付金额
//此处开始业务逻辑
payNotifyUpdateOrder(orderSn); //回调成功 处理订单
return Result.ok("操作成功");
}
return Result.error(ServiceErrCode.RECEIPT_ERROR);
/*************************************自己的业务逻辑结束***************************************************/
} else {
return Result.error(ServiceErrCode.RECEIPT_ERROR);
}
}
} catch (Exception e) {
log.error("苹果支付回调失败");
}
return Result.ok("操作成功");
}