java开发记录-微信H5支付

一点点关于微信H5支付开发的总结.

首先需要申请到响应的appid,key.具体申请方法前往官网:https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2Findex.php 可以选择对应的接入方式进行接入.

接入成功后就可以进行代码设计啦.

此次测试开发是在springboot环境下完成的,下面列出微信支付用到的相关依赖:

        <dependency>
        <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-pay</artifactId>
            <version>3.1.0</version>
        </dependency>

1.PayUtils工具类

用来保存appid,key,密钥以及商品户号信息,创建并配置好wxMpService用于后续启动微信统一下单的接口.

public class PayUtils {
	/**
	 * 公众号AppId
	 */
	public static final String APP_ID = "xxx";
	/**
	 * 公众号AppSecret
	 */
	public static final String APP_SECRET = "xxx";
	/**
	 * 微信支付商户号
	 */
	public static final String MCH_ID = "xxx";
	/**
	 * 微信支付API秘钥
	 */
	public static final String KEY = "xxx";
	/**
	 * 回调域名
	 */
	public static final String REDIRECT_DOMAIN = "http://xxx.com";

	private static final WxMpService wxMpService = new WxMpServiceImpl();
	private static final WxPayService wxPayService = new WxPayServiceImpl();

	static {
		WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
		config.setAppId(APP_ID);
		config.setSecret(APP_SECRET);
		wxMpService.setWxMpConfigStorage(config);

		WxPayConfig payConfig = new WxPayConfig();
		payConfig.setAppId(APP_ID);
		payConfig.setMchId(MCH_ID);
		payConfig.setMchKey(KEY);
		payConfig.setSignType(WxPayConstants.SignType.MD5);
		//h5公众号支付
		payConfig.setTradeType("MWEB");
		payConfig.setNotifyUrl(REDIRECT_DOMAIN + "/pay/success");
		wxPayService.setConfig(payConfig);
	}

	public static WxMpService wxMpService() {
		return wxMpService;
	}

	public static WxPayService wxPayService() {
		return wxPayService;
	}

}

2.后台controller:

		@RequestMapping(value = "/h5Pay")
	public void h5Pay(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Map<String,Object> result = new HashMap<>();
		result.put("success",false);
		try{
			WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
			orderRequest.setBody("微信H5支付测试");
			orderRequest.setOutTradeNo(UUID.randomUUID().toString().replace("-", ""));
			//商品支付金额
		
            //此处可直接写入数字来进行测试,也可通过参数形式赋值
orderRequest.setTotalFee(Integer.valueOf(request.getParameter("total_fee")));
			orderRequest.setSpbillCreateIp(getRealIp(request));
            //H5支付标识为MWEB
			orderRequest.setTradeType("MWEB");
			orderRequest.setSceneInfo("{\"h5_info\": {\"type\":\"Wap\",\"wap_url\": \""+PayUtils.REDIRECT_DOMAIN+"\",\"wap_name\": \"本地测试\"}}");
			orderRequest.setSignType("MD5");
			//签名创建
			Map<String,String> packageParams = new HashMap<>();
			packageParams.put("appid", PayUtils.APP_ID);
			packageParams.put("mch_id", PayUtils.MCH_ID);
			packageParams.put("nonce_str", UUID.randomUUID().toString().replace("-", ""));
			packageParams.put("signType",orderRequest.getSignType());
			String sign = SignUtils.createSign(packageParams,"MD5", PayUtils.KEY,false);
			orderRequest.setSign(sign);

			WxPayMwebOrderResult orderResult =PayUtils.wxPayService().createOrder(orderRequest);
			String mwebUrl = orderResult.getMwebUrl();
			response.sendRedirect(mwebUrl);
			result.put("success",true);
		}catch (Exception e){
			result.put("errormsg",e.getMessage());
		}
		model.addAttribute("result",result);
	}

//获取真实ip地址 通过阿帕奇代理的也能获取到真实ip
	public static String getRealIp(HttpServletRequest request){
		String ip = request.getHeader("x-forwarded-for");
		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.getRemoteAddr();
		}
		return ip;
	}

3.接口调用就不贴了,直接写在a标签或其它方式均可.

此次测试开发只填写了接口所需的必填参数,详细参数前往:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_20&index=1 这里就不截图了.里面也有详细的算法和参数格式解释,还有很详细的常见错误解决办法.

新人第一次写,望包涵.有错和不足之处希望指出.这里只是针对统一下单进行了整理,后续的API还在研究当中...若有转载,请注明出处谢谢.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值