Giter Site home page Giter Site logo

wechatpay-go's Introduction

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.

wechatpay-go's People

Contributors

gunsluo avatar clearcodecn avatar mengjin-tes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.