-
Notifications
You must be signed in to change notification settings - Fork 4
/
binancepay_test.go
157 lines (137 loc) · 6.55 KB
/
binancepay_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Created by Du, Chengbin on 2022/6/14.
*/
package binancepay
import (
"context"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestUnmarshalRequest(t *testing.T) {
body := `{
"bizType": "PAY",
"data": "{\"merchantTradeNo\":\"9825382937292\",\"totalFee\":0.88000000,\"transactTime\":1619508939664,\"currency\":\"BUSD\",\"openUserId\":\"1211HS10K81f4273ac031\",\"productType\":\"Food\",\"productName\":\"Ice Cream\",\"tradeType\":\"WEB\",\"transactionId\":\"M_R_282737362839373\"}",
"bizId": 29383937493038367292,
"bizStatus": "PAY_SUCCESS"
}`
var req webhookRawReq
err := json.Unmarshal([]byte(body), &req)
assert.Nil(t, err, err)
fmt.Println("bizType", req.BizType)
fmt.Println("bizId", req.BizId)
}
type fixedPublicKeyTestCache struct {
exist bool
publicKey string
}
func (c fixedPublicKeyTestCache) GetJSON(ctx context.Context, key string, i interface{}) (ok bool, err error) {
if !c.exist {
return false, nil
}
cert, _ := i.(*Certificate)
cert.CertSerial = "abc"
cert.CertPublic = c.publicKey
return true, nil
}
func (c fixedPublicKeyTestCache) SetJSON(ctx context.Context, key string, data interface{}, dur time.Duration) error {
return nil
}
func TestWebhookWithCachedPublicKey(t *testing.T) {
client := NewMerchant("", "", fixedPublicKeyTestCache{exist: true, publicKey: testDataPublicKey}, logger)
body := `{"bizType":"PAY","bizId":"111","data":"testdata","bizStatus":"SUCCESS"}`
httpReq, err := http.NewRequest("POST", "/", strings.NewReader(body))
assert.Nil(t, err, err)
timestamp := "1654943252000"
nonce := "abc"
h := http.Header{}
h.Set("BinancePay-Certificate-SN", "")
h.Set("BinancePay-Nonce", nonce)
h.Set("BinancePay-Timestamp", timestamp)
h.Set("BinancePay-Signature", "UVVd2NnJMjJRKHbM+e4544nHi2AgToklZdoNs9Gx4GdQPRsm0llIaWPKMM7JFG5bQV9pId/4z2T/wObPhRlDy8wa7UI2rWHpnCq1M48skeZjSzAauUr0u/jLV5BP9atbr9AKzFoAfMeLdpa0knaX3nOiutXzDZjgrb953fhFd//J7fEUN8pCTSXJXUtXUlXEH6bPePpRaiEeNMBC3tnTIQR93dZWpxHPE8LUkrSFAqxdS6m1Byd1b+6ebuDnUjgNjkBEuN04W7tCbirOYcEXRP67bLirc67DTX/k17mXYhdJrVDlobubDMFg3L9ZkOndDDij99DROtonW9SaNpoBxVsoB4fKr0aRaawxMzlUoDwjkuNb0JKOnj/MHB21N4R5qssCgEtAcs3+PcbzBZ943nF03Rne+wEK99+OEzzyzTXOTlmefErOYVxamn8/gnWkXAgvKILyR1S3ldoHOcb6SPWtociEr75z0JUoHpibjmp/WExVSKrSoRJg+JIB3dw3bNoNhdbR41SBSejuk58UPJSa+qcW1tpGj9F057o4sP5knjBoV1q15pFzZRv+qDa8BUSoaNdKrkR7hpCFW+DmKvjYHzD+8Fkr7XdANcEUEdz/0DpRCuhWEAkKtRDzYlVCnuj0cvQ+ZcIZ7jhR7hPYw55jY6rLWGz9FHEtxJRQjtg=")
h.Set("Content-Type", "application/json; charset=utf-8")
httpReq.Header = h
rawReq, err := client.VerifyAndParseWebhookRequest(httpReq)
assert.Nil(t, err, err)
assert.Equal(t, "testdata", rawReq.RawData)
}
func TestWebhookWithNoCachedPublicKey(t *testing.T) {
client := NewMerchant("", "", fixedPublicKeyTestCache{exist: false}, logger)
body := `{"bizType":"PAY","bizId":"111","data":"testdata","bizStatus":"SUCCESS"}`
httpReq, err := http.NewRequest("POST", "/", strings.NewReader(body))
assert.Nil(t, err, err)
timestamp := "1654943252000"
nonce := "abc"
h := http.Header{}
h.Set("BinancePay-Certificate-SN", "")
h.Set("BinancePay-Nonce", nonce)
h.Set("BinancePay-Timestamp", timestamp)
h.Set("BinancePay-Signature", "UVVd2NnJMjJRKHbM+e4544nHi2AgToklZdoNs9Gx4GdQPRsm0llIaWPKMM7JFG5bQV9pId/4z2T/wObPhRlDy8wa7UI2rWHpnCq1M48skeZjSzAauUr0u/jLV5BP9atbr9AKzFoAfMeLdpa0knaX3nOiutXzDZjgrb953fhFd//J7fEUN8pCTSXJXUtXUlXEH6bPePpRaiEeNMBC3tnTIQR93dZWpxHPE8LUkrSFAqxdS6m1Byd1b+6ebuDnUjgNjkBEuN04W7tCbirOYcEXRP67bLirc67DTX/k17mXYhdJrVDlobubDMFg3L9ZkOndDDij99DROtonW9SaNpoBxVsoB4fKr0aRaawxMzlUoDwjkuNb0JKOnj/MHB21N4R5qssCgEtAcs3+PcbzBZ943nF03Rne+wEK99+OEzzyzTXOTlmefErOYVxamn8/gnWkXAgvKILyR1S3ldoHOcb6SPWtociEr75z0JUoHpibjmp/WExVSKrSoRJg+JIB3dw3bNoNhdbR41SBSejuk58UPJSa+qcW1tpGj9F057o4sP5knjBoV1q15pFzZRv+qDa8BUSoaNdKrkR7hpCFW+DmKvjYHzD+8Fkr7XdANcEUEdz/0DpRCuhWEAkKtRDzYlVCnuj0cvQ+ZcIZ7jhR7hPYw55jY6rLWGz9FHEtxJRQjtg=")
h.Set("Content-Type", "application/json; charset=utf-8")
httpReq.Header = h
{
req := &QueryCertificateRequest{}
expectedResp := Response[QueryCertificateResult]{
Status: "SUCCESS",
Code: "1",
Data: []Certificate{
{
CertSerial: "abc",
CertPublic: testDataPublicKey,
},
},
ErrMsg: "",
}
client.httpClient = mockHttpClientWithAsserts(t, "POST", "/binancepay/openapi/certificates", req, expectedResp)
}
rawReq, err := client.VerifyAndParseWebhookRequest(httpReq)
assert.Nil(t, err, err)
assert.Equal(t, "testdata", rawReq.RawData)
}
func TestWebhook(t *testing.T) {
pk := `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVAMXVTMIuttHcP1kvSk9V39S7
BqDbY+LC9JfwMzyoubo3fdsqBf4EmB1PfBUigcOL5YBqMKGEE6xmVw6SXCTH4JwX
dqJ4IRiXf76YMt6PB0dMzu/qCmhaGFGkqT6vJM0hHyEbtS/P3FR9ZU+eaOvynLWb
FvXTzIoctZM1IvDrCwIDAQAB
-----END PUBLIC KEY-----`
client := NewMerchant("", "", fixedPublicKeyTestCache{exist: true, publicKey: pk}, logger)
body := `{"bizType":"PAY","data":"{\"merchantTradeNo\":\"9825382937292\",\"totalFee\":0.88000000,\"transactTime\":1619508939664,\"currency\":\"EUR\",\"commission\":0,\"openUserId\":\"1211HS10K81f4273ac031\",\"productType\":\"Food\",\"productName\":\"Ice Cream\",\"tradeType\":\"WEB\",\"transactionId\":\"M_R_282737362839373\"}","bizId":29383937493038367292,"bizStatus":"PAY_SUCCESS"}`
httpReq, err := http.NewRequest("POST", "/", strings.NewReader(body))
assert.Nil(t, err, err)
timestamp := "1611232922428"
nonce := "5RhaTrZPhknNv0kDSA2UQ67cPMVNS4sA"
h := http.Header{}
h.Set("BinancePay-Certificate-SN", "")
h.Set("BinancePay-Nonce", nonce)
h.Set("BinancePay-Timestamp", timestamp)
h.Set("BinancePay-Signature", "VtniwOFAi4oBKFnvHhY6UZ+wPARf7+yWVbE61Hc7JVdRgqKJ40Yk9k5Gb2Y0S0VLUYOOOhUNUsD7R8GDEr6WH84sAEf8bXa1xYMiyjgeGleTg1MxRJCtrHcxMeMCEXPWmKTeLhn6O+cdvDGq4ZpcTTiQnEvk5xHPcxFrBT637zg=\n")
h.Set("Content-Type", "application/json; charset=utf-8")
httpReq.Header = h
rawReq, err := client.VerifyAndParseWebhookRequest(httpReq)
assert.Nil(t, err, err)
assert.Equal(t, "PAY_SUCCESS", rawReq.BizStatus)
switch rawReq.BizType {
case NotiBizTypeOrder:
var orderNoti OrderNoti
err = json.Unmarshal([]byte(rawReq.RawData), &orderNoti)
assert.Nil(t, err, err)
assert.Equal(t, "9825382937292", orderNoti.MerchantTradeNo)
assert.Equal(t, 0.88, orderNoti.TotalFee.InexactFloat64())
respWriter := httptest.NewRecorder()
err = client.WebhookResponse(respWriter, true, "")
assert.Nil(t, err, err)
assert.Equal(t, "{\"returnCode\":\"SUCCESS\",\"returnMessage\":null}", respWriter.Body.String())
case NotiBizTypePayout:
panic("should never reach")
case NotiBizTypePayRefund:
panic("should never reach")
default:
panic("should never reach")
}
}