Created
October 31, 2020 07:48
-
-
Save punkmonday/0fa0e13e44e9b35b7ad59b389fb14c14 to your computer and use it in GitHub Desktop.
银联商务支付工具类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.facepay.lovefamily.pay.utils; | |
| /** | |
| * Created by faliny on 2017/8/25. | |
| */ | |
| import cn.hutool.core.date.DateUtil; | |
| import cn.hutool.core.util.RandomUtil; | |
| import com.alibaba.fastjson.JSONObject; | |
| import org.apache.commons.codec.digest.DigestUtils; | |
| import org.apache.commons.lang3.RandomStringUtils; | |
| import org.apache.commons.lang3.StringUtils; | |
| import org.apache.commons.lang3.time.DateFormatUtils; | |
| import javax.servlet.http.HttpServletRequest; | |
| import java.io.UnsupportedEncodingException; | |
| import java.math.BigDecimal; | |
| import java.math.RoundingMode; | |
| import java.time.LocalDateTime; | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.Date; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| /** | |
| * @author msi | |
| */ | |
| public class UmsPayUtil { | |
| public static String makeSign(String md5Key, Map<String, String> params) { | |
| String preStr = buildSignString(params); // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 | |
| String text = preStr + md5Key; | |
| return DigestUtils.sha256Hex(getContentBytes(text)).toUpperCase(); | |
| } | |
| public static boolean checkSign(String md5Key, Map<String, String> params) { | |
| String sign = params.get("sign"); | |
| if (StringUtils.isBlank(sign)) { | |
| return false; | |
| } | |
| String signV = makeSign(md5Key, params); | |
| return StringUtils.equalsIgnoreCase(sign, signV); | |
| } | |
| // 获取HttpServletRequest里面的参数,并decode | |
| public static Map<String, String> getRequestParams(HttpServletRequest request) { | |
| Map<String, String[]> params = request.getParameterMap(); | |
| Map<String, String> params2 = new HashMap<>(); | |
| for (String key : params.keySet()) { | |
| String[] values = params.get(key); | |
| if (values.length > 0) { | |
| params2.put(key, values[0]); | |
| } | |
| } | |
| return params2; | |
| } | |
| public static String genMerOrderId(String msgId) { | |
| String date = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS"); | |
| String rand = RandomStringUtils.randomNumeric(7); | |
| return msgId + date + rand; | |
| } | |
| // 使json-lib来进行json到map的转换,fastjson有排序问题,不能用 | |
| public static Map<String, String> jsonToMap(JSONObject json) { | |
| Map<String, String> map = new HashMap<>(); | |
| for (Object key : json.keySet()) { | |
| String value = json.getString((String) key); | |
| map.put((String) key, value); | |
| } | |
| return map; | |
| } | |
| // 构建签名字符串 | |
| private static String buildSignString(Map<String, String> params) { | |
| if (params == null || params.size() == 0) { | |
| return ""; | |
| } | |
| List<String> keys = new ArrayList<>(params.size()); | |
| for (String key : params.keySet()) { | |
| if ("sign".equals(key)) { | |
| continue; | |
| } | |
| if (StringUtils.isEmpty(params.get(key))) { | |
| continue; | |
| } | |
| keys.add(key); | |
| } | |
| Collections.sort(keys); | |
| StringBuilder buf = new StringBuilder(); | |
| for (int i = 0; i < keys.size(); i++) { | |
| String key = keys.get(i); | |
| String value = params.get(key); | |
| if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符 | |
| buf.append(key + "=" + value); | |
| } else { | |
| buf.append(key + "=" + value + "&"); | |
| } | |
| } | |
| return buf.toString(); | |
| } | |
| // 根据编码类型获得签名内容byte[] | |
| private static byte[] getContentBytes(String content) { | |
| try { | |
| return content.getBytes("UTF-8"); | |
| } catch (UnsupportedEncodingException e) { | |
| throw new RuntimeException("签名过程中出现错误"); | |
| } | |
| } | |
| public static long yuanTofen(BigDecimal amount) { | |
| BigDecimal am = amount.multiply(new BigDecimal(100)).setScale(0, RoundingMode.DOWN); | |
| return am.longValue(); | |
| } | |
| /** | |
| * merOrderId需要符合银商规范,以银商分配的4位系统编号作为订单号的前4位,且在商户系统中此订单号保证唯一。总长度需大于6位,小于32位。 | |
| * | |
| * @param sysCode 银商分配的4位系统编号 | |
| * @return 商户订单 | |
| */ | |
| public static String createMerOrderId(String sysCode) { | |
| return new StringBuilder(sysCode).append(DateUtil.format(LocalDateTime.now(), "yyyyMMddmmHHssSSS")).append(RandomUtil.randomNumbers(7)).toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment