支付的那些套路(手機支付寶篇)

安卓和IOS的支付方式是不一樣的,安卓走接口,IOS走內購,官方文檔寫的還算能夠,安卓的基本套路是這樣的:java

so:根據阿里提供的文檔拼接參數數據庫

Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("app_id", PayPropertiesUtils.getString("alipay.appPay.appId"));
            paramMap.put("method", PayPropertiesUtils.getString("alipay.appPay.method"));
            paramMap.put("format", PayPropertiesUtils.getString("alipay.appPay.format"));
            paramMap.put("charset", PayPropertiesUtils.getString("alipay.inputCharset"));
            //發送請求的時間,格式"yyyy-MM-dd HH:mm:ss"	2014-07-24 03:07:50
            paramMap.put("timestamp", UtilDate.getDateFormatter());
            paramMap.put("version", PayPropertiesUtils.getString("alipay.appPay.version"));
            paramMap.put("notify_url", PayPropertiesUtils.getString("alipay.appPay.notifyUrl"));
            //業務對象
            Map<String, String> bizContentMap = new HashMap<String, String>();
            bizContentMap.put("body", GlobalConstants.PAY_PRODUCT_NAME);
            bizContentMap.put("subject", GlobalConstants.PAY_PRODUCT_NAME);
            bizContentMap.put("out_trade_no", payExtendVo.getOrderId());
            bizContentMap.put("timeout_express",PayPropertiesUtils
            .getString("alipay.appPay.timeoutExpress"));
            bizContentMap.put("total_amount", String.valueOf(payExtendVo.getPayAmount()));
            bizContentMap.put("product_code", PayPropertiesUtils
            .getString("alipay.appPay.productCode"));
            String biz_content = JSON.toJSONString(bizContentMap);
            paramMap.put("biz_content", biz_content);
            String sign_type =  PayPropertiesUtils.getString("alipay.appPay.signType");
            paramMap.put("sign_type",sign_type);
            //去除空值
            Map<String,String> signMap = AlipayCore.paraAppFilter(paramMap);
            //生成簽名
            String sign = Signature.buildRequestMysign(signMap,sign_type,RSA_PRIVATE_KEY);
            sign = URLEncoder.encode(sign,"utf-8");
            signMap.put("sign", sign);
            //獲取業務參數串
            String reqOrderStr = AlipayCore.createLinkString(signMap);

而後篇拼接返回給App的參數:express

Map returnMap = new HashMap();
            returnMap.put("reqPayUrl", reqOrderStr);
            returnMap.put("orderId",payExtendVo.getOrderId());
            returnMap.put("payAmount",tradeLog.getPayAmount());
            returnMap.put("income",tradeLog.getIncome());
            returnMap.put("rewardFanpiao",tradeLog.getRewardFanpiao());
            returnMap.put("incomeAmount",tradeLog.getIncomeAmount());
            resultBean.setData(returnMap);

最後存庫,支付狀態是待支付。服務器

手機端經過SDK調用以後根據訂單ID請求服務器支付狀態。app

接下來是阿里給服務器的回調,若是代碼理由AOP或者攔截提早排除掉這個請求:ui

首先解析參數 :url

Map<String, String> checkParamsMap = new HashMap<String,String>();
            Map requestParams = thridPartyNotifyVo.getAliPayRequestParamsMap();
            for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
                String name = (String) iter.next();
                String[] values = (String[]) requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i]
                            : valueStr + values[i] + ",";
                }
                //亂碼解決,這段代碼在出現亂碼時使用。若是mysign和sign不相等也能夠使用這段代碼轉化
                //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
                checkParamsMap.put(name, valueStr);
            }

而後驗證參數是否和合法,是否是從阿里發出來的,能夠調用阿里給的demo裏的方法:code

AlipayNotify.verify(checkParamsMap,PayPropertiesUtils.getString("alipay.appPay.signType"),PayPropertiesUtils.getString("alipay.appPay.alipayPublicKey"))

若是是true:更數據庫的狀態:最後給支付寶一個success,告訴支付寶回調已成功。orm

相關文章
相關標籤/搜索