微信掃碼支付

1.首先要選擇支付模式

https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=2_1,進入這個鏈接可以查看相關的支付模式

web一般選擇都是掃碼支付。

類似於這個:

2.準備賬號

首先要申請一個公共賬號,最好申請的時候就是服務號,因爲微信支付的前提必須是服務號。

如果是公衆號需要先申請驗證才能升級爲服務號。

進入下面的連接

http://jingyan.baidu.com/article/22fe7ced23fa183002617fa1.html

應用祕鑰 (AppSecret)

在公共號平臺上找

 下面鏈接可以幫助你去找到這個賬號:

http://jingyan.baidu.com/article/22fe7ced23fa183002617fa1.html

注意:

以上兩個賬號都可以在類型爲服務號的公衆號上找到,下面的兩個賬號就必須要在商戶平臺上纔可以找到。

大概步驟:

一、公衆號爲服務號

http://jingyan.baidu.com/article/fea4511a7eaf2cf7bb9125a7.html

二、申請微信認證

http://kf.qq.com/faq/120911VrYVrA150929Fjqeei.html

三、申請掃碼支付

升級完之後左側菜單會多一個“微信支付”選項

第三步成功之後會收到一個郵件,郵件中會有登錄商戶平臺的賬號和密碼

商戶id(mch_id)

申請支付成功之後登錄商戶平臺就可以看到這個值了。

API祕鑰 (api_key)

這個必須要登錄商戶平臺去設置。

以上兩個賬號的值可以參考下面的鏈接:

http://help.ecmoban.com/article-2085.html

查看掃碼支付API瞭解流程

https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5

進行開發

<!-- 生成二維碼所需的jar包 開始 -->
        <dependency>
              <groupId>com.uqihong</groupId>
              <artifactId>qdcode</artifactId>
              <version>1.0.0</version>
        </dependency>
        <dependency>
              <groupId>com.uqihong</groupId>
              <artifactId>qdcodeSwetake</artifactId>
              <version>1.0.0</version>
        </dependency>
        <!-- 生成二維碼所需的jar包 結束 -->
<!-- 微信支付需要的jar包 -->
        <dependency>
            <groupId>xmlpull</groupId>
            <artifactId>xmlpull</artifactId>
            <version>1.1.3.1</version>
        </dependency>
        <dependency>
            <groupId>xpp3</groupId>
            <artifactId>xpp3</artifactId>
            <version>1.1.4c</version>
        </dependency>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>fluent-hc</artifactId>
            <version>4.3.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient-cache</artifactId>
            <version>4.3.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.5</version>
        </dependency>

 調用預生成訂單API獲取到掃碼鏈接

/**
     * 創建預支付訂單
     * 
     * @param goodsName 商品名稱 
     * @param orderId   訂單id
     * @param payPrice  支付價格
     * 
     * @return 微信支付二維碼鏈接
    */
    public String createPrePayOrder(String goodsName, String orderId, double payPrice) {
        return dowithWxReturn(sendReqGetPreOrder(goodsName, orderId, payPrice));
    }

    /**
     * 處理微信返回
     * <p>
     * 處理的是調用微信預支付訂單的返回值
     * 
     * @param result 微信的返回值
     * 
     * @return 二維碼鏈接
     */
    private String dowithWxReturn(String result) {
        String codeUrl = "";
        Map<String, Object> weixinPrepayInfo = MapUtil.map();
        try {
            weixinPrepayInfo = XMLParser.getMapFromXML(result);
            String return_code = (String) weixinPrepayInfo.get("return_code");
            if ("SUCCESS".equals(return_code)) {
                codeUrl = (String) weixinPrepayInfo.get("code_url");
                if (Util.isEmpty(codeUrl)) {
                    throw ExceptionUtil.bEx(ConvertUtil.obj2str(weixinPrepayInfo.get("err_code_des")));
                }
                return codeUrl;
            } else {
                throw ExceptionUtil.bEx("預支付失敗");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            ExceptionUtil.bEx("調用微信預支付接口出錯");
        }
        return codeUrl;
    }
    /**
     * 發送預生成訂單請求
     * 
     * @param goodsName 商品名稱
     * @param orderId 訂單號
     * @param payPrice 支付價格
     * 
     * @return 微信處理結果
    */
    private String sendReqGetPreOrder(String goodsName, String orderId, double payPrice) {
        ExceptionUtil.checkEmpty(orderId, "訂單id不能爲空");
        ExceptionUtil.checkEmpty(goodsName, "商品名稱不能爲空");
        Map<String, Object> params = MapUtil.map();
        //公共賬號id
        params.put("appid", WeixinH5PayConfigure.APPID);
        //商戶id
        params.put("mch_id", WeixinH5PayConfigure.MCH_ID);
        //設備號
        //ctrl+alt+T
        params.put("device_info", "WEB");
        //隨機字符串
        params.put("nonce_str", RandomUtil.randomString(randomNumLength));
        //商品描述
        params.put("body", goodsName);
        //訂單編號
        params.put("out_trade_no", orderId);
        //金額
        params.put("total_fee", AmountUtils.changeY2F(payPrice));
        //回調地址
        params.put("notify_url", WeixinH5PayConfigure.NOTIFY_ACTIVITY_URL);
        params.put("trade_type", WeixinH5PayConfigure.TRADE_TYPE);
        //簽名
        String sign = Signature.getSign(params, WeixinH5PayConfigure.API_KEY);
        params.put("sign", sign);
        return HttpRequest.sendPost(WeixinH5PayConfigure.PAY_UNIFIED_ORDER_API, params);
    }

根據掃碼鏈接生成二維碼

/**
     * 獲取二維碼
     *
     * @param orderId 訂單id
     * 
     * @return 二維碼的地址
    */
    public Object getQrCode(long orderId) {
        ExceptionUtil.checkId(orderId, "訂單id不能爲空");
        return getWxCodeImagePath(getWxPayCodeUrl(orderId));
    }
    /**
     * 獲取微信二維碼支付的鏈接
     * 
     * @param orderId 訂單號
     * 
     * @return 二維碼支付的鏈接
    */
    private String getWxPayCodeUrl(long orderId) {
        OrderEntity order = orderBaseService.getWithEx(orderId);
        PackageEntity pk = packageBaseService.getWithEx(order.getPackageId());
        //double paymentPrice = 0.1;
        double paymentPrice = order.getPaymentPrice();
        String wxPayCodeUrl = wxPayService.createPrePayOrder(pk.getName(), ConvertUtil.obj2str(orderId), paymentPrice);
        return wxPayCodeUrl;
    }
    /**
     * 獲取微信二維碼的圖片地址
     *
     * @param wxPayCodeUrl 微信二維碼鏈接
     * 
     * @return 含有微信掃碼支付的二維碼地址
    */
    private String getWxCodeImagePath(String wxPayCodeUrl) {
        String qrCodePath = QrcodeUtil.getQrCodePath(kvConfig, UqihongUploadFoldType.QRCODE);
        QrcodeUtil.encoderQRCode(wxPayCodeUrl, qrCodePath);
        return QrcodeUtil.getRelationPath(qrCodePath, kvConfig);
    }

編寫微信支付完成的回調邏輯

 /**
     * 微信提醒
     * <P>
     * 微信支付成功的回調
     * 
     * @throws Exception 
     */
    public void wxPayFinishNotify() throws Exception {
        HttpServletRequest request = Mvcs.getReq();
        HttpServletResponse response = Mvcs.getResp();
        String responseString = getWeiXinResponseContent(request);
        PrintWriter out = response.getWriter();
        String resp = "";
        String signKey = WeixinH5PayConfigure.API_KEY;
        boolean verify = Signature.checkIsSignValidFromResponseString(responseString, signKey);
        if (!verify) {
            resp = "簽名驗證失敗";
            out.write(resp);
            out.close();
            return;
        }
        Map<String, Object> map = XMLParser.getMapFromXML(responseString);
        String result_code = ConvertUtil.obj2str(map.get("result_code"));
        if (!"SUCCESS".equalsIgnoreCase(result_code)) {
            resp = PayCommonUtil.getResponseXML("ERROR", "ERROR");
            out.write(resp);
            out.close();
            return;
        }
        resp = handleOrder(map);
        out.write(resp);
        out.close();
    }
    @Aop("txDb")
    private String handleOrder(Map<String, Object> map) throws Exception {
        String resp = PayCommonUtil.getResponseXML("SUCCESS", "OK");
        //微信支付訂單號
        String transaction_id = ConvertUtil.obj2str(map.get("transaction_id"));
        //支付完成時間
        String time_end = ConvertUtil.obj2str(map.get("time_end"));
        //訂單號
        String out_trade_no = (String) map.get("out_trade_no");
        if (Util.isEmpty(transaction_id) || Util.isEmpty(time_end) || Util.isEmpty(out_trade_no)) {
            resp = PayCommonUtil.getResponseXML("ERROR", "參數錯誤,微信支付訂單號、支付完成時間、訂單號均不能爲空");
            return resp;
        }
        OrderEntity order = orderBaseService.get(ConvertUtil.obj2long(out_trade_no));
        if (Util.isEmpty(order)) {
            resp = PayCommonUtil.getResponseXML("ERROR", "訂單不存在");
            return resp;
        }
        //判斷訂單狀態,避免重複處理
        int orderStatus = order.getStatus();
        if (OrderStatusEnum.FINISHED.intKey() == orderStatus) {
            return resp;
        }
        if (OrderStatusEnum.WAITING_PAY.intKey() == orderStatus) {
            //在這裏編寫你的邏輯即可
                                。。。。。。

        }
        return resp;
    }

資源

微信掃碼支付demo

https://my.oschina.net/xiaohui249/blog/656511 

生成二維碼demo

http://www.cnblogs.com/zhengbin/p/6777639.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章