-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from alipay/fearure-250106
Fearure 250106
- Loading branch information
Showing
11 changed files
with
235 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package model | ||
|
||
type Discount struct { | ||
DiscountName string `json:"discountName,omitempty"` | ||
SavingsAmount Amount `json:"savingsAmount,omitempty"` | ||
DiscountTag string `json:"discountTag,omitempty"` | ||
DiscountName string `json:"discountName,omitempty"` | ||
SavingsAmount Amount `json:"savingsAmount,omitempty"` | ||
EstimateSavingsAmount Amount `json:"estimateSavingsAmount,omitempty"` | ||
} |
25 changes: 25 additions & 0 deletions
25
com/alipay/api/request/auth/AlipayAuthCreateSessionRequest.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/alipay/global-open-sdk-go/com/alipay/api/model" | ||
"github.com/alipay/global-open-sdk-go/com/alipay/api/request" | ||
responseAuth "github.com/alipay/global-open-sdk-go/com/alipay/api/response/auth" | ||
) | ||
|
||
type AlipayAuthCreateSessionRequest struct { | ||
ProductCode model.ProductCodeType `json:"productCode,omitempty"` | ||
AgreementInfo *model.AgreementInfo `json:"agreementInfo,omitempty"` | ||
Scopes []model.ScopeType `json:"scopes,omitempty"` | ||
PaymentMethod *model.PaymentMethod `json:"paymentMethod,omitempty"` | ||
PaymentRedirectUrl string `json:"paymentRedirectUrl,omitempty"` | ||
} | ||
|
||
func (alipayAuthCreateSessionRequest *AlipayAuthCreateSessionRequest) NewRequest() *request.AlipayRequest { | ||
return request.NewAlipayRequest(&alipayAuthCreateSessionRequest, model.CREATE_SESSION_PATH, &responseAuth.AlipayAuthCreateSessionResponse{}) | ||
} | ||
|
||
func NewAlipayAuthCreateSessionRequest() (*request.AlipayRequest, *AlipayAuthCreateSessionRequest) { | ||
alipayAuthCreateSessionRequest := &AlipayAuthCreateSessionRequest{} | ||
alipayRequest := request.NewAlipayRequest(alipayAuthCreateSessionRequest, model.CREATE_SESSION_PATH, &responseAuth.AlipayAuthCreateSessionResponse{}) | ||
return alipayRequest, alipayAuthCreateSessionRequest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
com/alipay/api/request/subscription/AlipaySubscriptionUpdateRequest.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package subscription | ||
|
||
import ( | ||
"github.com/alipay/global-open-sdk-go/com/alipay/api/model" | ||
"github.com/alipay/global-open-sdk-go/com/alipay/api/request" | ||
responseSubscription "github.com/alipay/global-open-sdk-go/com/alipay/api/response/subscription" | ||
) | ||
|
||
type AlipaySubscriptionUpdateRequest struct { | ||
SubscriptionUpdateRequestId string `json:"subscriptionUpdateRequestId,omitempty"` | ||
SubscriptionId string `json:"subscriptionId,omitempty"` | ||
SubscriptionDescription string `json:"subscriptionDescription,omitempty"` | ||
PeriodRule *model.PeriodRule `json:"periodRule,omitempty"` | ||
PaymentAmount *model.Amount `json:"paymentAmount,omitempty"` | ||
SubscriptionEndTime string `json:"subscriptionEndTime,omitempty"` | ||
OrderInfo *model.OrderInfo `json:"orderInfo,omitempty"` | ||
} | ||
|
||
func (alipaySubscriptionUpdateRequest *AlipaySubscriptionUpdateRequest) NewRequest() *request.AlipayRequest { | ||
return request.NewAlipayRequest(&alipaySubscriptionUpdateRequest, model.SUBSCRIPTION_UPDATE_PATH, &responseSubscription.AlipaySubscriptionUpdateResponse{}) | ||
} |
10 changes: 10 additions & 0 deletions
10
com/alipay/api/response/auth/AlipayAuthCreateSessionResponse.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package responseAuth | ||
|
||
import "github.com/alipay/global-open-sdk-go/com/alipay/api/response" | ||
|
||
type AlipayAuthCreateSessionResponse struct { | ||
response.AlipayResponse | ||
PaymentSessionId string `json:"paymentSessionId,omitempty"` | ||
PaymentSessionData string `json:"paymentSessionData,omitempty"` | ||
PaymentSessionExpiryTime string `json:"paymentSessionExpiryTime,omitempty"` | ||
} |
7 changes: 7 additions & 0 deletions
7
com/alipay/api/response/subscription/AlipaySubscriptionUpdateResponse.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package responseSubscription | ||
|
||
import "github.com/alipay/global-open-sdk-go/com/alipay/api/response" | ||
|
||
type AlipaySubscriptionUpdateResponse struct { | ||
response.AlipayResponse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package tools | ||
|
||
import ( | ||
"crypto" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/sha256" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"encoding/pem" | ||
"errors" | ||
"github.com/alipay/global-open-sdk-go/com/alipay/api/exception" | ||
"net/url" | ||
) | ||
|
||
func GenSign(httpMethod string, path string, clientId string, reqTime string, reqBody string, merchantPrivateKey string) (string, error) { | ||
block, _ := pem.Decode([]byte(getPkcsKey(merchantPrivateKey))) | ||
if block == nil { | ||
return "", &exception.AlipayLibraryError{Message: "Failed to decode private key"} | ||
} | ||
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes) | ||
if err != nil { | ||
return "", &exception.AlipayLibraryError{Message: "Failed to parse private key " + err.Error()} | ||
} | ||
payload := genSignContent(httpMethod, path, clientId, reqTime, reqBody) | ||
signature, err := Sign(privateKey.(*rsa.PrivateKey), []byte(payload)) | ||
if err != nil { | ||
return "", &exception.AlipayLibraryError{Message: "Failed to sign data " + err.Error()} | ||
} | ||
|
||
return signature, nil | ||
|
||
} | ||
|
||
func Sign(privateKey *rsa.PrivateKey, data []byte) (string, error) { | ||
// 计算数据的SHA256哈希 | ||
hashed := sha256.Sum256(data) | ||
|
||
// 使用私钥签名哈希值 | ||
signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.Hash.HashFunc(crypto.SHA256), hashed[:]) | ||
if err != nil { | ||
return "", err | ||
} | ||
//base64编码 如果signature直接传string会造成乱码 | ||
base64Signature := base64.StdEncoding.EncodeToString(signature) | ||
return Encode(base64Signature) | ||
} | ||
|
||
func Verify(httpMethod string, path string, clientId string, rspTimeStr string, rspBody string, signature string, alipayPublicKey string) (bool, error) { | ||
rspContent := genSignContent(httpMethod, path, clientId, rspTimeStr, rspBody) | ||
return verifySignatureWithSHA256RSA(rspContent, Decode(signature), alipayPublicKey) | ||
} | ||
|
||
func verifySignatureWithSHA256RSA(rspContent string, signature string, strPk string) (bool, error) { | ||
publicKey, err := getPublicKeyFromBase64String(strPk) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
hash := sha256.New() | ||
hash.Write([]byte(rspContent)) | ||
digest := hash.Sum(nil) | ||
|
||
signatureBytes, err := base64.StdEncoding.DecodeString(signature) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, digest, signatureBytes) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func getPublicKeyFromBase64String(publicKeyString string) (*rsa.PublicKey, error) { | ||
keyBytes, err := base64.StdEncoding.DecodeString(publicKeyString) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pub, err := x509.ParsePKIXPublicKey(keyBytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch pub := pub.(type) { | ||
case *rsa.PublicKey: | ||
return pub, nil | ||
default: | ||
return nil, errors.New("not an RSA public key") | ||
} | ||
} | ||
|
||
func genSignContent(httpMethod string, path string, clientId string, reqTime string, reqBody string) string { | ||
return httpMethod + " " + path + "\n" + clientId + "." + reqTime + "." + reqBody | ||
} | ||
|
||
func Encode(signature string) (string, error) { | ||
return url.QueryEscape(signature), nil | ||
} | ||
|
||
func Decode(originalStr string) string { | ||
unescape, err := url.QueryUnescape(originalStr) | ||
if err != nil { | ||
return "" | ||
} | ||
return unescape | ||
} | ||
|
||
func getPkcsKey(key string) string { | ||
return "-----BEGIN PRIVATE KEY-----\n" + key + "\n-----END PRIVATE KEY-----" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tools | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
func CheckSignature(requestUri, httpMethod, clientId, requestTime, responseBody, signature, alipayPublicKey string) (bool, error) { | ||
realSignature := "" | ||
|
||
// Check if signature is nil or empty | ||
if signature == "" { | ||
return false, errors.New("empty notify signature") | ||
} | ||
|
||
// Get valid part from raw signature | ||
parts := strings.Split(signature, "signature=") | ||
if len(parts) > 1 { | ||
realSignature = parts[1] | ||
} | ||
|
||
// Verify signature | ||
isValid, _ := Verify(httpMethod, requestUri, clientId, requestTime, responseBody, realSignature, alipayPublicKey) | ||
if !isValid { | ||
return false, errors.New("signature verification failed") | ||
} | ||
|
||
return true, nil | ||
} |