Java 后端微信支付demo
一、导入微信SDK
二、在微信商户平台下载证书放在项目的resources目录下的cert文件夹下(cert文件夹需要自己建)
三、实现微信的WXPayConfig接口
package com.ieou.demo.common; import com.github.wxpay.sdk.WXPayConfig;
import org.apache.commons.io.IOUtils; import java.io.ByteArrayInputStream;
import java.io.InputStream; /** 配置我们自己的信息 */ public class OurWxPayConfig implements WXPayConfig { /** 加载证书 这里证书需要到微信商户平台进行下载*/
private byte [] certData; public OurWxPayConfig() throws Exception{
InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cert/wxpay/apiclient_cert.p12");
this.certData = IOUtils.toByteArray(certStream);
certStream.close();
} /** 设置我们自己的appid
* 商户号
* 秘钥
* */ @Override
public String getAppID() {
return "wx7494893843843c";
} @Override
public String getMchID() {
return "4672984344";
} @Override
public String getKey() {
return "qbH5l4N3468798dfgK";
} @Override
public InputStream getCertStream() {
return new ByteArrayInputStream(this.certData);
} @Override
public int getHttpConnectTimeoutMs() {
return 0;
} @Override
public int getHttpReadTimeoutMs() {
return 0;
}
}
四、构建自己的参数
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;
}
}
五、配置微信统一下单接口需要的参数,并调用微信统一下单接口
package com.ieou.demo.controller; import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.ieou.demo.common.OurWxPayConfig;
import com.ieou.demo.common.WxpayParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.HashMap;
import java.util.Map; @RestController
@RequestMapping("/")
public class WxPayController { @GetMapping("/pay")
public Map<String,String> wxPayFunction() throws Exception{ WxpayParam wxpayParam = new WxpayParam();
String notifyUrl = "www.baidu.com"; //我这里的回调地址是随便写的,到时候需要换成处理业务的回调接口 OurWxPayConfig ourWxPayConfig = new OurWxPayConfig();
WXPay wxPay = new WXPay(ourWxPayConfig); //根据微信支付api来设置
Map<String,String> data = new HashMap<>();
data.put("appid",ourWxPayConfig.getAppID());
data.put("mch_id",ourWxPayConfig.getMchID()); //商户号
data.put("trade_type","APP"); //支付场景 APP 微信app支付 JSAPI 公众号支付 NATIVE 扫码支付
data.put("notify_url",notifyUrl); //回调地址
data.put("spbill_create_ip","127.0.0.1"); //终端ip
data.put("total_fee",wxpayParam.getTotalFee()); //订单总金额
data.put("fee_type","CNY"); //默认人民币
data.put("out_trade_no",wxpayParam.getOutTradeNo()); //交易号
data.put("body",wxpayParam.getBody());
data.put("nonce_str","bfrhncjkfdkfd"); // 随机字符串小于32位
String s = WXPayUtil.generateSignature(data, ourWxPayConfig.getKey()); //签名
data.put("sign",s); /** wxPay.unifiedOrder 这个方法中调用微信统一下单接口 */
Map<String, String> respData = wxPay.unifiedOrder(data);
if (respData.get("return_code").equals("SUCCESS")){ //返回给APP端的参数,APP端再调起支付接口
Map<String,String> repData = new HashMap<>();
repData.put("appid",ourWxPayConfig.getAppID());
repData.put("mch_id",ourWxPayConfig.getMchID());
repData.put("prepayid",respData.get("prepay_id"));
repData.put("package","WXPay");
repData.put("noncestr",respData.get("nonce_str"));
repData.put("timestamp",String.valueOf(System.currentTimeMillis()/1000));
String sign = WXPayUtil.generateSignature(repData,ourWxPayConfig.getKey()); //签名
respData.put("sign",sign);
respData.put("timestamp",repData.get("timestamp"));
respData.put("package","WXPay");
return respData;
}
throw new Exception(respData.get("return_msg"));
}
}
六、至此,后端代码已经结束,前端拿到返回值调用微信发起支付接口就可以了。