欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信小程序支付开发(Java后台)

程序员文章站 2024-03-22 08:14:58
...

小程序开发微信支付过程: 

申请好企业版小程序;

登录微信公众平台: https://mp.weixin.qq.com/

  • 开发管理开发设置,获取AppID,AppSecre设置服务器域名及对应IP白名单等信息。

微信小程序支付开发(Java后台)

获取商户ID(商户号)及支付秘钥(API安全那里)

微信小程序支付开发(Java后台)

微信小程序支付开发(Java后台)

  • 接下来编码:直接将代码搞进去,一些信息及逻辑换成自己的即可运行测试

官方开发文档见:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1

具体实现代码参考文章:https://blog.csdn.net/u011134780/article/details/90609548

代码经过实践基本没啥问题,只是有的地方没有,这里指出:

1、BeanToMap.getMapFromXML 这个其实就是用的PayUtil.getMapFromXML

2、有人问PayConstant这个类没必要出现,可以不用,成功的可以用字符串代替或者自己写常量类

3、PayConstant在PayConfig这个类也出现,所以这个类也一起删掉,就是从读取配置文件读取一些配置信息,这些信息可以写入常量类中,也直接用

@Value("${pay.env}")
private String ENV;

这种方式直接获取,方便很多

4、RequestStr.getRequestStr这个也没有,可以自己实现,解析前端传过来的参数,公司一般自己有,我的实现是用@RequestBody接受的,方便很多,没用HttpServletRequest request

5、HttpUtils.postData 这个比较关键,这里补充下,不过网上一搜也有很多发送post请求的,区别多了个SSL,这里贴下代码

public static String doHttpsPostXml(String url, String xmlParam) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost https = new HttpPost(url);
            // 创建请求内容
            https.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
            // 信任所有
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,
                    (TrustStrategy) (x509Certificates, authType) -> true).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            // 执行https请求
            response = httpClient.execute(https);

            if (response != null) {
                HttpEntity entity = response.getEntity();
                // 响应内容
                resultString = EntityUtils.toString(entity, Consts.UTF_8);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }

 

相关标签: 案例