您的位置:首页 - 文章 - 随笔 - 正文

Java对接微信支付

SDK使用方式

<dependency> <groupId>com.github.binarywang</groupId> <artifactId>(不同模块参考下文)</artifactId> <version>3.3.0</version>
</dependency>

各模块的artifactId:

  • 微信小程序:weixin-java-miniapp
  • 微信支付:weixin-java-pay
  • 微信开放平台:weixin-java-open
  • 公众号(包括订阅号和服务号):weixin-java-mp
  • 企业号/企业微信:weixin-java-cp

接口链接:https://github.com/binarywang
1.获得请求地址ip

/** * 获得请求地址ip * @param httpservletrequest * @return */ public static String getClientIP(HttpServletRequest httpservletrequest) { if (httpservletrequest == null) return null; String s = httpservletrequest.getHeader("X-Forwarded-For"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = httpservletrequest.getHeader("Proxy-Client-IP"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = httpservletrequest.getHeader("WL-Proxy-Client-IP"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = httpservletrequest.getHeader("HTTP_CLIENT_IP"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = httpservletrequest.getHeader("HTTP_X_FORWARDED_FOR"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = httpservletrequest.getRemoteAddr(); return s; }

2.封装统一下单类,获取"预支付交易会话标识"返回的结果 WxPayUnifiedOrderResult

public WxPayUnifiedOrderRequest toPayInfo(String clientIp) { WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest(); request.setDeviceInfo("WEB");//设备号 request.setBody(this.getProductName());//商品描述 request.setOutTradeNo(this.getOrderNo());//商户订单号 request.setDetail(this.getProductName());//商品详情 request.setTotalFee(this.getPrice());//总金额|分计 request.setSpbillCreateIp(clientIp);//终端IP request.setNotifyUrl(this.getNotifyUrl());//设置回调路径 request.setTradeType("JSAPI");//交易类型 request.setProductId(this.getProductId().toString());//商品id request.setOpenid(this.getOpenId()); //用户微信UUid return request; }

3.调用WxPayService接口的createOrder

/** * 调用统一下单接口,并组装生成支付所需参数对象. * * @param <T> 请使用{@link com.github.binarywang.wxpay.bean.order}包下的类 * @param request 统一下单请求参数 * @return 返回 {@link com.github.binarywang.wxpay.bean.order}包下的类对象 * @throws WxPayException the wx pay exception */
<T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException;

4.将返回的结果进行处理返回给前端
5.对回调函数进行处理 调用WxPayService 的 parseOrderNotifyResult方法

WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPayException;

项目实战:
依赖:

 <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-miniapp</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-pay</artifactId> <version>3.3.0</version> </dependency>
import org.springframework.context.annotation.Configuration;
@Configuration
public class WxProperties { //AppID(小程序ID) private String appId=""; //AppSecret(小程序密钥) private String appSecret=""; //支付商户mchId private String mchId=""; //支付商户mchKey private String mchKey=""; //支付回调 private String notifyUrl=""; //证书 private String keyPath=""; public String getNotifyUrl() { return notifyUrl; } public void setNotifyUrl(String notifyUrl) { this.notifyUrl = notifyUrl; } public String getMchKey() { return mchKey; } public void setMchKey(String mchKey) { this.mchKey = mchKey; } public String getAppId() { return this.appId; } public void setAppId(String appId) { this.appId = appId; } public String getAppSecret() { return appSecret; } public void setAppSecret(String appSecret) { this.appSecret = appSecret; } public String getMchId() { return mchId; } public void setMchId(String mchId) { this.mchId = mchId; } public String getKeyPath() { return keyPath; } public void setKeyPath(String keyPath) { this.keyPath = keyPath; }
}

SpringBoot配置

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WxConfig { @Autowired private WxProperties properties; @Bean public WxMaConfig wxMaConfig() { WxMaInMemoryConfig config = new WxMaInMemoryConfig(); config.setAppid(properties.getAppId()); config.setSecret(properties.getAppSecret()); return config; } @Bean public WxMaService wxMaService(WxMaConfig maConfig) { WxMaService service = new WxMaServiceImpl(); service.setWxMaConfig(maConfig); return service; } @Bean public WxPayConfig wxPayConfig() { WxPayConfig payConfig = new WxPayConfig(); payConfig.setAppId(properties.getAppId()); payConfig.setMchId(properties.getMchId()); payConfig.setMchKey(properties.getMchKey()); payConfig.setNotifyUrl(properties.getNotifyUrl()); payConfig.setKeyPath(properties.getKeyPath()); payConfig.setTradeType("JSAPI"); payConfig.setSignType("MD5"); return payConfig; } @Bean public WxPayService wxPayService(WxPayConfig payConfig) { WxPayService wxPayService = new WxPayServiceImpl(); wxPayService.setConfig(payConfig); return wxPayService; }
}

Controller类

@Resource
private WxPayService wxPayService;
@RequestMapping("/addOrder")
@ResponseBody
@ApiOperation("统一下单")
public Object addOrder(HttpServletRequest request) { WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest(); request.setDeviceInfo("WEB");//设备号 request.setBody(this.getProductName());//商品描述 request.setOutTradeNo(this.getOrderNo());//商户订单号 request.setDetail(this.getProductName());//商品详情 request.setTotalFee(this.getPrice());//总金额|分计 元转成分 request.setSpbillCreateIp(clientIp);//终端IP request.setNotifyUrl(this.getNotifyUrl());//设置回调路径 request.setTradeType("JSAPI");//交易类型 request.setProductId(this.getProductId().toString());//商品id request.setOpenid(this.getOpenId()); //return request; WxPayMpOrderResult result=wxPayService.createOrder(request);
}
//获取请求ip
private String getClientIP(HttpServletRequest httpservletrequest) { if (httpservletrequest == null) return null; String s = httpservletrequest.getHeader("X-Forwarded-For"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = httpservletrequest.getHeader("Proxy-Client-IP"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = httpservletrequest.getHeader("WL-Proxy-Client-IP"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = httpservletrequest.getHeader("HTTP_CLIENT_IP"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = httpservletrequest.getHeader("HTTP_X_FORWARDED_FOR"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = httpservletrequest.getRemoteAddr(); return s; }
WxPayUnifiedOrderRequest(
version = null, deviceInfo = null, body = 订单:1326800541645557760, detail = null, attach = null, outTradeNo = 1326800541645557760, feeType = null, totalFee = 17800, spbillCreateIp = 192.168.110.202, timeStart = null, timeExpire = null, goodsTag = null, notifyUrl = https: //www.abc.com/selectOrder, tradeType=JSAPI, productId=null, limitPay=null, openid=of7625P4x5GqlvXV2C1MNVp_eee, subOpenid=null, sceneInfo=null, fingerprint=null
)
WxPayMpOrderResult(
appId=wxa400000000000000, timeStamp=1605168875, nonceStr=1605168875182, packageValue=prepay_id=wx00000000000000000000000000, signType=MD5, paySign=8EBEDF882600000000000000000000
)

回调

@Resource private WxPayService wxPayService;
@RequestMapping("/selectOrder")
@ResponseBody
@ApiOperation("订单查询")
public void selectOrder(HttpServletRequest req, HttpServletResponse resp, @RequestBody String body) throws WxPayException, IOException { WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(body); // 结果正确 outTradeNo String orderId = result.getOutTradeNo(); String tradeNo = result.getTransactionId(); String totalFee = BaseWxPayResult.fenToYuan(result.getTotalFee()); System.out.println("交易单号:" + result.getTransactionId()); System.out.println("订单号:" + result.getOutTradeNo()); System.out.println("付款金额:" + BaseWxPayResult.fenToYuan(result.getTotalFee())); if ("SUCCESS".equals(result.getResultCode())) { System.out.println("支付回调进来了:付款成功"); } else { System.out.println("支付回调进来了:付款失败"); }
}

 

本文原创,作者:西决,其版权均为品创网络所有。如需转载,请注明出处:https://www.sxpcwlkj.com/038/

发表评论