支付的那些套路(apple pay篇)

ios支付比普通的支付要麻烦许多,因为要走内购,而且像支付宝,微信那种还可以给回调,Apple PAY根被不会回调啊,所以要么找第三方的支付平台,要么自行接入,本篇是直接接入的,applepay的支付手续费是总金额的30%,所以业务逻辑要自行改变。

流程是这样的:

164355_ZlFR_2421441.png

1,IOS传过来的参数经过非空校验及验证码重复校验后先要判断验证码的重复,验证码是手机端向apple请求后,apple返回的data经过MD5加密存库,留个备份,每次验证的时候判断一下重复。

2.向apple请求,参数是地址,IOS的data,订单ID,换取status,bid,productId,订单ID

  public static Map verifyReceipt(String reqUrl, String receiptDataJson, String appTransactionId) throws  Exception{
        reqUrl=https://buy.itunes.apple.com/verifyReceipt
        int status = -1;
        String bid = "";
        String productId = "";
        String transactionId = "";
        //This is the URL of the REST webservice in iTunes App Store
        URL url = new URL(reqUrl);
        //make connection, use post mode
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setAllowUserInteraction(false);
        //Write the JSON query object to the connection output stream
        PrintStream ps = new PrintStream(connection.getOutputStream());
        ps.print(receiptDataJson);
        ps.close();
        //Call the service
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        //Extract response
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = br.readLine()) != null) {
            sb.append(str);
        }
        br.close();
        String response = sb.toString();
        //System.out.println("AppleAppStoreVerifyRequest----response:"+response);
        JSONObject resutl = JSON.parseObject(response);
        status = resutl.containsKey("status") ? resutl.getInteger("status") : status;
        JSONObject receipt = resutl.containsKey("receipt") ? resutl.getJSONObject("receipt") : null;
        if(receipt != null && !StringUtils.isBlank(receipt.toString())){
            bid = receipt.containsKey("bundle_id") ? receipt.getString("bundle_id") : bid;
            JSONArray inApp =  receipt.getJSONArray("in_app");
            if(inApp != null && !StringUtils.isBlank(inApp.toString())) {
                if(StringUtils.isBlank(appTransactionId)) { //如果未给订单号,则默认获取第一个
                    JSONObject inAppJSONObjectTemp = inApp.getJSONObject(0);
                    if (inAppJSONObjectTemp != null && !inAppJSONObjectTemp.isEmpty()) {
                        productId = inAppJSONObjectTemp.containsKey("product_id") ? inAppJSONObjectTemp.getString("product_id") : productId;
                        transactionId = inAppJSONObjectTemp.containsKey("transaction_id") ? inAppJSONObjectTemp.getString("transaction_id") : transactionId;
                    }
                } else  { //如果APP已给定订单号时用订单号进行查找
                    for(int i=0; i<inApp.size(); i++) {
                        JSONObject inAppJSONObjectTemp = inApp.getJSONObject(i);
                        if (inAppJSONObjectTemp != null && !inAppJSONObjectTemp.isEmpty()) {
                            String transactionIdTemp = inAppJSONObjectTemp.containsKey("transaction_id") ? inAppJSONObjectTemp.getString("transaction_id") : transactionId;
                            if(appTransactionId.equals(transactionIdTemp)) {
                                productId = inAppJSONObjectTemp.containsKey("product_id") ? inAppJSONObjectTemp.getString("product_id") : productId;
                                transactionId = transactionIdTemp;
                            }
                        }
                    }
                }
            }
        }
        Map resMap = new HashMap();
        resMap.put("status",status);
        resMap.put("bid",bid);
        resMap.put("productId",productId);
        resMap.put("transactionId",transactionId);
        resMap.put("resutl",resutl == null ?"":resutl.toJSONString());
        return resMap;
    }

3.然后去验证返回的status是否合法:

 int resultStauts = Integer.valueOf(String.valueOf(verifyMap.get("status"))); 
 if (resultStauts == 21007) {  //本次支付为沙盒支付
                    if (!"165".equals(thridPartyNotifyVo.getUserId())) {  //非苹果审批账号 沙盒进行充值不进行饭票增加
                       return false;
                    }
                }

判断bid,productId是否合法,这一步一定要验,要不会出现替换充值的bug:

String bid = String.valueOf(verifyMap.get("bid"));
String productId = String.valueOf(verifyMap.get("productId"));
transactionId = String.valueOf(verifyMap.get("transactionId"));
if(!"申请的bid".equals(bid)) {//苹果内购支付使用的iPhone程序的bundle标识是否合法
       return false
}
if(StringUtils.isBlank(productId) || StringUtils.isBlank(transactionId)) { //是否返回商品信息
return false
}

4.最后进行下面的业务逻辑,更余额,把加密好的data存库留底,改状态。

注意:productID一定要给IOS,而且不能对错,6元的商品对应com.starunion.xxxxxx_Purchase_6Yuan.

         去IOS请求后的bid,一定要验。因为不是IOS给的回调验证一定要小心谨慎,

转载于:https://my.oschina.net/u/2421441/blog/843051

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值