当前位置: 首页 > 工具软件 > IJPay > 使用案例 >

IJPay-All 之 微信支付之app支付

丁经略
2023-12-01

IJPay-All文档地址

1. maven中引入开发包

<dependency>
    <groupId>com.github.javen205</groupId>
    <artifactId>IJPay-WxPay</artifactId>
    <version>2.4.0</version>
</dependency>

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

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

@Data
public class WxPayProperties {
    /**
     * 设置微信公众号或者小程序等的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: wx123234442341 # appid
    mchId: 12345678 # 商户号
    mchKey: U6gG1CqAqwedrfffffokr # 秘钥 微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
    notifyUrl: http://169b91h550.imwork.net:35084/wx/wxPayNotice #可以被外网访问的接口 

3. 创建WxPayProperties实例

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WxConfiguration {

    @Bean("wxPayProperties")
    public WxPayProperties wxPayProperties(){
        return new WxPayProperties();
    }
}

4. 构建请求体,发起统一下单请求 注意:app支付需要二次签名,很重要

// 构建请求体
Map<String, String> params = UnifiedOrderModel
                .builder()
                .appid(wxPayProperties.getAppId())
                .mch_id(wxPayProperties.getMchId())
                .nonce_str(WxPayKit.generateStr())
                .body("刘志强支付")
                .out_trade_no(WxPayKit.generateStr()))
                .total_fee("100") // 单位是分
                .spbill_create_ip("123.123.123.123") // 终端ip
                .notify_url(wxPayProperties.getNotifyUrl())
                .trade_type(TradeType.APP.getTradeType())
                .build()
                .createSign(wxPayProperties.getMchKey(), SignType.MD5);
// 发送请求
String xmlResult = WxPayApi.pushOrder(false, params);
Map<String, String> resultMap = WxPayKit.xmlToMap(xmlResult);
// 二次签名
Map<String, String> packageParams = new HashMap<String, String>();
packageParams.put("appid", resultMap.get("appid"));
packageParams.put("partnerid", wxPayProperties.getMchId());
packageParams.put("prepayid", resultMap.get("prepay_id"));
packageParams.put("package", "Sign=WXPay");
packageParams.put("noncestr", resultMap.get("nonce_str"));
packageParams.put("timestamp", System.currentTimeMillis() / 1000 + "");
String packageSign = WxPayKit.createSign(packageParams, wxPayProperties.getMchKey(), SignType.MD5);
packageParams.put("sign", packageSign);
packageParams.put("return_code","SUCCESS");
return packageParams;

5. 将packageParams发送给前端用于调起支付

6. 创建微信异步回调接口,也是在application.yml中配置的notifyUrl

@PostMapping("/wx/wxPayNotice")
public void parseOrderNotifyResult(@RequestBody String xmlData) {
    Map<String, String> resultMap = WxPayKit.xmlToMap(xmlData);
    // 根据resultMap处理订单业务
}

7. 获取终端ip的方法

    private String getClientIp() {
        String ipAddress = httpServletRequest.getRemoteAddr();
        if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
            //根据网卡获取本机配置的IP地址
            InetAddress inetAddress = null;
            try {
                inetAddress = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            ipAddress = inetAddress.getHostAddress();
        }
        //对于通过多个代理的情况,第一个IP为客户端真实的IP地址,多个IP按照','分割
        if (null != ipAddress && ipAddress.length() > 15) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress != null ? ipAddress : "123.12.12.123";
    }
 类似资料: