推荐使用v3接口,官方在v3接口实现未覆盖或gopay未开发的接口,还继续用v2接口,欢迎参与完善v3接口。
已实现API列表附录:API 列表附录
通用规则:通用规则
GoPay微信v2文档:GoPay微信v2文档 (部分接口仅v2版本支持)
注意:v3 版本接口持续增加中,不支持沙箱支付,测试请用1分钱测试法
具体API使用介绍,请参考
gopay/wechat/v3/client_test.go
import (
"github.com/go-pay/xlog"
"github.com/go-pay/gopay/wechat/v3"
)
// NewClientV3 初始化微信客户端 v3
// mchid:商户ID 或者服务商模式的 sp_mchid
// serialNo:商户证书的证书序列号
// apiV3Key:apiV3Key,商户平台获取
// privateKey:私钥 apiclient_key.pem 读取后的内容
client, err = wechat.NewClientV3(MchId, SerialNo, APIv3Key, PrivateKey)
if err != nil {
xlog.Error(err)
return
}
// 设置微信平台API证书和序列号(推荐开启自动验签,无需手动设置证书公钥等信息)
//client.SetPlatformCert([]byte(""), "")
// 启用自动同步返回验签,并定时更新微信平台API证书(开启自动验签时,无需单独设置微信平台API证书和序列号)
err = client.AutoVerifySign()
if err != nil {
xlog.Error(err)
return
}
// 自定义配置http请求接收返回结果body大小,默认 10MB
client.SetBodySize() // 没有特殊需求,可忽略此配置
// 设置自定义RequestId生成方法,非必须
client.SetRequestIdFunc()
// 打开Debug开关,输出日志,默认是关闭的
client.DebugSwitch = gopay.DebugOn
具体参数请根据不同接口查看:微信支付V3的API字典概览
import (
"github.com/go-pay/gopay"
)
expire := time.Now().Add(10 * time.Minute).Format(time.RFC3339)
// 初始化 BodyMap
bm := make(gopay.BodyMap)
bm.Set("sp_appid", "sp_appid").
Set("sp_mchid", "sp_mchid").
Set("sub_mchid", "sub_mchid").
Set("description", "测试Jsapi支付商品").
Set("out_trade_no", tradeNo).
Set("time_expire", expire).
Set("notify_url", "https://www.fmm.ink").
SetBodyMap("amount", func(bm gopay.BodyMap) {
bm.Set("total", 1).
Set("currency", "CNY")
}).
SetBodyMap("payer", func(bm gopay.BodyMap) {
bm.Set("sp_openid", "asdas")
})
wxRsp, err := client.V3TransactionJsapi(bm)
if err != nil {
xlog.Error(err)
return
}
if wxRsp.Code == Success {
xlog.Debugf("wxRsp: %#v", wxRsp.Response)
return
}
xlog.Errorf("wxRsp:%s", wxRsp.Error)
小程序调起支付API:小程序调起支付API
APP调起支付API:APP调起支付API
JSAPI调起支付API:JSAPI调起支付API
// 小程序
applet, err := client.PaySignOfApplet("appid", "prepayid")
// app
app, err := client.PaySignOfApp("appid", "prepayid")
// jsapi
jsapi, err := client.PaySignOfJSAPI("appid", "prepayid")
异步通知请求参数需要先解析,解析出来的结构体或BodyMap再验签(此处需要注意,
http.Request.Body
只能解析一次,如果需要解析前调试,请处理好Body复用问题)
import (
"github.com/go-pay/gopay/wechat/v3"
"github.com/go-pay/xlog"
)
wxRsp, err := client.V3TransactionJsapi(bm)
if err != nil {
xlog.Error(err)
return
}
pkMap := client.WxPublicKeyMap()
// wxPublicKey:微信平台证书公钥内容,通过 client.WxPublicKeyMap() 获取,然后根据 signInfo.HeaderSerial 获取相应的公钥
err = wechat.V3VerifySignByPK(wxRsp.SignInfo.HeaderTimestamp, wxRsp.SignInfo.HeaderNonce, wxRsp.SignInfo.SignBody, wxRsp.SignInfo.HeaderSignature, pkMap[wxRsp.SignInfo.HeaderSerial])
if err != nil {
xlog.Error(err)
return
}
import (
"github.com/go-pay/gopay/wechat/v3"
"github.com/go-pay/xlog"
)
notifyReq, err := wechat.V3ParseNotify()
if err != nil {
xlog.Error(err)
return
}
// 获取微信平台证书
certMap := client.WxPublicKeyMap()
// 验证异步通知的签名
err = notifyReq.VerifySignByPKMap(certMap)
if err != nil {
xlog.Error(err)
return
}
// ====↓↓↓====异步通知应答====↓↓↓====
// 退款通知http应答码为200且返回状态码为SUCCESS才会当做商户接收成功,否则会重试。
// 注意:重试过多会导致微信支付端积压过多通知而堵塞,影响其他正常通知。
// 此写法是 gin 框架返回微信的写法
c.JSON(http.StatusOK, &wechat.V3NotifyRsp{Code: gopay.SUCCESS, Message: "成功"})
// 此写法是 echo 框架返回微信的写法
return c.JSON(http.StatusOK, &wechat.V3NotifyRsp{Code: gopay.SUCCESS, Message: "成功"})
// ====↓↓↓====入参、出参加解密====↓↓↓====
// 敏感信息加密
client.V3EncryptText()
// 敏感信息解密
client.V3DecryptText()
// ====↓↓↓====异步通知参数解密====↓↓↓====
// 通用通知解密(推荐此方法)
result, err := notifyReq.DecryptCipherTextToStruct(apiV3Key, objPtr)
// 普通支付通知解密
result, err := notifyReq.DecryptPayCipherText(apiV3Key)
// 合单支付通知解密
result, err := notifyReq.DecryptCombineCipherText(apiV3Key)
// 退款通知解密
result, err := notifyReq.DecryptRefundCipherText(apiV3Key)
...
import (
"github.com/go-pay/gopay/wechat/v3"
)
// 获取微信平台证书和序列号信息,推荐使用后者
wechat.GetPlatformCerts()
或
client.GetAndSelectNewestCert()
// 请求参数 敏感信息加密,推荐使用后者
wechat.V3EncryptText() 或 client.V3EncryptText()
// 返回参数 敏感信息解密,推荐使用后者
wechat.V3DecryptText() 或 client.V3DecryptText()
// 回调通知 resource.ciphertext 敏感信息解密
wechat.V3DecryptNotifyCipherTextToStruct() // 推荐使用此方法
wechat.V3DecryptPayNotifyCipherText()
wechat.V3DecryptRefundNotifyCipherText()
wechat.V3DecryptCombineNotifyCipherText()
...
client.V3TransactionApp()
client.V3TransactionJsapi()
client.V3TransactionNative()
client.V3TransactionH5()
client.V3QQTransactionH5()
client.V3TransactionQueryOrder()
client.V3TransactionCloseOrder()
client.V3PartnerTransactionApp()
client.V3PartnerTransactionJsapi()
client.V3PartnerTransactionNative()
client.V3PartnerTransactionH5()
client.V3PartnerQueryOrder()
client.V3PartnerCloseOrder()
client.V3CombineTransactionApp()
client.V3CombineTransactionJsapi()
client.V3CombineTransactionNative()
client.V3CombineTransactionH5()
client.V3CombineQQTransactionH5()
client.V3CombineQueryOrder()
client.V3CombineCloseOrder()
client.V3Refund()
client.V3RefundQuery()
client.V3BillTradeBill()
client.V3BillFundFlowBill()
client.V3BillEcommerceFundFlowBill()
client.V3BillDownLoadBill()
client.V3Withdraw()
client.V3WithdrawStatus()
client.V3EcommerceWithdraw()
client.V3EcommerceWithdrawStatus()
client.V3WithdrawDownloadErrBill()
client.V3ScoreOrderCreate()
client.V3ScoreOrderQuery()
client.V3ScoreOrderCancel()
client.V3ScoreOrderModify()
client.V3ScoreOrderComplete()
client.V3ScoreOrderPay()
client.V3ScoreOrderSync()
client.V3ScoreDirectComplete()
client.V3ScorePermission()
client.V3ScorePermissionQuery()
client.V3ScorePermissionTerminate()
client.V3ScorePermissionOpenidQuery()
client.V3ScorePermissionOpenidTerminate()
client.V3DiscountCardApply()
client.V3DiscountCardAddUser()
client.V3DiscountCardQuery()
client.V3SmartGuideReg()
client.V3SmartGuideAssign()
client.V3SmartGuideQuery()
client.V3SmartGuideUpdate()
client.V3BusinessPointsSync()
client.V3BusinessAuthPointsQuery()
client.V3BusinessPointsStatusQuery()
client.V3BusinessParkingSync()
client.V3VehicleParkingQuery()
client.V3VehicleParkingIn()
client.V3VehicleParkingFee()
client.V3VehicleParkingOrder()
client.V3FavorBatchCreate()
client.V3FavorBatchStart()
client.V3FavorBatchGrant()
client.V3FavorBatchPause()
client.V3FavorBatchRestart()
client.V3FavorBatchList()
client.V3FavorBatchDetail()
client.V3FavorDetail()
client.V3FavorMerchant()
client.V3FavorItems()
client.V3FavorUserCoupons()
client.V3FavorUseFlowDownload()
client.V3FavorRefundFlowDownload()
client.V3FavorCallbackUrl()
client.V3FavorCallbackUrlSet()
client.V3BusiFavorBatchCreate()
client.V3BusiFavorBatchDetail()
client.V3BusiFavorUse()
client.V3BusiFavorUserCoupons()
client.V3BusiFavorUserCouponDetail()
client.V3BusiFavorCodeUpload()
client.V3BusiFavorCallbackUrlSet()
client.V3BusiFavorCallbackUrl()
client.V3BusiFavorAssociate()
client.V3BusiFavorDisassociate()
client.V3BusiFavorBatchUpdate()
client.V3BusiFavorInfoUpdate()
client.V3BusiFavorSend()
client.V3BusiFavorReturn()
client.V3BusiFavorDeactivate()
client.V3BusiFavorSubsidyPay()
client.V3BusiFavorSubsidyPayDetail()
client.V3PartnershipsBuild()
client.V3PartnershipsTerminate()
client.V3PartnershipsList()
client.V3ProfitShareOrder()
client.V3ProfitShareOrderQuery()
client.V3ProfitShareReturn()
client.V3ProfitShareReturnResult()
client.V3ProfitShareOrderUnfreeze()
client.V3ProfitShareUnsplitAmount()
client.V3ProfitShareAddReceiver()
client.V3ProfitShareDeleteReceiver()
client.V3ComplaintList()
client.V3ComplaintDetail()
client.V3ComplaintNegotiationHistory()
client.V3ComplaintNotifyUrlCreate()
client.V3ComplaintNotifyUrlQuery()
client.V3ComplaintNotifyUrlUpdate()
client.V3ComplaintNotifyUrlDelete()
client.V3ComplaintResponse()
client.V3ComplaintComplete()
client.V3ComplaintUpdateRefundProgress()
client.V3ComplaintUploadImage()
client.V3ComplaintImage()
client.V3ViolationNotifyUrlCreate()
client.V3ViolationNotifyUrlQuery()
client.V3ViolationNotifyUrlUpdate()
client.V3ViolationNotifyUrlDelete()
client.V3MediaUploadImage()
client.V3MediaUploadVideo()
client.V3FavorMediaUploadImage()
client.V3MediaDownloadImage()
client.V3Transfer()
client.V3TransferQuery()
client.V3TransferDetail()
client.V3TransferMerchantQuery()
client.V3TransferMerchantDetail()
client.V3TransferReceipt()
client.V3TransferReceiptQuery()
client.V3TransferDetailReceipt()
client.V3TransferDetailReceiptQuery()
client.V3PartnerTransfer()
client.V3PartnerTransferQuery()
client.V3PartnerTransferDetail()
client.V3PartnerTransferMerchantQuery()
client.V3PartnerTransferMerchantDetail()
client.V3EcommerceBalance()
client.V3EcommerceDayBalance()
client.V3MerchantBalance()
client.V3MerchantDayBalance()
client.V3MerchantIncomeRecord()
client.V3EcommerceIncomeRecord()
client.V3Apply4SubSubmit()
client.V3Apply4SubQueryByBusinessCode()
client.V3Apply4SubQueryByApplyId()
client.V3Apply4SubModifySettlement()
client.V3AsyncApply4SubModifySettlement()
client.V3Apply4SubQuerySettlement()
client.V3Apply4SubMerchantsApplication()
client.V3GoldPlanManage()
client.V3GoldPlanBillManage()
client.V3GoldPlanFilterManage()
client.V3GoldPlanOpenAdShow()
client.V3GoldPlanCloseAdShow()
client.V3EcommerceApply()
client.V3EcommerceApplyStatus()
client.V3EcommerceProfitShare()
client.V3EcommerceProfitShareResult()
client.V3EcommerceProfitShareReturn()
client.V3EcommerceProfitShareReturnResult()
client.V3EcommerceProfitShareFinish()
client.V3EcommerceProfitShareUnsplitAmount()
client.V3EcommerceProfitShareAddReceiver()
client.V3EcommerceProfitShareDeleteReceiver()
client.V3EcommerceProfitShare()
client.V3EcommerceSubsidiesReturn()
client.V3EcommerceSubsidiesCancel()
client.V3EcommerceRefund()
client.V3EcommerceRefundQueryById()
client.V3EcommerceRefundQueryByNo()
client.V3EcommerceRefundAdvance()
client.V3EcommerceRefundAdvanceResult()
client.V3BankSearchBank()
client.V3BankSearchPersonalList()
client.V3BankSearchCorporateList()
client.V3BankSearchProvinceList()
client.V3BankSearchCityList()
client.V3BankSearchBranchList()
wechat.GetPlatformCerts()
=> 获取微信平台证书公钥wechat.GetPlatformRSACerts()
=> 获取平台RSA证书列表wechat.GetPlatformSM2Certs()
=> 获取国密平台证书client.GetAndSelectNewestCert()
=> 获取证书Map集并选择最新的有效证书序列号(默认RSA证书)client.GetAndSelectNewestCertRSA()
=> 获取证书Map集并选择最新的有效RSA证书序列号client.GetAndSelectNewestCertSM2()
=> 获取证书Map集并选择最新的有效SM2证书序列号client.GetAndSelectNewestCertALL()
=> 获取证书Map集并选择最新的有效RSA+SM2证书序列号client.WxPublicKey()
=> 获取最新的有效证书client.WxPublicKeyMap()
=> 获取有效证书 Mapwechat.V3ParseNotify()
=> 解析微信回调请求的参数到 V3NotifyReq 结构体notify.VerifySignByPKMap()
=> 微信V3 异步通知验签client.V3EncryptText()
=> 敏感参数信息加密client.V3DecryptText()
=> 敏感参数信息解密wechat.V3EncryptText()
=> 敏感参数信息加密wechat.V3DecryptText()
=> 敏感参数信息解密wechat.V3DecryptNotifyCipherTextToStruct()
=> 解密 统一数据 到指针结构体对象(推荐统一使用此方法)wechat.V3DecryptNotifyCipherTextToBytes()
=> 解密 统一数据 到 []bytewechat.V3DecryptPayNotifyCipherText()
=> 解密 普通支付 回调中的加密信息wechat.V3DecryptPartnerPayNotifyCipherText()
=> 解密 服务商普通支付 回调中的加密信息wechat.V3DecryptRefundNotifyCipherText()
=> 解密 普通退款 回调中的加密信息wechat.V3DecryptCombineNotifyCipherText()
=> 解密 合单支付 回调中的加密信息wechat.V3DecryptScoreNotifyCipherText()
=> 解密 支付分确认订单 回调中的加密信息wechat.V3DecryptScorePermissionNotifyCipherText()
=> 解密 支付分开启/解除授权服务 回调中的加密信息wechat.V3DecryptBusifavorNotifyCipherText()
=> 解密 领券事件 回调中的加密信息wechat.V3DecryptParkingInNotifyCipherText()
=> 解密 停车入场状态变更 回调中的加密信息wechat.V3DecryptParkingPayNotifyCipherText()
=> 解密 停车支付结果 回调中的加密信息wechat.V3DecryptCouponUseNotifyCipherText()
=> 解密 代金券核销 回调中的加密信息wechat.V3DecryptInvoiceTitleNotifyCipherText()
=> 解密 用户发票抬头填写完成 回调中的加密信息wechat.V3DecryptInvoiceNotifyCipherText()
=> 解密 发票卡券作废/发票开具成功/发票冲红成功/发票插入用户卡包成功 回调中的加密信息client.PaySignOfJSAPI()
=> 获取 JSAPI 支付 paySignclient.PaySignOfApp()
=> 获取 APP 支付 paySignclient.PaySignOfApplet()
=> 获取 小程序 支付 paySign