package com.yeshi.buwan.controller; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; import com.yeshi.buwan.service.inter.order.OrderService; import com.yeshi.buwan.util.RedisManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Controller @RequestMapping("apple") public class AppleController { Logger logger = LoggerFactory.getLogger(AppleController.class); @Resource private RedisManager redisManager; @Resource private OrderService orderService; private String getRequestBody(HttpServletRequest request) { try { byte[] buffer = new byte[2048]; StringBuilder stringBuilder = new StringBuilder(); int readBytes; while ((readBytes = request.getInputStream().read(buffer)) > 0) { stringBuilder.append(new String(buffer, 0, readBytes)); } return stringBuilder.toString(); } catch (Exception var4) { return null; } } /** * 字段说明:https://developer.apple.com/documentation/appstoreservernotifications/responsebody * * @param request * @param response */ @RequestMapping("notify") public void notify(HttpServletRequest request, HttpServletResponse response) { String content = getRequestBody(request); logger.info("回调内容为:{}", content); ResponseBody responseBody = new Gson().fromJson(content, ResponseBody.class); //获取内容 switch (responseBody.getNotificationType()) { //初次购买订阅 case "INITIAL_BUY": break; //表示已成功自动续订过去未能续订的过期订阅。 case "DID_RECOVER": break; //表示客户的订阅已成功自动续订新的交易时段。 case "DID_RENEW": break; } } private class ResponseBody { @SerializedName("auto_renew_product_id") private String productId; @SerializedName("notification_type") private String notificationType; @SerializedName("original_transaction_id") private long originalTransactionId; @SerializedName("unified_receipt") private String unifiedReceipt; //Sandbox, PROD private String environment; @SerializedName("bid") private String bundleId; @SerializedName("bvrs") private String version; @SerializedName("auto_renew_status") private boolean status; public String getProductId() { return productId; } public void setProductId(String productId) { this.productId = productId; } public String getNotificationType() { return notificationType; } public void setNotificationType(String notificationType) { this.notificationType = notificationType; } public long getOriginalTransactionId() { return originalTransactionId; } public void setOriginalTransactionId(long originalTransactionId) { this.originalTransactionId = originalTransactionId; } public String getUnifiedReceipt() { return unifiedReceipt; } public void setUnifiedReceipt(String unifiedReceipt) { this.unifiedReceipt = unifiedReceipt; } public String getEnvironment() { return environment; } public void setEnvironment(String environment) { this.environment = environment; } public String getBundleId() { return bundleId; } public void setBundleId(String bundleId) { this.bundleId = bundleId; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } } }