舒大少博客

一个95后少年的个人博客

当前时间为:
欢迎大家来到舒大少博客https://www.9713job.com,广告合作以及淘宝商家推广请微信联系15357240395

java编写微信支付

2022-05-07 19:55:20
swq1822677238

手机扫码查看

java编写微信支付
  1. 注册微信商户
  2. 下载微信支付的 SDK
  3. 打开idea打开微信支付的sdk复制到自己的项目中
  4. 在项目里创建配置类,继承WXPayConfig类,在里面添加商户信息
    public class MyWxPayConfig extends WXPayConfig{
        @Override
        String getAppID() {
            return "商户appid";
        }
    
        @Override
        String getMchID() {
            return "商户号";
        }
    
        @Override
        String getKey() {
            return "证书秘钥";
        }
    
        @Override
        InputStream getCertStream() {
            return null;
        }
    
        @Override
        IWXPayDomain getWXPayDomain() {
            MyWXPayDomain domain=new MyWXPayDomain();
            return domain;
        }
    }
  5. 在项目中创建一个类实现IWXPayDomain接口
    @Override
    public void report(String domain, long elapsedTimeMillis, Exception ex) {
    
    }
    
    @Override
    public DomainInfo getDomain(WXPayConfig config) {
        DomainInfo info=new DomainInfo("api.mch.weixin.qq.com",true);
        return info;
    }
  6. 编写场景类支付
    public class TestPay {
        public static void main(String[] args) throws Exception {
            MyWxPayConfig config = new MyWxPayConfig();
    
            WXPay wxpay = new WXPay(config);
    
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", "腾讯充值中心-QQ会员充值");
            data.put("out_trade_no", "2022050719401100000012");
            data.put("device_info", "");
            data.put("fee_type", "CNY");
            data.put("total_fee", "1");
            data.put("spbill_create_ip", "123.12.12.123");
            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();
            }
        }
    }
  7. 内网穿透
    网址 natapp.cn,注册登录完成实名,然后购买免费隧道,复制token,下载客户端,在exe当前目录打开cmd,输入 natapp -authtoken=token
  8. 回应消息给微信后台
    @RequestMapping("/notify_url")
    public void doPayNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        byte[] buffer=new byte[1024];
        int len=0;
        while( (len= inputStream.read(buffer))!=-1 ){
            System.out.println(new String(buffer,0,len));
        }
        response.getWriter().write("<xml>\n" +
                "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                "</xml>");
    }
  9. zxing二维码生成
    <!--zxing二维码生成-->
    <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.3.3</version>
    </dependency>
    <!--zxing二维码生成-->
    <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>javase</artifactId>
        <version>3.4.0</version>
    </dependency>
  10. 绘制二维码
    // 绘制二维码
    @RequestMapping("/qrcode")
    public void qrcode(HttpServletResponse response)throws URISyntaxException, IOException {
        //二维码需要包含的文本内容
        String uri ="http://www.baidu.com";
        HashMap<EncodeHintType,Object> hints=new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN,2);
        try {
            BitMatrix bitMatrix=new MultiFormatWriter().encode(uri, BarcodeFormat.QR_CODE,200,200,hints);
            MatrixToImageWriter.writeToStream(bitMatrix,"PNG",response.getOutputStream());
            System.out.println("创建二维码完成");}
        catch(Exception e){
            e.printStackTrace();
        }
    }
  11. goeasy异步调用
    goeasy:网址:https://www.goeasy.io/
    加依赖

    <!-- goeasy 异步 -->
    <dependency>
        <groupId>io.goeasy</groupId>
        <artifactId>goeasy-sdk</artifactId>
        <version>0.3.16</version>
    </dependency>

    在 goeasy创建应用,获取Common key及hangzhou.goeasy.io

    在微信支付通知的控制层新增

    GoEasy goEasy = new GoEasy( "http://rest-hangzhou.goeasy.io/v2/pubsub/publish", "BC-3d456eb942cc4926b807cdb77dfb5216");
    goEasy.publish("wxpay", "success");

    下单页面

    <script src="http://hangzhou.goeasy.io/goeasy.js"></script>
    <script>
        //初始化
        var goeasy  = new GoEasy({
            host:"hangzhou.goeasy.io",  //若是新加坡区域:singapore.goeasy.io
            appkey:"BC-3d456eb942cc4926b807cdb77dfb5216",
            modules:['pubsub']//根据需要,传入‘pubsub’或'im’,或数组方式同时传入
        })
        var pubsub = goeasy.pubsub;
        goeasy.subscribe({
            channel: "wxpay",//替换为您自己的channel
            onMessage: function (message) {
                alert("收到:"+message.content);
                console.log("Channel:" + message.channel + " content:" + message.content);
            }
        });
    </script>

发表评论

邮箱地址不会被公开。 必填项已用*标注