Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/alipay API #456

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions alipay/client_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ func (a *Client) doAliPay(ctx context.Context, bm gopay.BodyMap, method string,
return nil, err
}
switch method {
case "alipay.trade.app.pay", "alipay.fund.auth.order.app.freeze", "zhima.credit.pe.zmgo.sign.apply", "zhima.credit.payafteruse.creditagreement.sign":
case "alipay.trade.app.pay", "alipay.fund.auth.order.app.freeze",
"alipay.fund.trans.app.pay", "alipay.user.agreement.page.sign",
"zhima.credit.pe.zmgo.sign.apply", "zhima.credit.payafteruse.creditagreement.sign":
return []byte(param), nil
case "alipay.trade.wap.pay", "alipay.trade.page.pay", "alipay.user.certify.open.certify":
if !a.IsProd {
Expand Down Expand Up @@ -218,7 +220,9 @@ func (a *Client) DoAliPay(ctx context.Context, bm gopay.BodyMap, method string,
return nil, err
}
switch method {
case "alipay.trade.app.pay", "alipay.fund.auth.order.app.freeze":
case "alipay.trade.app.pay", "alipay.fund.auth.order.app.freeze",
"alipay.fund.trans.app.pay", "alipay.user.agreement.page.sign",
"zhima.credit.pe.zmgo.sign.apply", "zhima.credit.payafteruse.creditagreement.sign":
return []byte(param), nil
case "alipay.trade.wap.pay", "alipay.trade.page.pay", "alipay.user.certify.open.certify":
if !a.IsProd {
Expand All @@ -244,7 +248,7 @@ func (a *Client) DoAliPay(ctx context.Context, bm gopay.BodyMap, method string,
}
}

// 保持和官方 SDK 命名方式一致
// Deprecated
func (a *Client) PageExecute(ctx context.Context, bm gopay.BodyMap, method string, authToken ...string) (url string, err error) {
var (
bizContent string
Expand Down
24 changes: 8 additions & 16 deletions alipay/funds_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (a *Client) FundAuthOrderVoucherCreate(ctx context.Context, bm gopay.BodyMa

// alipay.fund.auth.order.app.freeze(线上资金授权冻结接口)
// 文档地址: https://opendocs.alipay.com/open/02f912
func (a *Client) FundAuthOrderAppFreeze(ctx context.Context, bm gopay.BodyMap) (payParam string, err error) {
func (a *Client) FundAuthOrderAppFreeze(ctx context.Context, bm gopay.BodyMap) (orderStr string, err error) {
err = bm.CheckEmptyError("out_order_no", "out_request_no", "order_title", "amount", "product_code")
if err != nil {
return gopay.NULL, err
Expand All @@ -180,8 +180,8 @@ func (a *Client) FundAuthOrderAppFreeze(ctx context.Context, bm gopay.BodyMap) (
if bs, err = a.doAliPay(ctx, bm, "alipay.fund.auth.order.app.freeze"); err != nil {
return "", err
}
payParam = string(bs)
return payParam, nil
orderStr = string(bs)
return orderStr, nil
}

// alipay.fund.auth.order.unfreeze(资金授权解冻接口)
Expand Down Expand Up @@ -320,25 +320,17 @@ func (a *Client) FundBatchDetailQuery(ctx context.Context, bm gopay.BodyMap) (al

// alipay.fund.trans.app.pay(现金红包无线支付接口)
// 文档地址: https://opendocs.alipay.com/open/03rbyf
func (a *Client) FundTransAppPay(ctx context.Context, bm gopay.BodyMap) (aliRsp *FundTransAppPayResponse, err error) {
func (a *Client) FundTransAppPay(ctx context.Context, bm gopay.BodyMap) (pageRedirectionData string, err error) {
err = bm.CheckEmptyError("out_biz_no", "trans_amount", "product_code", "biz_scene")
if err != nil {
return nil, err
return gopay.NULL, err
}
var bs []byte
if bs, err = a.doAliPay(ctx, bm, "alipay.fund.trans.app.pay"); err != nil {
return nil, err
}
aliRsp = new(FundTransAppPayResponse)
if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
}
if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
return aliRsp, err
return "", err
}
signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
aliRsp.SignData = signData
return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
pageRedirectionData = string(bs)
return pageRedirectionData, nil
}

// alipay.fund.trans.payee.bind.query(资金收款账号绑定关系查询)
Expand Down
18 changes: 17 additions & 1 deletion alipay/funds_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,24 @@ func TestClient_FundTransPagePay(t *testing.T) {

aliRsp, err := client.FundTransPagePay(ctx, bm)
if err != nil {
//xlog.Errorf("client.FundTransPagePay(%+v),error:%+v", bm, err)
xlog.Errorf("client.FundTransPagePay(%+v),err:%+v", bm, err)
return
}
xlog.Debug("aliRsp:", *aliRsp)
}

func TestClient_FundTransAppPay(t *testing.T) {
bm := make(gopay.BodyMap)
bm.Set("out_biz_no", "20180628000035").
Set("trans_amount", "8.88").
Set("product_code", "STD_RED_PACKET").
Set("biz_scene", "PERSONAL_PAY").
Set("order_title", "钉钉拼手气红包")

pageRedirectionData, err := client.FundTransAppPay(ctx, bm)
if err != nil {
xlog.Errorf("client.FundTransAppPay(%+v),err:%+v", bm, err)
return
}
xlog.Debug("pageRedirectionData: ", pageRedirectionData)
}
36 changes: 26 additions & 10 deletions alipay/member_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (a *Client) UserCertifyOpenQuery(ctx context.Context, bm gopay.BodyMap) (al

// alipay.user.agreement.page.sign(支付宝个人协议页面签约接口)
// 文档地址:https://opendocs.alipay.com/open/8bccfa0b_alipay.user.agreement.page.sign
func (a *Client) UserAgreementPageSign(ctx context.Context, bm gopay.BodyMap) (ret string, err error) {
func (a *Client) UserAgreementPageSign(ctx context.Context, bm gopay.BodyMap) (pageRedirectionData string, err error) {
err = bm.CheckEmptyError("personal_product_code")
if err != nil {
return gopay.NULL, err
Expand All @@ -221,27 +221,43 @@ func (a *Client) UserAgreementPageSign(ctx context.Context, bm gopay.BodyMap) (r
return string(bs), nil
}

// alipay.user.agreement.page.sign(APP 支付宝个人协议页面签约接口)
// 文档地址:https://opendocs.alipay.com/open/00a05b 通过 App 唤起支付宝的签约页面
// alipay.user.agreement.page.sign(支付宝个人协议页面签约接口) - PC转二维码唤起签约页
// 文档地址:https://opendocs.alipay.com/open/08ayiq?pathHash=a2d4e097#PC%E8%BD%AC%E4%BA%8C%E7%BB%B4%E7%A0%81%E5%94%A4%E8%B5%B7%E7%AD%BE%E7%BA%A6%E9%A1%B5
func (a *Client) UserAgreementPageSignInQRCode(ctx context.Context, bm gopay.BodyMap) (qrcode string, err error) {
err = bm.CheckEmptyError("personal_product_code", "access_params")
if err != nil {
return gopay.NULL, err
}
var bs []byte
if bs, err = a.doAliPay(ctx, bm, "alipay.user.agreement.page.sign"); err != nil {
return "", err
}
// 该链接里面的 APPID 为固定值,不可修改
// 生成唤起客户端。把signParams使用 UTF-8 字符集整体做一次 encode
qrcode = "alipays://platformapi/startapp?appId=60000157&appClearTop=false&startMultApp=YES&sign_params=" + url.QueryEscape(string(bs))
return qrcode, nil
}

// Deprecated
// 后续会删除,请使用 UserAgreementPageSignInQRCode() 替代
func (a *Client) UserAgreementPageSignInApp(ctx context.Context, bm gopay.BodyMap) (ret string, err error) {
err = bm.CheckEmptyError("personal_product_code")
err = bm.CheckEmptyError("personal_product_code", "access_params")
if err != nil {
return gopay.NULL, err
}

var bs string
// 参考官方示例
// PageExecute get方式,生成url
if bs, err = a.PageExecute(ctx, bm, "alipay.user.agreement.page.sign"); err != nil {
// 参考官方示例 PageExecute get方式,生成 uri
uri, err := a.PageExecute(ctx, bm, "alipay.user.agreement.page.sign")
if err != nil {
return "", err
}

// / 生成的url地址去除 http://openapi.alipay.com/gateway.do
// / 生成的url地址去除 http://openapi.alipay.com/gateway.do?
replaceUrl := baseUrl + "?"
if !a.IsProd {
replaceUrl = sandboxBaseUrl + "?"
}
signParams := strings.Replace(bs, replaceUrl, "", 1)
signParams := strings.Replace(uri, replaceUrl, "", 1)

// 该链接里面的 APPID 为固定值,不可修改)
// 生成唤起客户端。把signParams使用 UTF-8 字符集整体做一次 encode
Expand Down
8 changes: 3 additions & 5 deletions alipay/member_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestUserAgreementTransfer(t *testing.T) {
xlog.Debug("aliRsp:", *aliRsp)
}

func TestUserAgreementPageSignInApp(t *testing.T) {
func TestUserAgreementPageSignInQRCode(t *testing.T) {
// 请求参数
bm := make(gopay.BodyMap)
bm.Set("personal_product_code", "CYCLE_PAY_AUTH_P")
Expand All @@ -143,8 +143,7 @@ func TestUserAgreementPageSignInApp(t *testing.T) {
})

// 发起请求
link, err := client.UserAgreementPageSignInApp(ctx, bm)
xlog.Info(err)
qrcode, err := client.UserAgreementPageSignInQRCode(ctx, bm)
if err != nil {
if bizErr, ok := IsBizError(err); ok {
xlog.Errorf("%+v", bizErr)
Expand All @@ -153,8 +152,7 @@ func TestUserAgreementPageSignInApp(t *testing.T) {
}
return
}

xlog.Debug("aliRsp:", link)
xlog.Debug("aliRsp:", qrcode)
}

func TestUserTwostageCommonUse(t *testing.T) {
Expand Down
7 changes: 0 additions & 7 deletions alipay/mode_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,6 @@ type FundBatchDetailQueryResponse struct {
Sign string `json:"sign"`
}

type FundTransAppPayResponse struct {
Response *FundTransAppPay `json:"alipay_fund_trans_app_pay_response"`
AlipayCertSn string `json:"alipay_cert_sn,omitempty"`
SignData string `json:"-"`
Sign string `json:"sign"`
}

type FundTransPayeeBindQueryRsp struct {
Response *FundTransPayeeBindQuery `json:"alipay_fund_trans_payee_bind_query_response"`
AlipayCertSn string `json:"alipay_cert_sn,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions alipay/payment_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (a *Client) TradePrecreate(ctx context.Context, bm gopay.BodyMap) (aliRsp *

// alipay.trade.app.pay(app支付接口2.0)
// 文档地址:https://opendocs.alipay.com/open/02e7gq
func (a *Client) TradeAppPay(ctx context.Context, bm gopay.BodyMap) (payParam string, err error) {
func (a *Client) TradeAppPay(ctx context.Context, bm gopay.BodyMap) (orderStr string, err error) {
err = bm.CheckEmptyError("out_trade_no", "total_amount", "subject")
if err != nil {
return gopay.NULL, err
Expand All @@ -70,8 +70,8 @@ func (a *Client) TradeAppPay(ctx context.Context, bm gopay.BodyMap) (payParam st
if bs, err = a.doAliPay(ctx, bm, "alipay.trade.app.pay"); err != nil {
return gopay.NULL, err
}
payParam = string(bs)
return payParam, nil
orderStr = string(bs)
return orderStr, nil
}

// alipay.trade.wap.pay(手机网站支付接口2.0)
Expand Down
6 changes: 3 additions & 3 deletions alipay/v3/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/go-pay/gopay"
"github.com/go-pay/gopay/alipay/cert"
"github.com/go-pay/gopay/pkg/js"
"github.com/go-pay/util"
"github.com/go-pay/util/js"
"github.com/go-pay/xlog"
)

Expand Down Expand Up @@ -68,7 +68,7 @@ func TestDoAliPayAPISelfV3(t *testing.T) {
xlog.Errorf("client.TradePrecreate(), err:%v", err)
return
}
xlog.Debugf("aliRsp:%s", js.Marshal(rsp))
xlog.Debugf("aliRsp:%s", js.MarshalString(rsp))
if res.StatusCode != Success {
xlog.Errorf("aliRsp.StatusCode:%d", res.StatusCode)
return
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestClientV3_Transfer(t *testing.T) {
return
}

xlog.Debugf("aliRsp:%s", js.Marshal(res))
xlog.Debugf("aliRsp:%s", js.MarshalString(res))
if res.StatusCode != Success {
xlog.Errorf("aliRsp.StatusCode:%d", res.StatusCode)
return
Expand Down
4 changes: 2 additions & 2 deletions alipay/v3/payment_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/js"
"github.com/go-pay/util"
"github.com/go-pay/util/js"
"github.com/go-pay/xlog"
)

Expand All @@ -22,7 +22,7 @@ func TestTradePrecreate(t *testing.T) {
xlog.Errorf("client.TradePrecreate(), err:%v", err)
return
}
xlog.Debugf("aliRsp:%s", js.Marshal(aliRsp))
xlog.Debugf("aliRsp:%s", js.MarshalString(aliRsp))

if aliRsp.StatusCode != Success {
xlog.Errorf("aliRsp.StatusCode:%d", aliRsp.StatusCode)
Expand Down
2 changes: 1 addition & 1 deletion constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const (
OK = "OK"
DebugOff = 0
DebugOn = 1
Version = "v1.5.108"
Version = "v1.5.109"
)

type DebugSwitch int8
2 changes: 1 addition & 1 deletion doc/alipay.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ xlog.Infof("%+v", phone)
* 交易分账查询接口:`client.TradeOrderSettleQuery()`
* 商家扣款
* 支付宝个人协议页面签约接口: `client.UserAgreementPageSign()`
* 支付宝个人协议页面签约接口(App 专用,生成唤醒签约页面链接): `client.UserAgreementPageSignInApp()`
* 支付宝个人协议页面签约接口 - PC转二维码唤起签约页: `client.UserAgreementPageSignInQRCode()`
* 支付宝个人代扣协议查询接口: `client.UserAgreementQuery()`
* 支付宝个人代扣协议解约接口: `client.UserAgreementPageUnSign()`
* 周期性扣款协议执行计划修改接口: `client.UserAgreementExecutionplanModify()`
Expand Down
27 changes: 0 additions & 27 deletions pkg/js/json.go

This file was deleted.

6 changes: 6 additions & 0 deletions release_note.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 版本号:v1.5.109

* 修改记录:
* 支付宝:作废 client.UserAgreementPageSignInApp() 方法,使用 client.UserAgreementPageSignInQRCode() 方法替换。
* 支付宝:修改 client.FundTransAppPay() 方法返回参数。

## 版本号:v1.5.108

* 修改记录:
Expand Down
Loading