资讯专栏INFORMATION COLUMN

苹果IAP支付二次验证

shleyZ / 3471人阅读

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 + "
";
            }
            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;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67482.html

相关文章

  • 不玩虚的,认真聊聊苹果审核的注意事项

    摘要:有人说苹果的审核人员比较任性,审核通不过是常事儿。然而这些文章并没有在认真介绍苹果审核的注意事项。通过这张图对苹果的审核项目进行梳理,也总结出了一些信息。 有人说苹果的审核人员比较任性,APP审核通不过是常事儿。确实,苹果在行业里扮演的就是一个极客的角色,要求多、难应付。奈何对方手里捏了上亿的用户,所以你得把他当女神一样哄着,满足她各种各样无礼的要求。 要问起谁把这个女神伺候得最满意,...

    edgardeng 评论0 收藏0

发表评论

0条评论

shleyZ

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<