This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

instructions

When the service needs the payment function, you can try using wechat Pay. Wechat Pay provides enterprises with the function of payment to users, supporting enterprises to pay through API interface, or through wechat Pay merchant platform webpage function operation payment.

Opening function

First of all, it needs to be opened on the official website, and the address is #PC website for access and payment

Authorized instructions

Obtain user’s OpenID through webpage authorization (enterprise payment to wechat user’s individual payment currently supports payment to designated wechat user’s OpenID.) For details, please refer to webpage authorization

API calling code

Sample request

Only some simple parameters are written. For details of specific parameters, see the payment developer documentation of wechat Pay

Map<String, String> reqData = new HashMap<>();
/ / signature
reqData.put("sign"."abc");
/ / users openid
reqData.put("openid"."1657446542123");
// Validate the user name option
reqData.put("check_name"."NO_CHECK");
// Amount of payment, in minutes
reqData.put("amount"."30.0");
// Payment remarks
reqData.put("desc"."Payment");
// Merchant account appid
reqData.put("mch_appid"."wx1234567065d35555");
/ / merchants
reqData.put("mchid"."1768826471");
/ / Ip address
reqData.put("spbill_create_ip", InetAddress.getLocalHost().getHostAddress());
// Random string
reqData.put("nonce_str"."lst");
// Convert to XML format
String xml = mapToXml(reqData);
String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
// Initiate a request
String response = HttpService.Post(xml, url, true.5000);
Copy the code
Map to XML method
public static String mapToXml(Map<String, String> data) throws Exception {
        Document document = PayXmlUtil.newDocument();
        Element root = document.createElement("xml");
        document.appendChild(root);
        Iterator var3 = data.keySet().iterator();

        while(var3.hasNext()) {
            String key = (String)var3.next();
            String value = (String)data.get(key);
            if (value == null) {
                value = "";
            }

            value = value.trim();
            Element filed = document.createElement(key);
            filed.appendChild(document.createTextNode(value));
            root.appendChild(filed);
        }

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        transformer.setOutputProperty("encoding"."UTF-8");
        transformer.setOutputProperty("indent"."yes");
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        String output = writer.getBuffer().toString();

        try {
            writer.close();
            return output;
        } catch(Exception e) { log.error(e.toString()); }}Copy the code

Pay attention to

The request header needs to be formatted as XML

httpPost.addHeader("Content-Type"."text/xml");
Copy the code

reference

  • 【 wechat Pay 】 Payment developer documents