huifupay

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2024 License: MIT Imports: 19 Imported by: 0

README

HuifuPay

汇付支付的Golang版本SDK。

安装

go get -u github.com/yuewokeji/huifupay

使用

初始化
config := huifupay.NewConfig("system_id", "product_id", "rsa_huifu_pub_key", "rsa_merch_pri_key", true)
client := v2.NewClient(config)

使用自定义的http Client,调用huifupay.SetGlobalHTTPClientFunc()(全局配置),或者huifupay.WithHTTPClientFunc()(只对当前实例有效)

同步接口
package main

import (
	"context"
	"fmt"
	"github.com/yuewokeji/huifupay"
	"github.com/yuewokeji/huifupay/v2"
)

func main() {
	config := huifupay.NewConfig("system_id", "product_id", "rsa_huifu_pub_key", "rsa_merch_pri_key", true)
	client := v2.NewClient(config)
	req := &v2.TradeAcctpaymentBalanceQueryRequest{
		// ...
	}
	resp, err := client.TradeAcctpaymentBalanceQuery(context.Background(), req)
	if err != nil {
		panic(err)
	}
	fmt.Println(resp.Data.RespCode)
}

由于时间关系,只实现了v2版本的部分api(详见api列表),有缘人请提交PR。

异步通知
package main

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/yuewokeji/huifupay"
	"github.com/yuewokeji/huifupay/v2"
)

func main() {
	config := huifupay.NewConfig("system_id", "product_id", "rsa_huifu_pub_key", "rsa_merch_pri_key", true)
	client := v2.NewClient(config)
	
	r := gin.Default()
	r.POST("/refund/notify", func(c *gin.Context) {
		n, err := client.TradePaymentScanpayRefundNotify(context.Background(), c.Request)
		if err != nil {
			panic(err)
		}
		fmt.Println(n.RespCode)
	})
	r.Run()
}

webhook
package main

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/yuewokeji/huifupay/webhook"
)

func main() {
	hook := webhook.NewWithSigner([]byte("your_access_key"))

	// 注册默认处理方法
	hook.SetDefaultHandler(func(ctx context.Context, data []byte) error {
		fmt.Printf("web hook: %s\n", string(data))
		return nil
	})

	// 注册其它处理方法
	// hook.Register(e, fn)

	r := gin.Default()
	r.POST("/webhook", func(c *gin.Context) {
		hr, err := hook.HandleRequest(context.Background(), c.Request)
		if err != nil {
			fmt.Printf("handle webhook error: %s\n", err.Error())
		} else {
			fmt.Printf("handle webhook: %s", hr.Event)
		}
	})
	r.Run()
}

json字符串与结构体互转

汇付接口中,有部分参数是json对象字符串,并不是json对象。

autoassign支持这两个不同类型的字段自动转换。

交易查询接口中,返回参数fee_formula_infos是一个string类型的json对象。

通过tag配置可以实现自动转换:

type TradePaymentScanPayQueryResponse struct {
    // ...
    FeeFormulaInfos       string                                   `json:"fee_formula_infos"` //手续费费率信息 交易成功时返回手续费费率信息
    FeeFormulaInfosObject []TradePaymentScanpayQueryFeeFormulaInfo `autoassign:"FeeFormulaInfos"`
}
debug

配置环境变量,打开debug功能。

变量名:HUIFU_DEBUG

变量值:参照debug说明

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetGlobalHTTPClientFunc

func SetGlobalHTTPClientFunc(fn HTTPClientFunc)

func Version

func Version() string

Types

type Client

type Client struct {
	*Logger
	// contains filtered or unexported fields
}

func NewClient

func NewClient(config Config, options ...ClientOption) *Client

func (*Client) BuildRequestWithSign

func (c *Client) BuildRequestWithSign(req request.Request) (httpReq *http.Request, err error)

func (*Client) DoNotifyRequest

func (c *Client) DoNotifyRequest(ctx context.Context, req *http.Request, n notify.Notify) (err error)

DoNotifyRequest 异步回调验证 https://paas.huifu.com/partners/jiekouguifan#/ybxx/jiekouguifan_ybxx

func (*Client) DoRequest

func (c *Client) DoRequest(ctx context.Context, req request.Request, resp response.Response) (err error)

func (*Client) DoUploadFile

func (c *Client) DoUploadFile(ctx context.Context, filePath string, req *request.FileRequest, resp response.Response) (err error)

func (*Client) GetSigner

func (c *Client) GetSigner() sign.Signer

func (*Client) Println

func (c *Client) Println(s string)

func (*Client) SetLogger

func (c *Client) SetLogger(level string, out io.Writer)

type ClientOption

type ClientOption func(client *Client)

func WithHTTPClientFunc

func WithHTTPClientFunc(fn HTTPClientFunc) ClientOption

WithHTTPClientFunc Client会优先使用WithHttpClientFunc(),如果没有设置再从SetGlobalHttpClientFunc()中获取

type Config

type Config struct {
	SystemID  string `json:"system_id"`
	ProductId string `json:"product_id"`

	// 汇付公钥
	RsaHuifuPublicKey string `json:"rsa_huifu_public_key"`

	// 商户私钥
	RsaMerchantPrivateKey string `json:"rsa_merchant_private_key"`

	// 是否对请求返回的结果进行验签
	VerifySign bool `json:"verify_sign"`
}

func NewConfig

func NewConfig(systemID, productID, rsaHuifuPublicKey, rsaMerchantPrivateKey string, verifySign bool) Config

type HTTPClientFunc

type HTTPClientFunc func(r *http.Request) *http.Client

HTTPClientFunc 用来自定义http.Client

type Logger

type Logger struct {
	*log.Logger
	// contains filtered or unexported fields
}

func (*Logger) Disable

func (l *Logger) Disable()

func (*Logger) Enable

func (l *Logger) Enable()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL