package module
Version: v0.0.0-...-ad9de86 Latest Latest

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

Go to latest
Published: Dec 9, 2016 License: MIT Imports: 13 Imported by: 0




Backend implementation of weixin pay(app) in golang


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 {

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

payRequest := appTrans.NewPaymentRequest(prepayId)

queryResult, err := appTrans.Query("1008450740201411110005820873")
if err != nil {


Please refer to gowalker




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


This section is empty.


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


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"`
	ReturnCode     string   `xml:"return_code"`
	ReturnMsg      string   `xml:"return_msg"`
	AppId          string   `xml:"appid"`
	MchId          string   `xml:"mch_id"`
	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"`
	DeviceInfo     string   `xml:"device_info"`
	OpenId         string   `xml:"open_id"`
	IsSubscribe    string   `xml:"is_subscribe"`
	TradeType      string   `xml:"trade_type"`
	TradeState     string   `xml:"trade_state"`
	TradeStateDesc string   `xml:"trade_state_desc"`
	BankType       string   `xml:"bank_type"`
	TotalFee       string   `xml:"total_fee"`
	FeeType        string   `xml:"fee_type"`
	CashFee        string   `xml:"cash_fee"`
	CashFeeType    string   `xml:"cash_fee_type"`
	CouponFee      string   `xml:"coupon_fee"`
	CouponCount    string   `xml:"coupon_count"`
	TransactionId  string   `xml:"transaction_id"`
	OrderId        string   `xml:"out_trade_no"`
	Attach         string   `xml:"attach"`
	TimeEnd        string   `xml:"time_end"`

QueryOrder Result represent query response message from weixin pay Refer to http://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_2&index=4

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
	Attach        string

Jump to

Keyboard shortcuts

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