baidu

package module
v0.0.0-...-436e6c8 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

README

百度相关接口(简易版)

百度小程序支付、登录、生成二维码等相关接口

安装

启用 Go module
go get github.com/Caiqm/baidu
import "github.com/Caiqm/baidu"
未启用 Go module
go get github.com/Caiqm/baidu
import "github.com/Caiqm/baidu"

如何使用

// 实例化百度接口
var client, err = baidu.New(appID, Secret)
关于密钥(Secret)

密钥是百度应用的唯一凭证密钥,即 AppSecret,获取方式同 appid

加载支持配置

// 加载接口链接,支付参数AppKey与DealId
client.LoadOptionFunc(WithApiHost(HOST), WithPayParams(APPKEY, DEALID))

// 或者一开始就加载支付参数AppKey与DealId
var client, err = wxpay.New(appID, Secret, WithPayParams(APPKEY, DEALID))
关于加载支持配置

系统内置了几种可支持的配置

// 设置请求链接,可自定义请求接口,传入host字符串
WithApiHost(HOST)

// 设置加密方式
WithSignType(signType)

// 设置支付参数
WithPayParams(appKey, dealId)

// 也可自定义传入配置,返回以下类型即可
type OptionFunc func(c *Client)

用户登录凭证

// 用户登陆凭证
func TestClient_SessionKey(t *testing.T) {
	t.Log("========== SessionKey ==========")
	client.LoadOptionFunc(WithApiHost("https://openapi.com/rest/2.0/smartapp/getsessionkey"))
	var p SessionKey
	p.Code = "123456"
	p.AccessToken = "123456123456123456123456123456123456123456123456123456"
	r, err := client.SessionKey(p)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(r)
}

百度收银台

// 百度收银台
func TestClient_TradePolymerPayment(t *testing.T) {
	t.Log("========== TradePolymerPayment ==========")
	client.LoadOptionFunc(WithPayParams("", ""))
	client.LoadAppPrivateKey("")
	var p TradePolymerPayment
	p.TotalAmount = "1"
	p.TpOrderId = "TS13245678997546546"
	p.NotifyUrl = "https://www.com"
	p.DealTitle = "支付测试"
	p.SignFieldsRange = "1"
	r, err := client.TradePolymerPayment(p)
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("%+v", r)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuxParam

type AuxParam struct {
}

func (AuxParam) NeedAppId

func (aux AuxParam) NeedAppId() bool

func (AuxParam) NeedSecret

func (aux AuxParam) NeedSecret() bool

func (AuxParam) NeedSign

func (aux AuxParam) NeedSign() bool

type BizInfo

type BizInfo struct {
	TpData TpData `json:"tpData"` // bizInfo 组装键值对集合
}

type Client

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

func New

func New(appId, secret string, opts ...OptionFunc) (nClient *Client, err error)

初始化

func (*Client) LoadAppPrivateKey

func (c *Client) LoadAppPrivateKey(priKey string)

加载私钥

func (*Client) LoadAppPublicKey

func (c *Client) LoadAppPublicKey(pubKey string)

加载公钥

func (*Client) LoadOptionFunc

func (c *Client) LoadOptionFunc(opts ...OptionFunc)

加载接口链接

func (*Client) OnReceivedData

func (c *Client) OnReceivedData(fn func(method string, data []byte))

返回内容

func (*Client) URLValues

func (c *Client) URLValues(param Param) (value url.Values, err error)

请求参数

func (*Client) VerifySign

func (c *Client) VerifySign(signContent, sign string) (checkRes bool, err error)

验证签名

type Err

type Err struct {
	ErrorS           string `json:"error"`             // 异常提示信息
	ErrorDescription string `json:"error_description"` // 异常情况详细的提示信息
}

TOKEN错误

func (Err) Error

func (e Err) Error() string

type ErrorCode

type ErrorCode struct {
	ErrorNo
	ErrorCode int    `json:"error_code"`
	ErrorMsg  string `json:"error_msg"`
}

openapi错误

func (ErrorCode) Error

func (e ErrorCode) Error() string

type ErrorNo

type ErrorNo struct {
	Errno  int    `json:"errno"`
	Errmsg string `json:"errmsg"`
	Msg    string `json:"msg"`
}

基本错误

func (ErrorNo) Error

func (e ErrorNo) Error() string

type GetAccessToken

type GetAccessToken struct {
	AuxParam
	GrantType string `json:"grant_type"` // 固定为:client_credentials
	Scope     string `json:"scope"`      // 固定为:smartapp_snsapi_base
}

GetAccessToken 获取小程序全局唯一后台接口调用凭据(access_token)https://smartprogram.baidu.com/docs/develop/serverapi/power_exp/

func (GetAccessToken) NeedSecret

func (a GetAccessToken) NeedSecret() bool

func (GetAccessToken) NeedSign

func (a GetAccessToken) NeedSign() bool

type GetAccessTokenRsp

type GetAccessTokenRsp struct {
	Err
	AccessToken string `json:"access_token"` // 获取到的凭证
	ExpiresIn   int    `json:"expires_in"`   // 凭证有效时间,单位:秒
}

GetAccessTokenRsp 获取小程序全局唯一后台接口调用凭据响应参数

type GetUnionId

type GetUnionId struct {
	AuxParam
	Openid string `json:"openid"`
}

GetUnionId 获取unionID https://smartprogram.baidu.com/docs/develop/api/open/log_getunionid/

func (GetUnionId) NeedSecret

func (a GetUnionId) NeedSecret() bool

func (GetUnionId) NeedSign

func (a GetUnionId) NeedSign() bool

type GetUnionIdRsp

type GetUnionIdRsp struct {
	ErrorNo
	Data struct {
		Unionid string `json:"unionid"` // 小程序用户 + 开发者主体维度唯一的 id
	} `json:"data"`
	RequestId string `json:"request_id"` // 请求 ID ,标识一次请求
	Timestamp int    `json:"timestamp"`  // 时间戳 1640140013
}

type LongQrcode

type LongQrcode struct {
	AuxParam
	Path    string `json:"path"`     // 扫码进入的小程序页面路径,最大长度 4000 字节,可以为空。
	Width   int    `json:"width"`    // 二维码的宽度(单位:px)。最小 280px,最大 1280px
	Mf      int    `json:"mf"`       // 是否包含二维码内嵌 logo 标识,1001 为不包含,默认包含
	ImgFlag int    `json:"img_flag"` // 返回值选项,默认或传 1 时只返回二维码 base64 编码字符串,传 0 只返回 url
}

LongQrcode 二维码长链 https://smartprogram.baidu.com/docs/develop/serverapi/get_unlimited_qrcode/ 获取小程序二维码长链接,适用于需要的码数量较多的业务场景。通过该接口生成的二维码,永久有效,无数量限制。

func (LongQrcode) NeedAppId

func (q LongQrcode) NeedAppId() bool

func (LongQrcode) NeedSign

func (q LongQrcode) NeedSign() bool

type OptionFunc

type OptionFunc func(c *Client)

func WithApiHost

func WithApiHost(host string) OptionFunc

设置请求连接

func WithPayParams

func WithPayParams(appKey, dealId string) OptionFunc

设置支付参数

func WithSignType

func WithSignType(signType string) OptionFunc

设置加密方式

type Param

type Param interface {
	// NeedSign 是否需要签名,有的接口不需要签名,比如:小程序登录与获取手机号接口
	NeedSign() bool

	// NeedAppId 是否需要APPID,有的接口不需要APPID,比如:获取应用授权调用凭证
	NeedAppId() bool

	// NeedSecret 是否需要密钥
	NeedSecret() bool
}

type QrcodeRsp

type QrcodeRsp struct {
	ErrorNo
	RequestId string `json:"request_id"` // 请求 ID,标识一次请求
	Timestamp int    `json:"timestamp"`  // 时间戳
	Data      struct {
		Base64Str string `json:"base64_str,omitempty"` // 二维码 base64 编码字符串
		Url       string `json:"url,omitempty"`        // 小程序二维码短链
	} `json:"data"`
}

QrcodeRsp 二维码短链响应参数

type SessionKey

type SessionKey struct {
	AuxParam
	AccessToken string `json:"access_token"` // 接口调用凭证
	Code        string `json:"code"`         // 通过 swan.getLoginCode 获取 Authorization Code 特殊说明:code 中有 @ 符号时,会请求对应的开源宿主,用户身份校验及 SessionKey 生成过程由开源宿主实现
}

SessionKey 用户登陆凭证 https://smartprogram.baidu.com/docs/develop/api/open/getSessionKey/

func (SessionKey) NeedAppId

func (a SessionKey) NeedAppId() bool

func (SessionKey) NeedSecret

func (a SessionKey) NeedSecret() bool

func (SessionKey) NeedSign

func (a SessionKey) NeedSign() bool

type SessionKeyRsp

type SessionKeyRsp struct {
	ErrorCode
	Data struct {
		SessionKey string `json:"session_key"` // 用户的 SessionKey
		OpenId     string `json:"open_id"`     // 用户身份标识 不同用户登录同一个小程序获取到的 openid 不同,同一个用户登录不同小程序获取到的 openid 也不同
	} `json:"data"`
	RequestId string `json:"request_id"` // 请求 ID ,标识一次请求
	Timestamp int    `json:"timestamp"`  // 时间戳 1640140013
}

SessionKeyRsp 用户登陆凭证响应参数

type ShortQrcode

type ShortQrcode struct {
	AuxParam
	Path    string `json:"path"`     // 扫码进入的小程序页面路径,最大长度 4000 字节,可以为空。
	Width   int    `json:"width"`    // 二维码的宽度(单位:px)。最小 280px,最大 1280px
	Mf      int    `json:"mf"`       // 是否包含二维码内嵌 logo 标识,1001 为不包含,默认包含
	ImgFlag int    `json:"img_flag"` // 返回值选项,默认或传 1 时只返回二维码 base64 编码字符串,传 0 只返回 url
}

ShortQrcode 二维码短链 https://smartprogram.baidu.com/docs/develop/serverapi/get_qrcode/ 获取小程序二维码短链,长度固定 35 个字符,适用于需要的码数量较少的业务场景。通过该接口生成的二维码,永久有效,有数量限制。

func (ShortQrcode) NeedAppId

func (q ShortQrcode) NeedAppId() bool

func (ShortQrcode) NeedSign

func (q ShortQrcode) NeedSign() bool

type TpData

type TpData struct {
	AppKey      string                 `json:"appKey"`       // 表示应用身份的唯一 ID
	DealId      string                 `json:"dealId"`       // 百度收银台的财务结算凭证
	TpOrderId   string                 `json:"tpOrderId"`    // 业务方唯一订单号
	TotalAmount string                 `json:"totalAmount"`  // 订单总金额(单位:分)
	ReturnData  map[string]interface{} `json:"returnData"`   // 业务方用于透传的业务变量 支付成功后会以 query 形式注入到 payResultUrl 页面中(query 可以在页面的 onLoad 生命周期内获取)
	DisplayData string                 `json:"display_data"` // 收银台定制页面展示属性,非定制业务请置空 用于支付页面展示订单详细信息
}

type TradeCloseOrder

type TradeCloseOrder struct {
	AuxParam
	AccessToken string `json:"access_token"` // 授权小程序的接口调用凭据
	TpOrderId   string `json:"tpOrderId"`    // 开发者订单 ID
	PmAppKey    string `json:"pmAppKey"`     // 调起百度收银台的支付服务 appKey
}

TradeCloseOrder 关闭订单 https://smartprogram.baidu.com/docs/third/pay/close_order/

func (TradeCloseOrder) NeedAppId

func (aux TradeCloseOrder) NeedAppId() bool

func (TradeCloseOrder) NeedSign

func (aux TradeCloseOrder) NeedSign() bool

type TradeCloseOrderRsp

type TradeCloseOrderRsp struct {
	ErrorNo
	Data bool `json:"data"`
}

TradeCloseOrderRsp 关闭订单响应参数

type TradeOrderQuery

type TradeOrderQuery struct {
	AuxParam
	AccessToken string `json:"access_token"` // 授权小程序的接口调用凭据
	TpOrderId   string `json:"tpOrderId"`    // 开发者订单 ID
	PmAppKey    string `json:"pmAppKey"`     // 调起百度收银台的支付服务 appKey
}

TradeOrderQuery 查询订单 https://smartprogram.baidu.com/docs/third/pay/get_by_tp_order_id/

func (TradeOrderQuery) NeedAppId

func (aux TradeOrderQuery) NeedAppId() bool

func (TradeOrderQuery) NeedSign

func (aux TradeOrderQuery) NeedSign() bool

type TradeOrderQueryRsp

type TradeOrderQueryRsp struct {
	ErrorNo
	Data struct {
		BizInfo       string `json:"bizInfo"`       // 业务扩展字段
		Count         int    `json:"count"`         // 数量
		CreateTime    int    `json:"createTime"`    // 创建时间
		DealId        int    `json:"dealId"`        // 跳转百度收银台支付必带参数之一
		OrderId       int    `json:"orderId"`       // 百度订单ID
		OriPrice      int    `json:"oriPrice"`      // 原价
		ParentOrderId int    `json:"parentOrderId"` // 购物车订单父订单ID
		ParentType    int    `json:"parentType"`    // 订单类型
		PayMoney      int    `json:"payMoney"`      // 支付金额
		SettlePrice   int    `json:"settlePrice"`   // 结算金额
		Status        int    `json:"status"`        // 订单状态 -1订单已取消/订单已异常退款  1订单支付中 2订单已支付
		SubStatus     int    `json:"subStatus"`     // 订单子状态
		TotalMoney    int    `json:"totalMoney"`    // 总金额
		TpId          int    `json:"tpId"`          // tpid
		TpOrderId     string `json:"tpOrderId"`     // 开发者订单ID
		TradeNo       string `json:"tradeNo"`       // 支付单号
		Type          int    `json:"type"`          // ordertype
		OpenId        int    `json:"openId"`        // 小程序用户id
		AppKey        int    `json:"appKey"`        // 小程序appkey
		AppId         int    `json:"appId"`         // 小程序appid
		UserId        int    `json:"userId"`        // 用户 id 与支付状态通知中的保持一致
	} `json:"data"`
}

TradeOrderQueryRsp 查询订单响应参数

type TradePolymerPayment

type TradePolymerPayment struct {
	TotalAmount     string `json:"totalAmount"`             // 订单金额(单位:人民币分)。注:小程序测试包测试金额不可超过 1000 分
	TpOrderId       string `json:"tpOrderId"`               // 小程序开发者系统创建的唯一订单 ID ,当支付状态发生变化时,会通过此订单 ID 通知开发者
	NotifyUrl       string `json:"notifyUrl"`               // 通知开发者支付状态的回调地址,必须是合法的 URL ,与开发者平台填写的支付回调地址作用一致,未填写的以平台回调地址为准
	DealTitle       string `json:"dealTitle"`               // 订单的名称
	SignFieldsRange string `json:"signFieldsRange"`         // 用于区分验签字段范围,signFieldsRange 的值:0:原验签字段 appKey+dealId+tpOrderId;1:包含 totalAmount 的验签,验签字段包括appKey+dealId+tpOrderId+totalAmount。固定值为 1 。
	BizInfo         string `json:"bizInfo,omitempty"`       // 订单详细信息,需要是一个可解析为 JSON Object 的字符串
	PayResultUrl    string `json:"payResultUrl,omitempty"`  // 当前页面 path。Web 态小程序支付成功后跳回的页面路径,例如:'/pages/payResult/payResult'
	InlinePaySign   string `json:"inlinePaySign,omitempty"` // 内嵌支付组件返回的支付信息加密 key,与 内嵌支付组件配套使用,此值不传或者传空时默认调起支付面板
	PromotionTag    string `json:"promotionTag,omitempty"`  // 平台营销信息,此处传可使用平台券的 spuid,支持通过英文逗号分割传入多个 spuid 值,仅与百度合作平台类目券的开发者需要填写
}

TradePolymerPayment 百度收银台 https://smartprogram.baidu.com/docs/develop/api/open/payment_swan-requestPolymerPayment/#orderInfo

type TradePolymerPaymentRsp

type TradePolymerPaymentRsp struct {
	DealId  string `json:"dealId"`  // 跳转百度收银台支付必带参数之一,是百度收银台的财务结算凭证,与账号绑定的结算协议一一对应,每笔交易将结算到 dealId 对应的协议主体
	AppKey  string `json:"appKey"`  // 支付能力开通后分配的支付 appKey,用以表示应用身份的唯一 ID ,在应用审核通过后进行分配,一经分配后不会发生更改,来唯一确定一个应用
	RsaSign string `json:"rsaSign"` // 对appKey+dealId+totalAmount+tpOrderId进行 RSA 加密后的签名,防止订单被伪造
	TradePolymerPayment
}

TradePolymerPaymentRsp 百度收银台响应参数

type TradeRefund

type TradeRefund struct {
	AuxParam
	AccessToken      string `json:"access_token" form:"access_token"`                   // 授权小程序的接口调用凭据
	ApplyRefundMoney int64  `json:"applyRefundMoney,omitempty" form:"applyRefundMoney"` // 退款金额(单位:分),该字段最大不能超过支付回调中的总金额(totalMoney) 1.如不填金额时,默认整单发起退款 2.含有百度平台营销的订单,目前只支持整单发起退款,不支持部分多次退款
	BizRefundBatchID string `json:"bizRefundBatchId" form:"bizRefundBatchId"`           // 开发者退款批次
	IsSkipAudit      int64  `json:"isSkipAudit" form:"isSkipAudit"`                     // 是否跳过审核,不需要百度请求开发者退款审核请传 1,默认为0; 0:不跳过开发者业务方审核;1:跳过开发者业务方审核。 若不跳过审核,请对接请求业务方退款审核接口
	OrderID          int64  `json:"orderId" form:"orderId"`                             // 百度收银台订单 ID
	RefundReason     string `json:"refundReason" form:"refundReason"`                   // 退款原因
	RefundType       int64  `json:"refundType" form:"refundType"`                       // 退款类型 1:用户发起退款;2:开发者业务方客服退款;3:开发者服务异常退款。
	TpOrderID        string `json:"tpOrderId" form:"tpOrderId"`                         // 开发者订单 ID
	UserID           int64  `json:"userId" form:"userId"`                               // 百度收银台用户 ID
	RefundNotifyURL  string `json:"refundNotifyUrl,omitempty" form:"refundNotifyUrl"`   // 退款通知 url ,不传时默认为在开发者后台配置的 url
	PmAppKey         string `json:"pmAppKey" form:"pmAppKey"`                           // 调起百度收银台的支付服务 appKey
}

TradeRefund 申请退款 https://smartprogram.baidu.com/docs/third/pay/apply_order_refund/

func (TradeRefund) NeedAppId

func (aux TradeRefund) NeedAppId() bool

func (TradeRefund) NeedSign

func (aux TradeRefund) NeedSign() bool

type TradeRefundQuery

type TradeRefundQuery struct {
	AuxParam
	AccessToken string `json:"access_token"` // 授权小程序的接口调用凭据
	TpOrderId   string `json:"tpOrderId"`    // 开发者订单 ID
	PmAppKey    string `json:"pmAppKey"`     // 调起百度收银台的支付服务 appKey
	UserId      string `json:"userId"`       // 百度收银台用户 ID
}

TradeRefundQuery 查询退款 https://smartprogram.baidu.com/docs/third/pay/get_order_refund/

func (TradeRefundQuery) NeedAppId

func (aux TradeRefundQuery) NeedAppId() bool

func (TradeRefundQuery) NeedSign

func (aux TradeRefundQuery) NeedSign() bool

type TradeRefundQueryRsp

type TradeRefundQueryRsp struct {
	ErrorNo
	Data []struct {
		BizRefundBatchId string `json:"bizRefundBatchId"` // 开发者退款批次id
		OrderId          int    `json:"orderId"`          // 退款订单号
		RefundBatchId    int    `json:"refundBatchId"`    // 退款批次id
		RefundStatus     int    `json:"refundStatus"`     // 退款状态 1 退款中 2 退款成功 3 退款失败
		UserId           int    `json:"userId"`           // 退款用户id
	} `json:"data"`
}

TradeRefundQueryRsp 查询退款响应参数

type TradeRefundRsp

type TradeRefundRsp struct {
	ErrorNo
	Data struct {
		RefundBatchId  string `json:"refundBatchId"`  // 平台退款批次号
		RefundPayMoney int    `json:"refundPayMoney"` // 平台可退退款金额【分为单位】
	} `json:"data"`
}

TradeRefundRsp 申请退款响应参数

Jump to

Keyboard shortcuts

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