1. 程式人生 > >微信掃碼支付(java版)

微信掃碼支付(java版)

1、maven專案的pom.xml中新增如下jar包:

        <dependency>
            <groupId>com.github.wxpay</groupId>
            <artifactId>wxpay-sdk</artifactId>
            <version>0.0.3</version>
        </dependency>
2、編寫WeWxConfig類:
package com.xx.wxpay;

import com.github.wxpay.sdk.WXPayConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;

/**
 * 描述:微信支付配置資訊
 *
 * @author ssl
 * @create 2018/04/24 19:25
 */
@Component
public class WeWxConfig implements WXPayConfig {
    @Value("${wechat.public.appid}")
    private String appId;
    @Value("${wechat.merchant}")
    private String mchId;
    @Value("${wechat.public.apikey}")
    private String apiKey;

    /**
     * 公眾賬號ID:微信支付分配的公眾賬號ID(企業號corpid即為此appId)
     *
     * @return
     */
    @Override
    public String getAppID() {
        return appId;
    }

    /**
     * 商戶號:微信支付分配的商戶號
     *
     * @return
     */
    @Override
    public String getMchID() {
        return mchId;
    }

    /**
     * @return
     */
    @Override
    public String getKey() {
        return apiKey;
    }

    @Override
    public InputStream getCertStream() {
        return null;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 0;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 0;
    }
}
2、編寫WeWxPayService:
package com.xx.wxpay;

import com.alibaba.fastjson.JSONObject;
import com.github.wxpay.sdk.WXPay;
import com.google.common.collect.Maps;
import com.xx.model.Order;
import com.xx.model.Product;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述:
 *
 * @author ssl
 * @create 2018/04/24 20:15
 */
@Service
public class WeWxPayService {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Value("${project.url}")
    private String projectUrl;
    @Autowired
    private WeWxConfig weWxConfig;


    /**
     * 統一下單
     *
     * @param product
     * @param order
     * @return
     */
    public Map<String, String> unifiedOrder(Product product, Order order) {
        Map<String, String> data = Maps.newHashMap();
        WXPay wxpay = new WXPay(weWxConfig);
        data.put("body", "XX-" + product.getName());
        data.put("detail", "詳細資訊");
        data.put("out_trade_no", order.getOrderNo());
        data.put("device_info", "WEB");
        data.put("fee_type", "CNY");
        data.put("total_fee", order.getAmount() + "");
        data.put("spbill_create_ip", "127.0.0.1");
        data.put("notify_url", projectUrl + "/base/order/notifyUrl");
        data.put("trade_type", "NATIVE");  // 此處指定為掃碼支付
        data.put("product_id", product.getId() + "");
        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            logger.debug(JSONObject.toJSONString(resp));
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 訂單查詢
     *
     * @param orderNo:訂單號
     * @return
     */
    public Map<String, String> orderQuery(String orderNo) {
        Map<String, String> reqData = Maps.newHashMap();
        reqData.put("out_trade_no", orderNo);
        WXPay wxpay = new WXPay(weWxConfig);
        try {
            Map<String, String> resp = wxpay.orderQuery(reqData);
            logger.debug(JSONObject.toJSONString(resp));
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getUrl() {
        WXPay wxpay = new WXPay(new WeWxConfig());
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "上屏名稱");
        data.put("detail", "商品詳情");
        data.put("out_trade_no", "2ab9071b06b9f739b950ddb41db2690d");
        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", "1");
        data.put("spbill_create_ip", "218.17.160.245");
        data.put("notify_url", "http://www.example.com/wxpay/notify");
        data.put("trade_type", "NATIVE");  // 此處指定為掃碼支付
        data.put("product_id", "12");

        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}

3、呼叫:

/** 向微信支付系統下單,並得到二維碼返回給使用者 */
Map<String, String> resData = weWxPayService.unifiedOrder(product, order);

4、resData.get("code_url")為微信下單成功後返回的二維碼地址,頁面中用QRCode.js來顯示該二維碼,且該頁面用定時器定時查詢訂單支付狀態