微信小程序开发之微信支付(四)组合签名(时间戳,随机串) 用户确认支付

我使用了这个SDK进行开发

签名算法需要5个参数


时间戳算法:

/**
 * 获取时间戳
 * 时间戳从1970年1月1日00:00:00至今的秒数
 */
public static long getTimeStamp() {
    Date d = new Date();
    long timeStamp = d.getTime() / 1000;     //getTime()得到的是微秒, 需要换算成秒
    return timeStamp;
}

随机串算法:

/**
 * 获取32位随机串
 */
public static String getNonceStr() {
    String s = UUID.randomUUID().toString().replace("-","");
    return s;
}

签名算法:

com.github.binarywang.wxpay.util

/**
 * 微信支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3).
 *
 * @param params         参数信息
 * @param signType       签名类型,如果为空,则默认为MD5
 * @param signKey        签名Key
 * @param ignoreSignType 签名时,是否忽略signType
 * @return 签名字符串
 */
public static String createSign(Map<String, String> params, String signType, String signKey, boolean ignoreSignType) {
  SortedMap<String, String> sortedMap = new TreeMap<>(params);

  StringBuilder toSign = new StringBuilder();
  for (String key : sortedMap.keySet()) {
    String value = params.get(key);
    boolean shouldSign = false;
    if (ignoreSignType && "sign_type".equals(key)) {
      shouldSign = false;
    } else if (StringUtils.isNotEmpty(value)
      && !Lists.newArrayList("sign", "key", "xmlString", "xmlDoc", "couponList").contains(key)) {
      shouldSign = true;
    }

    if (shouldSign) {
      toSign.append(key).append("=").append(value).append("&");
    }
  }

  toSign.append("key=").append(signKey);
  if (SignType.HMAC_SHA256.equals(signType)) {
    return createHmacSha256Sign(toSign.toString(), signKey);
  } else {
    return DigestUtils.md5Hex(toSign.toString()).toUpperCase();
  }
}

前端需要wx.requestPayment{}发起支付。

怎样自动填写参数呢?

我们使用freemarker进行参数的动态注入

在依赖文件中添加

                 <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>

以调用统一接口为例

/**
 * 发起与支付
 * 调用统一下单接口,获取“预支付交易会话标识”
 */
@PostMapping("/unifiedOrder")
public ModelAndView unifiedOrder(@RequestBody String orderId,
                                 @RequestBody HttpServletRequest request, 
                                 Map<String, Object>map) throws WxPayException {
    OrderDTO orderDTO = orderService.findOne(orderId);
    WxPayUnifiedOrderRequest orderRequest = payService.create(orderDTO, request);
    WxPayUnifiedOrderResult orderResult = wxPayService.unifiedOrder(orderRequest);
    map.put("orderRequest", orderRequest);
    map.put("orderResult", orderResult);
    map.put("timeStamp", payUtil.getTimeStamp());
    map.put("nonceStr", payUtil.getNonceStr());

    return new ModelAndView("pay/unifiedOrder", map);
}
wx.requestPayment(
        {
            'timeStamp': '${timeStamp}',
            'nonceStr': '${nonceStr}',
            'package': '${orderResult.prepayId}',
            'signType': 'MD5',
            'paySign': '',
            'success':function(res){},
            'fail':function(res){},
            'complete':function(res){}
        })

创建unifiedOrder.ftl文件。使用${}进行参数注入

讲的不太详细 ModelAndView可以参考这里学习

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值