Controller

   /**
     * IAP二次验证
     * @param pingPayRequest
     * @return
     */
    @RequestMapping("iap_verify")
    public BaseResponse iapVerify(@RequestBody PayRequest payRequest){
        Map verifyMap = null;
        try {
            verifyMap = IAPVerify.getSecondaryVerify(pingPayRequest.getData(),pingPayRequest.getTp());
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.warn("IAP返回:"+verifyMap);
        if(null == verifyMap){
            return ResponseUtils.returnResponse(ResponseEnum.PING_PAY_REWARD_ERROR);
        }
        pingPayRequest.setPingId("");//TODO 苹果返回id

        PingPay pingPay_save = pingPayService.save(pingPayRequest);
        if(null == pingPay_save){
            return ResponseUtils.returnResponse(ResponseEnum.PING_PAY_REWARD_ERROR);
        }

        if(null != verifyMap){
            return ResponseUtils.returnResponse(ResponseEnum.OK,verifyMap);
        }else {
            return ResponseUtils.returnResponse(ResponseEnum.PUBLICATION_IAP_V_FAIL);
        }


    }
public class IAPVerify {
    public static Map getSecondaryVerify(String data,String tp) throws Exception {
        Map verifyMap = new HashMap();
        if (data != null  && data.equals("") &&  null !=tp && !tp.equals("")) {
            URL dataUrl = new URL("https://buy.itunes.apple.com/verifyReceipt");
            if(tp.equals("t")){
                dataUrl = new URL("https://sandbox.itunes.apple.com/verifyReceipt");
            }

            HttpURLConnection con = (HttpURLConnection) dataUrl.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("content-type", "text/json");
            con.setRequestProperty("Proxy-Connection", "Keep-Alive");
            con.setDoOutput(true);
            con.setDoInput(true);
            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
            String str = String.format(Locale.CHINA, "{\"receipt-data\":\"" + data + "\"}");
            System.out.println(str);
            out.write(str);
            out.flush();
            out.close();
            InputStream is = con.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line = null;
            String result = "";
            while ((line = reader.readLine()) != null) {
                result += line + "\r\n";
            }
            org.json.JSONObject j;
            try {
                j = new org.json.JSONObject(result);
                String returnresult = j.get("status").toString();
                if (returnresult.equals("0")) {
                    verifyMap.put("status", 0);
                    verifyMap.put("receipt", j.get("receipt"));
                }else if (returnresult.equals("21002")) {
                    verifyMap.put("status", -6L);
                    verifyMap.put("receipt", j.get("receipt"));
                } else {
                    verifyMap.put("status", Long.valueOf(returnresult));
                    verifyMap.put("receipt", j.get("receipt"));
                }

                return verifyMap;
            } catch (Exception e) {
                System.out.println("接收返回类型:" + e.getMessage());
            }
        }
        return null;
    }
}

asoren
404 声望15 粉丝