From ea18a6ad79e65ad4287c82089cbc8f08440c3cfc Mon Sep 17 00:00:00 2001 From: Jerry <85411418@qq.com> Date: Tue, 24 May 2022 20:01:22 +0800 Subject: [PATCH] v1.5.79 --- alipay/client.go | 91 +++++++++++++++++++++++++++------------------ constant.go | 2 +- doc/alipay.md | 16 ++++---- doc/apple.md | 96 ++++++++++++++++++++++++------------------------ release_note.txt | 6 +++ 5 files changed, 119 insertions(+), 92 deletions(-) diff --git a/alipay/client.go b/alipay/client.go index 255da762..219a4cb3 100644 --- a/alipay/client.go +++ b/alipay/client.go @@ -195,19 +195,49 @@ func (a *Client) doAliPaySelf(ctx context.Context, bm gopay.BodyMap, method stri // 向支付宝发送请求 func (a *Client) doAliPay(ctx context.Context, bm gopay.BodyMap, method string, authToken ...string) (bs []byte, err error) { var ( - bodyStr, url string - bodyBs []byte - aat string + bizContent, url string + bodyBs []byte ) if bm != nil { - aat = bm.GetString("app_auth_token") bm.Remove("app_auth_token") if bodyBs, err = json.Marshal(bm); err != nil { return nil, fmt.Errorf("json.Marshal:%w", err) } - bodyStr = string(bodyBs) + bizContent = string(bodyBs) } + // 处理公共参数 + param, err := a.pubParamsHandle(bm, method, bizContent, authToken...) + switch method { + case "alipay.trade.app.pay", "alipay.fund.auth.order.app.freeze": + return []byte(param), nil + case "alipay.trade.wap.pay", "alipay.trade.page.pay", "alipay.user.certify.open.certify": + if !a.IsProd { + return []byte(sandboxBaseUrl + "?" + param), nil + } + return []byte(baseUrl + "?" + param), nil + default: + httpClient := xhttp.NewClient() + url = baseUrlUtf8 + if !a.IsProd { + url = sandboxBaseUrlUtf8 + } + res, bs, err := httpClient.Type(xhttp.TypeForm).Post(url).SendString(param).EndBytes(ctx) + if err != nil { + return nil, err + } + if a.DebugSwitch == gopay.DebugOn { + xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs)) + } + if res.StatusCode != 200 { + return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode) + } + return bs, nil + } +} + +// 公共参数处理 +func (a *Client) pubParamsHandle(bm gopay.BodyMap, method, bizContent string, authToken ...string) (param string, err error) { pubBody := make(gopay.BodyMap) pubBody.Set("app_id", a.AppId). Set("method", method). @@ -217,68 +247,57 @@ func (a *Client) doAliPay(ctx context.Context, bm gopay.BodyMap, method string, Set("version", "1.0"). Set("timestamp", time.Now().Format(util.TimeLayout)) + // version + if version := bm.GetString("version"); version != util.NULL { + pubBody.Set("version", version) + } if a.AppCertSN != util.NULL { pubBody.Set("app_cert_sn", a.AppCertSN) } if a.AliPayRootCertSN != util.NULL { pubBody.Set("alipay_root_cert_sn", a.AliPayRootCertSN) } + // return_url if a.ReturnUrl != util.NULL { pubBody.Set("return_url", a.ReturnUrl) } + if returnUrl := bm.GetString("return_url"); returnUrl != util.NULL { + pubBody.Set("return_url", returnUrl) + } if a.location != nil { pubBody.Set("timestamp", time.Now().In(a.location).Format(util.TimeLayout)) } + // notify_url if a.NotifyUrl != util.NULL { pubBody.Set("notify_url", a.NotifyUrl) } + if notifyUrl := bm.GetString("notify_url"); notifyUrl != util.NULL { + pubBody.Set("notify_url", notifyUrl) + } + // app_auth_token if a.AppAuthToken != util.NULL { pubBody.Set("app_auth_token", a.AppAuthToken) } - if aat != util.NULL { + if aat := bm.GetString("app_auth_token"); aat != util.NULL { pubBody.Set("app_auth_token", aat) } if len(authToken) > 0 { pubBody.Set("auth_token", authToken[0]) } - if bodyStr != util.NULL { - pubBody.Set("biz_content", bodyStr) + if bizContent != util.NULL { + pubBody.Set("biz_content", bizContent) } + // 计算sign sign, err := GetRsaSign(pubBody, pubBody.GetString("sign_type"), a.privateKey) if err != nil { - return nil, fmt.Errorf("GetRsaSign Error: %w", err) + return "", fmt.Errorf("GetRsaSign Error: %w", err) } pubBody.Set("sign", sign) if a.DebugSwitch == gopay.DebugOn { xlog.Debugf("Alipay_Request: %s", pubBody.JsonBody()) } - param := pubBody.EncodeURLParams() - switch method { - case "alipay.trade.app.pay", "alipay.fund.auth.order.app.freeze": - return []byte(param), nil - case "alipay.trade.wap.pay", "alipay.trade.page.pay", "alipay.user.certify.open.certify": - if !a.IsProd { - return []byte(sandboxBaseUrl + "?" + param), nil - } - return []byte(baseUrl + "?" + param), nil - default: - httpClient := xhttp.NewClient() - url = baseUrlUtf8 - if !a.IsProd { - url = sandboxBaseUrlUtf8 - } - res, bs, err := httpClient.Type(xhttp.TypeForm).Post(url).SendString(param).EndBytes(ctx) - if err != nil { - return nil, err - } - if a.DebugSwitch == gopay.DebugOn { - xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs)) - } - if res.StatusCode != 200 { - return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode) - } - return bs, nil - } + param = pubBody.EncodeURLParams() + return } // 公共参数检查 diff --git a/constant.go b/constant.go index 53590ad5..f5bcd31a 100644 --- a/constant.go +++ b/constant.go @@ -7,7 +7,7 @@ const ( OK = "OK" DebugOff = 0 DebugOn = 1 - Version = "1.5.78" + Version = "1.5.79" ) type DebugSwitch int8 diff --git a/doc/alipay.md b/doc/alipay.md index d18360f6..17932dbf 100644 --- a/doc/alipay.md +++ b/doc/alipay.md @@ -63,6 +63,8 @@ err := client.SetCertSnByContent("appCertPublicKey bytes", "alipayRootCert bytes > 业务错误处理:当 `err != nil` 时,可通过 `alipay.IsBizError()` 捕获业务错误状态码和说明。 > 不在乎 `BizError` 的可忽略统一判错处理 +> ★入参 BodyMap中,支持如下公共参数在当次请求中自定义设置:`version`、`return_url`、`notify_url`、`app_auth_token` + - 统一收单交易支付接口 - 示例 ```go @@ -81,13 +83,13 @@ bm.Set("subject", "条码支付"). aliRsp, err := client.TradePay(bm) if err != nil { - if bizErr, ok := alipay.IsBizError(err); ok { - xlog.Errorf("%+v", bizErr) - // do something - return - } - xlog.Errorf("client.TradePay(%+v),err:%+v", bm, err) - return + if bizErr, ok := alipay.IsBizError(err); ok { + xlog.Errorf("%+v", bizErr) + // do something + return + } + xlog.Errorf("client.TradePay(%+v),err:%+v", bm, err) + return } ``` diff --git a/doc/apple.md b/doc/apple.md index 08ad93a9..a9c0bf60 100644 --- a/doc/apple.md +++ b/doc/apple.md @@ -62,20 +62,20 @@ xlog.Debugf("payload.Data: %+v", payload.Data) bs1, _ := json.Marshal(payload) xlog.Color(xlog.RedBright).Info(string(bs1)) /* - { - "notificationType":"DID_RENEW", - "subtype":"", - "notificationUUID":"469bf30e-7715-4f9f-aae3-a7bfc12aea77", - "notificationVersion":"", - "data":{ - "appAppleId":0, - "bundleId":"com.audaos.audarecorder", - "bundleVersion":"7", - "environment":"Sandbox", - "signedRenewalInfo":"xxxxxxxxxx", - "signedTransactionInfo":"xxxxxxxxxxx" - } - } + { + "notificationType":"DID_RENEW", + "subtype":"", + "notificationUUID":"469bf30e-7715-4f9f-aae3-a7bfc12aea77", + "notificationVersion":"", + "data":{ + "appAppleId":0, + "bundleId":"com.audaos.audarecorder", + "bundleVersion":"7", + "environment":"Sandbox", + "signedRenewalInfo":"xxxxxxxxxx", + "signedTransactionInfo":"xxxxxxxxxxx" + } + } */ // decode renewalInfo @@ -88,19 +88,19 @@ xlog.Debugf("data.renewalInfo: %+v", renewalInfo) bs, _ := json.Marshal(renewalInfo) xlog.Color(xlog.GreenBright).Info(string(bs)) /* - { - "autoRenewProductId":"com.audaos.audarecorder.vip.m2", - "autoRenewStatus":1, - "expirationIntent":0, - "gracePeriodExpiresDate":0, - "isInBillingRetryPeriod":false, - "offerIdentifier":"", - "offerType":0, - "originalTransactionId":"2000000000842607", - "priceIncreaseStatus":0, - "productId":"com.audaos.audarecorder.vip.m2", - "signedDate":1646387008228 - } + { + "autoRenewProductId":"com.audaos.audarecorder.vip.m2", + "autoRenewStatus":1, + "expirationIntent":0, + "gracePeriodExpiresDate":0, + "isInBillingRetryPeriod":false, + "offerIdentifier":"", + "offerType":0, + "originalTransactionId":"2000000000842607", + "priceIncreaseStatus":0, + "productId":"com.audaos.audarecorder.vip.m2", + "signedDate":1646387008228 + } */ // decode transactionInfo @@ -113,26 +113,26 @@ xlog.Debugf("data.transactionInfo: %+v", transactionInfo) bs2, _ := json.Marshal(transactionInfo) xlog.Color(xlog.YellowBright).Info(string(bs2)) /* - { - "appAccountToken":"", - "bundleId":"com.audaos.audarecorder", - "expiresDate":1646387196000, - "inAppOwnershipType":"PURCHASED", - "isUpgraded":false, - "offerIdentifier":"", - "offerType":0, - "originalPurchaseDate":1646046037000, - "originalTransactionId":"2000000000842607", - "productId":"com.audaos.audarecorder.vip.m2", - "purchaseDate":1646387016000, - "quantity":1, - "revocationDate":0, - "revocationReason":"", - "signedDate":1646387008254, - "subscriptionGroupIdentifier":"20929536", - "transactionId":"2000000004047119", - "type":"Auto-Renewable Subscription", - "webOrderLineItemId":"2000000000302832" - } +{ + "appAccountToken":"", + "bundleId":"com.audaos.audarecorder", + "expiresDate":1646387196000, + "inAppOwnershipType":"PURCHASED", + "isUpgraded":false, + "offerIdentifier":"", + "offerType":0, + "originalPurchaseDate":1646046037000, + "originalTransactionId":"2000000000842607", + "productId":"com.audaos.audarecorder.vip.m2", + "purchaseDate":1646387016000, + "quantity":1, + "revocationDate":0, + "revocationReason":"", + "signedDate":1646387008254, + "subscriptionGroupIdentifier":"20929536", + "transactionId":"2000000004047119", + "type":"Auto-Renewable Subscription", + "webOrderLineItemId":"2000000000302832" +} */ ``` diff --git a/release_note.txt b/release_note.txt index d7bfb96b..69b90473 100644 --- a/release_note.txt +++ b/release_note.txt @@ -1,3 +1,9 @@ +版本号:Release 1.5.79 +修改记录: + (1) PayPal:增加订阅相关的v1接口 + (2) 微信V3:更新部分接口上注释的文档地址 + (3) 支付宝:接口请求中,如下公共参数(version、return_url、notify_url、app_auth_token)支持 BodyMap 中此次请求自定义设置 + 版本号:Release 1.5.78 修改记录: (1) 微信V3:V3EcommerceBalance() 缺失参数补充