1. 程式人生 > >Spring Boot + IOS內購(IAP)

Spring Boot + IOS內購(IAP)

分享一個關於IAP(IOS內購)的專案

IAP驗證工具類

/**
 * @program: learningapi
 * @description: IOS驗證工具
 * @author: Irving Wei
 * @create: 2018-09-10 17:20
 **/
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; //線上測試
        }
        //  將傳過來的轉義符 """ 替換成 "\""
        receipt = receipt.replaceAll(""","\"");

        //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("Proxy-Connection", "Keep-Alive");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            BufferedOutputStream hurlBufOus = new BufferedOutputStream(conn.getOutputStream());

            String str = String.format(Locale.CHINA, 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;
    }

}

業務程式碼

/**
 * @program: learningapi
 * @description: IOS 內購
 * @author: Irving Wei
 * @create: 2018-09-10 17:02
 **/

@Controller
@RequestMapping("/buy")
public class IapChargeController extends AbstractRestController {
    @Autowired
    private LoggerService loggerService;

    @ApiOperation(value = "ios內購-充值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "使用者 Token", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "payload", value = "需要客戶端傳過來的引數", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "transactionID", value = "交易單號,需要客戶端傳過來的引數", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/recharge", method = RequestMethod.GET)
    @Transactional
    public ResponseEntity<WrappedResponse<List<AppBannerEntity>>> getBanner(HttpServletRequest request) {
        String token = ServletRequestUtils.getStringParameter(request, "token", "");
        String transactionID = ServletRequestUtils.getStringParameter(request, "transactionID", "");
        String payload = ServletRequestUtils.getStringParameter(request, "payload", "");

        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("客戶端傳過來的值1:" + transactionID + "客戶端傳過來的值2:" + payload);

        String verifyResult = IosVerifyUtil.buyAppVerify(payload, 1);            //1.先線上測試    傳送平臺驗證
        if (verifyResult == null) {                                            // 蘋果伺服器沒有返回驗證結果
            System.out.println("無訂單資訊!");
        } else {                                                                // 蘋果驗證有返回結果
            System.out.println("線上,蘋果平臺返回JSON:" + verifyResult);
            JSONObject job = JSONObject.parseObject(verifyResult);
            String states = job.getString("status");

            if ("21007".equals(states)) {                                            //是沙盒環境,應沙盒測試,否則執行下面
                verifyResult = IosVerifyUtil.buyAppVerify(payload, 0);            //2.再沙盒測試  傳送平臺驗證
                System.out.println("沙盒環境,蘋果平臺返回JSON:" + verifyResult);
                job = JSONObject.parseObject(verifyResult);
                states = job.getString("status");
            }

            System.out.println("蘋果平臺返回值:job" + job);

          

            if (!"0".equals(states)) {
                // 記錄日誌,並返回失敗
                
                return this.fail();
            } else {// 前端所提供的收據是有效的    驗證成功
                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");   // 訂單號
/************************************************+自己的業務邏輯**********************************************************/

                if (transactionID.equals(transaction_id)) {
                    
                }
/************************************************+自己的業務邏輯end**********************************************************/
                return null;
            }
        }
        return null;
    }
}