微信公众平台支付接口「我爱吐槽555」

微信公众平台支付接口

微信支付接入

商户平台产品中心配置开发配置打开jsapi支付、原生支付、jsapi支付授权目录(前端项目域名)、原生支付回调链接appid账号配置。
此步骤完成商户与微信官方账号的关系。
帐户中心配置。
应用API证书设置apiV3密钥,微信公众号配置微信支付配置。
该平台链接到官方帐户。官方账户平台微信支付页面需要点击确认,完成相关操作。
微信支付关键数据列表。
名称
解释
阿皮德
公共帐户ID。
妇幼保健院没有
商号。
mch_uuuAPI_uuuuu认证
商户API证书。
mch_uuAPI_uuuu私有密钥
商户API私钥。
mch_uuuAPI_uuuuuuuuuuuuuuuuuuuuuuuu证书序列号
商户API证书序列号。
wx平台证书
微信支付平台证书。
wx?平台?证书?序列号
微信支付平台证书序列号。
wx_uuu平台uuu私钥
微信支付平台私钥。
api_uv3_uu键
Apiv3键。
微信平台证书获取。
邮差电话https://api.mch.weixin.qq.com/v3/certificates?,拿到证书。
解密证书密文。
包装公司。保险艺术。宣称回发;导入Java。木卫一。IOException;导入Java。安全一般安全例外;导入Java。安全无效算法参数异常;导入Java。安全无效钥匙例外;导入Java。安全NoSuchAlgorithmException;导入Java。util。Base64;importjavax。加密。密码importjavax。加密。没有这样的例外;importjavax。加密。spec.GCMParameterSpec;importjavax。加密。规范SecretKeySpec;Publicclassaesutil{staticfinalinantkey{ulength}字节=32;staticFinalinantTag{ulength}位=128;privatefinalbyte[]aeskey;publicaesutil(byte[]key){if(key.length!=key{ulength}字节{thrownnewillegalargumentexception(“无效的apiv3key,长度必须为32字节”);///该键为apiv3。此aesKey=key;}PublicStringDecryptoString(字节[]关联数据,字节[]nonce,字符串密文)通过GeneralSecurityException,IOException{try{Ciphercipher=Cipher.getInstance(“AES/GCM/NoPadding”);SecretKeySpeckey=newSecretKeySpec(aesKey,“AES”);GCMParameterSpecspec=newGCMParameterSpec(TAG_LENGTH_BIT,nonce);Cipher.init(Cipher.DECRYPT_MODE,key,spec);Cipher.updatead(associatedData);returnnewString(Cipher.doFinal(Base64.getDecoder().decode(ciphertext)),“utf-8”)}catch(nosuchagorithmexception|nosuchappingexception){thrownwillegalstateeexception(e);}catch(InvalidKeyException|invalidAlgorithmParameterException){thrownewIllegalArgumentException(e);}}}

微信公众平台支付接口(网络配图)

微信公众平台支付接口(网络配图)

商户api证书VS微信平台证书。
微信支付需要用到两种证书,商户api证书和微信平台证书,当时做接入的时候,懵逼。。微信的接入文档当时没看明白,不知道两者的作用是什么。
后面看了这个文档https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_0.shtml,整明白了。
商户api证书,用作签名。
微信支付APIv3要求商户对请求进行签名。微信支付会在收到请求后进行签名的验证。如果签名验证不通过,微信支付APIv3将会拒绝处理请求,并返回401Unauthorized。
商户api证书中包含了签名所需要的私钥和商户证书,如何签名,参考签名生成,或者使用微信提供的sdkwechatpay-apache-httpclient。
微信平台证书,验签。
微信平台证书,对微信应答签名的验签。
如果验证商户的请求签名正确,微信支付会在应答的HTTP头部中包括应答签名。我们建议商户验证应答签名。
同样的,微信支付会在回调的HTTP头部中包括回调报文的签名。商户必须验证回调的签名,以确保回调是由微信支付发送。
apiv3-key用作解密微信支付签名工具类。
importcom.wechat.pay.contrib.apache.httpclient.util.PemUtil;importorg.springframework.util.Base64Utils;importjava.io.ByteArrayInputStream;importjava.nio.charset.StandardCharsets;importjava.security.*;/***@author:shixianqing*@Date:2021/8/1717:36*@Description:**/publicclassSignUtils{/***@paramsignStr签名字符串*@paramprivateKeyStr商户证书私钥*/publicstaticStringcreateSign(StringsignStr,StringprivateKeyStr)throwsInvalidKeyException,NoSuchAlgorithmException,SignatureException{PrivateKeyprivateKey=PemUtil.loadPrivateKey(newByteArrayInputStream(privateKeyStr.getBytes(StandardCharsets.UTF_8)));Signaturesign=Signature.getInstance("SHA256withRSA");sign.initSign(privateKey);sign.update(signStr.getBytes(StandardCharsets.UTF_8));returnBase64Utils.encodeToString(sign.sign());}}。
微信支付验签。
publicclassWechatVerifyServiceImplimplementsWechatVerifyService{/***验签*@paramwxPlatformCert微信支付平台证书*@paramrequest*@paramrequestBody请求报文体*@return*/@Overridepublicbooleanverify(StringwxPlatformCert,HttpServletRequestrequest,StringrequestBody){Stringtimestamp=request.getHeader("Wechatpay-Timestamp");Stringnonce=request.getHeader("Wechatpay-Nonce");Stringserial=request.getHeader("Wechatpay-Serial");Stringsignature=request.getHeader("Wechatpay-Signature");log.info("支付通知,验签开始,timestamp:{},nonce:{},serial:{},signature:{}",timestamp,nonce,serial,signature);//验签处理器Verifierverifier=WechatVerifierUtils.getVerifier(wxPlatformCert);Stringbody=requestBody;StringbeforeSign=String.format("%s %s %s ",timestamp,nonce,body);returnverifier.verify(serial,beforeSign.getBytes(StandardCharsets.UTF_8),signature);}}publicclassWechatVerifierUtils{publicstaticVerifiergetVerifier(StringwxPlatformCert){X509CertificatewechatPayPlatformCertificate=PemUtil.loadCertificate(newByteArrayInputStream(wxPlatformCert.getBytes(StandardCharsets.UTF_8)));ArrayList<X509Certificate>wxPlatformCertList=newArrayList<>();wxPlatformCertList.add(wechatPayPlatformCertificate);VerifiercertificatesVerifier=newCertificatesVerifier(wxPlatformCertList);returncertificatesVerifier;}}。

微信公众平台支付接口(网络配图)

微信公众平台支付接口(网络配图)

微信支付解密。
packagecom.insuresmart.base.pay.common.util;importcom.google.common.base.CharMatcher;importcom.google.common.io.BaseEncoding;importcom.insuresmart.base.common.utils.StringUtils;importjavax.crypto.Cipher;importjavax.crypto.Mac;importjavax.crypto.NoSuchPaddingException;importjavax.crypto.spec.GCMParameterSpec;importjavax.crypto.spec.SecretKeySpec;importjava.io.IOException;importjava.security.GeneralSecurityException;importjava.security.InvalidAlgorithmParameterException;importjava.security.InvalidKeyException;importjava.security.NoSuchAlgorithmException;importjava.util.Base64;importjava.util.Map;importjava.util.SortedMap;importjava.util.TreeMap;publicclassAesUtils{staticfinalintKEY_LENGTH_BYTE=32;staticfinalintTAG_LENGTH_BIT=128;privatefinalbyte[]aesKey;publicAesUtils(byte[]key){if(key.length!=KEY_LENGTH_BYTE){thrownewIllegalArgumentException("无效的ApiV3Key,长度必须为32个字节");}//apiv3私钥this.aesKey=key;}publicstaticbyte[]decryptToByte(byte[]nonce,byte[]cipherData,byte[]key)throwsGeneralSecurityException{returndecryptToByte(null,nonce,cipherData,key);}publicstaticbyte[]decryptToByte(byte[]associatedData,byte[]nonce,byte[]cipherData,byte[]key)throwsGeneralSecurityException{try{Ciphercipher=Cipher.getInstance("AES/GCM/NoPadding");SecretKeySpecsecretKeySpec=newSecretKeySpec(key,"AES");GCMParameterSpecspec=newGCMParameterSpec(TAG_LENGTH_BIT,nonce);cipher.init(Cipher.DECRYPT_MODE,secretKeySpec,spec);if(associatedData!=null){cipher.updateAAD(associatedData);}returncipher.doFinal(cipherData);}catch(NoSuchAlgorithmException|NoSuchPaddingExceptione){thrownewIllegalStateException(e);}catch(InvalidKeyException|InvalidAlgorithmParameterExceptione){thrownewIllegalArgumentException(e);}}publicStringdecryptToString(byte[]associatedData,byte[]nonce,Stringciphertext)throwsGeneralSecurityException,IOException{try{Ciphercipher=Cipher.getInstance("AES/GCM/NoPadding");SecretKeySpeckey=newSecretKeySpec(aesKey,"AES");GCMParameterSpecspec=newGCMParameterSpec(TAG_LENGTH_BIT,nonce);cipher.init(Cipher.DECRYPT_MODE,key,spec);cipher.updateAAD(associatedData);returnnewString(cipher.doFinal(BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(ciphertext))),"utf-8");}catch(NoSuchAlgorithmException|NoSuchPaddingExceptione){thrownewIllegalStateException(e);}catch(InvalidKeyException|InvalidAlgorithmParameterExceptione){thrownewIllegalArgumentException(e);}}publicstaticStringdecryptToString(StringassociatedData,Stringnonce,Stringciphertext,StringapiV3Key)throwsGeneralSecurityException,IOException{try{Ciphercipher=Cipher.getInstance("AES/GCM/NoPadding");SecretKeySpeckey=newSecretKeySpec(apiV3Key.getBytes(),"AES");GCMParameterSpecspec=newGCMParameterSpec(TAG_LENGTH_BIT,nonce.getBytes());cipher.init(Cipher.DECRYPT_MODE,key,spec);associatedData=StringUtils.isNotBlank(associatedData)?associatedData:"";cipher.updateAAD(associatedData.getBytes());returnnewString(cipher.doFinal(Base64.getDecoder().decode(ciphertext)),"utf-8");}catch(NoSuchAlgorithmException|NoSuchPaddingExceptione){thrownewIllegalStateException(e);}catch(InvalidKeyException|InvalidAlgorithmParameterExceptione){thrownewIllegalArgumentException(e);}}publicstaticStringHMACSHA256(Stringdata,Stringkey){try{Macsha256_HMAC=Mac.getInstance("HmacSHA256");SecretKeySpecsecret_key=newSecretKeySpec(key.getBytes("UTF-8"),"HmacSHA256");sha256_HMAC.init(secret_key);byte[]array=sha256_HMAC.doFinal(data.getBytes("UTF-8"));StringBuildersb=newStringBuilder();for(byteitem:array){sb.append(Integer.toHexString((item&0xFF)|0x100).substring(1,3));}returnsb.toString().toUpperCase();}catch(Exceptione){e.printStackTrace();returnnull;}}}。

微信公众平台支付接口(网络配图)

微信公众平台支付接口(网络配图)

附录。
微信接入规范地址。
https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay3_1.shtml。
微信支付接口文档地址。
https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay3_1.shtml。

发表评论

免责声明:本站部分内容转载于网络,其中内容仅代表作者个人观点,与本网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。若有来源标注错误或侵犯了您的合法权益,请作者持权属证明与本网联系,我们将及时更正、删除,谢谢。

本站联系邮箱:douxingdu02@163.co m