123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766 |
- package alipay
- import (
- "encoding/json"
- "errors"
- "fmt"
- "strings"
- "sync"
- "time"
- "github.com/iGoogle-ink/gopay"
- )
- type Client struct {
- AppId string
- PrivateKeyType PKCSType
- PrivateKey string
- LocationName string
- AppCertSN string
- AliPayPublicCertSN string
- AliPayRootCertSN string
- ReturnUrl string
- NotifyUrl string
- Charset string
- SignType string
- AppAuthToken string
- AuthToken string
- IsProd bool
- location *time.Location
- mu sync.RWMutex
- }
- // 初始化支付宝客户端
- // 注意:如果使用支付宝公钥证书验签,请设置 支付宝根证书SN(client.SetAlipayRootCertSN())、应用公钥证书SN(client.SetAppCertSN())
- // appId:应用ID
- // privateKey:应用私钥,支持PKCS1和PKCS8
- // isProd:是否是正式环境
- func NewClient(appId, privateKey string, isProd bool) (client *Client) {
- return &Client{
- AppId: appId,
- PrivateKey: privateKey,
- IsProd: isProd,
- }
- }
- // alipay.trade.fastpay.refund.query(统一收单交易退款查询)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.fastpay.refund.query
- func (a *Client) TradeFastPayRefundQuery(bm gopay.BodyMap) (aliRsp *TradeFastpayRefundQueryResponse, err error) {
- if bm.Get("out_trade_no") == gopay.NULL && bm.Get("trade_no") == gopay.NULL {
- return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
- }
- err = bm.CheckEmptyError("out_request_no")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.fastpay.refund.query"); err != nil {
- return nil, err
- }
- aliRsp = new(TradeFastpayRefundQueryResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.order.settle(统一收单交易结算接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.order.settle
- func (a *Client) TradeOrderSettle(bm gopay.BodyMap) (aliRsp *TradeOrderSettleResponse, err error) {
- err = bm.CheckEmptyError("out_request_no", "trade_no", "royalty_parameters")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.order.settle"); err != nil {
- return nil, err
- }
- aliRsp = new(TradeOrderSettleResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.create(统一收单交易创建接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.create
- func (a *Client) TradeCreate(bm gopay.BodyMap) (aliRsp *TradeCreateResponse, err error) {
- err = bm.CheckEmptyError("out_trade_no", "total_amount", "subject")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.create"); err != nil {
- return nil, err
- }
- aliRsp = new(TradeCreateResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.close(统一收单交易关闭接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.close
- func (a *Client) TradeClose(bm gopay.BodyMap) (aliRsp *TradeCloseResponse, err error) {
- if bm.Get("out_trade_no") == gopay.NULL && bm.Get("trade_no") == gopay.NULL {
- return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.close"); err != nil {
- return nil, err
- }
- aliRsp = new(TradeCloseResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.cancel(统一收单交易撤销接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.cancel
- func (a *Client) TradeCancel(bm gopay.BodyMap) (aliRsp *TradeCancelResponse, err error) {
- if bm.Get("out_trade_no") == gopay.NULL && bm.Get("trade_no") == gopay.NULL {
- return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.cancel"); err != nil {
- return nil, err
- }
- aliRsp = new(TradeCancelResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.refund(统一收单交易退款接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.refund
- func (a *Client) TradeRefund(bm gopay.BodyMap) (aliRsp *TradeRefundResponse, err error) {
- if bm.Get("out_trade_no") == gopay.NULL && bm.Get("trade_no") == gopay.NULL {
- return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
- }
- err = bm.CheckEmptyError("refund_amount")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.refund"); err != nil {
- return nil, err
- }
- aliRsp = new(TradeRefundResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.page.refund(统一收单退款页面接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.page.refund
- func (a *Client) TradePageRefund(bm gopay.BodyMap) (aliRsp *TradePageRefundResponse, err error) {
- if bm.Get("out_trade_no") == gopay.NULL && bm.Get("trade_no") == gopay.NULL {
- return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
- }
- err = bm.CheckEmptyError("out_request_no", "refund_amount")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.page.refund"); err != nil {
- return nil, err
- }
- aliRsp = new(TradePageRefundResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.precreate(统一收单线下交易预创建)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.precreate
- func (a *Client) TradePrecreate(bm gopay.BodyMap) (aliRsp *TradePrecreateResponse, err error) {
- err = bm.CheckEmptyError("out_trade_no", "total_amount", "subject")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.precreate"); err != nil {
- return nil, err
- }
- aliRsp = new(TradePrecreateResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- if aliRsp.NullResponse != nil {
- info := aliRsp.NullResponse
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.pay(统一收单交易支付接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.pay
- func (a *Client) TradePay(bm gopay.BodyMap) (aliRsp *TradePayResponse, err error) {
- err = bm.CheckEmptyError("out_trade_no", "scene", "auth_code", "subject")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.pay"); err != nil {
- return nil, err
- }
- aliRsp = new(TradePayResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.query(统一收单线下交易查询)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.query
- func (a *Client) TradeQuery(bm gopay.BodyMap) (aliRsp *TradeQueryResponse, err error) {
- if bm.Get("out_trade_no") == gopay.NULL && bm.Get("trade_no") == gopay.NULL {
- return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.query"); err != nil {
- return nil, err
- }
- aliRsp = new(TradeQueryResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.app.pay(app支付接口2.0)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.app.pay
- func (a *Client) TradeAppPay(bm gopay.BodyMap) (payParam string, err error) {
- err = bm.CheckEmptyError("out_trade_no", "total_amount", "subject")
- if err != nil {
- return gopay.NULL, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.app.pay"); err != nil {
- return gopay.NULL, err
- }
- payParam = string(bs)
- return payParam, nil
- }
- // alipay.trade.wap.pay(手机网站支付接口2.0)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.wap.pay
- func (a *Client) TradeWapPay(bm gopay.BodyMap) (payUrl string, err error) {
- bm.Set("product_code", "QUICK_WAP_WAY")
- err = bm.CheckEmptyError("out_trade_no", "total_amount", "subject")
- if err != nil {
- return gopay.NULL, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.wap.pay"); err != nil {
- return gopay.NULL, err
- }
- payUrl = string(bs)
- return payUrl, nil
- }
- // alipay.trade.page.pay(统一收单下单并支付页面接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.page.pay
- func (a *Client) TradePagePay(bm gopay.BodyMap) (payUrl string, err error) {
- bm.Set("product_code", "FAST_INSTANT_TRADE_PAY")
- err = bm.CheckEmptyError("out_trade_no", "total_amount", "subject")
- if err != nil {
- return gopay.NULL, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.trade.page.pay"); err != nil {
- return gopay.NULL, err
- }
- payUrl = string(bs)
- return payUrl, nil
- }
- // alipay.fund.trans.toaccount.transfer(单笔转账到支付宝账户接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_28/alipay.fund.trans.toaccount.transfer
- // 注意:此接口官方以升级替换为 alipay.fund.trans.uni.transfer
- func (a *Client) FundTransToaccountTransfer(bm gopay.BodyMap) (aliRsp *FundTransToaccountTransferResponse, err error) {
- if bm.Get("out_biz_no") == gopay.NULL {
- return nil, errors.New("out_biz_no is not allowed to be null")
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.fund.trans.toaccount.transfer"); err != nil {
- return
- }
- aliRsp = new(FundTransToaccountTransferResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.fund.trans.uni.transfer(单笔转账接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_28/alipay.fund.trans.uni.transfer
- func (a *Client) FundTransUniTransfer(bm gopay.BodyMap) (aliRsp *FundTransUniTransferResponse, err error) {
- err = bm.CheckEmptyError("out_biz_no", "trans_amount", "product_code", "payee_info")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.fund.trans.uni.transfer"); err != nil {
- return nil, err
- }
- aliRsp = new(FundTransUniTransferResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.fund.trans.common.query(转账业务单据查询接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_28/alipay.fund.trans.common.query
- func (a *Client) FundTransCommonQuery(bm gopay.BodyMap) (aliRsp *FundTransCommonQueryResponse, err error) {
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.fund.trans.common.query"); err != nil {
- return nil, err
- }
- aliRsp = new(FundTransCommonQueryResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.fund.account.query(支付宝资金账户资产查询接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_28/alipay.fund.account.query
- func (a *Client) FundAccountQuery(bm gopay.BodyMap) (aliRsp *FundAccountQueryResponse, err error) {
- err = bm.CheckEmptyError("alipay_user_id")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.fund.account.query"); err != nil {
- return nil, err
- }
- aliRsp = new(FundAccountQueryResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.trade.orderinfo.sync(支付宝订单信息同步接口)
- // 文档地址:https://opendocs.alipay.com/apis/api_1/alipay.trade.orderinfo.sync
- func (a *Client) TradeOrderinfoSync(body gopay.BodyMap) {
- }
- // alipay.system.oauth.token(换取授权访问令牌)
- // 文档地址:https://opendocs.alipay.com/apis/api_9/alipay.system.oauth.token
- func (a *Client) SystemOauthToken(bm gopay.BodyMap) (aliRsp *SystemOauthTokenResponse, err error) {
- if bm.Get("code") == gopay.NULL && bm.Get("refresh_token") == gopay.NULL {
- return nil, errors.New("code and refresh_token are not allowed to be null at the same time")
- }
- err = bm.CheckEmptyError("grant_type")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = systemOauthToken(a.AppId, a.PrivateKeyType, a.PrivateKey, bm, "alipay.system.oauth.token", a.IsProd, a.SignType); err != nil {
- return nil, err
- }
- aliRsp = new(SystemOauthTokenResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.ErrorResponse != nil {
- info := aliRsp.ErrorResponse
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.user.info.share(支付宝会员授权信息查询接口)
- // body:此接口无需body参数
- // 文档地址:https://opendocs.alipay.com/apis/api_2/alipay.user.info.share
- func (a *Client) UserInfoShare() (aliRsp *UserInfoShareResponse, err error) {
- var bs []byte
- if bs, err = a.doAliPay(nil, "alipay.user.info.share"); err != nil {
- return nil, err
- }
- aliRsp = new(UserInfoShareResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.open.auth.token.app(换取应用授权令牌)
- // 文档地址:https://opendocs.alipay.com/apis/api_9/alipay.open.auth.token.app
- func (a *Client) OpenAuthTokenApp(bm gopay.BodyMap) (aliRsp *OpenAuthTokenAppResponse, err error) {
- if bm.Get("code") == gopay.NULL && bm.Get("refresh_token") == gopay.NULL {
- return nil, errors.New("code and refresh_token are not allowed to be null at the same time")
- }
- err = bm.CheckEmptyError("grant_type")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.open.auth.token.app"); err != nil {
- return nil, err
- }
- aliRsp = new(OpenAuthTokenAppResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // zhima.credit.score.get(芝麻分)
- // 文档地址:https://opendocs.alipay.com/apis/api_8/zhima.credit.score.get
- func (a *Client) ZhimaCreditScoreGet(bm gopay.BodyMap) (aliRsp *ZhimaCreditScoreGetResponse, err error) {
- if bm.Get("product_code") == gopay.NULL {
- bm.Set("product_code", "w1010100100000000001")
- }
- err = bm.CheckEmptyError("transaction_id")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "zhima.credit.score.get"); err != nil {
- return nil, err
- }
- aliRsp = new(ZhimaCreditScoreGetResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.user.certify.open.initialize(身份认证初始化服务)
- // 文档地址:https://opendocs.alipay.com/apis/api_2/alipay.user.certify.open.initialize
- func (a *Client) UserCertifyOpenInit(bm gopay.BodyMap) (aliRsp *UserCertifyOpenInitResponse, err error) {
- err = bm.CheckEmptyError("outer_order_no", "biz_code", "identity_param", "merchant_config")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.user.certify.open.initialize"); err != nil {
- return nil, err
- }
- aliRsp = new(UserCertifyOpenInitResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.user.certify.open.certify(身份认证开始认证)
- // API文档地址:https://opendocs.alipay.com/apis/api_2/alipay.user.certify.open.certify
- // 产品文档地址:https://opendocs.alipay.com/open/20181012100420932508/quickstart
- func (a *Client) UserCertifyOpenCertify(bm gopay.BodyMap) (certifyUrl string, err error) {
- err = bm.CheckEmptyError("certify_id")
- if err != nil {
- return gopay.NULL, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.user.certify.open.certify"); err != nil {
- return gopay.NULL, err
- }
- certifyUrl = string(bs)
- return certifyUrl, nil
- }
- // alipay.user.certify.open.query(身份认证记录查询)
- // 文档地址:https://opendocs.alipay.com/apis/api_2/alipay.user.certify.open.query
- func (a *Client) UserCertifyOpenQuery(bm gopay.BodyMap) (aliRsp *UserCertifyOpenQueryResponse, err error) {
- err = bm.CheckEmptyError("certify_id")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.user.certify.open.query"); err != nil {
- return nil, err
- }
- aliRsp = new(UserCertifyOpenQueryResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.user.info.auth(用户登陆授权)
- // 文档地址:https://opendocs.alipay.com/apis/api_9/alipay.user.info.auth
- func (a *Client) UserInfoAuth(bm gopay.BodyMap) (aliRsp *UserInfoAuthResponse, err error) {
- err = bm.CheckEmptyError("scopes", "state")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.user.info.auth"); err != nil {
- return nil, err
- }
- if strings.Contains(string(bs), "<head>") {
- return nil, errors.New(string(bs))
- }
- aliRsp = new(UserInfoAuthResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.data.bill.balance.query(支付宝商家账户当前余额查询)
- // 文档地址:https://opendocs.alipay.com/apis/api_15/alipay.data.bill.balance.query
- func (a *Client) DataBillBalanceQuery(bm gopay.BodyMap) (aliRsp *DataBillBalanceQueryResponse, err error) {
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.data.bill.balance.query"); err != nil {
- return nil, err
- }
- aliRsp = new(DataBillBalanceQueryResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // alipay.data.dataservice.bill.downloadurl.query(查询对账单下载地址)
- // 文档地址:https://opendocs.alipay.com/apis/api_15/alipay.data.dataservice.bill.downloadurl.query
- func (a *Client) DataBillDownloadUrlQuery(bm gopay.BodyMap) (aliRsp *DataBillDownloadUrlQueryResponse, err error) {
- err = bm.CheckEmptyError("bill_type", "bill_date")
- if err != nil {
- return nil, err
- }
- var bs []byte
- if bs, err = a.doAliPay(bm, "alipay.data.dataservice.bill.downloadurl.query"); err != nil {
- return nil, err
- }
- aliRsp = new(DataBillDownloadUrlQueryResponse)
- if err = json.Unmarshal(bs, aliRsp); err != nil {
- return nil, err
- }
- if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
- info := aliRsp.Response
- return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
- }
- aliRsp.SignData = getSignData(bs)
- return aliRsp, nil
- }
- // 向支付宝发送请求
- func (a *Client) doAliPay(bm gopay.BodyMap, method string) (bs []byte, err error) {
- var (
- bodyStr, url string
- bodyBs []byte
- )
- if bm != nil {
- if bodyBs, err = json.Marshal(bm); err != nil {
- return nil, fmt.Errorf("json.Marshal:%w", err)
- }
- bodyStr = string(bodyBs)
- }
- pubBody := make(gopay.BodyMap)
- pubBody.Set("app_id", a.AppId)
- pubBody.Set("method", method)
- pubBody.Set("format", "JSON")
- if a.AppCertSN != gopay.NULL {
- a.mu.RLock()
- pubBody.Set("app_cert_sn", a.AppCertSN)
- a.mu.RUnlock()
- }
- if a.AliPayRootCertSN != gopay.NULL {
- a.mu.RLock()
- pubBody.Set("alipay_root_cert_sn", a.AliPayRootCertSN)
- a.mu.RUnlock()
- }
- if a.ReturnUrl != gopay.NULL {
- a.mu.RLock()
- pubBody.Set("return_url", a.ReturnUrl)
- a.mu.RUnlock()
- }
- if a.Charset == gopay.NULL {
- pubBody.Set("charset", "utf-8")
- } else {
- a.mu.RLock()
- pubBody.Set("charset", a.Charset)
- a.mu.RUnlock()
- }
- if a.SignType == gopay.NULL {
- pubBody.Set("sign_type", RSA2)
- } else {
- a.mu.RLock()
- pubBody.Set("sign_type", a.SignType)
- a.mu.RUnlock()
- }
- if a.LocationName != gopay.NULL && a.location != nil {
- a.mu.RLock()
- pubBody.Set("timestamp", time.Now().In(a.location).Format(gopay.TimeLayout))
- a.mu.RUnlock()
- } else {
- pubBody.Set("timestamp", time.Now().Format(gopay.TimeLayout))
- }
- pubBody.Set("version", "1.0")
- if a.NotifyUrl != gopay.NULL {
- a.mu.RLock()
- pubBody.Set("notify_url", a.NotifyUrl)
- a.mu.RUnlock()
- }
- if a.AppAuthToken != gopay.NULL {
- a.mu.RLock()
- pubBody.Set("app_auth_token", a.AppAuthToken)
- a.mu.RUnlock()
- }
- if a.AuthToken != gopay.NULL {
- a.mu.RLock()
- pubBody.Set("auth_token", a.AuthToken)
- a.mu.RUnlock()
- }
- if bodyStr != gopay.NULL {
- pubBody.Set("biz_content", bodyStr)
- }
- sign, err := GetRsaSign(pubBody, pubBody.Get("sign_type"), a.PrivateKeyType, a.PrivateKey)
- if err != nil {
- return nil, fmt.Errorf("GetRsaSign Error: %v", err)
- }
- pubBody.Set("sign", sign)
- param := FormatURLParam(pubBody)
- switch method {
- case "alipay.trade.app.pay":
- return []byte(param), nil
- case "alipay.trade.wap.pay", "alipay.trade.page.pay", "alipay.user.certify.open.certify":
- if !a.IsProd {
- return []byte(sandboxBaseUrl + "?" + param), nil
- }
- return []byte(baseUrl + "?" + param), nil
- default:
- httpClient := gopay.NewHttpClient()
- if !a.IsProd {
- url = sandboxBaseUrlUtf8
- } else {
- url = baseUrlUtf8
- }
- res, bs, errs := httpClient.Type(gopay.TypeForm).Post(url).SendString(param).EndBytes()
- if len(errs) > 0 {
- return nil, errs[0]
- }
- if res.StatusCode != 200 {
- return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
- }
- return bs, nil
- }
- }
- func getSignData(bs []byte) (signData string) {
- str := string(bs)
- indexStart := strings.Index(str, `":`)
- indexEnd := strings.Index(str, `,"sign"`)
- signData = str[indexStart+2 : indexEnd]
- return
- }
|