alipay

package module
v0.0.0-...-f6e17eb Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2022 License: MIT Imports: 17 Imported by: 0

README

alipay

CI Go Reference License

支付宝 SDK for Go

使用指南

安装支付宝 SDK

go get -u github.com/go-wheels/alipay

初始化支付宝 SDK

alipayOptions := alipay.Options{
    Gateway:         alipay.GatewayProduction, // 支付宝网关
    AppID:           "", // 应用ID
    AppPrivateKey:   "", // 应用私钥
    AlipayPublicKey: "", // 支付宝公钥
}
alipayClient, _ := alipay.NewClient(alipayOptions)

支付 API

交易查询

tradeQueryRequest := alipay.TradeQueryRequest{
    OutTradeNo: "1586150366616",
}
var tradeQueryResponse alipay.TradeQueryResponse
alipayClient.Execute(tradeQueryRequest, &tradeQueryResponse)
log.Printf("%#v", tradeQueryResponse)

条码支付

tradePayRequest := alipay.TradePayRequest{
    NotifyURL:   "http://example.com/notify_url",
    OutTradeNo:  "1586150366616",
    Scene:       "bar_code",
    AuthCode:    "283189274716618278",
    Subject:     "fresh meat",
    TotalAmount: "0.01",
}
var tradePayResponse alipay.TradePayResponse
alipayClient.Execute(tradePayRequest, &tradePayResponse)
log.Printf("%#v", tradePayResponse)

扫码支付

tradePrecreateRequest := alipay.TradePrecreateRequest{
    NotifyURL:   "http://example.com/notify_url",
    OutTradeNo:  "1586150366616",
    TotalAmount: "0.01",
    Subject:     "fresh meat",
}
var tradePrecreateResponse alipay.TradePrecreateResponse
alipayClient.Execute(tradePrecreateRequest, &tradePrecreateResponse)
log.Printf("%#v", tradePrecreateResponse)

App 支付

tradeAppPayRequest := alipay.TradeAppPayRequest{
    ReturnURL:   "http://example.com/return_url",
    NotifyURL:   "http://example.com/notify_url",
    OutTradeNo:  "1586150366616",
    TotalAmount: "0.01",
    Subject:     "fresh meat",
}
query, _ := alipayClient.SDKExecute(tradeAppPayRequest)
log.Print(query)

手机网站支付

tradeWapPayRequest := alipay.TradeWapPayRequest{
    ReturnURL:   "http://example.com/return_url",
    NotifyURL:   "http://example.com/notify_url",
    Subject:     "fresh meat",
    OutTradeNo:  "1586150366616",
    TotalAmount: "0.01",
    QuitURL:     "http://example.com/quit_url",
    ProductCode: "QUICK_WAP_WAY",
}
url, _ := alipayClient.PageExecute(tradeWapPayRequest)
log.Print(url)

电脑网站支付

tradePagePayRequest := alipay.TradePagePayRequest{
    ReturnURL:   "http://example.com/return_url",
    NotifyURL:   "http://example.com/notify_url",
    OutTradeNo:  "1586150366616",
    ProductCode: "FAST_INSTANT_TRADE_PAY",
    TotalAmount: "0.01",
    Subject:     "fresh meat",
}
url, _ := alipayClient.PageExecute(tradePagePayRequest)
log.Print(url)

通知验签

http.HandleFunc("/notify_url", func(w http.ResponseWriter, r *http.Request) {
    err := alipayClient.VerifyNotification(r)
    if err != nil {
        // 验签失败
        return
    }
    // 验签成功
    w.Write([]byte("success"))
})

Documentation

Index

Constants

View Source
const (
	GatewayDevelopment = "https://openapi.alipaydev.com/gateway.do"
	GatewayProduction  = "https://openapi.alipay.com/gateway.do"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(options Options) (client *Client, err error)

func (*Client) Execute

func (c *Client) Execute(request Request, response interface{}) (err error)

func (*Client) PageExecute

func (c *Client) PageExecute(request Request) (url string, err error)

func (*Client) SDKExecute

func (c *Client) SDKExecute(request Request) (query string, err error)

func (*Client) VerifyNotification

func (c *Client) VerifyNotification(request *http.Request) (err error)

type Options

type Options struct {
	Gateway         string
	AppID           string
	AppPrivateKey   string
	AlipayPublicKey string
}

type Request

type Request interface {
	Method() string
}

type ResponseCommon

type ResponseCommon struct {
	Code    string `json:"code"`
	Msg     string `json:"msg"`
	SubCode string `json:"sub_code"`
	SubMsg  string `json:"sub_msg"`
}

func (ResponseCommon) IsSuccess

func (r ResponseCommon) IsSuccess() bool

type TradeAppPayRequest

type TradeAppPayRequest struct {
	ReturnURL   string `json:"-" url:"return_url,omitempty"`
	NotifyURL   string `json:"-" url:"notify_url,omitempty"`
	TotalAmount string `url:"-" json:"total_amount,omitempty"`
	Subject     string `url:"-" json:"subject,omitempty"`
	OutTradeNo  string `url:"-" json:"out_trade_no,omitempty"`
}

App 支付请求

func (TradeAppPayRequest) Method

func (TradeAppPayRequest) Method() string

type TradePagePayRequest

type TradePagePayRequest struct {
	ReturnURL   string `json:"-" url:"return_url,omitempty"`
	NotifyURL   string `json:"-" url:"notify_url,omitempty"`
	OutTradeNo  string `url:"-" json:"out_trade_no,omitempty"`
	ProductCode string `url:"-" json:"product_code,omitempty"`
	TotalAmount string `url:"-" json:"total_amount,omitempty"`
	Subject     string `url:"-" json:"subject,omitempty"`
}

电脑网站支付请求

func (TradePagePayRequest) Method

func (TradePagePayRequest) Method() string

type TradePayRequest

type TradePayRequest struct {
	NotifyURL   string `json:"-" url:"notify_url,omitempty"`
	OutTradeNo  string `url:"-" json:"out_trade_no,omitempty"`
	Scene       string `url:"-" json:"scene,omitempty"`
	AuthCode    string `url:"-" json:"auth_code,omitempty"`
	Subject     string `url:"-" json:"subject,omitempty"`
	TotalAmount string `url:"-" json:"total_amount,omitempty"`
}

条码支付请求

func (TradePayRequest) Method

func (TradePayRequest) Method() string

type TradePayResponse

type TradePayResponse struct {
	ResponseCommon
	TradeNo       string `json:"trade_no"`
	OutTradeNo    string `json:"out_trade_no"`
	BuyerLogonID  string `json:"buyer_logon_id"`
	TotalAmount   string `json:"total_amount"`
	ReceiptAmount string `json:"receipt_amount"`
	GMTPayment    string `json:"gmt_payment"`
	BuyerUserID   string `json:"buyer_user_id"`
}

条码支付响应

type TradePrecreateRequest

type TradePrecreateRequest struct {
	NotifyURL   string `json:"-" url:"notify_url,omitempty"`
	OutTradeNo  string `url:"-" json:"out_trade_no,omitempty"`
	TotalAmount string `url:"-" json:"total_amount,omitempty"`
	Subject     string `url:"-" json:"subject,omitempty"`
}

扫码支付请求

func (TradePrecreateRequest) Method

func (TradePrecreateRequest) Method() string

type TradePrecreateResponse

type TradePrecreateResponse struct {
	ResponseCommon
	OutTradeNo string `json:"out_trade_no"`
	QRCode     string `json:"qr_code"`
}

扫码支付响应

type TradeQueryRequest

type TradeQueryRequest struct {
	OutTradeNo string `url:"-" json:"out_trade_no,omitempty"`
	TradeNo    string `url:"-" json:"trade_no,omitempty"`
}

交易查询请求

func (TradeQueryRequest) Method

func (TradeQueryRequest) Method() string

type TradeQueryResponse

type TradeQueryResponse struct {
	ResponseCommon
	TradeNo      string `json:"trade_no"`
	OutTradeNo   string `json:"out_trade_no"`
	BuyerLogonID string `json:"buyer_logon_id"`
	TradeStatus  string `json:"trade_status"`
	TotalAmount  string `json:"total_amount"`
	BuyerUserID  string `json:"buyer_user_id"`
}

交易查询响应

type TradeWapPayRequest

type TradeWapPayRequest struct {
	ReturnURL   string `json:"-" url:"return_url,omitempty"`
	NotifyURL   string `json:"-" url:"notify_url,omitempty"`
	Subject     string `url:"-" json:"subject,omitempty"`
	OutTradeNo  string `url:"-" json:"out_trade_no,omitempty"`
	TotalAmount string `url:"-" json:"total_amount,omitempty"`
	QuitURL     string `url:"-" json:"quit_url,omitempty"`
	ProductCode string `url:"-" json:"product_code,omitempty"`
}

手机网站支付请求

func (TradeWapPayRequest) Method

func (TradeWapPayRequest) Method() string

Jump to

Keyboard shortcuts

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