Java 后端微信支付demo

 

  

Java 后端微信支付demo

一、导入微信SDK

二、在微信商户平台下载证书放在项目的resources目录下的cert文件夹下(cert文件夹需要自己建)

三、实现微信的WXPayConfig接口

 1 package com.ieou.demo.common;
 2 
 3 
 4 import com.github.wxpay.sdk.WXPayConfig;
 5 import org.apache.commons.io.IOUtils;
 6 
 7 import java.io.ByteArrayInputStream;
 8 import java.io.InputStream;
 9 
10 /** 配置我们自己的信息  */
11 
12 public class OurWxPayConfig implements WXPayConfig {
13 
14     /** 加载证书  这里证书需要到微信商户平台进行下载*/
15     private byte [] certData;
16 
17     public OurWxPayConfig() throws  Exception{
18         InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cert/wxpay/apiclient_cert.p12");
19         this.certData = IOUtils.toByteArray(certStream);
20         certStream.close();
21     }
22 
23     /** 设置我们自己的appid
24      * 商户号
25      * 秘钥
26      * */
27 
28     @Override
29     public String getAppID() {
30         return "wx7494893843843c";
31     }
32 
33     @Override
34     public String getMchID() {
35         return "4672984344";
36     }
37 
38     @Override
39     public String getKey() {
40         return "qbH5l4N3468798dfgK";
41     }
42 
43     @Override
44     public InputStream getCertStream() {
45         return new ByteArrayInputStream(this.certData);
46     }
47 
48     @Override
49     public int getHttpConnectTimeoutMs() {
50         return 0;
51     }
52 
53     @Override
54     public int getHttpReadTimeoutMs() {
55         return 0;
56     }
57 }

四、构建自己的参数

package com.ieou.demo.common;

import java.math.BigDecimal;

public class WxpayParam {


    /** 微信支付的金额是String类型 并且是以分为单位
     * 下面举个例子单位是元是怎么转为分的
     * */
    BigDecimal totalPrice  = new BigDecimal(1); //此时的单位是元

    private String body = "xxx等商品信息";
    private String totalFee = totalPrice.multiply(new BigDecimal(100)).toBigInteger().toString();
    /** 随机数字字符串*/
    private String outTradeNo = "4784984230432842944";

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getTotalFee() {
        return totalFee;
    }

    public void setTotalFee(String totalFee) {
        this.totalFee = totalFee;
    }

    public String getOutTradeNo() {
        return outTradeNo;
    }

    public void setOutTradeNo(String outTradeNo) {
        this.outTradeNo = outTradeNo;
    }
}

五、配置微信统一下单接口需要的参数,并调用微信统一下单接口

 1 package com.ieou.demo.controller;
 2 
 3 import com.github.wxpay.sdk.WXPay;
 4 import com.github.wxpay.sdk.WXPayUtil;
 5 import com.ieou.demo.common.OurWxPayConfig;
 6 import com.ieou.demo.common.WxpayParam;
 7 import org.springframework.web.bind.annotation.GetMapping;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 import java.util.HashMap;
12 import java.util.Map;
13 
14 
15 @RestController
16 @RequestMapping("/")
17 public class WxPayController {
18 
19     @GetMapping("/pay")
20     public Map<String,String> wxPayFunction() throws Exception{
21 
22         WxpayParam wxpayParam = new WxpayParam();
23         String notifyUrl = "www.baidu.com";  //我这里的回调地址是随便写的,到时候需要换成处理业务的回调接口
24 
25         OurWxPayConfig ourWxPayConfig = new OurWxPayConfig();
26         WXPay wxPay = new WXPay(ourWxPayConfig);
27 
28         //根据微信支付api来设置
29         Map<String,String> data = new HashMap<>();
30         data.put("appid",ourWxPayConfig.getAppID());
31         data.put("mch_id",ourWxPayConfig.getMchID());         //商户号
32         data.put("trade_type","APP");                         //支付场景 APP 微信app支付 JSAPI 公众号支付  NATIVE 扫码支付
33         data.put("notify_url",notifyUrl);                     //回调地址
34         data.put("spbill_create_ip","127.0.0.1");             //终端ip
35         data.put("total_fee",wxpayParam.getTotalFee());       //订单总金额
36         data.put("fee_type","CNY");                           //默认人民币
37         data.put("out_trade_no",wxpayParam.getOutTradeNo());   //交易号
38         data.put("body",wxpayParam.getBody());
39         data.put("nonce_str","bfrhncjkfdkfd");   // 随机字符串小于32位
40         String s = WXPayUtil.generateSignature(data, ourWxPayConfig.getKey());  //签名
41         data.put("sign",s);
42         
43         /** wxPay.unifiedOrder 这个方法中调用微信统一下单接口 */
44         Map<String, String> respData = wxPay.unifiedOrder(data); 
45         if (respData.get("return_code").equals("SUCCESS")){
46 
47             //返回给APP端的参数,APP端再调起支付接口
48             Map<String,String> repData = new HashMap<>();
49             repData.put("appid",ourWxPayConfig.getAppID());
50             repData.put("mch_id",ourWxPayConfig.getMchID());
51             repData.put("prepayid",respData.get("prepay_id"));
52             repData.put("package","WXPay");
53             repData.put("noncestr",respData.get("nonce_str"));
54             repData.put("timestamp",String.valueOf(System.currentTimeMillis()/1000));
55             String sign = WXPayUtil.generateSignature(repData,ourWxPayConfig.getKey()); //签名
56             respData.put("sign",sign);
57             respData.put("timestamp",repData.get("timestamp"));
58             respData.put("package","WXPay");
59             return respData;
60         }
61         throw new Exception(respData.get("return_msg"));
62     }
63 }

六、至此,后端代码已经结束,前端拿到返回值调用微信发起支付接口就可以了。

posted @ 2018-03-26 15:11  低调的小白  阅读(44818)  评论(9编辑  收藏  举报