微信支付 h5支付

1. 调起微信支付的域名要和微信支付配置的一致,导致无法本地测试

解决方案: 修改本地host文件将配置的域名指向本机, 手机wifi使用本机代理上网。

IJPay-All文档地址

1. maven中引入开发包

        <dependency>
            <groupId>com.github.javen205groupId>
            <artifactId>IJPay-AllartifactId>
            <version>2.2.0version>
        dependency>

2. 创建请求需要的参数类。配置信息在application中

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;

@Data
@Configuration
public class WxPayProperties {
    /**
     * 设置微信公众号或者小程序,h5等的appid.
     */
    @Value("${wx.pay.appId}")
    private String appId;

    /**
     * 微信支付商户号.
     */
    @Value("${wx.pay.mchId}")
    private String mchId;

    /**
     * 微信支付商户密钥.
     */
    @Value("${wx.pay.mchKey}")
    private String mchKey;

    /**
     * 异步回调地址
     */
    @Value("${wx.pay.notifyUrl}")
    private String notifyUrl;
}

application配置文件 如下
wx:
  pay:
    appId: wx123242342341 # appid
    mchId: 123452123 # 商户号
    mchKey: U6gG1CqAqwedrfghxH8eMuDrvfXFBokr # 秘钥 微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
    notifyUrl: http://169b91h550.imwork.net:35084/wx/wxPayNotice #可以被外网访问的接口 

3. 请求示例

        // 支付金额
        BigDecimal paymentAmount = new BigDecimal("0.01");
        // 支付ip
        String ip = getIpAddr();
        // 创建商户订单号
        String platformOrderNumber = WxPayKit.generateStr();
        Map<String, String> params = UnifiedOrderModel
                .builder()
                .appid(wxPayProperties.getAppId())
                .mch_id(wxPayProperties.getMchId())
                .nonce_str(platformOrderNumber) // 随机字符串
                .body("微信支付")
                .out_trade_no(platformOrderNumber)
                .total_fee(String.valueOf((paymentAmount.multiply(new BigDecimal("100"))).intValue()))
                .spbill_create_ip(ip)
                .notify_url(wxPayProperties.getNotifyUrl())
                .trade_type(TradeType.MWEB.getTradeType())
                .build()
                .createSign(wxPayProperties.getMchKey(), SignType.MD5);
        String xmlResult = WxPayApi.pushOrder(false, params);
        Map<String, String> resultMap = WxPayKit.xmlToMap(xmlResult);
        if (StringUtils.equals(resultMap.get("return_code"),"SUCCESS")) {
            // 下单成功 
            // 前端重定向的地址  js使用  window.location.href 跳转此地址就行了 
            String url = resultMap.get("mweb_url")
            ....
         }
获取客户端IP地址
    private String getIpAddr() {
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值,第一个ip才是真实ip
            if (ip.indexOf(",") != -1) {
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip != null ? ip : "192.168.0.1";
    }

异步通知

    @PostMapping("/notifyUrl")
    public void notifyUrl(@RequestBody String xmlData) {
        Map<String, String> resultMap = WxPayKit.xmlToMap(xmlData);
         // 平台订单号
        String platformOrderNumber =  resultMap.get("out_trade_no");
        // 实际支付金额
        BigDecimal paymentAmount = new BigDecimal(resultMap.get("cash_fee"));
        // 微信订单号
        String transactionId =  resultMap.get("transaction_id");
        // 支付完成时间
        String timeEnd =  resultMap.get("timeEnd");
        ....
    }

你可能感兴趣的:(后端)