wxpayslim

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 20 Imported by: 0

README

wxpayslim

Transfer

import "github.com/caiguanhao/wxpayslim"

client = wxpayslim.NewClient("1111111111", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

// client.Debug = true

client.SetCertificate("-----BEGIN CERTIFICATE-----\n...", "-----BEGIN PRIVATE KEY-----\n...")

ctx := context.Background()

resp, err := client.Transfer(ctx, wxpayslim.TransferRequest{
	AppId:          "wxxxxxxxxxxxxxxxxx",
	PartnerTradeNo: "TESTz20220311z111122",
	OpenId:         "oAxxxxxxxxxxxxxxxxxxxxxxxxxx",
	Amount:         100,
	Desc:           "one-yuan",
})

// resp = {
//   ReturnCode:SUCCESS
//   ReturnMsg:
//   NonceStr:ZneDMNUuaOidCoYaQ2DAAVOkWP4kOUyf
//   ResultCode:SUCCESS
//   ErrCode:
//   ErrCodeDes:
//   AppId:wxxxxxxxxxxxxxxxxx
//   MchId:1111111111
//   DeviceInfo:
//   PartnerTradeNo:TESTz20220311z111122
//   PaymentNo:10000000000000000000000000000000
//   PaymentTime:2022-03-11 11:11:23 +0800 UTC+8
// }

resp2, err := client.TransferQuery(ctx, wxpayslim.TransferQueryRequest{
	AppId:          "wxxxxxxxxxxxxxxxxx",
	PartnerTradeNo: "TESTz20220311z111122",
})

// resp2 = {
//   ReturnCode:SUCCESS
//   ReturnMsg:
//   NonceStr:
//   ResultCode:SUCCESS
//   ErrCode:
//   ErrCodeDes:
//   AppId:wxxxxxxxxxxxxxxxxx
//   MchId:1111111111
//   DetailId:10000000000000000000000000000000
//   Status:SUCCESS
//   Reason:
//   OpenId:oAxxxxxxxxxxxxxxxxxxxxxxxxxx
//   TransferName:
//   PartnerTradeNo:TESTz20220311z111122
//   PaymentAmount:100
//   TransferTime:2022-03-11 11:11:22 +0800 UTC+8
//   PaymentTime:2022-03-11 11:11:23 +0800 UTC+8
//   Desc:one-yuan
// }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	MchId string
	Key   string
	Debug bool

	TLSClientConfig *tls.Config
}

func NewClient

func NewClient(mchId, key string) *Client

NewClient creates a new client.

func (*Client) CreateOrder added in v1.1.0

func (client *Client) CreateOrder(ctx context.Context, req CreateOrderRequest) (*CreateOrderResponse, error)

CreateOrder initiates payment.

func (*Client) JSAPIPayParams added in v1.1.1

func (client *Client) JSAPIPayParams(appId, prepayId string) *JSAPIPayParams

Generate pay params for JSAPI.

func (*Client) MustSetCertificate added in v1.1.1

func (client *Client) MustSetCertificate(certPEM, keyPem string)

MustSetCertificate is like SetCertificate but panics if operation fails.

func (*Client) QueryOrder added in v1.1.0

func (client *Client) QueryOrder(ctx context.Context, req QueryOrderRequest) (*QueryOrderResponse, error)

QueryOrder gets information of an order by Transaction ID or Trade No.

func (*Client) QueryRefundOrder added in v1.2.0

func (client *Client) QueryRefundOrder(ctx context.Context, req QueryRefundOrderRequest) (*QueryRefundOrderResponse, error)

QueryRefundOrder gets information of a refund order by Transaction ID or Trade No.

func (*Client) RefundOrder added in v1.2.0

func (client *Client) RefundOrder(ctx context.Context, req RefundOrderRequest) (*RefundOrderResponse, error)

RefundOrder initiates refund. Need to set certificate (client.SetCertificate) first.

func (*Client) SetCertificate

func (client *Client) SetCertificate(certPEM, keyPem string) error

Set certificate (apiclient_cert.pem, string starts with -----BEGIN CERTIFICATE-----) and private key (apiclient_key.pem, string starts with -----BEGIN PRIVATE KEY-----). If you have different certificate format, set client's TLSClientConfig property directly.

If you have p12 file (apiclient_cert.p12), you can use following command to get certificate and private key:

openssl pkcs12 -in apiclient_cert.p12 -nodes

func (*Client) Transfer

func (client *Client) Transfer(ctx context.Context, req TransferRequest) (*TransferResponse, error)

Transfer money to user. Docs: https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2

func (*Client) TransferQuery

func (client *Client) TransferQuery(ctx context.Context, req TransferQueryRequest) (*TransferQueryResponse, error)

Query existing transaction. Docs: https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3

type CreateOrderRequest added in v1.1.0

type CreateOrderRequest struct {
	AppId          string // required
	DeviceInfo     string // optional
	SignType       string // optional, either MD5 (default) or HMAC-SHA256
	Body           string // required, max length is 127
	Detail         string // optional, max length is 6000
	Attach         string // optional, max length is 127
	OutTradeNo     string // required, max length is 32, min length is 6
	FeeType        string // optional, defaults to CNY
	TotalFee       int    // required, in cents
	SpbillCreateIp string // required, user's ip address
	TimeStart      string // optional, UTC+8 time format: 20060102150405
	TimeExpire     string // optional, UTC+8 time format: 20060102150405
	GoodsTag       string // optional, max length is 32
	NotifyURL      string // required, max length is 256
	TradeType      string // required, can be JSAPI, NATIVE, APP
	ProductId      string // required if TradeType == NATIVE, max length is 32
	LimitPay       string // optional, set to no_credit to disallow credit cards
	OpenId         string // required if TradeType == JSAPI
	Receipt        string // optional, set to Y to enable receipt
	ProfitSharing  string // optional, either Y or N (default)
	SceneInfo      string // optional
}

type CreateOrderResponse added in v1.1.0

type CreateOrderResponse struct {
	Response
	AppId      string `xml:"mch_appid,omitempty"`
	MchId      string `xml:"mchid,omitempty"`
	DeviceInfo string `xml:"device_info,omitempty"`
	TradeType  string `xml:"trade_type"`
	PrepayId   string `xml:"prepay_id"`
	CodeUrl    string `xml:"code_url"`
}

func (CreateOrderResponse) AsError added in v1.1.0

func (r CreateOrderResponse) AsError() error

type JSAPIPayParams added in v1.1.1

type JSAPIPayParams struct {
	AppId     string `json:"appId"`
	TimeStamp string `json:"timeStamp"`
	NonceStr  string `json:"nonceStr"`
	Package   string `json:"package"`
	SignType  string `json:"signType"`
	PaySign   string `json:"paySign"`
}

type QueryOrderRequest added in v1.1.0

type QueryOrderRequest struct {
	AppId         string // required
	TransactionId string // either TransactionId or OutTradeNo is required
	OutTradeNo    string
}

type QueryOrderResponse added in v1.1.0

type QueryOrderResponse struct {
	Response
	AppId string `xml:"appid,omitempty"`
	MchId string `xml:"mch_id,omitempty"`

	DeviceInfo         string `xml:"device_info,omitempty"`
	OpenId             string `xml:"openid"`
	IsSubscribe        string `xml:"is_subscribe"`
	TradeType          string `xml:"trade_type"`
	TradeState         string `xml:"trade_state"`
	BankType           string `xml:"bank_type"`
	TotalFee           int    `xml:"total_fee"`
	SettlementTotalFee int    `xml:"settlement_total_fee"`
	FeeType            string `xml:"fee_type"`
	CashFee            int    `xml:"cash_fee"`
	CashFeeType        string `xml:"cash_fee_type"`
	CouponFee          int    `xml:"coupon_fee"`
	CouponCount        int    `xml:"coupon_count"`
	TransactionId      string `xml:"transaction_id"`
	OutTradeNo         string `xml:"out_trade_no"`
	Attach             string `xml:"attach"`
	TimeEnd            string `xml:"time_end"`
	TradeStateDesc     string `xml:"trade_state_desc"`
}

func (QueryOrderResponse) AsError added in v1.1.0

func (r QueryOrderResponse) AsError() error

func (QueryOrderResponse) Paid added in v1.1.1

func (r QueryOrderResponse) Paid() bool

Check if order is successfully paid.

type QueryRefundOrderRequest added in v1.2.0

type QueryRefundOrderRequest struct {
	AppId         string // required
	TransactionId string // either TransactionId, OutTradeNo, OutRefundNo or RefundId is required
	OutTradeNo    string
	OutRefundNo   string
	RefundId      string
	Offset        int // optional
}

type QueryRefundOrderResponse added in v1.2.0

type QueryRefundOrderResponse struct {
	Response
	AppId string `xml:"appid"`
	MchId string `xml:"mch_id"`

	TotalRefundCount     int    `xml:"total_refund_count"`
	TransactionId        string `xml:"transaction_id"`
	OutTradeNo           string `xml:"out_trade_no"`
	TotalFee             int    `xml:"total_fee"`
	SettlementTotalFee   int    `xml:"settlement_total_fee"`
	FeeType              string `xml:"fee_type,omitempty"`
	CashFee              int    `xml:"cash_fee"`
	RefundCount          int    `xml:"refund_count"`
	OutRefundNo0         string `xml:"out_refund_no_0"`
	RefundId0            string `xml:"refund_id_0"`
	RefundChannel0       string `xml:"refund_channel_0"`
	RefundFee0           int    `xml:"refund_fee_0"`
	RefundFee            int    `xml:"refund_fee"`
	CouponRefundFee      int    `xml:"coupon_refund_fee"`
	SettlementRefundFee0 int    `xml:"settlement_refund_fee_0"`
	RefundStatus0        string `xml:"refund_status_0"`
	RefundAccount0       string `xml:"refund_account_0"`
	RefundRecvAccout0    string `xml:"refund_recv_accout_0"`
	RefundSuccessTime0   string `xml:"refund_success_time_0"`
	CashRefundFee        int    `xml:"cash_refund_fee"`
}

func (QueryRefundOrderResponse) AsError added in v1.2.0

func (r QueryRefundOrderResponse) AsError() error

func (QueryRefundOrderResponse) Refunded added in v1.2.0

func (r QueryRefundOrderResponse) Refunded() bool

Check if order is successfully refunded.

type RefundOrderRequest added in v1.2.0

type RefundOrderRequest struct {
	AppId         string // required
	SignType      string // optional, either MD5 (default) or HMAC-SHA256
	TransactionId string // either TransactionId or OutTradeNo is required
	OutTradeNo    string
	OutRefundNo   string // required, max length is 64
	TotalFee      int    // required, in cents
	RefundFee     int    // required, in cents
	RefundFeeType string // optional, defaults to CNY
	RefundDesc    string // optional
	RefundAccount string // optional
	NotifyURL     string // optional
}

type RefundOrderResponse added in v1.2.0

type RefundOrderResponse struct {
	Response
	AppId               string `xml:"appid,omitempty"`
	MchId               string `xml:"mch_id,omitempty"`
	TransactionId       string `xml:"transaction_id"`
	OutTradeNo          string `xml:"out_trade_no"`
	OutRefundNo         string `xml:"out_refund_no"`
	RefundId            string `xml:"refund_id"`
	RefundFee           int    `xml:"refund_fee"`
	SettlementRefundFee int    `xml:"settlement_refund_fee"`
	TotalFee            int    `xml:"total_fee"`
	SettlementTotalFee  int    `xml:"settlement_total_fee"`
	FeeType             string `xml:"fee_type"`
	CashFee             int    `xml:"cash_fee"`
	CashFeeType         string `xml:"cash_fee_type"`
	CashRefundFee       int    `xml:"cash_refund_fee"`
}

func (RefundOrderResponse) AsError added in v1.2.0

func (r RefundOrderResponse) AsError() error

type Response

type Response struct {
	ReturnCode string `xml:"return_code"`
	ReturnMsg  string `xml:"return_msg"`
	NonceStr   string `xml:"nonce_str,omitempty"`
	Sign       string `xml:"sign,omitempty"`
	ResultCode string `xml:"result_code,omitempty"`
	ErrCode    string `xml:"err_code,omitempty"`
	ErrCodeDes string `xml:"err_code_des,omitempty"`
}

func (Response) Success

func (r Response) Success() bool

type ResponseError

type ResponseError Response

func (ResponseError) Error

func (r ResponseError) Error() string

type TransferQueryRequest

type TransferQueryRequest struct {
	AppId          string // required
	PartnerTradeNo string // required
}

TransferQueryRequest is used in TransferQuery() function.

type TransferQueryResponse

type TransferQueryResponse struct {
	Response
	AppId          string    `xml:"appid,omitempty"`
	MchId          string    `xml:"mch_id,omitempty"`
	DetailId       string    `xml:"detail_id,omitempty"`
	Status         string    `xml:"status,omitempty"`
	Reason         string    `xml:"reason,omitempty"`
	OpenId         string    `xml:"openid,omitempty"`
	TransferName   string    `xml:"transfer_name,omitempty"`
	PartnerTradeNo string    `xml:"partner_trade_no"`
	PaymentAmount  int       `xml:"payment_amount"`
	TransferTime   *Utc8Time `xml:"transfer_time"`
	PaymentTime    *Utc8Time `xml:"payment_time"`
	Desc           string    `xml:"desc"`
}

func (TransferQueryResponse) AsError

func (r TransferQueryResponse) AsError() error

type TransferRequest

type TransferRequest struct {
	AppId          string // required
	OpenId         string // required
	DeviceInfo     string // optional
	PartnerTradeNo string // required
	CheckName      string // optional, either NO_CHECK (default) or FORCE_CHECK
	ReUserName     string // required if CheckName is FORCE_CHECK
	Amount         int    // required, must not lower than 100 (1.00 yuan)
	Desc           string // required
	SpbillCreateIp string // optional, user's IP address
}

TransferRequest is used in Transfer() function.

type TransferResponse

type TransferResponse struct {
	Response
	AppId          string    `xml:"mch_appid,omitempty"`
	MchId          string    `xml:"mchid,omitempty"`
	DeviceInfo     string    `xml:"device_info,omitempty"`
	PartnerTradeNo string    `xml:"partner_trade_no"`
	PaymentNo      string    `xml:"payment_no"`
	PaymentTime    *Utc8Time `xml:"payment_time"`
}

func (TransferResponse) AsError

func (r TransferResponse) AsError() error

type Utc8Time

type Utc8Time time.Time

func (Utc8Time) MarshalJSON added in v1.0.1

func (tm Utc8Time) MarshalJSON() ([]byte, error)

func (Utc8Time) String

func (tm Utc8Time) String() string

func (*Utc8Time) UnmarshalJSON added in v1.0.1

func (tm *Utc8Time) UnmarshalJSON(data []byte) error

func (*Utc8Time) UnmarshalXML

func (tm *Utc8Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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