Help us shape the future of Go! Take the 2026 Go Developer Survey

wxpay

package module
v0.0.0-...-4001fe8 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2020 License: MIT Imports: 13 Imported by: 1

README

wxpay

Go语言微信App支付后台实现

Backend implementation of weixin pay(app) in golang

usage

//初始化
cfg := &wxpay.WxConfig{
	AppId:         "应用程序Id, 从https://open.weixin.qq.com上可以看得到",
	AppKey:        "API密钥, 在 商户平台->账户设置->API安全 中设置",
	MchId:         "商户号",
	NotifyUrl:     "后台通知地址",
	PlaceOrderUrl: "https://api.mch.weixin.qq.com/pay/unifiedorder",
	QueryOrderUrl: "https://api.mch.weixin.qq.com/pay/orderquery",
	TradeType:     "APP",
}
appTrans, err := wxpay.NewAppTrans(cfg)
if err != nil {
	panic(err)
}

//获取prepay id,手机端得到prepay id后加上验证就可以使用这个id发起支付调用
prepayId, err := appTrans.Submit("WOBHXLNSDFFALB7NLKN4FLVMPY", 1, "订单描述", "114.25.139.11")
if err != nil {
	panic(err)
}
fmt.Println(prepayId)

//加上Sign,已方便手机直接调用
payRequest := appTrans.NewPaymentRequest(prepayId)
fmt.Println(payRequest)

//查询订单接口
queryResult, err := appTrans.Query("1008450740201411110005820873")
if err != nil {
	panic(err)
}
fmt.Println(queryResult)

document

Please refer to gowalker

Documentation

Index

Constants

View Source
const ChinaTimeZoneOffset = 8 * 60 * 60 //Beijing(UTC+8:00)

Variables

This section is empty.

Functions

func NewNonceString

func NewNonceString() string

NewNonceString return random string in 32 characters

func NewTimestampString

func NewTimestampString() string

NewTimestampString return

func Sign

func Sign(param map[string]string, key string) string

Sign the parameter in form of map[string]string with app key. Empty string and "sign" key is excluded before sign. Please refer to http://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=4_3

func SortAndConcat

func SortAndConcat(param map[string]string) string

SortAndConcat sort the map by key in ASCII order, and concat it in form of "k1=v1&k2=2"

func ToMap

func ToMap(in interface{}) (map[string]string, error)

ToMap convert the xml struct to map[string]string

func ToXmlString

func ToXmlString(param map[string]string) string

ToXmlString convert the map[string]string to xml string

Types

type AppTrans

type AppTrans struct {
	Config *WxConfig
}

AppTrans is abstact of Transaction handler. With AppTrans, we can get prepay id

func NewAppTrans

func NewAppTrans(cfg *WxConfig) (*AppTrans, error)

Initialized the AppTrans with specific config

func (*AppTrans) NewPaymentRequest

func (this *AppTrans) NewPaymentRequest(prepayId string) PaymentRequest

NewPaymentRequest build the payment request structure for app to start a payment. Return stuct of PaymentRequest, please refer to http://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_12&index=2

func (*AppTrans) Query

func (this *AppTrans) Query(transId string) (QueryOrderResult, error)

Query the order from weixin pay server by transaction id of weixin pay

func (*AppTrans) Submit

func (this *AppTrans) Submit(orderId string, amount float64, desc string, clientIp string) (string, error)

Submit the order to weixin pay and return the prepay id if success, Prepay id is used for app to start a payment If fail, error is not nil, check error for more information

type PaymentRequest

type PaymentRequest struct {
	AppId     string
	PartnerId string
	PrepayId  string
	Package   string
	NonceStr  string
	Timestamp string
	Sign      string
}

type PlaceOrderResult

type PlaceOrderResult struct {
	XMLName     xml.Name `xml:"xml"`
	ReturnCode  string   `xml:"return_code"`
	ReturnMsg   string   `xml:"return_msg"`
	AppId       string   `xml:"appid"`
	MchId       string   `xml:"mch_id"`
	DeviceInfo  string   `xml:"device_info"`
	NonceStr    string   `xml:"nonce_str"`
	Sign        string   `xml:"sign"`
	ResultCode  string   `xml:"result_code"`
	ErrCode     string   `xml:"err_code"`
	ErrCodeDesc string   `xml:"err_code_des"`
	TradeType   string   `xml:"trade_type"`
	PrepayId    string   `xml:"prepay_id"`
	CodeUrl     string   `xml:"code_url"`
}

PlaceOrderResult represent place order reponse message from weixin pay. For field explanation refer to: http://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_1

func ParsePlaceOrderResult

func ParsePlaceOrderResult(resp []byte) (PlaceOrderResult, error)

Parse the reponse message from weixin pay to struct of PlaceOrderResult

func (*PlaceOrderResult) ToMap

func (this *PlaceOrderResult) ToMap() map[string]string

type QueryOrderResult

type QueryOrderResult struct {
	XMLName            xml.Name `xml:"xml"`
	Appid              string   `xml:"appid"`
	Attach             string   `xml:"attach"`
	BankType           string   `xml:"bank_type"`
	CashFee            string   `xml:"cash_fee"`
	CashFeeType        string   `xml:"cash_fee_type"`
	CouponCount        string   `xml:"coupon_count"`
	CouponFee          string   `xml:"coupon_fee"`
	CouponFee0         string   `xml:"coupon_fee_0"`
	CouponFee1         string   `xml:"coupon_fee_1"`
	CouponFee2         string   `xml:"coupon_fee_2"`
	CouponFee3         string   `xml:"coupon_fee_3"`
	CouponFee4         string   `xml:"coupon_fee_4"`
	CouponFee5         string   `xml:"coupon_fee_5"`
	CouponFee6         string   `xml:"coupon_fee_6"`
	CouponFee7         string   `xml:"coupon_fee_7"`
	CouponFee8         string   `xml:"coupon_fee_8"`
	CouponFee9         string   `xml:"coupon_fee_9"`
	CouponID0          string   `xml:"coupon_id_0"`
	CouponID1          string   `xml:"coupon_id_1"`
	CouponID2          string   `xml:"coupon_id_2"`
	CouponID3          string   `xml:"coupon_id_3"`
	CouponID4          string   `xml:"coupon_id_4"`
	CouponID5          string   `xml:"coupon_id_5"`
	CouponID6          string   `xml:"coupon_id_6"`
	CouponID7          string   `xml:"coupon_id_7"`
	CouponID8          string   `xml:"coupon_id_8"`
	CouponID9          string   `xml:"coupon_id_9"`
	CouponType0        string   `xml:"coupon_type_0"`
	CouponType1        string   `xml:"coupon_type_1"`
	CouponType2        string   `xml:"coupon_type_2"`
	CouponType3        string   `xml:"coupon_type_3"`
	CouponType4        string   `xml:"coupon_type_4"`
	CouponType5        string   `xml:"coupon_type_5"`
	CouponType6        string   `xml:"coupon_type_6"`
	CouponType7        string   `xml:"coupon_type_7"`
	CouponType8        string   `xml:"coupon_type_8"`
	CouponType9        string   `xml:"coupon_type_9"`
	FeeType            string   `xml:"fee_type"`
	IsSubscribe        string   `xml:"is_subscribe"`
	MchID              string   `xml:"mch_id"`
	NonceStr           string   `xml:"nonce_str"`
	Openid             string   `xml:"openid"`
	OutTradeNo         string   `xml:"out_trade_no"`
	ResultCode         string   `xml:"result_code"`
	ReturnCode         string   `xml:"return_code"`
	SettlementTotalFee string   `xml:"settlement_total_fee"`
	Sign               string   `xml:"sign"`
	SignType           string   `xml:"sign_type"`
	SubMchID           string   `xml:"sub_mch_id"`
	TimeEnd            string   `xml:"time_end"`
	TotalFee           string   `xml:"total_fee"`
	TradeType          string   `xml:"trade_type"`
	TransactionID      string   `xml:"transaction_id"`
}

QueryOrder Result represent query response message from weixin pay Refer to https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8

func ParseQueryOrderResult

func ParseQueryOrderResult(resp []byte) (QueryOrderResult, error)

func (*QueryOrderResult) ToMap

func (this *QueryOrderResult) ToMap() map[string]string

type WxConfig

type WxConfig struct {
	AppId         string
	AppKey        string
	MchId         string
	NotifyUrl     string
	PlaceOrderUrl string
	QueryOrderUrl string
	TradeType     string
}

Jump to

Keyboard shortcuts

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