WeChat scan code payment (java version)

1. Add the following jar package to the pom.xml of the maven project:

        <dependency>
            <groupId>com.github.wxpay</groupId>
            <artifactId>wxpay-sdk</artifactId>
            <version>0.0.3</version>
        </dependency>
2. Write the WeWxConfig class:
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;

/**
 * Description: WeChat payment configuration information
 *
 * @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;

    /**
     * Public Account ID: The public account ID assigned by WeChat Pay (Corporate ID is the appId)
     *
     * @return
     */
    @Override
    public String getAppID() {
        return appId;
    }

    /**
     * Merchant ID: Merchant ID assigned by WeChat Pay
     *
     * @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. Write 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;

/**
 * describe:
 *
 * @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;


    /**
     * Unified order
     *
     * @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", "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"); // This is designated as scan code payment
        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;
    }

    /**
     * Order Tracking
     *
     * @param orderNo: order number
     * @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", "Product Details");
        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"); // This is designated as scan code payment
        data.put("product_id", "12");

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

3. Call:

/** Place an order with the WeChat payment system and get a QR code to return to the user*/
Map<String, String> resData = weWxPayService.unifiedOrder(product, order);

4. resData.get("code_url") is the QR code address returned after the WeChat order is successfully placed. QRCode.js is used on the page to display the QR code, and the page uses a timer to regularly query the payment status of the order

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325879352&siteId=291194637