Skip to content

Commit

Permalink
v1.5.79
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed May 24, 2022
1 parent ea0a6e4 commit ea18a6a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 92 deletions.
91 changes: 55 additions & 36 deletions alipay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
}

// 公共参数检查
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 = "1.5.78"
Version = "1.5.79"
)

type DebugSwitch int8
16 changes: 9 additions & 7 deletions doc/alipay.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
```

Expand Down
96 changes: 48 additions & 48 deletions doc/apple.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
}
*/
```
6 changes: 6 additions & 0 deletions release_note.txt
Original file line number Diff line number Diff line change
@@ -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() 缺失参数补充
Expand Down

0 comments on commit ea18a6a

Please sign in to comment.