golang实现微信支付v3版本的方法
来源:脚本之家
时间:2023-01-21 21:36:30 337浏览 收藏
来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《golang实现微信支付v3版本的方法》,介绍一下微信支付,希望对大家的知识积累有所帮助,助力实战开发!
一、准备阶段
获取私钥
官方文档 https://kf.qq.com/faq/161222N...
获取私钥证书的序列号 https://pay.weixin.qq.com/wik...
openssl x509 -in 1900009191_20180326_cert.pem -noout -serial serial=1DDE55AD98ED71D6EDD4A4A16996DE7B47773A8C
私钥获取后有三个文件
apiclient_key.p12 apiclient_cert.pem apiclient_key.pem
本次示例程序中,使用的是文件 apiclient_key.pem内容
获取公钥(平台证书)
官方文档
更新证书 https://pay.weixin.qq.com/wik...
平台证书会提前10天生成新证书,微信官方推荐在旧证书过期前5-10天部署新证书
获取证书API文档 https://pay.weixin.qq.com/wik...
身份证认证信息生成文档 https://pay.weixin.qq.com/wik...
常量
const appId = "" // 小程序或者公众号的appid const mchId = "" // 微信支付的商户id const privateSerialNo = "" // 私钥证书号 const aesKey = "" // 微信支付aes key
生成数字签名
// 对消息的散列值进行数字签名 func signPKCS1v15(msg, privateKey []byte, hashType crypto.Hash) ([]byte, error) { block, _ := pem.Decode(privateKey) if block == nil { return nil, errors.New("private key decode error") } pri, err := x509.ParsePKCS8PrivateKey(block.Bytes) if err != nil { return nil, errors.New("parse private key error") } key, ok := pri.(*rsa.PrivateKey) if ok == false { return nil, errors.New("private key format error") } sign, err := rsa.SignPKCS1v15(cryptoRand.Reader, key, hashType, msg) if err != nil { return nil, errors.New("sign error") } return sign, nil } // base编码 func base64EncodeStr(src []byte) string { return base64.StdEncoding.EncodeToString(src) }
生成身份认证信息
func authorization(method string, paramMap map[string]interface{}, rawUrl string) (token string, err error) { var body string if len(paramMap) != 0 { paramJsonBytes, err := json.Marshal(paramMap) if err != nil { return token, err } body = string(paramJsonBytes) } urlPart, err := url.Parse(rawUrl) if err != nil { return token, err } canonicalUrl := urlPart.RequestURI() timestamp := time.Now().Unix() nonce := getRandomString(32) message := fmt.Sprintf("%s\n%s\n%d\n%s\n%s\n", method, canonicalUrl, timestamp, nonce, body) open, err := os.Open("/Users/apple/data/www/go/work/src/study/testwechantpay/private.pem") if err != nil { return token, err } defer open.Close() privateKey, err := ioutil.ReadAll(open) if err != nil { return token, err } signBytes, err := signPKCS1v15(hasha256(message), privateKey, crypto.SHA256) if err != nil { return token, err } sign := base64EncodeStr(signBytes) token = fmt.Sprintf("mchid=\"%s\",nonce_str=\"%s\",timestamp=\"%d\",serial_no=\"%s\",signature=\"%s\"", mchId, nonce, timestamp, privateSerialNo, sign) return token, nil }
报文解密
func decryptGCM(aesKey, nonceV, ciphertextV, additionalDataV string) ([]byte, error) { key := []byte(aesKey) nonce := []byte(nonceV) additionalData := []byte(additionalDataV) ciphertext, err := base64.StdEncoding.DecodeString(ciphertextV) if err != nil { return nil, err } block, err := aes.NewCipher(key) if err != nil { return nil, err } aesGCM, err := cipher.NewGCM(block) if err != nil { return nil, err } plaintext, err := aesGCM.Open(nil, nonce, ciphertext, additionalData) if err != nil { return nil, err } return plaintext, err }
获取平台证书
// 获取公钥 const publicKeyUrl = "https://api.mch.weixin.qq.com/v3/certificates" type TokenResponse struct { Data []TokenResponseData `json:"data"` } type TokenResponseData struct { EffectiveTime string `json:"effective_time"` EncryptCertificate EncryptCertificate `json:"encrypt_certificate"` ExpireTime string `json:"expire_time"` SerialNo string `json:"serial_no"` } type EncryptCertificate struct { Algorithm string `json:"algorithm"` AssociatedData string `json:"associated_data"` Ciphertext string `json:"ciphertext"` Nonce string `json:"nonce"` } var publicSyncMap sync.Map // 获取公钥 func getPublicKey() (key string, err error) { var prepareTime int64 = 24 * 3600 * 3 // 证书提前三天过期旧证书,获取新证书 nowTime := time.Now().Unix() // 读取公钥缓存数据 cacheValueKey := fmt.Sprintf("app_id:%s:public_key:value", appId) cacheExpireTimeKey := fmt.Sprintf("app_id:%s:public_key:expire_time", appId) cacheValue, keyValueOk := publicSyncMap.Load(cacheValueKey) cacheExpireTime, expireTimeOk := publicSyncMap.Load(cacheExpireTimeKey) if keyValueOk && expireTimeOk { // 格式化时间 local, _ := time.LoadLocation("Local") location, _ := time.ParseInLocation(time.RFC3339, cacheExpireTime.(string), local) // 判断是否过期,证书没有过期直接返回 if location.Unix()-prepareTime > nowTime { return cacheValue.(string), nil } } token, err := authorization(http.MethodGet, nil, publicKeyUrl) if err != nil { return key, err } request, err := http.NewRequest(http.MethodGet, publicKeyUrl, nil) if err != nil { return key, err } request.Header.Add("Authorization", "WECHATPAY2-SHA256-RSA2048 "+token) request.Header.Add("User-Agent", "用户代理(https://zh.wikipedia.org/wiki/User_agent)") request.Header.Add("Content-type", "application/json;charset='utf-8'") request.Header.Add("Accept", "application/json") client := http.DefaultClient response, err := client.Do(request) if err != nil { return key, err } defer response.Body.Close() bodyBytes, err := ioutil.ReadAll(response.Body) if err != nil { return key, err } //fmt.Println(string(bodyBytes)) var tokenResponse TokenResponse if err = json.Unmarshal(bodyBytes, &tokenResponse); err != nil { return key, err } for _, encryptCertificate := range tokenResponse.Data { // 格式化时间 local, _ := time.LoadLocation("Local") location, err := time.ParseInLocation(time.RFC3339, encryptCertificate.ExpireTime, local) if err != nil { return key, err } // 判断是否过期,证书没有过期直接返回 if location.Unix()-prepareTime > nowTime { decryptBytes, err := decryptGCM(aesKey, encryptCertificate.EncryptCertificate.Nonce, encryptCertificate.EncryptCertificate.Ciphertext, encryptCertificate.EncryptCertificate.AssociatedData) if err != nil { return key, err } key = string(decryptBytes) publicSyncMap.Store(cacheValueKey, key) publicSyncMap.Store(cacheExpireTimeKey, encryptCertificate.ExpireTime) return key, nil } } return key, errors.New("get public key error") }
二、发起微信支付
jsapi 发起支付
调用统一下单接口
统一下单接口文档 https://pay.weixin.qq.com/wik...
// 统一下单接口 func commonPay() (payResMap map[string]string, err error) { payResMap = make(map[string]string) amount := 10 paramMap := make(map[string]interface{}) paramMap["appid"] = appId paramMap["mchid"] = mchId paramMap["description"] = fmt.Sprintf("微信充值:¥%d", amount) paramMap["out_trade_no"] = fmt.Sprintf("test%s%s", time.Now().Format("20060102150405"), randNumber()) paramMap["notify_url"] = "http://tools.localhost/notify" paramMap["amount"] = map[string]interface{}{"total": amount * 100, "currency": "CNY"} paramMap["payer"] = map[string]string{"openid": "opCO05utXkPQh3Vje13WjEdQpAZ4"} token, err := authorization(http.MethodPost, paramMap, commonPayUrl) if err != nil { return payResMap, err } marshal, _ := json.Marshal(paramMap) request, err := http.NewRequest(http.MethodPost, commonPayUrl, bytes.NewReader(marshal)) if err != nil { return payResMap, err } request.Header.Add("Authorization", "WECHATPAY2-SHA256-RSA2048 "+token) request.Header.Add("User-Agent", "用户代理(https://zh.wikipedia.org/wiki/User_agent)") request.Header.Add("Content-type", "application/json;charset='utf-8'") request.Header.Add("Accept", "application/json") client := http.DefaultClient response, err := client.Do(request) if err != nil { return payResMap, err } defer func() { response.Body.Close() }() bodyBytes, err := ioutil.ReadAll(response.Body) if err != nil { return payResMap, err } if err = json.Unmarshal(bodyBytes, &payResMap); err != nil { return payResMap, err } if payResMap["prepay_id"] == "" { return payResMap, errors.New("code:" + payResMap["code"] + "err:" + payResMap["message"]) } return payResMap, nil }
生成jsapi发起支付
JSAPI 调起支付接口文档 https://pay.weixin.qq.com/wik...
func jsApi(payResMap map[string]string) (payJson string, err error) { payMap := make(map[string]string) timeStamp := time.Now().Unix() nonce := getRandomString(32) packageStr := "prepay_id=" + payResMap["prepay_id"] payMap["appId"] = appId payMap["timeStamp"] = fmt.Sprintf("%v", timeStamp) payMap["nonceStr"] = nonce payMap["package"] = packageStr // 签名 message := fmt.Sprintf("%s\n%s\n%s\n%s\n", appId, fmt.Sprintf("%v", timeStamp), nonce, packageStr) open, err := os.Open("/Users/apple/data/www/go/work/src/study/testwechantpay/private.pem") if err != nil { return payJson, err } defer open.Close() privateKey, err := ioutil.ReadAll(open) if err != nil { return payJson, err } signBytes, err := signPKCS1v15(hasha256(message), privateKey, crypto.SHA256) if err != nil { return payJson, err } sign := base64EncodeStr(signBytes) payMap["signType"] = sign payMap["paySign"] = "RSA" payJsonBytes, err := json.Marshal(payMap) if err != nil { return payJson, err } payJson = string(payJsonBytes) return payJson, nil }
前台发起支付js
需要加载微信js http://res.wx.qq.com/open/js/jweixin-1.6.0.js
调用微信js需要在微信支付平台,设置支付目录
指引文档 https://pay.weixin.qq.com/wik...
三、异步通知
签名校验
文档 https://pay.weixin.qq.com/wik...
验证签名
//验证数字签名 func VerifyRsaSign(msg []byte, sign []byte, publicStr []byte, hashType crypto.Hash) bool { //pem解码 block, _ := pem.Decode(publicStr) //x509解码 publicKeyInterface, err := x509.ParseCertificate(block.Bytes) if err != nil { panic(err) } publicKey := publicKeyInterface.PublicKey.(*rsa.PublicKey) //验证数字签名 err = rsa.VerifyPKCS1v15(publicKey, hashType, msg, sign) //crypto.SHA1 return err == nil } // 验证签名 func notifyValidate(timeStamp ,nonce,rawPost,signature string) (bool, error) { signature = base64DecodeStr(signature) message := fmt.Sprintf("%s\n%s\n%s\n", timeStamp, nonce, rawPost) publicKey, err := getPublicKey() if err != nil { return false, err } return VerifyRsaSign(hasha256(message), []byte(signature), []byte(publicKey), crypto.SHA256), nil }
报文解密
type NotifyResponse struct { CreateTime string `json:"create_time"` Resource NotifyResource `json:"resource"` } type NotifyResource struct { Ciphertext string `json:"ciphertext"` AssociatedData string `json:"associated_data"` Nonce string `json:"nonce"` } func notifyDecrypt(rawPost string) (decrypt string, err error) { var notifyResponse NotifyResponse if err = json.Unmarshal([]byte(rawPost), ¬ifyResponse); err != nil { return decrypt, err } decryptBytes, err := decryptGCM(aesKey, notifyResponse.Resource.Nonce, notifyResponse.Resource.Ciphertext, notifyResponse.Resource.AssociatedData) if err != nil { return decrypt, err } decrypt = string(decryptBytes) return decrypt, nil }
四、查询订单
文档 https://pay.weixin.qq.com/wik...
查询订单
const searchTradeUrl = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/%s?mchid=%s" // 查询交易 func searchTrade(orderId string) (trade string, err error) { rawUrl := fmt.Sprintf(searchTradeUrl, orderId, mchId) token, err := authorization(http.MethodGet, nil, rawUrl) if err != nil { return trade, err } request, err := http.NewRequest(http.MethodGet, rawUrl, nil) if err != nil { return trade, err } request.Header.Add("Authorization", "WECHATPAY2-SHA256-RSA2048 "+token) request.Header.Add("User-Agent", "用户代理(https://zh.wikipedia.org/wiki/User_agent)") request.Header.Add("Content-type", "application/json;charset='utf-8'") request.Header.Add("Accept", "application/json") client := http.DefaultClient response, err := client.Do(request) if err != nil { return trade, err } defer response.Body.Close() bodyBytes, err := ioutil.ReadAll(response.Body) if err != nil { return trade, err } return string(bodyBytes), nil }
五、申请退款
文档 https://pay.weixin.qq.com/wik...
申请退款
const refundUrl = "https://api.mch.weixin.qq.com/v3/refund/domestic/refunds" func refundTrade(orderId string, amount float64) (trade string, err error) { paramMap := make(map[string]interface{}) paramMap["out_trade_no"] = orderId paramMap["out_refund_no"] = orderId + "-1" paramMap["amount"] = map[string]interface{}{"refund": amount * 100, "total": amount * 100, "currency": "CNY"} token, err := authorization(http.MethodPost, paramMap, refundUrl) if err != nil { return trade, err } marshal, _ := json.Marshal(paramMap) request, err := http.NewRequest(http.MethodPost, refundUrl, bytes.NewReader(marshal)) if err != nil { return trade, err } request.Header.Add("Authorization", "WECHATPAY2-SHA256-RSA2048 "+token) request.Header.Add("User-Agent", "用户代理(https://zh.wikipedia.org/wiki/User_agent)") request.Header.Add("Content-type", "application/json;charset='utf-8'") request.Header.Add("Accept", "application/json") client := http.DefaultClient response, err := client.Do(request) if err != nil { return trade, err } defer func() { response.Body.Close() }() bodyBytes, err := ioutil.ReadAll(response.Body) if err != nil { return trade, err } return string(bodyBytes), nil }
理论要掌握,实操不能落!以上关于《golang实现微信支付v3版本的方法》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
-
260 收藏
-
290 收藏
-
202 收藏
-
199 收藏
-
145 收藏
-
168 收藏
-
165 收藏
-
473 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习
-
- 从容的黑米
- 这篇博文真是及时雨啊,细节满满,受益颇多,码起来,关注老哥了!希望老哥能多写Golang相关的文章。
- 2023-04-11 06:24:14
-
- 健忘的微笑
- 这篇技术文章出现的刚刚好,太详细了,太给力了,码住,关注作者了!希望作者能多写Golang相关的文章。
- 2023-03-22 05:24:21
-
- 虚拟的飞机
- 受益颇多,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢老哥分享博文!
- 2023-03-15 21:01:54
-
- 温暖的小蝴蝶
- 这篇博文真是及时雨啊,很详细,很有用,mark,关注老哥了!希望老哥能多写Golang相关的文章。
- 2023-01-27 10:48:13
-
- 有魅力的鼠标
- 太细致了,mark,感谢博主的这篇文章,我会继续支持!
- 2023-01-22 18:40:42