|
|
@@ -1,4 +1,4 @@
|
|
|
-package gopay
|
|
|
+package alipay
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
@@ -7,11 +7,14 @@ import (
|
|
|
"strings"
|
|
|
"sync"
|
|
|
"time"
|
|
|
+
|
|
|
+ "github.com/iGoogle-ink/gopay"
|
|
|
)
|
|
|
|
|
|
-type AliPayClient struct {
|
|
|
+type Client struct {
|
|
|
AppId string
|
|
|
PrivateKey string
|
|
|
+ LocationName string
|
|
|
AppCertSN string
|
|
|
AliPayPublicCertSN string
|
|
|
AliPayRootCertSN string
|
|
|
@@ -22,6 +25,7 @@ type AliPayClient struct {
|
|
|
AppAuthToken string
|
|
|
AuthToken string
|
|
|
IsProd bool
|
|
|
+ location *time.Location
|
|
|
mu sync.RWMutex
|
|
|
}
|
|
|
|
|
|
@@ -30,8 +34,8 @@ type AliPayClient struct {
|
|
|
// appId:应用ID
|
|
|
// PrivateKey:应用私钥
|
|
|
// IsProd:是否是正式环境
|
|
|
-func NewAliPayClient(appId, privateKey string, isProd bool) (client *AliPayClient) {
|
|
|
- return &AliPayClient{
|
|
|
+func NewClient(appId, privateKey string, isProd bool) (client *Client) {
|
|
|
+ return &Client{
|
|
|
AppId: appId,
|
|
|
PrivateKey: privateKey,
|
|
|
IsProd: isProd,
|
|
|
@@ -40,21 +44,21 @@ func NewAliPayClient(appId, privateKey string, isProd bool) (client *AliPayClien
|
|
|
|
|
|
// alipay.trade.fastpay.refund.query(统一收单交易退款查询)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.fastpay.refund.query
|
|
|
-func (a *AliPayClient) AliPayTradeFastPayRefundQuery(body BodyMap) (aliRsp *AliPayTradeFastpayRefundQueryResponse, err error) {
|
|
|
+func (a *Client) TradeFastPayRefundQuery(bm gopay.BodyMap) (aliRsp *TradeFastpayRefundQueryResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("out_trade_no") == null && body.Get("trade_no") == null {
|
|
|
+ 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")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.fastpay.refund.query"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.fastpay.refund.query"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayTradeFastpayRefundQueryResponse)
|
|
|
+ aliRsp = new(TradeFastpayRefundQueryResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -64,21 +68,21 @@ func (a *AliPayClient) AliPayTradeFastPayRefundQuery(body BodyMap) (aliRsp *AliP
|
|
|
|
|
|
// alipay.trade.order.settle(统一收单交易结算接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.order.settle
|
|
|
-func (a *AliPayClient) AliPayTradeOrderSettle(body BodyMap) (aliRsp *AliPayTradeOrderSettleResponse, err error) {
|
|
|
+func (a *Client) TradeOrderSettle(bm gopay.BodyMap) (aliRsp *TradeOrderSettleResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("out_request_no") == null || body.Get("trade_no") == null {
|
|
|
+ if bm.Get("out_request_no") == gopay.NULL || bm.Get("trade_no") == gopay.NULL {
|
|
|
return nil, errors.New("out_request_no or trade_no are not allowed to be null")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.order.settle"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.order.settle"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayTradeOrderSettleResponse)
|
|
|
+ aliRsp = new(TradeOrderSettleResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -88,21 +92,21 @@ func (a *AliPayClient) AliPayTradeOrderSettle(body BodyMap) (aliRsp *AliPayTrade
|
|
|
|
|
|
// alipay.trade.create(统一收单交易创建接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.create
|
|
|
-func (a *AliPayClient) AliPayTradeCreate(body BodyMap) (aliRsp *AliPayTradeCreateResponse, err error) {
|
|
|
+func (a *Client) TradeCreate(bm gopay.BodyMap) (aliRsp *TradeCreateResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("out_trade_no") == null && body.Get("buyer_id") == null {
|
|
|
+ if bm.Get("out_trade_no") == gopay.NULL && bm.Get("buyer_id") == gopay.NULL {
|
|
|
return nil, errors.New("out_trade_no and buyer_id are not allowed to be null at the same time")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.create"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.create"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayTradeCreateResponse)
|
|
|
+ aliRsp = new(TradeCreateResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -112,21 +116,21 @@ func (a *AliPayClient) AliPayTradeCreate(body BodyMap) (aliRsp *AliPayTradeCreat
|
|
|
|
|
|
// alipay.trade.close(统一收单交易关闭接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.close
|
|
|
-func (a *AliPayClient) AliPayTradeClose(body BodyMap) (aliRsp *AliPayTradeCloseResponse, err error) {
|
|
|
+func (a *Client) TradeClose(bm gopay.BodyMap) (aliRsp *TradeCloseResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("out_trade_no") == null && body.Get("trade_no") == null {
|
|
|
+ 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")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.close"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.close"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayTradeCloseResponse)
|
|
|
+ aliRsp = new(TradeCloseResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -136,21 +140,21 @@ func (a *AliPayClient) AliPayTradeClose(body BodyMap) (aliRsp *AliPayTradeCloseR
|
|
|
|
|
|
// alipay.trade.cancel(统一收单交易撤销接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.cancel
|
|
|
-func (a *AliPayClient) AliPayTradeCancel(body BodyMap) (aliRsp *AliPayTradeCancelResponse, err error) {
|
|
|
+func (a *Client) TradeCancel(bm gopay.BodyMap) (aliRsp *TradeCancelResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("out_trade_no") == null && body.Get("trade_no") == null {
|
|
|
+ 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")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.cancel"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.cancel"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayTradeCancelResponse)
|
|
|
+ aliRsp = new(TradeCancelResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -160,21 +164,21 @@ func (a *AliPayClient) AliPayTradeCancel(body BodyMap) (aliRsp *AliPayTradeCance
|
|
|
|
|
|
// alipay.trade.refund(统一收单交易退款接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.refund
|
|
|
-func (a *AliPayClient) AliPayTradeRefund(body BodyMap) (aliRsp *AliPayTradeRefundResponse, err error) {
|
|
|
+func (a *Client) TradeRefund(bm gopay.BodyMap) (aliRsp *TradeRefundResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("out_trade_no") == null && body.Get("trade_no") == null {
|
|
|
+ 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")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.refund"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.refund"); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- aliRsp = new(AliPayTradeRefundResponse)
|
|
|
+ aliRsp = new(TradeRefundResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -182,23 +186,23 @@ func (a *AliPayClient) AliPayTradeRefund(body BodyMap) (aliRsp *AliPayTradeRefun
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// alipay.trade.refund(统一收单退款页面接口)
|
|
|
+// alipay.trade.page.refund(统一收单退款页面接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.page.refund
|
|
|
-func (a *AliPayClient) AliPayTradePageRefund(body BodyMap) (aliRsp *AliPayTradePageRefundResponse, err error) {
|
|
|
+func (a *Client) TradePageRefund(bm gopay.BodyMap) (aliRsp *TradePageRefundResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("out_trade_no") == null && body.Get("trade_no") == null {
|
|
|
+ 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")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.page.refund"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.page.refund"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayTradePageRefundResponse)
|
|
|
+ aliRsp = new(TradePageRefundResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -208,41 +212,45 @@ func (a *AliPayClient) AliPayTradePageRefund(body BodyMap) (aliRsp *AliPayTradeP
|
|
|
|
|
|
// alipay.trade.precreate(统一收单线下交易预创建)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.precreate
|
|
|
-func (a *AliPayClient) AliPayTradePrecreate(body BodyMap) (aliRsp *AliPayTradePrecreateResponse, err error) {
|
|
|
+func (a *Client) TradePrecreate(bm gopay.BodyMap) (aliRsp *TradePrecreateResponse, err error) {
|
|
|
var bs []byte
|
|
|
- if body.Get("out_trade_no") == null {
|
|
|
+ if bm.Get("out_trade_no") == gopay.NULL {
|
|
|
return nil, errors.New("out_trade_no is not allowed to be null")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.precreate"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.precreate"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayTradePrecreateResponse)
|
|
|
+ aliRsp = new(TradePrecreateResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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
|
|
|
}
|
|
|
|
|
|
// alipay.trade.pay(统一收单交易支付接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.pay
|
|
|
-func (a *AliPayClient) AliPayTradePay(body BodyMap) (aliRsp *AliPayTradePayResponse, err error) {
|
|
|
+func (a *Client) TradePay(bm gopay.BodyMap) (aliRsp *TradePayResponse, err error) {
|
|
|
var bs []byte
|
|
|
- if body.Get("out_trade_no") == null {
|
|
|
+ if bm.Get("out_trade_no") == gopay.NULL {
|
|
|
return nil, errors.New("out_trade_no is not allowed to be null")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.pay"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.pay"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayTradePayResponse)
|
|
|
+ aliRsp = new(TradePayResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -252,21 +260,21 @@ func (a *AliPayClient) AliPayTradePay(body BodyMap) (aliRsp *AliPayTradePayRespo
|
|
|
|
|
|
// alipay.trade.query(统一收单线下交易查询)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.query
|
|
|
-func (a *AliPayClient) AliPayTradeQuery(body BodyMap) (aliRsp *AliPayTradeQueryResponse, err error) {
|
|
|
+func (a *Client) TradeQuery(bm gopay.BodyMap) (aliRsp *TradeQueryResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("out_trade_no") == null && body.Get("trade_no") == null {
|
|
|
+ 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")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.query"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.query"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayTradeQueryResponse)
|
|
|
+ aliRsp = new(TradeQueryResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -276,13 +284,13 @@ func (a *AliPayClient) AliPayTradeQuery(body BodyMap) (aliRsp *AliPayTradeQueryR
|
|
|
|
|
|
// alipay.trade.app.pay(app支付接口2.0)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.app.pay
|
|
|
-func (a *AliPayClient) AliPayTradeAppPay(body BodyMap) (payParam string, err error) {
|
|
|
+func (a *Client) TradeAppPay(bm gopay.BodyMap) (payParam string, err error) {
|
|
|
var bs []byte
|
|
|
- if body.Get("out_trade_no") == null {
|
|
|
- return null, errors.New("out_trade_no is not allowed to be null")
|
|
|
+ if bm.Get("out_trade_no") == gopay.NULL {
|
|
|
+ return gopay.NULL, errors.New("out_trade_no is not allowed to be null")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.app.pay"); err != nil {
|
|
|
- return null, err
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.app.pay"); err != nil {
|
|
|
+ return gopay.NULL, err
|
|
|
}
|
|
|
payParam = string(bs)
|
|
|
return
|
|
|
@@ -290,14 +298,14 @@ func (a *AliPayClient) AliPayTradeAppPay(body BodyMap) (payParam string, err err
|
|
|
|
|
|
// alipay.trade.wap.pay(手机网站支付接口2.0)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.wap.pay
|
|
|
-func (a *AliPayClient) AliPayTradeWapPay(body BodyMap) (payUrl string, err error) {
|
|
|
+func (a *Client) TradeWapPay(bm gopay.BodyMap) (payUrl string, err error) {
|
|
|
var bs []byte
|
|
|
- if body.Get("out_trade_no") == null {
|
|
|
- return null, errors.New("out_trade_no is not allowed to be null")
|
|
|
+ if bm.Get("out_trade_no") == gopay.NULL {
|
|
|
+ return gopay.NULL, errors.New("out_trade_no is not allowed to be null")
|
|
|
}
|
|
|
- body.Set("product_code", "QUICK_WAP_WAY")
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.wap.pay"); err != nil {
|
|
|
- return null, err
|
|
|
+ bm.Set("product_code", "QUICK_WAP_WAY")
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.wap.pay"); err != nil {
|
|
|
+ return gopay.NULL, err
|
|
|
}
|
|
|
payUrl = string(bs)
|
|
|
return
|
|
|
@@ -305,14 +313,14 @@ func (a *AliPayClient) AliPayTradeWapPay(body BodyMap) (payUrl string, err error
|
|
|
|
|
|
// alipay.trade.page.pay(统一收单下单并支付页面接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.page.pay
|
|
|
-func (a *AliPayClient) AliPayTradePagePay(body BodyMap) (payUrl string, err error) {
|
|
|
+func (a *Client) TradePagePay(bm gopay.BodyMap) (payUrl string, err error) {
|
|
|
var bs []byte
|
|
|
- if body.Get("out_trade_no") == null {
|
|
|
- return null, errors.New("out_trade_no is not allowed to be null")
|
|
|
+ if bm.Get("out_trade_no") == gopay.NULL {
|
|
|
+ return gopay.NULL, errors.New("out_trade_no is not allowed to be null")
|
|
|
}
|
|
|
- body.Set("product_code", "FAST_INSTANT_TRADE_PAY")
|
|
|
- if bs, err = a.doAliPay(body, "alipay.trade.page.pay"); err != nil {
|
|
|
- return null, err
|
|
|
+ bm.Set("product_code", "FAST_INSTANT_TRADE_PAY")
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.trade.page.pay"); err != nil {
|
|
|
+ return gopay.NULL, err
|
|
|
}
|
|
|
payUrl = string(bs)
|
|
|
return
|
|
|
@@ -320,19 +328,19 @@ func (a *AliPayClient) AliPayTradePagePay(body BodyMap) (payUrl string, err erro
|
|
|
|
|
|
// alipay.fund.trans.toaccount.transfer(单笔转账到支付宝账户接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer
|
|
|
-func (a *AliPayClient) AliPayFundTransToaccountTransfer(body BodyMap) (aliRsp *AliPayFundTransToaccountTransferResponse, err error) {
|
|
|
+func (a *Client) FundTransToaccountTransfer(bm gopay.BodyMap) (aliRsp *FundTransToaccountTransferResponse, err error) {
|
|
|
var bs []byte
|
|
|
- if body.Get("out_biz_no") == null {
|
|
|
+ if bm.Get("out_biz_no") == gopay.NULL {
|
|
|
return nil, errors.New("out_biz_no is not allowed to be null")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.fund.trans.toaccount.transfer"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.fund.trans.toaccount.transfer"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayFundTransToaccountTransferResponse)
|
|
|
+ aliRsp = new(FundTransToaccountTransferResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -342,28 +350,28 @@ func (a *AliPayClient) AliPayFundTransToaccountTransfer(body BodyMap) (aliRsp *A
|
|
|
|
|
|
// alipay.trade.orderinfo.sync(支付宝订单信息同步接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.orderinfo.sync
|
|
|
-func (a *AliPayClient) AliPayTradeOrderinfoSync(body BodyMap) {
|
|
|
+func (a *Client) TradeOrderinfoSync(body gopay.BodyMap) {
|
|
|
|
|
|
}
|
|
|
|
|
|
// alipay.system.oauth.token(换取授权访问令牌)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_9/alipay.system.oauth.token
|
|
|
-func (a *AliPayClient) AliPaySystemOauthToken(body BodyMap) (aliRsp *AliPaySystemOauthTokenResponse, err error) {
|
|
|
+func (a *Client) SystemOauthToken(bm gopay.BodyMap) (aliRsp *SystemOauthTokenResponse, err error) {
|
|
|
var bs []byte
|
|
|
- if body.Get("grant_type") == null {
|
|
|
+ if bm.Get("grant_type") == gopay.NULL {
|
|
|
return nil, errors.New("grant_type is not allowed to be null")
|
|
|
}
|
|
|
- if body.Get("code") == null && body.Get("refresh_token") == null {
|
|
|
+ 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")
|
|
|
}
|
|
|
- if bs, err = aliPaySystemOauthToken(a.AppId, a.PrivateKey, body, "alipay.system.oauth.token", a.IsProd); err != nil {
|
|
|
+ if bs, err = systemOauthToken(a.AppId, a.PrivateKey, bm, "alipay.system.oauth.token", a.IsProd); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPaySystemOauthTokenResponse)
|
|
|
+ aliRsp = new(SystemOauthTokenResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.AccessToken == null {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -374,16 +382,16 @@ func (a *AliPayClient) AliPaySystemOauthToken(body BodyMap) (aliRsp *AliPaySyste
|
|
|
// alipay.user.info.share(支付宝会员授权信息查询接口)
|
|
|
// body:此接口无需body参数
|
|
|
// 文档地址:https://docs.open.alipay.com/api_2/alipay.user.info.share
|
|
|
-func (a *AliPayClient) AliPayUserInfoShare() (aliRsp *AliPayUserInfoShareResponse, err error) {
|
|
|
+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(AliPayUserInfoShareResponse)
|
|
|
+ aliRsp = new(UserInfoShareResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -393,22 +401,22 @@ func (a *AliPayClient) AliPayUserInfoShare() (aliRsp *AliPayUserInfoShareRespons
|
|
|
|
|
|
// alipay.open.auth.token.app(换取应用授权令牌)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_9/alipay.open.auth.token.app
|
|
|
-func (a *AliPayClient) AliPayOpenAuthTokenApp(body BodyMap) (aliRsp *AliPayOpenAuthTokenAppResponse, err error) {
|
|
|
+func (a *Client) OpenAuthTokenApp(bm gopay.BodyMap) (aliRsp *OpenAuthTokenAppResponse, err error) {
|
|
|
var bs []byte
|
|
|
- if body.Get("grant_type") == null {
|
|
|
+ if bm.Get("grant_type") == gopay.NULL {
|
|
|
return nil, errors.New("grant_type is not allowed to be null")
|
|
|
}
|
|
|
- if body.Get("code") == null && body.Get("refresh_token") == null {
|
|
|
+ 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")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.open.auth.token.app"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.open.auth.token.app"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayOpenAuthTokenAppResponse)
|
|
|
+ aliRsp = new(OpenAuthTokenAppResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -418,24 +426,24 @@ func (a *AliPayClient) AliPayOpenAuthTokenApp(body BodyMap) (aliRsp *AliPayOpenA
|
|
|
|
|
|
// zhima.credit.score.get(芝麻分)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_8/zhima.credit.score.get
|
|
|
-func (a *AliPayClient) ZhimaCreditScoreGet(body BodyMap) (aliRsp *ZhimaCreditScoreGetResponse, err error) {
|
|
|
+func (a *Client) ZhimaCreditScoreGet(bm gopay.BodyMap) (aliRsp *ZhimaCreditScoreGetResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("product_code") == null {
|
|
|
- body.Set("product_code", "w1010100100000000001")
|
|
|
+ if bm.Get("product_code") == gopay.NULL {
|
|
|
+ bm.Set("product_code", "w1010100100000000001")
|
|
|
}
|
|
|
- if body.Get("transaction_id") == null {
|
|
|
+ if bm.Get("transaction_id") == gopay.NULL {
|
|
|
return nil, errors.New("transaction_id is not allowed to be null")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "zhima.credit.score.get"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "zhima.credit.score.get"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
aliRsp = new(ZhimaCreditScoreGetResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -445,30 +453,30 @@ func (a *AliPayClient) ZhimaCreditScoreGet(body BodyMap) (aliRsp *ZhimaCreditSco
|
|
|
|
|
|
// alipay.user.certify.open.initialize(身份认证初始化服务)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_2/alipay.user.certify.open.initialize
|
|
|
-func (a *AliPayClient) AliPayUserCertifyOpenInit(body BodyMap) (aliRsp *AliPayUserCertifyOpenInitResponse, err error) {
|
|
|
+func (a *Client) UserCertifyOpenInit(bm gopay.BodyMap) (aliRsp *UserCertifyOpenInitResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("biz_code") == null {
|
|
|
+ if bm.Get("biz_code") == gopay.NULL {
|
|
|
return nil, errors.New("biz_code is not allowed to be null")
|
|
|
}
|
|
|
- if body.Get("outer_order_no") == null {
|
|
|
+ if bm.Get("outer_order_no") == gopay.NULL {
|
|
|
return nil, errors.New("outer_order_no is not allowed to be null")
|
|
|
}
|
|
|
- if body.Get("identity_param") == null {
|
|
|
+ if bm.Get("identity_param") == gopay.NULL {
|
|
|
return nil, errors.New("identity_param is not allowed to be null")
|
|
|
}
|
|
|
- if body.Get("merchant_config") == null {
|
|
|
+ if bm.Get("merchant_config") == gopay.NULL {
|
|
|
return nil, errors.New("merchant_config is not allowed to be null")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.user.certify.open.initialize"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.user.certify.open.initialize"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayUserCertifyOpenInitResponse)
|
|
|
+ aliRsp = new(UserCertifyOpenInitResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -479,15 +487,15 @@ func (a *AliPayClient) AliPayUserCertifyOpenInit(body BodyMap) (aliRsp *AliPayUs
|
|
|
// alipay.user.certify.open.certify(身份认证开始认证)
|
|
|
// API文档地址:https://docs.open.alipay.com/api_2/alipay.user.certify.open.certify
|
|
|
// 产品文档地址:https://docs.open.alipay.com/20181012100420932508/quickstart
|
|
|
-func (a *AliPayClient) AliPayUserCertifyOpenCertify(body BodyMap) (certifyUrl string, err error) {
|
|
|
+func (a *Client) UserCertifyOpenCertify(bm gopay.BodyMap) (certifyUrl string, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("certify_id") == null {
|
|
|
- return null, errors.New("certify_id is not allowed to be null")
|
|
|
+ if bm.Get("certify_id") == gopay.NULL {
|
|
|
+ return gopay.NULL, errors.New("certify_id is not allowed to be null")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.user.certify.open.certify"); err != nil {
|
|
|
- return null, err
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.user.certify.open.certify"); err != nil {
|
|
|
+ return gopay.NULL, err
|
|
|
}
|
|
|
certifyUrl = string(bs)
|
|
|
return
|
|
|
@@ -495,21 +503,21 @@ func (a *AliPayClient) AliPayUserCertifyOpenCertify(body BodyMap) (certifyUrl st
|
|
|
|
|
|
// alipay.user.certify.open.query(身份认证记录查询)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_2/alipay.user.certify.open.query
|
|
|
-func (a *AliPayClient) AliPayUserCertifyOpenQuery(body BodyMap) (aliRsp *AliPayUserCertifyOpenQueryResponse, err error) {
|
|
|
+func (a *Client) UserCertifyOpenQuery(bm gopay.BodyMap) (aliRsp *UserCertifyOpenQueryResponse, err error) {
|
|
|
var (
|
|
|
bs []byte
|
|
|
)
|
|
|
- if body.Get("certify_id") == null {
|
|
|
+ if bm.Get("certify_id") == gopay.NULL {
|
|
|
return nil, errors.New("certify_id is not allowed to be null")
|
|
|
}
|
|
|
- if bs, err = a.doAliPay(body, "alipay.user.certify.open.query"); err != nil {
|
|
|
+ if bs, err = a.doAliPay(bm, "alipay.user.certify.open.query"); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- aliRsp = new(AliPayUserCertifyOpenQueryResponse)
|
|
|
+ aliRsp = new(UserCertifyOpenQueryResponse)
|
|
|
if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- if aliRsp.Response.Code != "10000" {
|
|
|
+ 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)
|
|
|
}
|
|
|
@@ -518,75 +526,82 @@ func (a *AliPayClient) AliPayUserCertifyOpenQuery(body BodyMap) (aliRsp *AliPayU
|
|
|
}
|
|
|
|
|
|
// 向支付宝发送请求
|
|
|
-func (a *AliPayClient) doAliPay(body BodyMap, method string) (bs []byte, err error) {
|
|
|
+func (a *Client) doAliPay(bm gopay.BodyMap, method string) (bs []byte, err error) {
|
|
|
var (
|
|
|
- bodyStr, sign, url string
|
|
|
- bodyBs []byte
|
|
|
+ bodyStr, url string
|
|
|
+ bodyBs []byte
|
|
|
)
|
|
|
- if body != nil {
|
|
|
- if bodyBs, err = json.Marshal(body); err != nil {
|
|
|
+ if bm != nil {
|
|
|
+ if bodyBs, err = json.Marshal(bm); err != nil {
|
|
|
return nil, fmt.Errorf("json.Marshal:%s", err.Error())
|
|
|
}
|
|
|
bodyStr = string(bodyBs)
|
|
|
}
|
|
|
- pubBody := make(BodyMap)
|
|
|
+ pubBody := make(gopay.BodyMap)
|
|
|
pubBody.Set("app_id", a.AppId)
|
|
|
pubBody.Set("method", method)
|
|
|
pubBody.Set("format", "JSON")
|
|
|
- if a.AppCertSN != null {
|
|
|
+ if a.AppCertSN != gopay.NULL {
|
|
|
a.mu.RLock()
|
|
|
pubBody.Set("app_cert_sn", a.AppCertSN)
|
|
|
a.mu.RUnlock()
|
|
|
}
|
|
|
- if a.AliPayRootCertSN != null {
|
|
|
+ if a.AliPayRootCertSN != gopay.NULL {
|
|
|
a.mu.RLock()
|
|
|
pubBody.Set("alipay_root_cert_sn", a.AliPayRootCertSN)
|
|
|
a.mu.RUnlock()
|
|
|
}
|
|
|
- if a.ReturnUrl != null {
|
|
|
+ if a.ReturnUrl != gopay.NULL {
|
|
|
a.mu.RLock()
|
|
|
pubBody.Set("return_url", a.ReturnUrl)
|
|
|
a.mu.RUnlock()
|
|
|
}
|
|
|
- if a.Charset == null {
|
|
|
+ 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 == null {
|
|
|
+ if a.SignType == gopay.NULL {
|
|
|
pubBody.Set("sign_type", "RSA2")
|
|
|
} else {
|
|
|
a.mu.RLock()
|
|
|
pubBody.Set("sign_type", a.SignType)
|
|
|
a.mu.RUnlock()
|
|
|
}
|
|
|
- pubBody.Set("timestamp", time.Now().Format(TimeLayout))
|
|
|
+ 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 != null {
|
|
|
+ if a.NotifyUrl != gopay.NULL {
|
|
|
a.mu.RLock()
|
|
|
pubBody.Set("notify_url", a.NotifyUrl)
|
|
|
a.mu.RUnlock()
|
|
|
}
|
|
|
- if a.AppAuthToken != null {
|
|
|
+ if a.AppAuthToken != gopay.NULL {
|
|
|
a.mu.RLock()
|
|
|
pubBody.Set("app_auth_token", a.AppAuthToken)
|
|
|
a.mu.RUnlock()
|
|
|
}
|
|
|
- if a.AuthToken != null {
|
|
|
+ if a.AuthToken != gopay.NULL {
|
|
|
a.mu.RLock()
|
|
|
pubBody.Set("auth_token", a.AuthToken)
|
|
|
a.mu.RUnlock()
|
|
|
}
|
|
|
- if bodyStr != null {
|
|
|
+ if bodyStr != gopay.NULL {
|
|
|
pubBody.Set("biz_content", bodyStr)
|
|
|
}
|
|
|
- if sign, err = getRsaSign(pubBody, pubBody.Get("sign_type"), FormatPrivateKey(a.PrivateKey)); err != nil {
|
|
|
+ sign, err := getRsaSign(pubBody, pubBody.Get("sign_type"), FormatPrivateKey(a.PrivateKey))
|
|
|
+ if err != nil {
|
|
|
return
|
|
|
}
|
|
|
pubBody.Set("sign", sign)
|
|
|
- param := FormatAliPayURLParam(pubBody)
|
|
|
+ param := FormatURLParam(pubBody)
|
|
|
|
|
|
switch method {
|
|
|
case "alipay.trade.app.pay":
|
|
|
@@ -597,13 +612,13 @@ func (a *AliPayClient) doAliPay(body BodyMap, method string) (bs []byte, err err
|
|
|
}
|
|
|
return []byte(zfbBaseUrl + "?" + param), nil
|
|
|
default:
|
|
|
- httpClient := NewHttpClient()
|
|
|
+ httpClient := gopay.NewHttpClient()
|
|
|
if !a.IsProd {
|
|
|
url = zfbSandboxBaseUrlUtf8
|
|
|
} else {
|
|
|
url = zfbBaseUrlUtf8
|
|
|
}
|
|
|
- res, bs, errs := httpClient.Type(TypeForm).Post(url).SendString(param).EndBytes()
|
|
|
+ res, bs, errs := httpClient.Type(gopay.TypeForm).Post(url).SendString(param).EndBytes()
|
|
|
if len(errs) > 0 {
|
|
|
return nil, errs[0]
|
|
|
}
|