//购买所需参数类
public class AddIosOrderPM {
/**
* ios支付所需参数
*/
private String transactionId;
/**
* ios支付所需参数
*/
private String receipt;
/**
* 用户id
*/
private Integer userId;
/**
* 游客id
*/
private String touristId;
}
//购买ios虚拟币
public ResultVM iosPay(AddIosOrderPM addIosOrderPM) {
Map<String, Object> map = new HashMap<>();
Map<String, Object> mapChange = new HashMap<String, Object>();
log.info("客户端传过来的值1:" + addIosOrderPM.getTransactionId() + "客户端传过来的值2:" + addIosOrderPM.getReceipt());
String verifyResult = IosVerifyUtil.buyAppVerify(addIosOrderPM.getReceipt(), 1); //1.先线上测试 发送平台验证
if (verifyResult == null) { // 苹果服务器没有返回验证结果
System.out.println("无订单信息!");
} else {
// 苹果验证有返回结果
log.info("线上,苹果平台返回JSON:" + verifyResult);
JSONObject job = JSONObject.parseObject(verifyResult);
String states = job.getString("status");
if ("21007".equals(states)) { //是沙盒环境,应沙盒测试,否则执行下面
verifyResult = IosVerifyUtil.buyAppVerify(addIosOrderPM.getReceipt(), 0); //2.再沙盒测试 发送平台验证
System.out.println("沙盒环境,苹果平台返回JSON:" + verifyResult);
log.info("沙盒环境,苹果平台返回JSON:" + verifyResult);
job = JSONObject.parseObject(verifyResult);
states = job.getString("status");
}
System.out.println("苹果平台返回值:job" + job);
log.info("苹果平台返回值:job" + job);
if (states.equals("0")) { // 前端所提供的收据是有效的 验证成功
String r_receipt = job.getString("receipt");
JSONObject returnJson = JSONObject.parseObject(r_receipt);
String in_app = returnJson.getString("in_app");
JSONObject in_appJson = JSONObject.parseObject(in_app.substring(1, in_app.length() - 1));
String product_id = in_appJson.getString("product_id");
String transaction_id = in_appJson.getString("transaction_id"); // 订单号
/************************************************+自己的业务逻辑**********************************************************/
/************************************************+自己的业务逻辑end**********************************************************/
// if (mapChange.get("status").equals("0")) {//用户钻石数量新增成功
// map.put("success", true);
// map.put("message", "作品购买成功!");
// map.put("status", states);
// } else {
// map.put("success", false);
// map.put("message", "作品购买失败!");
// }
} else {
map.put("success", false);
map.put("message", "receipt数据有问题");
map.put("status", states);
log.info("失败" + map);
return ResultVMUtil.error(10, "receipt数据有问题" + map);
}
}
return ResultVMUtil.error(10, "无返回值");
}
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;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* 苹果IAP内购验证工具类
* @ClassName: IosVerify
* @Description:Apple Pay
*/
public class IosVerifyUtil {
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
private static final String url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
private static final String url_verify = "https://buy.itunes.apple.com/verifyReceipt";
/**
* 苹果服务器验证
*
* @param receipt
* 账单
* @url 要验证的地址
* @return null 或返回结果 沙盒 https://sandbox.itunes.apple.com/verifyReceipt
*
*/
public static String buyAppVerify(String receipt,int type) {
//环境判断 线上/开发环境用不同的请求链接
String url = "";
if(type==0){
url = url_sandbox; //沙盒测试
}else{
url = url_verify; //线上测试
}
//String url = EnvUtils.isOnline() ?url_verify : url_sandbox;
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setRequestMethod("POST");
// conn.setRequestProperty("content-type", "text/json");
conn.setRequestProperty("Content-Type","application/json");
conn.setRequestProperty("Proxy-Connection", "Keep-Alive");
conn.setDoInput(true);
conn.setDoOutput(true);
BufferedOutputStream hurlBufOus = new BufferedOutputStream(conn.getOutputStream());
String str = String.format(Locale.CHINA, "{\"receipt-data\":\"" + receipt + "\"}");//拼成固定的格式传给平台
hurlBufOus.write(str.getBytes());
hurlBufOus.flush();
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception ex) {
System.out.println("苹果服务器异常");
ex.printStackTrace();
}
return null;
}
/**
* 用BASE64加密
*
* @param str
* @return
*/
public static String getBASE64(String str) {
byte[] b = str.getBytes();
String s = null;
if (b != null) {
s = new sun.misc.BASE64Encoder().encode(b);
}
return s;
}
}
注:ios支付需要二次验证,验证时传的参数为应用向Apple服务器请求购买,成功之后,Apple会返回给应用的四个参数中的receipt和transactionIdentifier进行验证否则会报21002错误
如果后台需二次验证receipt所传示例
MIITtgYJKoZIhvcNAQcCoIITpzCCE6MCAQExCzAJBgUrDgMCGgUAMIIDVwYJKoZIhvcNAQcBoIIDSASCA0QxggNAMAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgEDAgEBBAMMATkwCwIBCwIBAQQDAgEAMAsCAQ8CAQEEAwIBADALAgEQAgEBBAMCAQAwCwIBGQIBAQQDAgEDMAwCAQoCAQEEBBYCNCswDAIBDgIBAQQEAgIAiTANAgENAgEBBAUCAwHV7TANAgETAgEBBAUMAzEuMDAOAgEJAgEBBAYCBFAyNTMwGAIBBAIBAgQQzfk9iobAl6DXDsnsvedtljAbAgEAAgEBBBMMEVByb2R1Y3Rpb25TYW5kYm94MBwCAQUCAQEEFIO2mnoF589yXugyBSTnopURY3DXMB4CAQICAQEEFgwUY29tLnl1YW5zaGl4aW9uZy5ob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fyqZtAjizUsZAS2L70c5vu0mQPy3lPNNiiPvl4/2vIB+x9OYOLUyDTOMSxv5pPCmv/K/xZpwUJfBdAVhEedNO3iyM7R6PVbyTi69G3cN8PReEnyvFteO3ntRcXqNx+IjXKJdXZD9Zr1KIkIxH3oayPc4FgxhtbCS+SsvhESPBgOJ4V9T0mZyCKM2r3DYLP3uujL/lTaltkwGMzd/c6ByxW69oPIQ7aunMZT7XZNn/Bh1XZp5m5MkL72NVxnn6hUrcbvZNCJBIqxw8dtk2cXmPIS4AXUKqK1drk/NAJBzewdXUhMYIByzCCAccCAQEwgaMwgZYxCzAJBgNVBAYTAlVTMRMwEQYDVQQKDApBcHBsZSBJbmMuMSwwKgYDVQQLDCNBcHBsZSBXb3JsZHdpZGUgRGV2ZWxvcGVyIFJlbGF0aW9uczFEMEIGA1UEAww7QXBwbGUgV29ybGR3aWRlIERldmVsb3BlciBSZWxhdGlvbnMgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkCCA7rV4fnngmNMAkGBSsOAwIaBQAwDQYJKoZIhvcNAQEBBQAEggEAgZzXoH3lVesatnFpiHe6T73+7oM3fT+Vao2awPjn3bTcQeWWhUU59qJEz9D38SkSzQi5JGtXkAZH3DJ9hvKR5XJZTbVdKY9VvNmsT2i5iZhHLd6sKE6AIcHfWMfI1+LVKtDhlNevXCmKH3v1DWaBcF04KmlwT31ZpsHpfBajGBglD7WVajN7XrKR1jYEXZLU6oOsmxXvcfILVeB8oLFrZ4fJzcN+rahZ5KZZup9MvYlJvBV8v5n11iRRitUnFkTGs/rdA0jGfKWNOuFiuZTtJa3G3wXIX9qZ/aOdvpptlmRfOfeQvFF6a11VJSSLYTor2900BHo9+i63l6/DPN3rEQ==
---------------------------------------------------------------------------------------------------------------------------------------------------------------
如果后台不需要二次验证那么就不需要工具类,所传参数如
{
"receipt": {
"receipt_type": "ProductionSandbox",
"adam_id": 0,
"app_item_id": 0,
"bundle_id": "com.operate.classroom",
"application_version": "1.0.0",
"download_id": 0,
"version_external_identifier": 0,
"receipt_creation_date": "2020-09-01 10:12:26 Etc/GMT",
"receipt_creation_date_ms": "1598955146000",
"receipt_creation_date_pst": "2020-09-01 03:12:26 America/Los_Angeles",
"request_date": "2020-09-01 10:12:31 Etc/GMT",
"request_date_ms": "1598955151271",
"request_date_pst": "2020-09-01 03:12:31 America/Los_Angeles",
"original_purchase_date": "2013-08-01 07:00:00 Etc/GMT",
"original_purchase_date_ms": "1375340400000",
"original_purchase_date_pst": "2013-08-01 00:00:00 America/Los_Angeles",
"original_application_version": "1.0",
"in_app": [{
"quantity": "1",
"product_id": "com.operate.classroom.25",
"transaction_id": "1000000713408396",
"original_transaction_id": "1000000713408396",
"purchase_date": "2020-09-01 10:12:26 Etc/GMT",
"purchase_date_ms": "1598955146000",
"purchase_date_pst": "2020-09-01 03:12:26 America/Los_Angeles",
"original_purchase_date": "2020-09-01 10:12:26 Etc/GMT",
"original_purchase_date_ms": "1598955146000",
"original_purchase_date_pst": "2020-09-01 03:12:26 America/Los_Angeles",
"is_trial_period": "false"
}]
},
"environment": "Sandbox",
"status": 0
}
如果ios传过来这样的参数后台不需要进行二次验证,可直接写逻辑,建议进行二次验证