支付流程,APP支付成功后 前端调用后端接口,后端接口将前端支付成功后拿到的凭据传给苹果服务器检查,如果接口返回成功了,就视为支付。
代码,productId就是苹果开发者后台提前设置好的 产品id
public CommonResult<String> appleRecharge(AppleRechargeVo request) {String receipt = request.getReceipt();String orderId = request.getOrderId();log.info("receipt -- {}", receipt);log.info("orderId -- {}", orderId);// 构造 Apple 验证请求体JSONObject body = new JSONObject();body.put("receipt-data", receipt);// body.put("password", "你的共享密钥(可选,用于自动订阅)");// 先请求正式环境String response = HttpUtil.post("https://buy.itunes.apple.com/verifyReceipt", body.toJSONString());JSONObject jsonResponse = JSONObject.parseObject(response);// 状态码为21007时说明是沙盒订单if ("21007".equals(jsonResponse.getString("status"))) {response = HttpUtil.post("https://sandbox.itunes.apple.com/verifyReceipt", body.toJSONString());jsonResponse = JSONObject.parseObject(response);}log.info("APPLE 接口返回值 -- {}", response);// 根据验证状态处理逻辑if (0 == jsonResponse.getInteger("status")) {// log.info("状态成功!!!");// 验证成功,做后续订单处理// 取出 in_app 数组JSONArray inAppArray = jsonResponse.getJSONObject("receipt").getJSONArray("in_app");// log.info("inAppArray !!!{}", inAppArray);if (inAppArray != null && !inAppArray.isEmpty()) {JSONObject firstPurchase = inAppArray.getJSONObject(0);String productId = firstPurchase.getString("product_id");String transactionId = firstPurchase.getString("transaction_id");log.info("productId !!!{}", productId);log.info("transactionId !!!{}", transactionId);BigDecimal amount = null;switch (productId) {case "xxxx":amount = new BigDecimal("7");break;case "xxxxx":amount = new BigDecimal("70");break;case "xxxxx":amount = new BigDecimal("140");break;case "sdsad":amount = new BigDecimal("350");break;case "132":amount = new BigDecimal("700");break;case "sdsds":amount = new BigDecimal("1400");break;}// 加余额if (amount != null) {Long userId = LoginUtil.getUserId();if (userId != null) {// 加余额userMapper.update(null, Wrappers.<User>lambdaUpdate().setSql("amount = amount +" + amount).eq(User::getId, userId));// 增加充值记录UserWalletRecord userWalletRecord = new UserWalletRecord();userWalletRecord.setUserId(userId);userWalletRecord.setTitle("APP充值");userWalletRecord.setAmount(amount);userWalletRecord.setType("1");userWalletRecordMapper.insert(userWalletRecord);}}}return CommonResult.ok("支付成功");} else {return CommonResult.ok("验证失败");}}