Skip to content

gunsluo/wechatpay-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WechatPay GO(v3)

License CI codecov GoDoc Go Report Card

Introduction

Wechat Pay SDK(V3) Write by Go. API V3 of Office document is here.

Features

  • Signature/Verify messages
  • Encrypt/Decrypt cert
  • APIv3 Endpoints
  • None third-party dependency package

When developing, you can use the Makefile for doing the following operations:

Endpoint Description supported
pay Merchant send the payment transaction ✔️
query Merchant query payment transactions ✔️
close Merchant close the payment transaction ✔️
notify WeChat Pay notifies the merchant of the user's payment status ✔️
certificate obtain the platform cert and decrypt it to public key ✔️
tradebill obtain the download url of trade bill ✔️
fundflowbill obtain the download url of trade bill ✔️
refund Merchant send the refund transaction ✔️
refundquery Merchant query payment transactions ✔️
refundnotify WeChat Pay notifies the merchant of the refund status ✔️
combine pay Merchant send the combine payment, includes some sub transcation ✔️
combine close Merchant close the combine payment transactions ✔️
combine query Merchant query the combine payment transaction ✔️

Getting Started

Prepare your wechatp pay information, it includes App Id/Mech Id/Apiv3 Secret/Serial Number/Private Key Cert. You can find a getting started guide as shown below:

  1. import package
import "github.com/gunsluo/wechatpay-go/v3"
  1. use wechatpay-go package
// create a client of wechat pay
client, err := wechatpay.NewClient(
    wechatpay.Config{
       ...
    })

// create a pay request
req := &wechatpay.PayRequest{
    AppId:       appId,
    MchId:       mchId,
    Description: "for testing",
        ...
    TradeType: wechatpay.Native,
}

resp, err := req.Do(r.Context(), client)
if err != nil {
    // do something
}
codeUrl := resp.CodeUrl

Config

Click Wechat Pay and apply your account and configuration.

wechatpay.Config{
    AppId:       appId,
    MchId:       mchId,
    Apiv3Secret: apiv3Secret,
    Cert: wechatpay.CertSuite{
        SerialNo:       serialNo,
        PrivateKeyPath: privateKeyPath,
    },
}

Payment

Create a pay request and send it to wechat pay service.

req := &wechatpay.PayRequest{
    Description: "for testing",
    OutTradeNo:  tradeNo,
    TimeExpire:  time.Now().Add(10 * time.Minute),
    Attach:      "cipher code",
    NotifyUrl:   notifyURL,
    Amount: wechatpay.PayAmount{
        Total:    int(amount * 100),
        Currency: "CNY",
    },
    TradeType: wechatpay.Native,
}

resp, err := req.Do(r.Context(), payClient)
//resp, err := payClient(r.Context(), req)
if err != nil {
    e := &wechatpay.Error{}
    if errors.As(err, &e) {
        fmt.Println("status", e.Status, "code:", e.Code, "message:", e.Message)
    }
    return
}
codeUrl := resp.CodeUrl
// use this code url to generate qr code

Notify

Receive the notification from wechat pay, use ParseHttpRequest or Parse to get notification information.

func notifyForPay(w http.ResponseWriter, r *http.Request) {
    notification := &wechatpay.PayNotification{}
    trans, err := notification.ParseHttpRequest(payClient, r)

    ...
}

func notifyForRefund(w http.ResponseWriter, r *http.Request) {
    notification := &wechatpay.RefundNotification{}
    trans, err := notification.ParseHttpRequest(payClient, r)

    ...
}

There is a full example for wechatpay-go.

Download

download bill and unpack data. Download get the decrypted byte array, UnmarshalDownload get a struct data.

req := wechatpay.TradeBillRequest{
    BillDate: billDate,
    BillType: wechatpay.AllBill,
    TarType:  wechatpay.GZIP,
}

ctx := context.Background()
data, err := req.Download(ctx, payClient)
//data, err := payClient.DownloadTradeBill(ctx, req)
//resp, err := req.UnmarshalDownload(ctx, payClient)

Contributing

See the contributing documentation.