|
@@ -4,14 +4,15 @@ import (
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
"errors"
|
|
"errors"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
- "log"
|
|
|
|
|
"strings"
|
|
"strings"
|
|
|
"time"
|
|
"time"
|
|
|
|
|
+
|
|
|
|
|
+ "github.com/parnurzeal/gorequest"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type AliPayClient struct {
|
|
type AliPayClient struct {
|
|
|
AppId string
|
|
AppId string
|
|
|
- privateKey string
|
|
|
|
|
|
|
+ PrivateKey string
|
|
|
AlipayRootCertSN string
|
|
AlipayRootCertSN string
|
|
|
AppCertSN string
|
|
AppCertSN string
|
|
|
ReturnUrl string
|
|
ReturnUrl string
|
|
@@ -20,384 +21,363 @@ type AliPayClient struct {
|
|
|
SignType string
|
|
SignType string
|
|
|
AppAuthToken string
|
|
AppAuthToken string
|
|
|
AuthToken string
|
|
AuthToken string
|
|
|
- isProd bool
|
|
|
|
|
|
|
+ IsProd bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//初始化支付宝客户端
|
|
//初始化支付宝客户端
|
|
|
// 注意:如果使用支付宝公钥证书验签,请设置 支付宝根证书SN(client.SetAlipayRootCertSN())、应用公钥证书SN(client.SetAppCertSN())
|
|
// 注意:如果使用支付宝公钥证书验签,请设置 支付宝根证书SN(client.SetAlipayRootCertSN())、应用公钥证书SN(client.SetAppCertSN())
|
|
|
// appId:应用ID
|
|
// appId:应用ID
|
|
|
-// privateKey:应用私钥
|
|
|
|
|
-// isProd:是否是正式环境
|
|
|
|
|
|
|
+// PrivateKey:应用私钥
|
|
|
|
|
+// IsProd:是否是正式环境
|
|
|
func NewAliPayClient(appId, privateKey string, isProd bool) (client *AliPayClient) {
|
|
func NewAliPayClient(appId, privateKey string, isProd bool) (client *AliPayClient) {
|
|
|
- client = new(AliPayClient)
|
|
|
|
|
- client.AppId = appId
|
|
|
|
|
- client.privateKey = privateKey
|
|
|
|
|
- client.isProd = isProd
|
|
|
|
|
- return client
|
|
|
|
|
|
|
+ return &AliPayClient{
|
|
|
|
|
+ AppId: appId,
|
|
|
|
|
+ PrivateKey: privateKey,
|
|
|
|
|
+ IsProd: isProd,
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.fastpay.refund.query(统一收单交易退款查询)
|
|
//alipay.trade.fastpay.refund.query(统一收单交易退款查询)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.fastpay.refund.query
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.fastpay.refund.query
|
|
|
-func (this *AliPayClient) AliPayTradeFastPayRefundQuery(body BodyMap) (aliRsp *AliPayTradeFastpayRefundQueryResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
- trade1 := body.Get("out_trade_no")
|
|
|
|
|
- trade2 := body.Get("trade_no")
|
|
|
|
|
- if trade1 == null && trade2 == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradeFastPayRefundQuery(body BodyMap) (aliRsp *AliPayTradeFastpayRefundQueryResponse, err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ p1, p2 string
|
|
|
|
|
+ bs []byte
|
|
|
|
|
+ )
|
|
|
|
|
+ p1 = body.Get("out_trade_no")
|
|
|
|
|
+ p2 = body.Get("trade_no")
|
|
|
|
|
+ if p1 == null && p2 == null {
|
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.fastpay.refund.query")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.fastpay.refund.query"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AliPayTradeFastPayRefundQuery::::", string(bytes))
|
|
|
|
|
aliRsp = new(AliPayTradeFastpayRefundQueryResponse)
|
|
aliRsp = new(AliPayTradeFastpayRefundQueryResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, fmt.Errorf("json.Unmarshal:%v", err.Error())
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AliPayTradeFastpayRefundQueryResponse.Code != "10000" {
|
|
if aliRsp.AliPayTradeFastpayRefundQueryResponse.Code != "10000" {
|
|
|
info := aliRsp.AliPayTradeFastpayRefundQueryResponse
|
|
info := aliRsp.AliPayTradeFastpayRefundQueryResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.order.settle(统一收单交易结算接口)
|
|
//alipay.trade.order.settle(统一收单交易结算接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.order.settle
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.order.settle
|
|
|
-func (this *AliPayClient) AliPayTradeOrderSettle(body BodyMap) (aliRsp *AliPayTradeOrderSettleResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
- trade1 := body.Get("out_request_no")
|
|
|
|
|
- trade2 := body.Get("trade_no")
|
|
|
|
|
- if trade1 == null || trade2 == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradeOrderSettle(body BodyMap) (aliRsp *AliPayTradeOrderSettleResponse, err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ p1, p2 string
|
|
|
|
|
+ bs []byte
|
|
|
|
|
+ )
|
|
|
|
|
+ p1 = body.Get("out_request_no")
|
|
|
|
|
+ p2 = body.Get("trade_no")
|
|
|
|
|
+ if p1 == null || p2 == null {
|
|
|
return nil, errors.New("out_request_no or trade_no are not allowed to be null")
|
|
return nil, errors.New("out_request_no or trade_no are not allowed to be null")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.order.settle")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.order.settle"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AliPayTradeOrderSettle::::", string(bytes))
|
|
|
|
|
aliRsp = new(AliPayTradeOrderSettleResponse)
|
|
aliRsp = new(AliPayTradeOrderSettleResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AliPayTradeOrderSettleResponse.Code != "10000" {
|
|
if aliRsp.AliPayTradeOrderSettleResponse.Code != "10000" {
|
|
|
info := aliRsp.AliPayTradeOrderSettleResponse
|
|
info := aliRsp.AliPayTradeOrderSettleResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.create(统一收单交易创建接口)
|
|
//alipay.trade.create(统一收单交易创建接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.create
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.create
|
|
|
-func (this *AliPayClient) AliPayTradeCreate(body BodyMap) (aliRsp *AliPayTradeCreateResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
- trade1 := body.Get("out_trade_no")
|
|
|
|
|
- trade2 := body.Get("buyer_id")
|
|
|
|
|
- if trade1 == null && trade2 == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradeCreate(body BodyMap) (aliRsp *AliPayTradeCreateResponse, err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ p1, p2 string
|
|
|
|
|
+ bs []byte
|
|
|
|
|
+ )
|
|
|
|
|
+ p1 = body.Get("out_trade_no")
|
|
|
|
|
+ p2 = body.Get("buyer_id")
|
|
|
|
|
+ if p1 == null && p2 == null {
|
|
|
return nil, errors.New("out_trade_no and buyer_id are not allowed to be null at the same time")
|
|
return nil, errors.New("out_trade_no and buyer_id are not allowed to be null at the same time")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.create")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.create"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AliPayTradeCreateResponse::::", string(bytes))
|
|
|
|
|
aliRsp = new(AliPayTradeCreateResponse)
|
|
aliRsp = new(AliPayTradeCreateResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AliPayTradeCreateResponse.Code != "10000" {
|
|
if aliRsp.AliPayTradeCreateResponse.Code != "10000" {
|
|
|
info := aliRsp.AliPayTradeCreateResponse
|
|
info := aliRsp.AliPayTradeCreateResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.close(统一收单交易关闭接口)
|
|
//alipay.trade.close(统一收单交易关闭接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.close
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.close
|
|
|
-func (this *AliPayClient) AliPayTradeClose(body BodyMap) (aliRsp *AliPayTradeCloseResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
- trade1 := body.Get("out_trade_no")
|
|
|
|
|
- trade2 := body.Get("trade_no")
|
|
|
|
|
- if trade1 == null && trade2 == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradeClose(body BodyMap) (aliRsp *AliPayTradeCloseResponse, err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ p1, p2 string
|
|
|
|
|
+ bs []byte
|
|
|
|
|
+ )
|
|
|
|
|
+ p1 = body.Get("out_trade_no")
|
|
|
|
|
+ p2 = body.Get("trade_no")
|
|
|
|
|
+ if p1 == null && p2 == null {
|
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.close")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.close"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AliPayTradeCloseResponse::::", string(bytes))
|
|
|
|
|
aliRsp = new(AliPayTradeCloseResponse)
|
|
aliRsp = new(AliPayTradeCloseResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AliPayTradeCloseResponse.Code != "10000" {
|
|
if aliRsp.AliPayTradeCloseResponse.Code != "10000" {
|
|
|
info := aliRsp.AliPayTradeCloseResponse
|
|
info := aliRsp.AliPayTradeCloseResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.cancel(统一收单交易撤销接口)
|
|
//alipay.trade.cancel(统一收单交易撤销接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.cancel
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.cancel
|
|
|
-func (this *AliPayClient) AliPayTradeCancel(body BodyMap) (aliRsp *AliPayTradeCancelResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
- trade1 := body.Get("out_trade_no")
|
|
|
|
|
- trade2 := body.Get("trade_no")
|
|
|
|
|
- if trade1 == null && trade2 == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradeCancel(body BodyMap) (aliRsp *AliPayTradeCancelResponse, err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ p1, p2 string
|
|
|
|
|
+ bs []byte
|
|
|
|
|
+ )
|
|
|
|
|
+ p1 = body.Get("out_trade_no")
|
|
|
|
|
+ p2 = body.Get("trade_no")
|
|
|
|
|
+ if p1 == null && p2 == null {
|
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.cancel")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.cancel"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AliPayTradeCancel::::", string(bytes))
|
|
|
|
|
aliRsp = new(AliPayTradeCancelResponse)
|
|
aliRsp = new(AliPayTradeCancelResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AliPayTradeCancelResponse.Code != "10000" {
|
|
if aliRsp.AliPayTradeCancelResponse.Code != "10000" {
|
|
|
info := aliRsp.AliPayTradeCancelResponse
|
|
info := aliRsp.AliPayTradeCancelResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.refund(统一收单交易退款接口)
|
|
//alipay.trade.refund(统一收单交易退款接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.refund
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.refund
|
|
|
-func (this *AliPayClient) AliPayTradeRefund(body BodyMap) (aliRsp *AliPayTradeRefundResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
- trade1 := body.Get("out_trade_no")
|
|
|
|
|
- trade2 := body.Get("trade_no")
|
|
|
|
|
- if trade1 == null && trade2 == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradeRefund(body BodyMap) (aliRsp *AliPayTradeRefundResponse, err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ p1, p2 string
|
|
|
|
|
+ bs []byte
|
|
|
|
|
+ )
|
|
|
|
|
+ p1 = body.Get("out_trade_no")
|
|
|
|
|
+ p2 = body.Get("trade_no")
|
|
|
|
|
+ if p1 == null && p2 == null {
|
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.refund")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.refund"); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
- //log.Println("AliPayTradeRefundResponse::::", string(bytes))
|
|
|
|
|
aliRsp = new(AliPayTradeRefundResponse)
|
|
aliRsp = new(AliPayTradeRefundResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AlipayTradeRefundResponse.Code != "10000" {
|
|
if aliRsp.AlipayTradeRefundResponse.Code != "10000" {
|
|
|
info := aliRsp.AlipayTradeRefundResponse
|
|
info := aliRsp.AlipayTradeRefundResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.refund(统一收单退款页面接口)
|
|
//alipay.trade.refund(统一收单退款页面接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.page.refund
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.page.refund
|
|
|
-func (this *AliPayClient) AliPayTradePageRefund(body BodyMap) (aliRsp *AliPayTradePageRefundResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
- trade1 := body.Get("out_trade_no")
|
|
|
|
|
- trade2 := body.Get("trade_no")
|
|
|
|
|
- if trade1 == null && trade2 == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradePageRefund(body BodyMap) (aliRsp *AliPayTradePageRefundResponse, err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ p1, p2 string
|
|
|
|
|
+ bs []byte
|
|
|
|
|
+ )
|
|
|
|
|
+ p1 = body.Get("out_trade_no")
|
|
|
|
|
+ p2 = body.Get("trade_no")
|
|
|
|
|
+ if p1 == null && p2 == null {
|
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, " alipay.trade.page.refund")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, " alipay.trade.page.refund"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AliPayTradePageRefundResponse::::", string(bytes))
|
|
|
|
|
aliRsp = new(AliPayTradePageRefundResponse)
|
|
aliRsp = new(AliPayTradePageRefundResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AliPayTradePageRefundResponse.Code != "10000" {
|
|
if aliRsp.AliPayTradePageRefundResponse.Code != "10000" {
|
|
|
info := aliRsp.AliPayTradePageRefundResponse
|
|
info := aliRsp.AliPayTradePageRefundResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.precreate(统一收单线下交易预创建)
|
|
//alipay.trade.precreate(统一收单线下交易预创建)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.precreate
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.precreate
|
|
|
-func (this *AliPayClient) AliPayTradePrecreate(body BodyMap) (aliRsp *AlipayTradePrecreateResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
- trade1 := body.Get("out_trade_no")
|
|
|
|
|
- if trade1 == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradePrecreate(body BodyMap) (aliRsp *AlipayTradePrecreateResponse, err error) {
|
|
|
|
|
+ var bs []byte
|
|
|
|
|
+ p := body.Get("out_trade_no")
|
|
|
|
|
+ if p == null {
|
|
|
return nil, errors.New("out_trade_no is not allowed to be null")
|
|
return nil, errors.New("out_trade_no is not allowed to be null")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.precreate")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.precreate"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AlipayTradePrecreateResponse::::", string(bytes))
|
|
|
|
|
aliRsp = new(AlipayTradePrecreateResponse)
|
|
aliRsp = new(AlipayTradePrecreateResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AlipayTradePrecreateResponse.Code != "10000" {
|
|
if aliRsp.AlipayTradePrecreateResponse.Code != "10000" {
|
|
|
info := aliRsp.AlipayTradePrecreateResponse
|
|
info := aliRsp.AlipayTradePrecreateResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.pay(统一收单交易支付接口)
|
|
//alipay.trade.pay(统一收单交易支付接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.pay
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.pay
|
|
|
-func (this *AliPayClient) AliPayTradePay(body BodyMap) (aliRsp *AliPayTradePayResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
- trade := body.Get("out_trade_no")
|
|
|
|
|
- if trade == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradePay(body BodyMap) (aliRsp *AliPayTradePayResponse, err error) {
|
|
|
|
|
+ var bs []byte
|
|
|
|
|
+ p := body.Get("out_trade_no")
|
|
|
|
|
+ if p == null {
|
|
|
return nil, errors.New("out_trade_no is not allowed to be null")
|
|
return nil, errors.New("out_trade_no is not allowed to be null")
|
|
|
}
|
|
}
|
|
|
- //===============product_code值===================
|
|
|
|
|
- //body.Set("product_code", "FACE_TO_FACE_PAYMENT")
|
|
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.pay")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.pay"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- //log.Println("AliPayTradePayResponse::::", string(bytes))
|
|
|
|
|
aliRsp = new(AliPayTradePayResponse)
|
|
aliRsp = new(AliPayTradePayResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AliPayTradePayResponse.Code != "10000" {
|
|
if aliRsp.AliPayTradePayResponse.Code != "10000" {
|
|
|
info := aliRsp.AliPayTradePayResponse
|
|
info := aliRsp.AliPayTradePayResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.query(统一收单线下交易查询)
|
|
//alipay.trade.query(统一收单线下交易查询)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.query
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.query
|
|
|
-func (this *AliPayClient) AliPayTradeQuery(body BodyMap) (aliRsp *AliPayTradeQueryResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
- trade1 := body.Get("out_trade_no")
|
|
|
|
|
- trade2 := body.Get("trade_no")
|
|
|
|
|
- if trade1 == null && trade2 == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradeQuery(body BodyMap) (aliRsp *AliPayTradeQueryResponse, err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ p1, p2 string
|
|
|
|
|
+ bs []byte
|
|
|
|
|
+ )
|
|
|
|
|
+ p1 = body.Get("out_trade_no")
|
|
|
|
|
+ p2 = body.Get("trade_no")
|
|
|
|
|
+ if p1 == null && p2 == null {
|
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.query")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.query"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AliPayTradeQueryResponse::::", string(bytes))
|
|
|
|
|
aliRsp = new(AliPayTradeQueryResponse)
|
|
aliRsp = new(AliPayTradeQueryResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AliPayTradeQueryResponse.Code != "10000" {
|
|
if aliRsp.AliPayTradeQueryResponse.Code != "10000" {
|
|
|
info := aliRsp.AliPayTradeQueryResponse
|
|
info := aliRsp.AliPayTradeQueryResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.app.pay(app支付接口2.0)
|
|
//alipay.trade.app.pay(app支付接口2.0)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.app.pay
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.app.pay
|
|
|
-func (this *AliPayClient) AliPayTradeAppPay(body BodyMap) (payParam string, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradeAppPay(body BodyMap) (payParam string, err error) {
|
|
|
|
|
+ var bs []byte
|
|
|
trade := body.Get("out_trade_no")
|
|
trade := body.Get("out_trade_no")
|
|
|
if trade == null {
|
|
if trade == null {
|
|
|
return null, errors.New("out_trade_no is not allowed to be null")
|
|
return null, errors.New("out_trade_no is not allowed to be null")
|
|
|
}
|
|
}
|
|
|
- //===============product_code值===================
|
|
|
|
|
- //body.Set("product_code", "QUICK_MSECURITY_PAY")
|
|
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.app.pay")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.app.pay"); err != nil {
|
|
|
return null, err
|
|
return null, err
|
|
|
}
|
|
}
|
|
|
- payParam = string(bytes)
|
|
|
|
|
- return payParam, nil
|
|
|
|
|
|
|
+ payParam = string(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.wap.pay(手机网站支付接口2.0)
|
|
//alipay.trade.wap.pay(手机网站支付接口2.0)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.wap.pay
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.wap.pay
|
|
|
-func (this *AliPayClient) AliPayTradeWapPay(body BodyMap) (payUrl string, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradeWapPay(body BodyMap) (payUrl string, err error) {
|
|
|
|
|
+ var bs []byte
|
|
|
trade := body.Get("out_trade_no")
|
|
trade := body.Get("out_trade_no")
|
|
|
if trade == null {
|
|
if trade == null {
|
|
|
return null, errors.New("out_trade_no is not allowed to be null")
|
|
return null, errors.New("out_trade_no is not allowed to be null")
|
|
|
}
|
|
}
|
|
|
- //===============product_code值===================
|
|
|
|
|
body.Set("product_code", "QUICK_WAP_WAY")
|
|
body.Set("product_code", "QUICK_WAP_WAY")
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.wap.pay")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- //log.Println("err::", err.Error())
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.wap.pay"); err != nil {
|
|
|
return null, err
|
|
return null, err
|
|
|
}
|
|
}
|
|
|
- payUrl = string(bytes)
|
|
|
|
|
- return payUrl, nil
|
|
|
|
|
|
|
+ payUrl = string(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.page.pay(统一收单下单并支付页面接口)
|
|
//alipay.trade.page.pay(统一收单下单并支付页面接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.page.pay
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.page.pay
|
|
|
-func (this *AliPayClient) AliPayTradePagePay(body BodyMap) (payUrl string, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradePagePay(body BodyMap) (payUrl string, err error) {
|
|
|
|
|
+ var bs []byte
|
|
|
trade := body.Get("out_trade_no")
|
|
trade := body.Get("out_trade_no")
|
|
|
if trade == null {
|
|
if trade == null {
|
|
|
return null, errors.New("out_trade_no is not allowed to be null")
|
|
return null, errors.New("out_trade_no is not allowed to be null")
|
|
|
}
|
|
}
|
|
|
- //===============product_code值===================
|
|
|
|
|
body.Set("product_code", "FAST_INSTANT_TRADE_PAY")
|
|
body.Set("product_code", "FAST_INSTANT_TRADE_PAY")
|
|
|
- bytes, err = this.doAliPay(body, "alipay.trade.page.pay")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- //log.Println("err::", err.Error())
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.trade.page.pay"); err != nil {
|
|
|
return null, err
|
|
return null, err
|
|
|
}
|
|
}
|
|
|
- payUrl = string(bytes)
|
|
|
|
|
- return payUrl, nil
|
|
|
|
|
|
|
+ payUrl = string(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.fund.trans.toaccount.transfer(单笔转账到支付宝账户接口)
|
|
//alipay.fund.trans.toaccount.transfer(单笔转账到支付宝账户接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer
|
|
// 文档地址:https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer
|
|
|
-func (this *AliPayClient) AlipayFundTransToaccountTransfer(body BodyMap) (aliRsp *AlipayFundTransToaccountTransferResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
|
|
+func (a *AliPayClient) AlipayFundTransToaccountTransfer(body BodyMap) (aliRsp *AlipayFundTransToaccountTransferResponse, err error) {
|
|
|
|
|
+ var bs []byte
|
|
|
trade1 := body.Get("out_biz_no")
|
|
trade1 := body.Get("out_biz_no")
|
|
|
if trade1 == null {
|
|
if trade1 == null {
|
|
|
return nil, errors.New("out_biz_no is not allowed to be null")
|
|
return nil, errors.New("out_biz_no is not allowed to be null")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, "alipay.fund.trans.toaccount.transfer")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.fund.trans.toaccount.transfer"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AlipayFundTransToaccountTransferResponse::::", string(bytes))
|
|
|
|
|
aliRsp = new(AlipayFundTransToaccountTransferResponse)
|
|
aliRsp = new(AlipayFundTransToaccountTransferResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AlipayFundTransToaccountTransferResponse.Code != "10000" {
|
|
if aliRsp.AlipayFundTransToaccountTransferResponse.Code != "10000" {
|
|
|
info := aliRsp.AlipayFundTransToaccountTransferResponse
|
|
info := aliRsp.AlipayFundTransToaccountTransferResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.trade.orderinfo.sync(支付宝订单信息同步接口)
|
|
//alipay.trade.orderinfo.sync(支付宝订单信息同步接口)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.orderinfo.sync
|
|
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.orderinfo.sync
|
|
|
-func (this *AliPayClient) AliPayTradeOrderinfoSync(body BodyMap) {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPayTradeOrderinfoSync(body BodyMap) {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.system.oauth.token(换取授权访问令牌)
|
|
//alipay.system.oauth.token(换取授权访问令牌)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_9/alipay.system.oauth.token
|
|
// 文档地址:https://docs.open.alipay.com/api_9/alipay.system.oauth.token
|
|
|
-func (this *AliPayClient) AliPaySystemOauthToken(body BodyMap) (aliRsp *AliPaySystemOauthTokenResponse, err error) {
|
|
|
|
|
|
|
+func (a *AliPayClient) AliPaySystemOauthToken(body BodyMap) (aliRsp *AliPaySystemOauthTokenResponse, err error) {
|
|
|
var bs []byte
|
|
var bs []byte
|
|
|
grantType := body.Get("grant_type")
|
|
grantType := body.Get("grant_type")
|
|
|
if grantType == null {
|
|
if grantType == null {
|
|
@@ -408,15 +388,11 @@ func (this *AliPayClient) AliPaySystemOauthToken(body BodyMap) (aliRsp *AliPaySy
|
|
|
if code == null && refreshToken == null {
|
|
if code == null && refreshToken == null {
|
|
|
return nil, errors.New("code and refresh_token are not allowed to be null at the same time")
|
|
return nil, errors.New("code and refresh_token are not allowed to be null at the same time")
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- bs, err = aliPaySystemOauthToken(this.AppId, this.privateKey, body, "alipay.system.oauth.token", this.isProd)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = aliPaySystemOauthToken(a.AppId, a.PrivateKey, body, "alipay.system.oauth.token", a.IsProd); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AliPaySystemOauthToken::::", string(bytes))
|
|
|
|
|
aliRsp = new(AliPaySystemOauthTokenResponse)
|
|
aliRsp = new(AliPaySystemOauthTokenResponse)
|
|
|
- err = json.Unmarshal(bs, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AliPaySystemOauthTokenResponse.AccessToken == null {
|
|
if aliRsp.AliPaySystemOauthTokenResponse.AccessToken == null {
|
|
@@ -424,22 +400,19 @@ func (this *AliPayClient) AliPaySystemOauthToken(body BodyMap) (aliRsp *AliPaySy
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
aliRsp.SignData = getSignData(bs)
|
|
aliRsp.SignData = getSignData(bs)
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.user.info.share(支付宝会员授权信息查询接口)
|
|
//alipay.user.info.share(支付宝会员授权信息查询接口)
|
|
|
// body:此接口无需body参数
|
|
// body:此接口无需body参数
|
|
|
// 文档地址:https://docs.open.alipay.com/api_2/alipay.user.info.share
|
|
// 文档地址:https://docs.open.alipay.com/api_2/alipay.user.info.share
|
|
|
-func (this *AliPayClient) AlipayUserInfoShare() (aliRsp *AlipayUserInfoShareResponse, err error) {
|
|
|
|
|
|
|
+func (a *AliPayClient) AlipayUserInfoShare() (aliRsp *AlipayUserInfoShareResponse, err error) {
|
|
|
var bs []byte
|
|
var bs []byte
|
|
|
- bs, err = this.doAliPay(nil, "alipay.user.info.share")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(nil, "alipay.user.info.share"); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
- //log.Println("AlipayUserInfoShare::::", string(bs))
|
|
|
|
|
aliRsp = new(AlipayUserInfoShareResponse)
|
|
aliRsp = new(AlipayUserInfoShareResponse)
|
|
|
- err = json.Unmarshal(bs, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AlipayUserInfoShareResponse.Code != "10000" {
|
|
if aliRsp.AlipayUserInfoShareResponse.Code != "10000" {
|
|
@@ -447,12 +420,12 @@ func (this *AliPayClient) AlipayUserInfoShare() (aliRsp *AlipayUserInfoShareResp
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
aliRsp.SignData = getSignData(bs)
|
|
aliRsp.SignData = getSignData(bs)
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//alipay.open.auth.token.app(换取应用授权令牌)
|
|
//alipay.open.auth.token.app(换取应用授权令牌)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_9/alipay.open.auth.token.app
|
|
// 文档地址:https://docs.open.alipay.com/api_9/alipay.open.auth.token.app
|
|
|
-func (this *AliPayClient) AlipayOpenAuthTokenApp(body BodyMap) (aliRsp *AlipayOpenAuthTokenAppResponse, err error) {
|
|
|
|
|
|
|
+func (a *AliPayClient) AlipayOpenAuthTokenApp(body BodyMap) (aliRsp *AlipayOpenAuthTokenAppResponse, err error) {
|
|
|
var bs []byte
|
|
var bs []byte
|
|
|
grantType := body.Get("grant_type")
|
|
grantType := body.Get("grant_type")
|
|
|
if grantType == null {
|
|
if grantType == null {
|
|
@@ -463,15 +436,11 @@ func (this *AliPayClient) AlipayOpenAuthTokenApp(body BodyMap) (aliRsp *AlipayOp
|
|
|
if code == null && refreshToken == null {
|
|
if code == null && refreshToken == null {
|
|
|
return nil, errors.New("code and refresh_token are not allowed to be null at the same time")
|
|
return nil, errors.New("code and refresh_token are not allowed to be null at the same time")
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- bs, err = this.doAliPay(body, "alipay.open.auth.token.app")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "alipay.open.auth.token.app"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("AlipayOpenAuthTokenApp::::", string(bs))
|
|
|
|
|
aliRsp = new(AlipayOpenAuthTokenAppResponse)
|
|
aliRsp = new(AlipayOpenAuthTokenAppResponse)
|
|
|
- err = json.Unmarshal(bs, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.AlipayOpenAuthTokenAppResponse.Code != "10000" {
|
|
if aliRsp.AlipayOpenAuthTokenAppResponse.Code != "10000" {
|
|
@@ -479,147 +448,122 @@ func (this *AliPayClient) AlipayOpenAuthTokenApp(body BodyMap) (aliRsp *AlipayOp
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
aliRsp.SignData = getSignData(bs)
|
|
aliRsp.SignData = getSignData(bs)
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//zhima.credit.score.get(芝麻分)
|
|
//zhima.credit.score.get(芝麻分)
|
|
|
// 文档地址:https://docs.open.alipay.com/api_8/zhima.credit.score.get
|
|
// 文档地址:https://docs.open.alipay.com/api_8/zhima.credit.score.get
|
|
|
-func (this *AliPayClient) ZhimaCreditScoreGet(body BodyMap) (aliRsp *ZhimaCreditScoreGetResponse, err error) {
|
|
|
|
|
- var bytes []byte
|
|
|
|
|
-
|
|
|
|
|
- trade1 := body.Get("product_code")
|
|
|
|
|
- if trade1 == null {
|
|
|
|
|
|
|
+func (a *AliPayClient) ZhimaCreditScoreGet(body BodyMap) (aliRsp *ZhimaCreditScoreGetResponse, err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ p1, p2 string
|
|
|
|
|
+ bs []byte
|
|
|
|
|
+ )
|
|
|
|
|
+ if p1 = body.Get("product_code"); p1 == null {
|
|
|
body.Set("product_code", "w1010100100000000001")
|
|
body.Set("product_code", "w1010100100000000001")
|
|
|
}
|
|
}
|
|
|
- trade2 := body.Get("transaction_id")
|
|
|
|
|
- if trade2 == null {
|
|
|
|
|
|
|
+ if p2 = body.Get("transaction_id"); p2 == null {
|
|
|
return nil, errors.New("transaction_id is not allowed to be null")
|
|
return nil, errors.New("transaction_id is not allowed to be null")
|
|
|
}
|
|
}
|
|
|
- bytes, err = this.doAliPay(body, "zhima.credit.score.get")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bs, err = a.doAliPay(body, "zhima.credit.score.get"); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- //log.Println("ZhimaCreditScoreGet::::", string(bytes))
|
|
|
|
|
aliRsp = new(ZhimaCreditScoreGetResponse)
|
|
aliRsp = new(ZhimaCreditScoreGetResponse)
|
|
|
- err = json.Unmarshal(bytes, aliRsp)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err = json.Unmarshal(bs, aliRsp); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
if aliRsp.ZhimaCreditScoreGetResponse.Code != "10000" {
|
|
if aliRsp.ZhimaCreditScoreGetResponse.Code != "10000" {
|
|
|
info := aliRsp.ZhimaCreditScoreGetResponse
|
|
info := aliRsp.ZhimaCreditScoreGetResponse
|
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
|
|
|
}
|
|
}
|
|
|
- aliRsp.SignData = getSignData(bytes)
|
|
|
|
|
- return aliRsp, nil
|
|
|
|
|
|
|
+ aliRsp.SignData = getSignData(bs)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//向支付宝发送请求
|
|
//向支付宝发送请求
|
|
|
-func (this *AliPayClient) doAliPay(body BodyMap, method string) (bytes []byte, err error) {
|
|
|
|
|
- //===============转换body参数===================
|
|
|
|
|
- var bodyStr string
|
|
|
|
|
|
|
+func (a *AliPayClient) doAliPay(body BodyMap, method string) (bytes []byte, err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ bodyStr, sign, url, urlParam string
|
|
|
|
|
+ bodyBs []byte
|
|
|
|
|
+ res gorequest.Response
|
|
|
|
|
+ errs []error
|
|
|
|
|
+ )
|
|
|
if body != nil {
|
|
if body != nil {
|
|
|
- bodyBs, err := json.Marshal(body)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- log.Println("json.Marshal:", err)
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if bodyBs, err = json.Marshal(body); err != nil {
|
|
|
|
|
+ return nil, fmt.Errorf("json.Marshal:%v", err.Error())
|
|
|
}
|
|
}
|
|
|
bodyStr = string(bodyBs)
|
|
bodyStr = string(bodyBs)
|
|
|
}
|
|
}
|
|
|
- //fmt.Println(string(bodyStr))
|
|
|
|
|
- //===============生成参数===================
|
|
|
|
|
pubBody := make(BodyMap)
|
|
pubBody := make(BodyMap)
|
|
|
- pubBody.Set("app_id", this.AppId)
|
|
|
|
|
|
|
+ pubBody.Set("app_id", a.AppId)
|
|
|
pubBody.Set("method", method)
|
|
pubBody.Set("method", method)
|
|
|
pubBody.Set("format", "JSON")
|
|
pubBody.Set("format", "JSON")
|
|
|
- if this.AppCertSN != null {
|
|
|
|
|
- pubBody.Set("app_cert_sn", this.AppCertSN)
|
|
|
|
|
|
|
+ if a.AppCertSN != null {
|
|
|
|
|
+ pubBody.Set("app_cert_sn", a.AppCertSN)
|
|
|
}
|
|
}
|
|
|
- if this.AlipayRootCertSN != null {
|
|
|
|
|
- pubBody.Set("alipay_root_cert_sn", this.AlipayRootCertSN)
|
|
|
|
|
|
|
+ if a.AlipayRootCertSN != null {
|
|
|
|
|
+ pubBody.Set("alipay_root_cert_sn", a.AlipayRootCertSN)
|
|
|
}
|
|
}
|
|
|
- if this.ReturnUrl != null {
|
|
|
|
|
- pubBody.Set("return_url", this.ReturnUrl)
|
|
|
|
|
|
|
+ if a.ReturnUrl != null {
|
|
|
|
|
+ pubBody.Set("return_url", a.ReturnUrl)
|
|
|
}
|
|
}
|
|
|
- if this.Charset == null {
|
|
|
|
|
|
|
+ if a.Charset == null {
|
|
|
pubBody.Set("charset", "utf-8")
|
|
pubBody.Set("charset", "utf-8")
|
|
|
} else {
|
|
} else {
|
|
|
- pubBody.Set("charset", this.Charset)
|
|
|
|
|
|
|
+ pubBody.Set("charset", a.Charset)
|
|
|
}
|
|
}
|
|
|
- if this.SignType == null {
|
|
|
|
|
|
|
+ if a.SignType == null {
|
|
|
pubBody.Set("sign_type", "RSA2")
|
|
pubBody.Set("sign_type", "RSA2")
|
|
|
} else {
|
|
} else {
|
|
|
- pubBody.Set("sign_type", this.SignType)
|
|
|
|
|
|
|
+ pubBody.Set("sign_type", a.SignType)
|
|
|
}
|
|
}
|
|
|
pubBody.Set("timestamp", time.Now().Format(TimeLayout))
|
|
pubBody.Set("timestamp", time.Now().Format(TimeLayout))
|
|
|
pubBody.Set("version", "1.0")
|
|
pubBody.Set("version", "1.0")
|
|
|
- if this.NotifyUrl != null {
|
|
|
|
|
- pubBody.Set("notify_url", this.NotifyUrl)
|
|
|
|
|
|
|
+ if a.NotifyUrl != null {
|
|
|
|
|
+ pubBody.Set("notify_url", a.NotifyUrl)
|
|
|
}
|
|
}
|
|
|
- if this.AppAuthToken != null {
|
|
|
|
|
- pubBody.Set("app_auth_token", this.AppAuthToken)
|
|
|
|
|
|
|
+ if a.AppAuthToken != null {
|
|
|
|
|
+ pubBody.Set("app_auth_token", a.AppAuthToken)
|
|
|
}
|
|
}
|
|
|
- if this.AuthToken != null {
|
|
|
|
|
- pubBody.Set("auth_token", this.AuthToken)
|
|
|
|
|
|
|
+ if a.AuthToken != null {
|
|
|
|
|
+ pubBody.Set("auth_token", a.AuthToken)
|
|
|
}
|
|
}
|
|
|
- //fmt.Println("biz_content", string(bodyStr))
|
|
|
|
|
if bodyStr != null {
|
|
if bodyStr != null {
|
|
|
pubBody.Set("biz_content", bodyStr)
|
|
pubBody.Set("biz_content", bodyStr)
|
|
|
}
|
|
}
|
|
|
- //===============获取签名===================
|
|
|
|
|
- pKey := FormatPrivateKey(this.privateKey)
|
|
|
|
|
- sign, err := getRsaSign(pubBody, pubBody.Get("sign_type"), pKey)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ if sign, err = getRsaSign(pubBody, pubBody.Get("sign_type"), FormatPrivateKey(a.PrivateKey)); err != nil {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
pubBody.Set("sign", sign)
|
|
pubBody.Set("sign", sign)
|
|
|
- //fmt.Println("rsaSign:", sign)
|
|
|
|
|
- //===============发起请求===================
|
|
|
|
|
- urlParam := FormatAliPayURLParam(pubBody)
|
|
|
|
|
- //fmt.Println("urlParam:", urlParam)
|
|
|
|
|
|
|
+ urlParam = FormatAliPayURLParam(pubBody)
|
|
|
if method == "alipay.trade.app.pay" {
|
|
if method == "alipay.trade.app.pay" {
|
|
|
return []byte(urlParam), nil
|
|
return []byte(urlParam), nil
|
|
|
}
|
|
}
|
|
|
if method == "alipay.trade.page.pay" {
|
|
if method == "alipay.trade.page.pay" {
|
|
|
- if !this.isProd {
|
|
|
|
|
- //沙箱环境
|
|
|
|
|
- return []byte(zfb_sanbox_base_url + "?" + urlParam), nil
|
|
|
|
|
|
|
+ if !a.IsProd {
|
|
|
|
|
+ return []byte(zfbSandboxBaseUrl + "?" + urlParam), nil
|
|
|
} else {
|
|
} else {
|
|
|
- //正式环境
|
|
|
|
|
- return []byte(zfb_base_url + "?" + urlParam), nil
|
|
|
|
|
|
|
+ return []byte(zfbBaseUrl + "?" + urlParam), nil
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- var url string
|
|
|
|
|
agent := HttpAgent()
|
|
agent := HttpAgent()
|
|
|
- if !this.isProd {
|
|
|
|
|
- //沙箱环境
|
|
|
|
|
- url = zfb_sanbox_base_url_utf8
|
|
|
|
|
- //fmt.Println(url)
|
|
|
|
|
- agent.Post(url)
|
|
|
|
|
|
|
+ if !a.IsProd {
|
|
|
|
|
+ url = zfbSandboxBaseUrlUtf8
|
|
|
} else {
|
|
} else {
|
|
|
- //正式环境
|
|
|
|
|
- url = zfb_base_url_utf8
|
|
|
|
|
- //fmt.Println(url)
|
|
|
|
|
- agent.Post(url)
|
|
|
|
|
- }
|
|
|
|
|
- res, bs, errs := agent.
|
|
|
|
|
- Type("form-data").
|
|
|
|
|
- SendString(urlParam).
|
|
|
|
|
- EndBytes()
|
|
|
|
|
- if len(errs) > 0 {
|
|
|
|
|
|
|
+ url = zfbBaseUrlUtf8
|
|
|
|
|
+ }
|
|
|
|
|
+ if res, bytes, errs = agent.Post(url).Type("form-data").SendString(urlParam).EndBytes(); len(errs) > 0 {
|
|
|
return nil, errs[0]
|
|
return nil, errs[0]
|
|
|
}
|
|
}
|
|
|
- //fmt.Println("res:", res.StatusCode)
|
|
|
|
|
if res.StatusCode != 200 {
|
|
if res.StatusCode != 200 {
|
|
|
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %v", res.StatusCode)
|
|
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %v", res.StatusCode)
|
|
|
}
|
|
}
|
|
|
if method == "alipay.trade.wap.pay" {
|
|
if method == "alipay.trade.wap.pay" {
|
|
|
- //fmt.Println("rsp:::", rsp.Body)
|
|
|
|
|
- if res.Request.URL.String() == zfb_sanbox_base_url || res.Request.URL.String() == zfb_base_url {
|
|
|
|
|
- return nil, errors.New("请求手机网站支付出错,请检查各个参数或秘钥是否正确")
|
|
|
|
|
|
|
+ if res.Request.URL.String() == zfbSandboxBaseUrl || res.Request.URL.String() == zfbBaseUrl {
|
|
|
|
|
+ return nil, errors.New("alipay.trade.wap.pay error,please check the parameters")
|
|
|
}
|
|
}
|
|
|
return []byte(res.Request.URL.String()), nil
|
|
return []byte(res.Request.URL.String()), nil
|
|
|
}
|
|
}
|
|
|
- return bs, nil
|
|
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func getSignData(bs []byte) (signData string) {
|
|
func getSignData(bs []byte) (signData string) {
|