| | |
| | | |
| | | import cn.hutool.core.util.RandomUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alipay.api.AlipayApiException; |
| | | import com.alipay.api.AlipayClient; |
| | |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.crypto.BadPaddingException; |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.IllegalBlockSizeException; |
| | | import javax.crypto.NoSuchPaddingException; |
| | | import javax.crypto.*; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.*; |
| | |
| | | import java.security.NoSuchProviderException; |
| | | import java.security.Security; |
| | | import java.util.*; |
| | | |
| | | import static com.dsh.account.util.akeylogin.Md5Util.byteArrayToHexString; |
| | | |
| | | /** |
| | | * 第三方支付工具类 |
| | |
| | | private String certPath = "/usr/playpai/cert/weixin/apiclient_cert.p12";//微信证书 |
| | | |
| | | |
| | | |
| | | // public static void main(String[] args) { |
| | | // Map<String, Object> body = new HashMap<>(); |
| | | // body.put("type", "MERCHANT_ID"); |
| | | // body.put("account", "1231232121"); |
| | | // body.put("amount", "asda"); |
| | | // body.put("description", "订单分账"); |
| | | // JSONObject jsonObject = new JSONObject(body); |
| | | // JSONArray objects = new JSONArray(); |
| | | // objects.add(jsonObject); |
| | | // System.err.println(objects); |
| | | // System.err.println(objects.toJSONString()); |
| | | // } |
| | | /** |
| | | * 发起分账 |
| | | * @param order 微信订单号 |
| | | * @return |
| | | */ |
| | | public ResultUtil fenzhang(String order,BigDecimal amount,String merchantNumber) throws Exception { |
| | | public ResultUtil fenzhang(String order,BigDecimal amount,String merchantNumber,String description) throws Exception { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("mch_id", mchId); |
| | | map.put("appid", appid); |
| | | String nonce_str = UUIDUtil.getRandomCode(16); |
| | | String out_order_no = UUIDUtil.getRandomCode(16); |
| | | map.put("out_order_no", out_order_no); |
| | | map.put("nonce_str", nonce_str); |
| | | |
| | | map.put("transaction_id", order); |
| | | // 将这个字符串使用json格式拼接起来 |
| | | Map<String, Object> body = new HashMap<>(); |
| | | body.put("type", "MERCHANT_ID"); |
| | | body.put("account", merchantNumber); |
| | | body.put("amount", amount); |
| | | body.put("description", "订单分账"); |
| | | int i = amount.multiply(new BigDecimal("100")).intValue(); |
| | | body.put("amount", i); |
| | | body.put("description", description); |
| | | JSONObject jsonObject = new JSONObject(body); |
| | | String jsonString = jsonObject.toString(); |
| | | map.put("receiver",jsonString); |
| | | String s = this.weixinSignature(map); |
| | | JSONArray objects = new JSONArray(); |
| | | objects.add(jsonObject); |
| | | map.put("receivers",objects.toJSONString()); |
| | | String s = this.weixinSignature1(map); |
| | | map.put("sign", s); |
| | | String url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing"; |
| | | //设置请求头 |
| | |
| | | Map<String, String> map1 = null; |
| | | String body1 = null; |
| | | try { |
| | | body1 = HttpClientUtil.pushHttpsRequsetXml(url, xmlString.toString(), new HashMap<>(), mchId, certPath, "PKCS12"); |
| | | body1 = HttpClientUtil.pushHttpsRequsetXml1(url, xmlString.toString(), new HashMap<>(), mchId, certPath, "PKCS12"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | |
| | | |
| | | |
| | | Map<String, String> map = new HashMap<>(); |
| | | String out_trade_no = params.get("out_trade_no"); |
| | | String subject = params.get("subject"); |
| | | String total_amount = params.get("total_amount"); |
| | | String trade_no = params.get("trade_no"); |
| | | String passback_params = params.get("passback_params"); |
| | | map.put("out_trade_no", out_trade_no);//商家订单号 |
| | | map.put("subject", subject); |
| | | map.put("total_amount", total_amount); |
| | | map.put("trade_no", trade_no);//支付宝交易号 |
| | | map.put("passback_params", passback_params);//回传参数 |
| | | return map; |
| | | System.err.println("返回码"+params); |
| | | if (params.get("trade_status").equals("TRADE_SUCCESS")){ |
| | | String out_trade_no = params.get("out_trade_no"); |
| | | String subject = params.get("subject"); |
| | | String total_amount = params.get("total_amount"); |
| | | String trade_no = params.get("trade_no"); |
| | | String passback_params = params.get("passback_params"); |
| | | map.put("out_trade_no", out_trade_no);//商家订单号 |
| | | map.put("subject", subject); |
| | | map.put("total_amount", total_amount); |
| | | map.put("trade_no", trade_no);//支付宝交易号 |
| | | map.put("passback_params", passback_params);//回传参数 |
| | | System.err.println("回调map"); |
| | | return map; |
| | | }else{ |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private String weixinSignature1(Map<String, Object> map) { |
| | | try { |
| | | Set<Map.Entry<String, Object>> entries = map.entrySet(); |
| | | List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(entries); |
| | | // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序) |
| | | Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() { |
| | | public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) { |
| | | return (o1.getKey()).toString().compareTo(o2.getKey()); |
| | | } |
| | | }); |
| | | // 构造签名键值对的格式 |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (Map.Entry<String, Object> item : infoIds) { |
| | | if (item.getKey() != null || item.getKey() != "") { |
| | | String key = item.getKey(); |
| | | Object val = item.getValue(); |
| | | if (!(val == "" || val == null)) { |
| | | sb.append(key + "=" + val + "&"); |
| | | } |
| | | } |
| | | } |
| | | sb.append("key=" + key); |
| | | String sign = sha256_HMAC(sb.toString(), key).toUpperCase(); |
| | | return sign; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | /** |
| | | * sha256_HMAC加密 |
| | | * @param message 消息 |
| | | * @param secret 秘钥 |
| | | * @return 加密后字符串 |
| | | */ |
| | | public String sha256_HMAC(String message, String secret) { |
| | | String hash = ""; |
| | | try { |
| | | Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); |
| | | SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256"); |
| | | sha256_HMAC.init(secret_key); |
| | | byte[] bytes = sha256_HMAC.doFinal(message.getBytes()); |
| | | hash = byteArrayToHexString(bytes); |
| | | } catch (Exception e) { |
| | | System.out.println("Error HmacSHA256 ===========" + e.getMessage()); |
| | | } |
| | | return hash; |
| | | } |
| | | |
| | | /** |
| | | * 微信退款成功后的解密 |