类 PayKit

java.lang.Object
com.ijpay.core.kit.PayKit

public class PayKit extends Object

IJPay 让支付触手可及,封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。

不依赖任何第三方 mvc 框架,仅仅作为工具使用简单快速完成支付模块的开发,可轻松嵌入到任何系统里。

IJPay 交流群: 723992875、864988890

Node.js 版: https://gitee.com/javen205/TNWX

IJPay 工具类

作者:
Javen
  • 字段详细资料

    • CLASS_PATH_PREFIX

      public static final String CLASS_PATH_PREFIX
      对象路径前缀
      另请参阅:
  • 构造器详细资料

    • PayKit

      public PayKit()
  • 方法详细资料

    • getSmPrivateKey

      public static PrivateKey getSmPrivateKey(String privateKey) throws Exception
      获取国密证书私钥
      参数:
      privateKey - 私钥
      返回:
      返回值
      抛出:
      Exception - 异常信息
    • getSmPublicKey

      public static PublicKey getSmPublicKey(String publicKey) throws Exception
      获取国密证书公钥
      参数:
      publicKey - 公钥
      返回:
      返回值
      抛出:
      Exception - 异常信息
    • getSmPrivateKey

      public static PrivateKey getSmPrivateKey(byte[] encPrivate) throws Exception
      获取国密证书私钥
      参数:
      encPrivate - 私钥
      返回:
      返回值
      抛出:
      Exception - 异常信息
    • getSmPublicKey

      public static PublicKey getSmPublicKey(byte[] encPublic) throws Exception
      获取国密证书公钥
      参数:
      encPublic - 公钥
      返回:
      返回值
      抛出:
      Exception - 异常信息
    • sm2SignWithSm3

      public static String sm2SignWithSm3(String privateKey, String content) throws Exception
      签名
      参数:
      privateKey - 私钥
      content - 需要签名的内容
      返回:
      返回结果
      抛出:
      Exception - 异常信息
    • sm2SignWithSm3

      public static String sm2SignWithSm3(PrivateKey privateKey, String content) throws Exception
      签名
      参数:
      privateKey - 私钥
      content - 需要签名的内容
      返回:
      返回结果
      抛出:
      Exception - 异常信息
    • sm3Hash

      public static byte[] sm3Hash(String content) throws Exception
      SM3 Hash
      参数:
      content - 原始内容
      返回:
      返回结果
      抛出:
      Exception - 异常信息
    • sm4DecryptToString

      public static String sm4DecryptToString(String key3, String cipherText, String nonce, String associatedData) throws Exception
      下载平台证书以及回调通知加解密
      参数:
      key3 - APIv3密钥
      cipherText - 密文
      nonce - 随机串
      associatedData - 附加数据
      返回:
      解密后的明文
      抛出:
      Exception - 异常信息
    • sm4Verify

      public static boolean sm4Verify(String publicKey, String plainText, String originalSignature) throws Exception
      抛出:
      Exception
    • sm4Verify

      public static boolean sm4Verify(PublicKey publicKey, String data, String originalSignature) throws Exception
      国密验签
      参数:
      publicKey - 平台证书公钥
      data - 待验签的签名原文
      originalSignature - 签名值
      返回:
      验签结果
      抛出:
      Exception - 异常信息
    • hmacSha256

      public static String hmacSha256(String data, String key)
      生成16进制的 sha256 字符串
      参数:
      data - 数据
      key - 密钥
      返回:
      sha256 字符串
    • sha1

      public static String sha1(File dataFile)
      SHA1加密文件,生成16进制SHA1字符串
      参数:
      dataFile - 被加密文件
      返回:
      SHA1 字符串
    • sha1

      public static String sha1(InputStream data)
      SHA1加密,生成16进制SHA1字符串
      参数:
      data - 数据
      返回:
      SHA1 字符串
    • sha1

      public static String sha1(String data)
      SHA1加密,生成16进制SHA1字符串
      参数:
      data - 数据
      返回:
      SHA1 字符串
    • md5

      public static String md5(String data)
      生成16进制 MD5 字符串
      参数:
      data - 数据
      返回:
      MD5 字符串
    • decryptData

      public static String decryptData(String base64Data, String key)
      AES 解密
      参数:
      base64Data - 需要解密的数据
      key - 密钥
      返回:
      解密后的数据
    • encryptData

      public static String encryptData(String data, String key)
      AES 加密
      参数:
      data - 需要加密的数据
      key - 密钥
      返回:
      加密后的数据
    • generateStr

      public static String generateStr()
      简化的UUID,去掉了横线,使用性能更好的 ThreadLocalRandom 生成UUID
      返回:
      简化的 UUID,去掉了横线
    • getSnowflake

      public static cn.hutool.core.lang.Snowflake getSnowflake(long workerId, long dataCenterId)
      雪花算法
      参数:
      workerId - 终端ID
      dataCenterId - 数据中心ID
      返回:
      Snowflake
    • createLinkString

      public static String createLinkString(Map<String,String> params)
      把所有元素排序
      参数:
      params - 需要排序并参与字符拼接的参数组
      返回:
      拼接后字符串
    • createLinkString

      public static String createLinkString(Map<String,String> params, boolean encode)
      参数:
      params - 需要排序并参与字符拼接的参数组
      encode - 是否进行URLEncoder
      返回:
      拼接后字符串
    • createLinkString

      public static String createLinkString(Map<String,String> params, String connStr, boolean encode)
      参数:
      params - 需要排序并参与字符拼接的参数组
      connStr - 连接符号
      encode - 是否进行URLEncoder
      返回:
      拼接后字符串
    • createLinkString

      public static String createLinkString(Map<String,String> params, String connStr, boolean encode, boolean quotes)
    • urlEncode

      public static String urlEncode(String src)
      URL 编码
      参数:
      src - 需要编码的字符串
      返回:
      编码后的字符串
    • forEachMap

      public static StringBuffer forEachMap(Map<String,String> params, String prefix, String suffix)
      遍历 Map 并构建 xml 数据
      参数:
      params - 需要遍历的 Map
      prefix - xml 前缀
      suffix - xml 后缀
      返回:
      xml 字符串
    • toXml

      public static String toXml(Map<String,String> params)
      微信下单 map to xml
      参数:
      params - Map 参数
      返回:
      xml 字符串
    • xmlToMap

      public static Map<String,String> xmlToMap(String xmlStr)
      针对支付的 xml,没有嵌套节点的简单处理
      参数:
      xmlStr - xml 字符串
      返回:
      转化后的 Map
    • buildSignMessage

      public static String buildSignMessage(RequestMethodEnum method, String url, long timestamp, String nonceStr, String body)
      构造签名串
      参数:
      method - RequestMethodEnum GET,POST,PUT等
      url - 请求接口 /v3/certificates
      timestamp - 获取发起请求时的系统当前时间戳
      nonceStr - 随机字符串
      body - 请求报文主体
      返回:
      待签名字符串
    • buildSignMessage

      public static String buildSignMessage(String timestamp, String nonceStr, String body)
      构造签名串
      参数:
      timestamp - 应答时间戳
      nonceStr - 应答随机串
      body - 应答报文主体
      返回:
      应答待签名字符串
    • buildSignMessage

      public static String buildSignMessage(ArrayList<String> signMessage)
      构造签名串
      参数:
      signMessage - 待签名的参数
      返回:
      构造后带待签名串
    • createSign

      public static String createSign(ArrayList<String> signMessage, String keyPath, String authType) throws Exception
      v3 接口创建签名
      参数:
      signMessage - 待签名的参数
      keyPath - key.pem 证书路径
      返回:
      生成 v3 签名
      抛出:
      Exception - 异常信息
    • createSign

      public static String createSign(ArrayList<String> signMessage, PrivateKey privateKey) throws Exception
      v3 接口创建签名
      参数:
      signMessage - 待签名的参数
      privateKey - 商户私钥
      返回:
      生成 v3 签名
      抛出:
      Exception - 异常信息
    • createSign

      public static String createSign(String signMessage, String keyPath, String authType) throws Exception
      v3 接口创建签名
      参数:
      signMessage - 待签名的参数
      keyPath - key.pem 证书路径
      返回:
      生成 v3 签名
      抛出:
      Exception - 异常信息
    • createSign

      public static String createSign(String signMessage, PrivateKey privateKey) throws Exception
      v3 接口创建签名
      参数:
      signMessage - 待签名的参数
      privateKey - 商户私钥
      返回:
      生成 v3 签名
      抛出:
      Exception - 异常信息
    • getAuthorization

      public static String getAuthorization(String mchId, String serialNo, String nonceStr, String timestamp, String signature, String authType)
      获取授权认证信息
      参数:
      mchId - 商户号
      serialNo - 商户API证书序列号
      nonceStr - 请求随机串
      timestamp - 时间戳
      signature - 签名值
      authType - 认证类型
      返回:
      请求头 Authorization
    • getPrivateKeyStr

      public static String getPrivateKeyStr(String keyPath, String authType) throws Exception
      获取商户私钥
      参数:
      keyPath - 商户私钥证书路径
      返回:
      String 商户私钥
      抛出:
      Exception - 异常信息
    • getPrivateKey

      public static PrivateKey getPrivateKey(String keyPath, String authType) throws Exception
      获取商户私钥
      参数:
      keyPath - 商户私钥证书路径
      返回:
      PrivateKey 商户私钥
      抛出:
      Exception - 异常信息
    • getPrivateKeyByKeyContent

      public static PrivateKey getPrivateKeyByKeyContent(String originalKey, String authType) throws Exception
      获取商户私钥
      参数:
      originalKey - 私钥文本内容
      返回:
      PrivateKey 商户私钥
      抛出:
      Exception - 异常信息
    • getPrivateKeyByContent

      public static String getPrivateKeyByContent(String originalKey)
      获取证书内容
      参数:
      originalKey - 私钥文本内容
      返回:
      商户私钥
    • getPublicKeyByContent

      public static String getPublicKeyByContent(String originalKey)
      获取证书内容
      参数:
      originalKey - 公钥文本内容
      返回:
      商户公钥
    • getCertificate

      public static X509Certificate getCertificate(InputStream inputStream)
      获取证书
      参数:
      inputStream - 证书文件
      返回:
      X509Certificate 获取证书
    • getCertificate

      public static X509Certificate getCertificate(String path)
      获取证书
      参数:
      path - 证书路径,支持相对路径以及绝得路径
      返回:
      X509Certificate 获取证书
    • getCertificateInfo

      public static CertificateModel getCertificateInfo(X509Certificate certificate)
      获取证书详细信息
      参数:
      certificate - X509Certificate 证书
      返回:
      CertificateModel 获取证书详细信息
    • getCertificateInfo

      public static CertificateModel getCertificateInfo(String path)
      获取证书详细信息
      参数:
      path - 证书路径,支持相对路径以及绝得路径
      返回:
      CertificateModel 获取证书详细信息
    • checkCertificateIsValid

      public static boolean checkCertificateIsValid(CertificateModel model, String mchId, int offsetDay)
      检查证书是否可用
      参数:
      model - CertificateModel 证书详细 model
      mchId - 商户号
      offsetDay - 偏移天数,正数向未来偏移,负数向历史偏移
      返回:
      true 有效 false 无效
    • checkCertificateIsValid

      public static boolean checkCertificateIsValid(X509Certificate certificate, String mchId, int offsetDay)
      检查证书是否可用
      参数:
      certificate - X509Certificate 证书
      mchId - 商户号
      offsetDay - 偏移天数,正数向未来偏移,负数向历史偏移
      返回:
      true 有效 false 无效
    • checkCertificateIsValid

      public static boolean checkCertificateIsValid(String path, String mchId, int offsetDay)
      检查证书是否可用
      参数:
      path - 证书路径,支持相对路径以及绝得路径
      mchId - 商户号
      offsetDay - 偏移天数,正数向未来偏移,负数向历史偏移
      返回:
      true 有效 false 无效
    • rsaEncryptOAEP

      public static String rsaEncryptOAEP(String data, X509Certificate certificate) throws Exception
      公钥加密
      参数:
      data - 待加密数据
      certificate - 平台公钥证书
      返回:
      加密后的数据
      抛出:
      Exception - 异常信息
    • rsaDecryptOAEP

      public static String rsaDecryptOAEP(String cipherText, PrivateKey privateKey) throws Exception
      私钥解密
      参数:
      cipherText - 加密字符
      privateKey - 私钥
      返回:
      解密后的数据
      抛出:
      Exception - 异常信息
    • getFileToStream

      public static InputStream getFileToStream(String classPath)
      传入 classPath 静态资源路径返回文件输入流
      参数:
      classPath - 静态资源路径
      返回:
      InputStream
    • getAbsolutePath

      public static String getAbsolutePath(String classPath)
      传入 classPath 静态资源路径返回绝对路径
      参数:
      classPath - 静态资源路径
      返回:
      绝对路径
    • getCertFileInputStream

      public static InputStream getCertFileInputStream(String path) throws IOException
      通过路径获取证书文件的输入流
      参数:
      path - 文件路径
      返回:
      文件流
      抛出:
      IOException - 异常信息
    • getCertFileContent

      public static String getCertFileContent(String path) throws IOException
      通过路径获取证书文件的内容
      参数:
      path - 文件路径
      返回:
      文件内容
      抛出:
      IOException
    • getFilePath

      public static String getFilePath(String path)
      获取文件真实路径
      参数:
      path - 文件地址
      返回:
      返回文件真实路径