admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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;
        }
    }
 
 
}