Jerry пре 6 година
родитељ
комит
f753c1d167
2 измењених фајлова са 12 додато и 5 уклоњено
  1. 5 4
      alipay/client.go
  2. 7 1
      alipay/model.go

+ 5 - 4
alipay/client.go

@@ -13,6 +13,7 @@ import (
 
 type Client struct {
 	AppId              string
+	PrivateKeyType     PKCSType
 	PrivateKey         string
 	LocationName       string
 	AppCertSN          string
@@ -32,8 +33,8 @@ type Client struct {
 // 初始化支付宝客户端
 //    注意:如果使用支付宝公钥证书验签,请设置 支付宝根证书SN(client.SetAlipayRootCertSN())、应用公钥证书SN(client.SetAppCertSN())
 //    appId:应用ID
-//    PrivateKey:应用私钥
-//    IsProd:是否是正式环境
+//    privateKey:应用私钥,支持PKCS1和PKCS8
+//    isProd:是否是正式环境
 func NewClient(appId, privateKey string, isProd bool) (client *Client) {
 	return &Client{
 		AppId:      appId,
@@ -691,7 +692,7 @@ func (a *Client) doAliPay(bm gopay.BodyMap, method string) (bs []byte, err error
 		a.mu.RUnlock()
 	}
 	if a.SignType == gopay.NULL {
-		pubBody.Set("sign_type", "RSA2")
+		pubBody.Set("sign_type", RSA2)
 	} else {
 		a.mu.RLock()
 		pubBody.Set("sign_type", a.SignType)
@@ -723,7 +724,7 @@ func (a *Client) doAliPay(bm gopay.BodyMap, method string) (bs []byte, err error
 	if bodyStr != gopay.NULL {
 		pubBody.Set("biz_content", bodyStr)
 	}
-	sign, err := GetRsaSign(pubBody, pubBody.Get("sign_type"), FormatPrivateKey(a.PrivateKey))
+	sign, err := GetRsaSign(pubBody, pubBody.Get("sign_type"), a.PrivateKeyType, a.PrivateKey)
 	if err != nil {
 		return
 	}

+ 7 - 1
alipay/model.go

@@ -7,9 +7,15 @@ const (
 	baseUrlUtf8        = "https://openapi.alipay.com/gateway.do?charset=utf-8"
 	sandboxBaseUrlUtf8 = "https://openapi.alipaydev.com/gateway.do?charset=utf-8"
 
-	locationShanghai = "Asia/Shanghai"
+	locationShanghai          = "Asia/Shanghai"
+	PKCS1            PKCSType = 1
+	PKCS8            PKCSType = 2
+	RSA                       = "RSA"
+	RSA2                      = "RSA2"
 )
 
+type PKCSType uint8
+
 type NotifyRequest struct {
 	NotifyTime        string                   `json:"notify_time,omitempty"`
 	NotifyType        string                   `json:"notify_type,omitempty"`