WeChat Pay API v3 Go SDK - Simple and efficient WeChat Pay client implementation
- ✅ WeChat Pay API v3 support
- ✅ Automatic signature and verification
- ✅ Automatic certificate serial number calculation
- ✅ JSAPI order, query, refund and more
- ✅ Zero dependencies, Go standard library only
go get github.com/lsongdev/wechatpay-gopackage main
import (
"fmt"
"github.com/lsongdev/wechatpay-go/wechatpay"
)
func main() {
cfg := &wechatpay.Config{
API: "https://api.mch.weixin.qq.com",
AppID: "wxd2520e2febed3ac2",
MchID: "1105155963",
APIV3Key: "45a0a3b80364857f0a1d5fc7bc2cc2e3", // 32-byte key
}
// Load certificate
err := cfg.LoadCert("./apiclient_key.pem", "./apiclient_cert.pem")
if err != nil {
panic(err)
}
// Create client
client, err := wechatpay.NewWeChatPay(cfg)
if err != nil {
panic(err)
}
// Create order
order := &wechatpay.OrderRequest{
Description: "Test Order",
OutTradeNo: "1234567890",
NotifyUrl: "https://www.example.com/notify",
Amount: wechatpay.OrderAmount{
Total: 100, // smallest currency unit
Currency: "CNY",
},
}
resp, err := client.CreateOrder(order)
if err != nil {
panic(err)
}
fmt.Println("Code URL:", resp.CodeUrl)
}// Query by out trade number
trade, err := client.QueryByOutTradeNo("1234567890")
if err != nil {
panic(err)
}
fmt.Println("Trade State:", trade.TradeState)
fmt.Println("Trade State Desc:", trade.TradeStateDesc)
// Query by WeChat Pay transaction ID
trade, err = client.QueryByTransactionId("4200001234567890")err := client.CloseOrder("1234567890")
if err != nil {
panic(err)
}refund := &wechatpay.RefundRequest{
OutTradeNo: "1234567890",
OutRefundNo: "refund_1234567890",
Amount: wechatpay.RefundAmount{
Refund: 100, // refund amount in cents
Total: 100, // order amount in cents
Currency: "CNY",
From: []wechatpay.RefundFrom{
{Account: "AVAILABLE", Amount: 100},
},
},
Reason: "User requested refund",
}
resp, err := client.Refund(refund)
if err != nil {
panic(err)
}
fmt.Println("Refund ID:", resp.RefundId)
fmt.Println("Refund Status:", resp.Status)| Field | Type | Description |
|---|---|---|
API |
string | WeChat Pay API URL |
AppID |
string | Official Account/Mini Program AppID |
MchID |
string | Merchant ID |
APIV3Key |
string | API v3 key (32 bytes) |
PrivateKey |
string | Merchant private key (PEM format) |
MchCert |
string | Merchant certificate (PEM format) |
Loads private key and certificate from specified paths and automatically sets them to Config. Certificate serial number is automatically calculated when creating the client.
| Status | Description |
|---|---|
SUCCESS |
Payment successful |
REFUND |
Refunded |
NOTPAY |
Not paid |
CLOSED |
Closed |
REVOKED |
Revoked |
USERPAYING |
User paying |
PAYERROR |
Payment failed |
SDK returns errors with the following structure:
type ErrorMessage struct {
Code string `json:"code"`
Message string `json:"message"`
}Example:
resp, err := client.CreateOrder(order)
if err != nil {
// Handle error
fmt.Println("Error:", err)
}For complete example code, see examples/ directory.
- WeChat Pay API v3 Documentation
- JSAPI Order Interface
- Query Order Interface
- Apply for Refund Interface
Apache License 2.0