?支付流程,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("驗證失敗");}}