|
|
|
@ -5,10 +5,12 @@ import cn.hutool.json.JSONObject;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.luoo.user.pojo.AppleOrder;
|
|
|
|
|
import com.luoo.user.pojo.AppleResponse;
|
|
|
|
|
import com.luoo.user.util.IosVerifyUtil;
|
|
|
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
|
|
|
import io.swagger.annotations.ApiImplicitParams;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
@ -37,6 +39,11 @@ public class ApplePayController {
|
|
|
|
|
|
|
|
|
|
@Value("${applePay.certificate}")
|
|
|
|
|
private String certificateUrl;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IosVerifyUtil iosVerifyUtil;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private static final String certificateUrl = "https://buy.itunes.apple.com/verifyReceipt";
|
|
|
|
|
//测试的购买凭证验证地址
|
|
|
|
|
// private static final String certificateUrlTest = "https://sandbox.itunes.apple.com/verifyReceipt";
|
|
|
|
@ -60,7 +67,7 @@ public class ApplePayController {
|
|
|
|
|
*
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
* @param receipt 前端支付成功后:苹果给前端的一长串字符串
|
|
|
|
|
* @param chooseEnv false代表苹果沙箱环境,true代表真实支付环境
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@PostMapping("/setIapCertificate")
|
|
|
|
|
@ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户Id"), @ApiImplicitParam(name = "receipt", value = "苹果传递前端支付成功的值")})
|
|
|
|
@ -96,36 +103,40 @@ public class ApplePayController {
|
|
|
|
|
return "调用传参错误!";
|
|
|
|
|
}
|
|
|
|
|
String line = null;
|
|
|
|
|
|
|
|
|
|
String result = iosVerifyUtil.buyAppVerify(receipt, 1);
|
|
|
|
|
|
|
|
|
|
System.out.println(result+"");
|
|
|
|
|
// todo 理论应该锁UserId,因为后台只能允许一个线程执行给玩家发送道具!
|
|
|
|
|
try {
|
|
|
|
|
// 设置SSLContext
|
|
|
|
|
SSLContext ssl = SSLContext.getInstance("SSL");
|
|
|
|
|
ssl.init(null, new TrustManager[]{myX509TrustManager}, null);
|
|
|
|
|
// 打开连接
|
|
|
|
|
HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
|
|
|
|
|
// 设置套接工厂
|
|
|
|
|
conn.setSSLSocketFactory(ssl.getSocketFactory());
|
|
|
|
|
// 加入数据
|
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
|
conn.setRequestProperty("Content-type", "application/json");
|
|
|
|
|
// JSONObject obj = new JSONObject();
|
|
|
|
|
JSONObject obj = new JSONObject();
|
|
|
|
|
obj.set("receipt-data", receipt);
|
|
|
|
|
// 发送请求
|
|
|
|
|
BufferedOutputStream buffOutStr = new BufferedOutputStream(conn.getOutputStream());
|
|
|
|
|
buffOutStr.write(obj.toString().getBytes());
|
|
|
|
|
buffOutStr.flush();
|
|
|
|
|
buffOutStr.close();
|
|
|
|
|
// 获取接收响应的对象reader
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
|
|
// 转换苹果响应的所有结果!
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
sb.append(line);
|
|
|
|
|
}
|
|
|
|
|
// // 设置SSLContext
|
|
|
|
|
// SSLContext ssl = SSLContext.getInstance("SSL");
|
|
|
|
|
// ssl.init(null, new TrustManager[]{myX509TrustManager}, null);
|
|
|
|
|
// // 打开连接
|
|
|
|
|
// HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
|
|
|
|
|
// // 设置套接工厂
|
|
|
|
|
// conn.setSSLSocketFactory(ssl.getSocketFactory());
|
|
|
|
|
// // 加入数据
|
|
|
|
|
// conn.setRequestMethod("POST");
|
|
|
|
|
// conn.setDoOutput(true);
|
|
|
|
|
// conn.setRequestProperty("Content-type", "application/json");
|
|
|
|
|
// // JSONObject obj = new JSONObject();
|
|
|
|
|
// JSONObject obj = new JSONObject();
|
|
|
|
|
// obj.set("receipt-data", receipt);
|
|
|
|
|
// // 发送请求
|
|
|
|
|
// BufferedOutputStream buffOutStr = new BufferedOutputStream(conn.getOutputStream());
|
|
|
|
|
// buffOutStr.write(obj.toString().getBytes());
|
|
|
|
|
// buffOutStr.flush();
|
|
|
|
|
// buffOutStr.close();
|
|
|
|
|
// // 获取接收响应的对象reader
|
|
|
|
|
// BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
|
|
// // 转换苹果响应的所有结果!
|
|
|
|
|
// StringBuffer sb = new StringBuffer();
|
|
|
|
|
// while ((line = reader.readLine()) != null) {
|
|
|
|
|
// sb.append(line);
|
|
|
|
|
// }
|
|
|
|
|
// 处理苹果响应的结果
|
|
|
|
|
AppleResponse appleResponse = JSONUtil.toBean(sb.toString(), AppleResponse.class);
|
|
|
|
|
AppleResponse appleResponse = JSONUtil.toBean(result, AppleResponse.class);
|
|
|
|
|
log.info("用户Id{},回调获取到了{}", userId, appleResponse.toString());
|
|
|
|
|
// 苹果说了,只有订单状态是0才算成功,其他均是错误,可查:https://developer.apple.com/documentation/appstorereceipts/status
|
|
|
|
|
if (StringUtils.equalsAny(appleResponse.getStatus(), "0")) {
|
|
|
|
|