Skip to content

lsongdev/wechatpay-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

WeChat Pay Go

WeChat Pay API v3 Go SDK - Simple and efficient WeChat Pay client implementation

Features

  • ✅ 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

Installation

go get github.com/lsongdev/wechatpay-go

Quick Start

1. Initialize Config

package 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)
}

2. Query Order

// 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")

3. Close Order

err := client.CloseOrder("1234567890")
if err != nil {
    panic(err)
}

4. Refund

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)

Config Reference

Config Struct

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)

LoadCert Method

Loads private key and certificate from specified paths and automatically sets them to Config. Certificate serial number is automatically calculated when creating the client.

Order Status

Status Description
SUCCESS Payment successful
REFUND Refunded
NOTPAY Not paid
CLOSED Closed
REVOKED Revoked
USERPAYING User paying
PAYERROR Payment failed

Error Handling

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)
}

Examples

For complete example code, see examples/ directory.

Documentation Links

License

Apache License 2.0

About

WeChat Pay API v3 Go SDK - Simple and efficient WeChat Pay client implementation

Resources

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages