微信APP支付

微信支付開發(fā)文檔地址
SDK與DEMO下載--JAVA .NET C# PHP

微信支付參數(shù)配置

public class WXPayConfig {
    //應(yīng)用ID
    public final static String APP_ID = "yourAppId";
    
    //API密鑰
    public final static String APP_KEY = "yourAppKey";
    
    //商戶號(hào)
    public final static String MCH_ID = "yourMchId";
    
    //統(tǒng)一下單地址
    public final static String URL_UNIFIED_ORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    
    //查詢賬單地址
    public final static String URL_ORDER_QUERY = "https://api.mch.weixin.qq.com/pay/orderquery";
    
    //統(tǒng)一下單回調(diào)地址
    public final static String NOTIFY_URL = "yourNotifyUrl";
}

統(tǒng)一下單

public static JSONObject wxUnifiedOrder(String orderId, String cost,
            String body, String ip, long expireTime) {
        JSONObject ret = new JSONObject();
        SortedMap<String, String> param = new TreeMap<String, String>();
        param.put("appid", WXPayConfig.APP_ID);
        param.put("mch_id", WXPayConfig.MCH_ID);
        param.put("nonce_str", WXPayUtil.generateNonceStr());
        param.put("body", body);
        param.put("out_trade_no", orderId);
        param.put("fee_type", "CNY");
        param.put("total_fee", cost);
        param.put("spbill_create_ip", ip);
        param.put("notify_url", WXPayConfig.NOTIFY_URL);
        param.put("trade_type", "APP");
        param.put("time_expire", timeExpire(expireTime));
        // 生成sign并轉(zhuǎn)化成xml格式
        String xml = "";
        try {
            xml = WXPayUtil.generateSignedXml(param, WXPayConfig.APP_KEY);
        } catch (Exception e) {
            ret.put("code", -1);
            ret.put("msg", e.getMessage());
        }

        String response = "";
        try {
            response = HttpRequest.post(WXPayConfig.URL_UNIFIED_ORDER, xml);
        } catch (Exception e) {
            ret.put("code", -1);
            ret.put("msg", e.getMessage());
        }

        Map<String, String> result = null;
        try {
            result = WXPayUtil.xmlToMap(response);
        } catch (Exception e) {
            ret.put("code", -1);
            ret.put("msg", e.getMessage());
        }

        if (!result.isEmpty() && result.get("return_code").equals("SUCCESS")) {
            if (result.get("result_code").equals("SUCCESS")) {
                try {
                    ret = wxAppPay(result);
                } catch (Exception e) {
                }
                ret.put("code", 0);
                logger.error("InfoMsg:--- 微信統(tǒng)一下單請(qǐng)求交易成功");
            } else {
                String message = result.get("err_code_des");
                ret.put("code", -1);
                ret.put("msg", "微信統(tǒng)一下單請(qǐng)求交易解析失敗");
                logger.error("errorMsg:--- 微信統(tǒng)一下單請(qǐng)求交易解析失敗" + message);
            }
        } else {
            ret.put("code", -1);
            ret.put("msg", "微信統(tǒng)一下單請(qǐng)求交易解析失敗");
            logger.error("errorMsg:--- 微信統(tǒng)一下單請(qǐng)求交易解析失敗");
        }
        return ret;
    }

賬單查詢

public static String wxQueryOrder() {
        System.out.println("infoMsg:--- 微信支付訂單查詢開始");
        String message = "";
        try {
            SortedMap<String, String> param = new TreeMap<String, String>();
            param.put("appid", WXPayConfig.APP_ID);
            param.put("mch_id", WXPayConfig.MCH_ID);
            param.put("nonce_str", WXPayUtil.generateNonceStr());
            param.put("out_trade_no", "20171205");

            String xml = WXPayUtil
                    .generateSignedXml(param, WXPayConfig.APP_KEY);

            String response = HttpRequest
                    .post(WXPayConfig.URL_ORDER_QUERY, xml);

            if (response != null && response != "") {
                Map<String, String> result = WXPayUtil.xmlToMap(response);

                // System.out.println(result);

                if (!result.isEmpty()) {
                    String return_code = result.get("return_code");
                    if (return_code.equals("SUCCESS")) {
                        String result_code = result.get("return_code");
                        if (result_code.equals("SUCCESS")) {
                            message = (String) result.get("trade_state_desc");
                        }
                    } else {
                        message = (String) result.get("err_code_des");
                    }
                } else {

                }
            } else {

            }
            // System.out.println("infoMsg:--- 微信支付訂單查詢結(jié)束");
        } catch (Exception e) {
            // System.out.println("erroroMsg:--- 微信支付訂單查詢失敗" + e.getMessage()
            // + message);
        }
        return message;
    }

支付結(jié)果異步回調(diào)

public static JSONObject wxPayNotify(String body) {
        JSONObject result = new JSONObject();
        System.out.println("infoMsg:--- 微信異步通知開始");
        String wx_map = "";
        try {
            String xml = body;
            if (xml != null && xml != "") {
                Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
                // 驗(yàn)簽
                if (!WXPayUtil.isSignatureValid(xml, WXPayConfig.APP_KEY)) {
                    if (resultMap.get("return_code").equals("SUCCESS")) {
                        Double amount = Double.parseDouble(resultMap
                                .get("total_fee"));
                        String passbackParams = resultMap.get("total_fee");
                        String outOrderNo = resultMap.get("out_trade_no");
                        Map<String, String> map = new HashMap<>();
                        map.put("return_code", "SUCCESS");
                        map.put("return_msg", "OK");
                        wx_map = WXPayUtil.mapToXml(map);
                        result.put("out_trade_no", outOrderNo);
                        result.put("code", 0);
                    }
                }
            } else {
                result.put("code", -1);
                result.put("msg", "微信異步通知失敗");
                logger.error("infoMsg:--- 微信異步通知失敗");
            }
        } catch (Exception e) {
            result.put("code", -1);
            result.put("msg", "微信異步通知失敗" + e.getMessage());
            logger.error("msg", "微信異步通知失敗" + e.getMessage());
        }
        result.put("wx_map", wx_map);
        return result;
    }

支付二次簽名

public static JSONObject wxAppPay(Map<String, String> param)
            throws Exception {
        JSONObject result = new JSONObject();
        SortedMap<String, String> sortedMap = new TreeMap<String, String>();
        sortedMap.put("appid", WXPayConfig.APP_ID);
        sortedMap.put("partnerid", WXPayConfig.MCH_ID);
        sortedMap.put("prepayid", param.get("prepay_id"));
        sortedMap.put("package", "Sign=WXPay");
        String nonceStr = WXPayUtil.generateNonceStr();
        long timestamp = System.currentTimeMillis() / 1000;
        sortedMap.put("noncestr", nonceStr);
        sortedMap.put("timestamp", String.valueOf(timestamp));

        String sign = WXPayUtil.generateSignature(sortedMap,
                WXPayConfig.APP_KEY);

        result.put("appid", WXPayConfig.APP_ID);
        result.put("partnerid", WXPayConfig.MCH_ID);
        result.put("prepayid", param.get("prepay_id"));
        result.put("package", "Sign=WXPay");
        result.put("noncestr", nonceStr);
        result.put("timestamp", String.valueOf(timestamp));
        result.put("sign", sign);
        return result;
    }

過期時(shí)間格式轉(zhuǎn)化

private static String timeExpire(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyyMMddHHmmss");
        Date date = new Date(time * 1000);
        return simpleDateFormat.format(date);
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 歡迎留言、轉(zhuǎn)發(fā)微信極速開發(fā)系列文章(微信支付、授權(quán)獲取用戶信息等):點(diǎn)擊這里 目錄1、注冊(cè)賬號(hào)、開發(fā)者認(rèn)證2、添加...
    Javen205閱讀 11,715評(píng)論 10 66
  • 【申請(qǐng)流程】 開發(fā)微信APP支付,需要先去微信開放平臺(tái)申請(qǐng)移動(dòng)應(yīng)用,并開通微信支付功能,通過審核后方可進(jìn)行開發(fā); ...
    狼鳳皇閱讀 4,530評(píng)論 0 6
  • 引子: 以下的部分代碼、思路來自于 CSDN的一位博客主 ,以及 后宮 —— 易水 的支持。 官方文檔 1.前期申...
    冰凝雪國(guó)閱讀 1,347評(píng)論 0 4
  • 先看看支付時(shí)序圖 一、時(shí)序圖 打眼一看好像挺復(fù)雜的,仔細(xì)一看很明了,思路邏輯很清晰,很有條理,邏輯縝密。大體流程和...
    追沐閱讀 2,217評(píng)論 0 1
  • “你哭過了。” 他突然開口,驚的心中群鳥高飛起。挑眉略帶迷惘,自認(rèn)為倒是掩飾挺好,怎的就被他瞧出個(gè)一二來了?抬...
    戰(zhàn)且歌閱讀 581評(píng)論 0 0

友情鏈接更多精彩內(nèi)容