Skip to content

Commit

Permalink
V1.5.56 (#176)
Browse files Browse the repository at this point in the history
* big change

* v1.5.56
  • Loading branch information
iGoogle-ink authored Aug 21, 2021
1 parent ca17561 commit 3bb5e0b
Show file tree
Hide file tree
Showing 57 changed files with 563 additions and 660 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@ func main() {

---

### Apple Pay 支付校验收据

* [苹果校验收据文档](https://developer.apple.com/documentation/appstorereceipts/verifyreceipt)

> url 请选择 apple.UrlSandbox 或 apple.UrlProd
* `apple.VerifyReceipt()` => 苹果支付校验收据API

---

# 二、文档说明

* [GoPay 文档地址](https://pkg.go.dev/github.com/go-pay/gopay)
Expand Down Expand Up @@ -390,8 +400,8 @@ import (
// mchid:商户ID 或者服务商模式的 sp_mchid
// serialNo:商户证书的证书序列号
// apiV3Key:apiV3Key,商户平台获取
// pkContent:私钥 apiclient_key.pem 读取后的内容
client, err = wechat.NewClientV3(MchId, SerialNo, APIv3Key, PKContent)
// privateKey:私钥 apiclient_key.pem 读取后的内容
client, err = wechat.NewClientV3(MchId, SerialNo, APIv3Key, PrivateKey)
if err != nil {
xlog.Error(err)
return
Expand Down Expand Up @@ -432,7 +442,6 @@ client.DebugSwitch = gopay.DebugOn
// 设置支付宝请求 公共参数
// 注意:具体设置哪些参数,根据不同的方法而不同,此处列举出所有设置参数
client.SetLocation(). // 设置时区,不设置或出错均为默认服务器时间
SetPrivateKeyType(). // 设置 支付宝 私钥类型,alipay.PKCS1 或 alipay.PKCS8,默认 PKCS1
SetAliPayRootCertSN(). // 设置支付宝根证书SN,通过 alipay.GetRootCertSN() 获取
SetAppCertSN(). // 设置应用公钥证书SN,通过 alipay.GetCertSN() 获取
SetAliPayPublicCertSN(). // 设置支付宝公钥证书SN,通过 alipay.GetCertSN() 获取
Expand Down
56 changes: 31 additions & 25 deletions alipay/client.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package alipay

import (
"crypto/rsa"
"encoding/json"
"fmt"
"sync"
"time"

"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/util"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/gopay/pkg/xlog"
"github.com/go-pay/gopay/pkg/xpem"
"github.com/go-pay/gopay/pkg/xrsa"
)

type Client struct {
AppId string
PrivateKeyType PKCSType
PrivateKey string
LocationName string
AppCertSN string
AliPayPublicCertSN string
Expand All @@ -26,33 +26,43 @@ type Client struct {
SignType string
AppAuthToken string
IsProd bool
aliPayPkContent []byte // 支付宝证书公钥内容 alipayCertPublicKey_RSA2.crt
privateKey *rsa.PrivateKey
aliPayPublicKey *rsa.PublicKey // 支付宝证书公钥内容 alipayCertPublicKey_RSA2.crt
autoSign bool
DebugSwitch gopay.DebugSwitch
location *time.Location
mu sync.RWMutex
}

// 初始化支付宝客户端
// 注意:如果使用支付宝公钥证书验签,请设置 支付宝根证书SN(client.SetAlipayRootCertSN())、应用公钥证书SN(client.SetAppCertSN())
// appId:应用ID
// privateKey:应用私钥,支持PKCS1和PKCS8
// isProd:是否是正式环境
func NewClient(appId, privateKey string, isProd bool) (client *Client) {
return &Client{
func NewClient(appId, privateKey string, isProd bool) (client *Client, err error) {
key := xrsa.FormatAlipayPrivateKey(privateKey)
priKey, err := xpem.DecodePrivateKey([]byte(key))
if err != nil {
return nil, err
}
client = &Client{
AppId: appId,
PrivateKey: privateKey,
privateKey: priKey,
IsProd: isProd,
DebugSwitch: gopay.DebugOff,
}
return client, nil
}

// 开启请求完自动验签功能(默认不开启,推荐开启,只支持证书模式)
// 注意:只支持证书模式
// aliPayPkContent:支付宝公钥证书文件内容[]byte
func (a *Client) AutoVerifySign(aliPayPkContent []byte) {
if aliPayPkContent != nil {
a.aliPayPkContent = aliPayPkContent
// alipayPublicKeyContent:支付宝公钥证书文件内容[]byte
func (a *Client) AutoVerifySign(alipayPublicKeyContent []byte) {
pubKey, err := xpem.DecodePublicKey(alipayPublicKeyContent)
if err != nil {
xlog.Errorf("AutoVerifySign(%s),err:%+v", alipayPublicKeyContent, err)
}
if pubKey != nil {
a.aliPayPublicKey = pubKey
a.autoSign = true
}
}
Expand Down Expand Up @@ -100,7 +110,7 @@ func (a *Client) RequestParam(bm gopay.BodyMap, method string) (string, error) {

// check sign
if bm.GetString("sign") == "" {
sign, err = GetRsaSign(bm, bm.GetString("sign_type"), a.PrivateKeyType, a.PrivateKey)
sign, err = GetRsaSign(bm, bm.GetString("sign_type"), a.privateKey)
if err != nil {
return "", fmt.Errorf("GetRsaSign Error: %v", err)
}
Expand All @@ -111,8 +121,7 @@ func (a *Client) RequestParam(bm gopay.BodyMap, method string) (string, error) {
req, _ := json.Marshal(bm)
xlog.Debugf("Alipay_Request: %s", req)
}
param := FormatURLParam(bm)
return param, nil
return bm.EncodeURLParams(), nil
}

// PostAliPayAPISelfV2 支付宝接口自行实现方法
Expand Down Expand Up @@ -146,32 +155,28 @@ func (a *Client) doAliPaySelf(bm gopay.BodyMap, method string) (bs []byte, err e
url, sign string
)
bm.Set("method", method)

// check public parameter
a.checkPublicParam(bm)

// check sign
if bm.GetString("sign") == "" {
sign, err = GetRsaSign(bm, bm.GetString("sign_type"), a.PrivateKeyType, a.PrivateKey)
sign, err = GetRsaSign(bm, bm.GetString("sign_type"), a.privateKey)
if err != nil {
return nil, fmt.Errorf("GetRsaSign Error: %v", err)
}
bm.Set("sign", sign)
}

if a.DebugSwitch == gopay.DebugOn {
req, _ := json.Marshal(bm)
xlog.Debugf("Alipay_Request: %s", req)
}
param := FormatURLParam(bm)

httpClient := xhttp.NewClient()
if a.IsProd {
url = baseUrlUtf8
} else {
url = sandboxBaseUrlUtf8
}
res, bs, errs := httpClient.Type(xhttp.TypeForm).Post(url).SendString(param).EndBytes()
res, bs, errs := httpClient.Type(xhttp.TypeForm).Post(url).SendString(bm.EncodeURLParams()).EndBytes()
if len(errs) > 0 {
return nil, errs[0]
}
Expand Down Expand Up @@ -229,10 +234,11 @@ func (a *Client) doAliPay(bm gopay.BodyMap, method string, authToken ...string)
if a.NotifyUrl != util.NULL {
pubBody.Set("notify_url", a.NotifyUrl)
}
if a.AppAuthToken != util.NULL {
pubBody.Set("app_auth_token", a.AppAuthToken)
}
if aat != util.NULL {
pubBody.Set("app_auth_token", aat)
} else if a.AppAuthToken != util.NULL {
pubBody.Set("app_auth_token", a.AppAuthToken)
}
if method == "alipay.user.info.share" {
pubBody.Set("auth_token", authToken[0])
Expand All @@ -241,7 +247,7 @@ func (a *Client) doAliPay(bm gopay.BodyMap, method string, authToken ...string)
if bodyStr != util.NULL {
pubBody.Set("biz_content", bodyStr)
}
sign, err := GetRsaSign(pubBody, pubBody.GetString("sign_type"), a.PrivateKeyType, a.PrivateKey)
sign, err := GetRsaSign(pubBody, pubBody.GetString("sign_type"), a.privateKey)
if err != nil {
return nil, fmt.Errorf("GetRsaSign Error: %v", err)
}
Expand All @@ -250,7 +256,7 @@ func (a *Client) doAliPay(bm gopay.BodyMap, method string, authToken ...string)
req, _ := json.Marshal(pubBody)
xlog.Debugf("Alipay_Request: %s", req)
}
param := FormatURLParam(pubBody)
param := pubBody.EncodeURLParams()
switch method {
case "alipay.trade.app.pay", "alipay.fund.auth.order.app.freeze":
return []byte(param), nil
Expand Down
15 changes: 9 additions & 6 deletions alipay/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var (
client *Client

err error
// 普通公钥模式时,验签使用
//aliPayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1wn1sU/8Q0rYLlZ6sq3enrPZw2ptp6FecHR2bBFLjJ+sKzepROd0bKddgj+Mr1ffr3Ej78mLdWV8IzLfpXUi945DkrQcOUWLY0MHhYVG2jSs/qzFfpzmtut2Cl2TozYpE84zom9ei06u2AXLMBkU6VpznZl+R4qIgnUfByt3Ix5b3h4Cl6gzXMAB1hJrrrCkq+WvWb3Fy0vmk/DUbJEz8i8mQPff2gsHBE1nMPvHVAMw1GMk9ImB4PxucVek4ZbUzVqxZXphaAgUXFK2FSFU+Q+q1SPvHbUsjtIyL+cLA6H/6ybFF9Ffp27Y14AHPw29+243/SpMisbGcj2KD+evBwIDAQAB"
)
Expand All @@ -23,17 +23,20 @@ func TestMain(m *testing.M) {
// appId:应用ID
// privateKey:应用私钥,支持PKCS1和PKCS8
// isProd:是否是正式环境
client = NewClient(cert.Appid, cert.PrivateKey, false)

client, err = NewClient(cert.Appid, cert.PrivateKey, false)
if err != nil {
xlog.Error(err)
return
}
// 打开Debug开关,输出日志
client.DebugSwitch = gopay.DebugOn

// 配置公共参数
client.SetCharset("utf-8").
SetSignType(RSA2).
SetPrivateKeyType(PKCS1)
// SetReturnUrl("https://www.fmm.ink").
// SetNotifyUrl("https://www.fmm.ink")
// SetAppAuthToken("")
SetReturnUrl("https://www.fmm.ink").
SetNotifyUrl("https://www.fmm.ink")

// 自动同步验签(只支持证书模式)
// 传入 alipayCertPublicKey_RSA2.crt 内容
Expand Down
Loading

0 comments on commit 3bb5e0b

Please sign in to comment.