参考
https://juejin.cn/post/6877086563088465933
进入App Store Connect 中心
https://link.juejin.cn/?target=https%3A%2F%2Flinks.jianshu.com%2Fgo%3Fto%3Dhttps%253A%252F%252Fappstoreconnect.apple.com%252Flogin
进入我的app 创建内购项目
进入 协议、税务和银行业务 免费App已经正常了,付费App点后面操作的查看并同意条款并填写变为有效
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class ApplePayParam {
@ApiModelProperty("接受的支付数据")
private String receipt;
@ApiModelProperty("环境 0沙盒环境 1正式环境")
private Boolean env;
}
@ApiOperation("苹果内购支付回调(app调用,不是官方回调)")
@PostMapping("/apple_pay_call_back")
@ResponseResult
public Boolean applePayOrderCallBack(@RequestBody ApplePayParam param) {
log.info("支付数据存在:{}", StringUtils.isNotBlank(param.getReceipt()));
return applePayService.verifyOrder(param);
}
public Boolean verifyOrder(ApplePayParam param) {
//支付结果
String result = this.result(param.getEnv(), param.getReceipt());
if (StringUtils.isBlank(result)) {
log.info("苹果服务器未返回消息!");
throw new ErrorException("苹果服务器未返回消息!");
} else {
log.info("支付结果:{}", result);
JSONObject jsonResult = JSON.parseObject(result);
String environment = jsonResult.getString("environment");
//获取用户id
this.userId = loginService.getTokenUserId();
//再次判断环境
if ("Sandbox".equals(environment)) {
//沙盒环境
log.info("沙盒环境");
return this.verifyBySandbox(jsonResult);
} else {
//正式环境
log.info("正式环境");
return this.verifyByFormal(jsonResult);
}
}
}
//此处正式环境同样调用的沙盒环境根据自己的需求做出更改
//此处是为了审核,审核过后app可隐藏
private Boolean verifyByFormal(JSONObject result) {
return this.verifyBySandbox(result);
}
private Boolean verifyBySandbox(JSONObject result) {
JSONArray in_app = result.getJSONObject("receipt").getJSONArray("in_app");
log.info(in_app.toJSONString());
if (CollectionUtils.isNotEmpty(in_app)) {
in_app.forEach(i -> {
log.info("订单: {}", i.toString());
JSONObject json = (JSONObject) i;
ApplePayReceipt receipt = JSON.parseObject(((JSONObject) i).toJSONString(), ApplePayReceipt.class);
//订单id
final String transaction_id = json.getString("transaction_id");
//产品id
final String product_id = json.getString("product_id");
//业务代码
});
}
//获取商品id 和订单号以后 此处可以处理本地的订单逻辑
log.info("apple 验证订单成功");
return true;
}
private String result(Boolean env, String receipt) {
final String result;
//判断正式环境
if (env) {
//正式环境
String URL_VERIFY = "https://buy.itunes.apple.com/verifyReceipt";
result = IosUtil.appBuyVerify(receipt, URL_VERIFY);
} else {
//测试环境
String URL_SANDBOX = "https://sandbox.itunes.apple.com/verifyReceipt";
result = IosUtil.appBuyVerify(receipt, URL_SANDBOX);
}
return result;
}
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import javax.net.ssl.*;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Locale;
@Slf4j
public class IosUtil {
/**
* 苹果服务器验证
*
* @param receipt 账单
* @param url 请求地址
*/
public static String appBuyVerify(String receipt, String url) {
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setSSLSocketFactory(sslContext.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setRequestMethod("POST");
conn.setRequestProperty("content-type", "text/json");
conn.setRequestProperty("Proxy-Connection", "Keep-Alive");
conn.setDoInput(true);
conn.setDoOutput(true);
BufferedOutputStream hurlBufOus = new BufferedOutputStream(conn.getOutputStream());
JSONObject json = new JSONObject();
json.put("receipt-data",receipt);
hurlBufOus.write(json.toString().getBytes());
hurlBufOus.flush();
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
log.error("苹果服务器验证出错:{}", e.getMessage());
}
return null;
}
private static class TrustAnyTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
}
}