core

package
v0.0.0-...-9ceaa28 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2020 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ApiDomain                   = "https://api.mch.weixin.qq.com/"
	ApiCertification            = "/v3/certificates"                          // 平台证书下载
	ApiCombinedTransactionApp   = "/v3/combine-transactions/app"              // App合单支付
	ApiCombinedTransactionJsApi = "/v3/combine-transactions/jsapi"            // JsApi合单支付
	ApiProfitSharing            = "/v3/ecommerce/profitsharing/orders"        // 分账请求
	ApiProfitSharingReturn      = "/v3/ecommerce/profitsharing/returnorders"  // 分账回退请求
	ApiProfitSharingStatus      = "/v3/ecommerce/profitsharing/orders"        // 分账状态查询
	ApiWithdrawFund             = "/v3/ecommerce/fund/withdraw"               // 提现请求
	ApiWithdrawFundStatus       = "/v3/ecommerce/fund/withdraw/{withdraw_id}" // 提现状态查询
	ApiRefund                   = "/v3/ecommerce/refunds/apply"               // 退款
	ApiFundBalance              = "/v3/ecommerce/fund/balance/{sub_mchid}"    // 余额查询
)
View Source
const (
	AlgorithmAEADAES256GCM = "AEAD_AES_256_GCM"
)

Variables

View Source
var (
	ErrInvalidWechatPaySerial    = errors.New("invalid Wechatpay-Serial")
	ErrInvalidWechatPayTimestamp = errors.New("invalid Wechatpay-Timestamp")
	ErrInvalidWechatPayNonce     = errors.New("invalid Wechatpay-Nonce")
	ErrInvalidWechatPaySignature = errors.New("invalid Wechatpay-Signature")
	ErrPemDecodeFailed           = errors.New("pem decode failed")
	ErrInvalidResponse           = errors.New("verify response failed")
	ErrEventTypeNotSuccess       = errors.New("notify event_type is not TRANSACTION.SUCCESS")
	ErrEmptyNotifyResource       = errors.New("empty notify resource")
	ErrDecryptFailed             = errors.New("decrypt failed")
	ErrIncorrectResourceType     = errors.New("incorrect resource-type")
)
View Source
var (
	ErrInvalidBase = errors.New("invalid base")
)

Functions

func BufWriteByteWithLn

func BufWriteByteWithLn(writer *bufio.Writer, c byte) error

func BufWriteStringWithLn

func BufWriteStringWithLn(writer *bufio.Writer, s string) error

func BufWriteWithLn

func BufWriteWithLn(writer *bufio.Writer, p []byte) error

func BuildMessage

func BuildMessage(httpMethod string, urlString string, body []byte, nonceStr string, timestamp int64) ([]byte, error)

组织签名串信息 签名串格式:

HTTP请求方法\n
URL\n
请求时间戳\n
请求随机串\n
请求报文主体\n

func BuildUrl

func BuildUrl(params map[string]string, query url2.Values, subRoutes ...string) string

@param params url中的path,例:/v3/ecommerce/fund/withdraw/{withdraw_id}中的{withdraw_id} @param query url中的query信息 @subRoutes domain后的路由路径,例:https://api.mch.weixin.qq.com/v3/certificates中的/v3/certificates

func FormatCustomBase

func FormatCustomBase(num int64) string

十进制 > 定义的进制

func NewRequest

func NewRequest(authorization string, url string, method string, body io.Reader) (*http.Request, error)

func NonceStr

func NonceStr() string

生成随机字符串

func ParseCertification

func ParseCertification(certKey string) (*x509.Certificate, error)

func ParseCustomBase

func ParseCustomBase(str string) (int64, error)

自定义进制 > 十进制

func ParsePrivateKey

func ParsePrivateKey(privateKeyStr string) (*rsa.PrivateKey, error)

func Sign

func Sign(message []byte, privateKey *rsa.PrivateKey) (string, error)

利用api证书私钥对签名串进行签名,采用sha256-rsa,对结果进行base64加密

func SimpleRequest

func SimpleRequest(client *http.Client, url string, method string, authorization string, body []byte) (*http.Response, error)

func WithBaseHeader

func WithBaseHeader(req *http.Request, authorization string) *http.Request

Types

type ApiCert

type ApiCert struct {
	ApiSerialNo      string
	ApiPrivateKeyStr string
	ApiCertKey       string
}

type Client

type Client interface {
	/// 获取签名Authorization,由认证类型和签名信息组成
	Authorization(httpMethod string, urlString string, body []byte) (string, error)
	// 获取签名信息
	Token(httpMethod string, rawUrl string, body []byte) (string, error)
	// 合单支付
	CombineTransactions(req *dto.CombineTransactionsReq) (*dto.CombineTransactionsResp, error)
	// 提现
	WithdrawFund(req *dto.WithdrawFundReq) (*dto.WithdrawFundResp, error)
	// 提现状态查询
	QueryWithdrawalStatus(req *dto.QueryWithdrawalStatusReq) (*dto.QueryWithdrawalStatusResp, error)
	// 查询账户余额
	QueryFundBalance(req *dto.QueryFundBalanceReq) (*dto.QueryFundBalanceResp, error)
	// 分账
	ProfitSharing(req *dto.ProfitSharingReq) (*dto.ProfitSharingResp, error)
	// 分账退回
	ReturnProfitSharing(req *dto.ReturnProfitSharingReq) (*dto.ReturnProfitSharingResp, error)
	// 退款
	Refund(req *dto.RefundReq) (*dto.RefundResp, error)
	// 验证Response
	VerifyResponse(httpStatus int, header *http.Header, body []byte) error
	// 验证回调
	VerifyNotify(header *http.Header, body []byte) error
	// 证书和报文解密
	Decrypt(algorithm string, cipherText string, associatedData string, nonce string) ([]byte, error)
	// 下载平台证书
	Certificate() (*dto.CertificateResp, error)
	// 利用api证书私钥对签名串进行签名得到签名值
	Sign(message []byte) (string, error)
	// 对支付成功通知进行验签和解密
	HandlePaymentNotify(headerSerial string, headerSignature string, headerTimestamp string, headerNonce string, body []byte) (*dto.PaymentNotifyResp, error)
	// 对分账对账通知验签和解密
	HandleProfitSharingNotify(headerSerial string, headerSignature string, headerTimestamp string, headerNonce string, body []byte) (*dto.ProfitSharingNotifyResp, error)
	// 对退款通知验签和解密
	HandleRefundNotify(headerSerial string, headerSignature string, headerTimestamp string, headerNonce string, body []byte) (*dto.RefundNotifyResp, error)
	// 获取客户端调起支付时请求参数
	BuildTurnUpPaymentArgumentBody(tradeType dto.TradeType, prepayId string) (string, error)
	// 查询分账状态
	QueryProfitSharingStatus(req *dto.QueryProfitSharingStatusReq) (*dto.QueryProfitSharingStatusResp, error)
}

type ClientConf

type ClientConf struct {
	AppId      map[dto.TradeType]string // App平台的AppId
	MchId      string
	ApiV3Key   string
	ApiCert    *ApiCert
	PlatCert   *PlatformCert
	HttpClient *http.Client
}

type ErrResponseBody

type ErrResponseBody struct {
	HttpStatus int             `json:"http_status"`
	Code       string          `json:"code"`
	Message    string          `json:"message"`
	ReqId      string          `json:"req_id"`
	Detail     json.RawMessage `json:"detail"`
}

func (*ErrResponseBody) Error

func (r *ErrResponseBody) Error() string

type PlatformCert

type PlatformCert struct {
	PlatformSerialNo string
	PlatformCertKey  string
}

type TurnUpPaymentArgument

type TurnUpPaymentArgument interface {
	SetPaySign(privateKey *rsa.PrivateKey) error
	GetPaySign() string
}

type TurnUpPaymentArgumentApp

type TurnUpPaymentArgumentApp struct {
	Appid        string `json:"appid"`
	Partnerid    string `json:"partnerid"`
	Prepayid     string `json:"prepayid"`
	Noncestr     string `json:"noncestr"`
	Timestamp    string `json:"timestamp"`
	PackageValue string `json:"_package"`
	Sign         string `json:"sign"`
}

APP调起支付的参数

func (*TurnUpPaymentArgumentApp) GetPaySign

func (a *TurnUpPaymentArgumentApp) GetPaySign() string

func (*TurnUpPaymentArgumentApp) SetPaySign

func (a *TurnUpPaymentArgumentApp) SetPaySign(privateKey *rsa.PrivateKey) error

type TurnUpPaymentArgumentJsApi

type TurnUpPaymentArgumentJsApi struct {
	AppId     string `json:"appId"`
	TimeStamp string `json:"timeStamp"`
	Package   string `json:"package"`
	SignType  string `json:"signType"`
	PaySign   string `json:"paySign"`
	NonceStr  string `json:"nonceStr"`
}

JSAPI调起支付的参数

func (*TurnUpPaymentArgumentJsApi) GetPaySign

func (a *TurnUpPaymentArgumentJsApi) GetPaySign() string

func (*TurnUpPaymentArgumentJsApi) SetPaySign

func (a *TurnUpPaymentArgumentJsApi) SetPaySign(privateKey *rsa.PrivateKey) error

type TurnUpPaymentArgumentXcx

type TurnUpPaymentArgumentXcx struct {
	AppId     string `json:"appId"`
	TimeStamp string `json:"timeStamp"`
	Package   string `json:"package"`
	SignType  string `json:"signType"`
	PaySign   string `json:"paySign"`
	NonceStr  string `json:"nonceStr"`
}

小程序调起支付的参数

func (*TurnUpPaymentArgumentXcx) GetPaySign

func (a *TurnUpPaymentArgumentXcx) GetPaySign() string

func (*TurnUpPaymentArgumentXcx) SetPaySign

func (a *TurnUpPaymentArgumentXcx) SetPaySign(privateKey *rsa.PrivateKey) error

Jump to

Keyboard shortcuts

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