Skip to content

Commit

Permalink
feature# AliPayClient 增加 RSAType 属性
Browse files Browse the repository at this point in the history
  • Loading branch information
huangxing committed Aug 27, 2019
1 parent 73008e3 commit 4c4c806
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
38 changes: 33 additions & 5 deletions client/alipay.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/milkbobo/gopay/common"
"hash"
"net/url"
"sort"
"strings"
Expand All @@ -21,6 +23,8 @@ type AliPayClient struct {

PrivateKey *rsa.PrivateKey
PublicKey *rsa.PublicKey

RSAType string // RSA or RSA2
}

func (this *AliPayClient) PayToClient(charge *common.Charge) (map[string]string, error) {
Expand All @@ -33,7 +37,7 @@ func (this *AliPayClient) PayToClient(charge *common.Charge) (map[string]string,
m["charset"] = "utf-8"
m["timestamp"] = time.Now().Format("2006-01-02 15:04:05")
m["version"] = "1.0"
m["sign_type"] = "RSA2"
m["sign_type"] = this.RSAType

bizContent["out_biz_no"] = charge.TradeNum
bizContent["amount"] = AliyunMoneyFeeToString(charge.MoneyFee)
Expand Down Expand Up @@ -82,7 +86,8 @@ func (this *AliPayClient) GenSign(m map[string]string) string {
sort.Strings(data)
signData := strings.Join(data, "&")

s := sha256.New()
s := this.getHash(this.RSAType)

_, err := s.Write([]byte(signData))
if err != nil {
panic(err)
Expand All @@ -102,13 +107,13 @@ func (this *AliPayClient) CheckSign(signData, sign string) {
if err != nil {
panic(err)
}
s := sha256.New()
s := this.getHash(this.RSAType)
_, err = s.Write([]byte(signData))
if err != nil {
panic(err)
}
hash := s.Sum(nil)
err = rsa.VerifyPKCS1v15(this.PublicKey, crypto.SHA256, hash, signByte)
hashByte := s.Sum(nil)
err = rsa.VerifyPKCS1v15(this.PublicKey, this.getCrypto(), hashByte, signByte)
if err != nil {
panic(err)
}
Expand All @@ -122,3 +127,26 @@ func (this *AliPayClient) ToURL(m map[string]string) string {
}
return strings.Join(buf, "&")
}

func (this *AliPayClient) getRsa() string {
if this.RSAType == "" {
this.RSAType = "RSA"
}

return this.RSAType
}

func (this *AliPayClient) getCrypto() crypto.Hash {
if this.RSAType == "RSA2" {
return crypto.SHA256
}
return crypto.SHA1

}

func (this *AliPayClient) getHash(rasType string) hash.Hash {
if rasType == "RSA2" {
return sha256.New()
}
return sha1.New()
}
4 changes: 2 additions & 2 deletions client/alipayApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (this *AliAppClient) Pay(charge *common.Charge) (map[string]string, error)
m["timestamp"] = time.Now().Format("2006-01-02 15:04:05")
m["version"] = "1.0"
m["notify_url"] = charge.CallbackURL
m["sign_type"] = "RSA2"
m["sign_type"] = this.AliPayClient.RSAType
bizContent["subject"] = TruncatedText(charge.Describe, 32)
bizContent["out_trade_no"] = charge.TradeNum
bizContent["product_code"] = "QUICK_MSECURITY_PAY"
Expand Down Expand Up @@ -71,7 +71,7 @@ func (this *AliAppClient) QueryOrder(outTradeNo string) (common.AliWebAppQueryRe
m["charset"] = "utf-8"
m["timestamp"] = time.Now().Format("2006-01-02 15:04:05")
m["version"] = "1.0"
m["sign_type"] = "RSA2"
m["sign_type"] = this.AliPayClient.RSAType
bizContent := map[string]string{"out_trade_no": outTradeNo}
bizContentJson, err := json.Marshal(bizContent)
if err != nil {
Expand Down

0 comments on commit 4c4c806

Please sign in to comment.