Skip to content

mzmuer/alipay-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

alipay-sdk

支付宝sdk的go版本

安装

使用 go get 下载安装 SDK

$ go get -u github.com/mzmuer/alipay-sdk

快速开始

使用密钥

package main

import (
	"fmt"

	"github.com/mzmuer/alipay-sdk/request"
	"github.com/mzmuer/alipay-sdk/response"

	"github.com/mzmuer/alipay-sdk"
)

func main() {
	p, err := alipay.NewClient("your appId", "public key", "priv_key", false)
	if err != nil {
		panic(err)
	}
	
	req := request.TradeCreateReq{}
	req.NotifyUrl = "notify url"
	req.BizModel = request.TradeCreateBizModel{
		Body:           "body",
		Subject:        "subject",
		OutTradeNo:     "orderid",
		TimeoutExpress: "15m",
		TotalAmount:    "0.01",
		BuyerId:        "buyrtid",
	}

	result := response.TradeCreateResp{}
	_, err = p.Execute(&req, &result)

	fmt.Println(result, err)
}

使用证书

package main

import (
	"fmt"

	"github.com/mzmuer/alipay-sdk/request"
	"github.com/mzmuer/alipay-sdk/response"

	"github.com/mzmuer/alipay-sdk"
)

func main() {
	c, err := alipay.NewCertClient("your appId", "privateKey", `appPubCert`, "alipayRootCert", "alipayPubCert", false)
	if err != nil {
		panic(err)
	}

	req := request.SystemOauthTokenReq{}
	req.GrantType = "authorization_code"
	req.Code = "cc6c559845a64762b24e2cd63c4fZX47"

	result := response.SystemOauthTokenResp{}
	_, err = c.Execute(&req, &result)

	fmt.Println(result, err)
}

如何使用未支持的接口

package main

import (
	"fmt"

	"github.com/mzmuer/alipay-sdk/request"
	"github.com/mzmuer/alipay-sdk/response"

	"github.com/mzmuer/alipay-sdk"
)

type UimplementedReq struct {
	request.BaseRequest
	Field1 string `json:"field1"`
	Field2 string `json:"field2"`
	// ....
}

// 如果接口参数是在biz_model中,参考(request.TradeCreateReq)
type UimplementedModel struct {
	Field1 string `json:"field1"`
	Field2 string `json:"field2"`
    // ....
}

type UimplementedResp struct {
	response.BaseResponse
	Res1 string `json:"res1"`
	Res2 string `json:"res2"` 
    // .....
}

// 必须实现该接口
func (*UimplementedReq) GetMethod() string {
	return "method.name"
}

// 如果除了request.BaseRequest还有额外参数,必须实现该接口(参考request.SystemOauthTokenReq)
func (r *UimplementedReq) GetTextParams() map[string]string {
    m := r.UdfParams
	if m == nil {
		m = map[string]string{}
	}

	m["field1"] = r.Field1
	m["field2"] = r.Field1
	return m
}


func main() {
	c, err := alipay.NewCertClient("your appId", "privateKey", `appPubCert`, "alipayRootCert", "alipayPubCert", false)
	if err != nil {
		panic(err)
	}

	req := UimplementedReq{}
	req.Field1 = ""
	req.Field2 = ""
	req.BizModel = UimplementedModel{
		Field1: "",
		Field2: "",
	}

	result := UimplementedResp{}
	_, err = c.Execute(&req, &result)

	fmt.Println(result, err)
}

目前支持接口

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages