This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
googlepay_test.go
185 lines (169 loc) · 5.12 KB
/
googlepay_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package qiwi
import (
"bytes"
"compress/zlib"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func Examplezlibzompress() {
data := "Hello world"
res := zlibzompress(data)
fmt.Println(res)
// Output: [120 156 242 72 205 201 201 87 40 207 47 202 73 1 4 0 0 255 255 24 171 4 61]
}
func TestGooglePay(t *testing.T) {
type GooglePayToken struct {
ProtoVer string `json:"protocolVersion"`
}
//nolint
googlePayToken := `{
"protocolVersion":"ECv2",
"signature":"MEQCIH6Q4OwQ0jAceFEkGF0JID6sJNXxOEi4r+mA7biRxqBQAiAondqoUpU/bdsrAOpZIsrHQS9nwiiNwOrr24RyPeHA0Q\u003d\u003d",
"intermediateSigningKey":{
"signedKey": "{\"keyExpiration\":\"1542323393147\",\"keyValue\":\"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE/1+3HBVSbdv+j7NaArdgMyoSAM43yRydzqdg1TxodSzA96Dj4Mc1EiKroxxunavVIvdxGnJeFViTzFvzFRxyCw\\u003d\\u003d\"}",
"signatures": ["MEYCIQCO2EIi48s8VTH+ilMEpoXLFfkxAwHjfPSCVED/QDSHmQIhALLJmrUlNAY8hDQRV/y1iKZGsWpeNmIP+z+tCQHQxP0v"]
},
"signedMessage":"{\"tag\":\"jpGz1F1Bcoi/fCNxI9n7Qrsw7i7KHrGtTf3NrRclt+U\\u003d\",\"ephemeralPublicKey\":\"BJatyFvFPPD21l8/uLP46Ta1hsKHndf8Z+tAgk+DEPQgYTkhHy19cF3h/bXs0tWTmZtnNm+vlVrKbRU9K8+7cZs\\u003d\",\"encryptedMessage\":\"mKOoXwi8OavZ\"}"
}`
// HTTP MOCK
serv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
var buf, b bytes.Buffer
var p Payment
var tk GooglePayToken
if r.Method != http.MethodPut {
fmt.Fprintln(w, `{
"serviceName" : "payin-core",
"errorCode" : "validation.wrongmethod",
"description" : "bad http method",
"userMessage" : "Validation error",
"dateTime" : "2018-11-13T16:49:59.166+03:00",
"traceId" : "fd0e2a08c63ace83"
}`)
return
}
_, err = io.Copy(&buf, r.Body)
if err != nil {
fmt.Fprintln(w, `{
"serviceName" : "payin-core",
"errorCode" : "validation.copyerr",
"description" : "`+err.Error()+`",
"userMessage" : "Validation error",
"dateTime" : "2018-11-13T16:49:59.166+03:00",
"traceId" : "fd0e2a08c63ace83"
}`)
return
}
err = json.Unmarshal(buf.Bytes(), &p)
if err != nil {
fmt.Fprintln(w, `{
"serviceName" : "payin-core",
"errorCode" : "validation.json",
"description" : "`+err.Error()+`",
"userMessage" : "Validation error",
"dateTime" : "2018-11-13T16:49:59.166+03:00",
"traceId" : "fd0e2a08c63ace83"
}`)
return
}
dec, err := base64.StdEncoding.DecodeString(p.PaymentMethod.Token)
if err != nil {
fmt.Fprintln(w, `{
"serviceName" : "payin-core",
"errorCode" : "validation.base64",
"description" : "`+err.Error()+`",
"userMessage" : "Validation error",
"dateTime" : "2018-11-13T16:49:59.166+03:00",
"traceId" : "fd0e2a08c63ace83"
}`)
return
}
br := bytes.NewReader(dec)
data, err := zlib.NewReader(br)
if err != nil {
fmt.Fprintln(w, `{
"serviceName" : "payin-core",
"errorCode" : "validation.zlib",
"description" : "`+err.Error()+`",
"userMessage" : "Validation error",
"dateTime" : "2018-11-13T16:49:59.166+03:00",
"traceId" : "fd0e2a08c63ace83"
}`)
return
}
defer data.Close()
_, err = io.Copy(&b, data) //nolint
if err != nil {
fmt.Fprintln(w, `{
"serviceName" : "payin-core",
"errorCode" : "validation.copyerr",
"description" : "`+err.Error()+`",
"userMessage" : "Validation error",
"dateTime" : "2018-11-13T16:49:59.166+03:00",
"traceId" : "fd0e2a08c63ace83"
}`)
return
}
err = json.Unmarshal(b.Bytes(), &tk)
if err != nil {
fmt.Fprintln(w, `{
"serviceName" : "payin-core",
"errorCode" : "validation.tokenjson",
"description" : "`+err.Error()+`",
"userMessage" : "Validation error",
"dateTime" : "2018-11-13T16:49:59.166+03:00",
"traceId" : "fd0e2a08c63ace83"
}`)
return
}
if tk.ProtoVer != "ECv2" {
fmt.Fprintln(w, `{
"serviceName" : "payin-core",
"errorCode" : "validation.tokenversionpayload",
"description" : "`+err.Error()+`",
"userMessage" : "Validation error",
"dateTime" : "2018-11-13T16:49:59.166+03:00",
"traceId" : "fd0e2a08c63ace83"
}`)
return
}
fmt.Fprintln(w, `{
"siteId": "23044",
"billId": "893794793973",
"amount": {
"value": 100.00,
"currency": "RUB"
},
"status": {
"value": "CREATED",
"changedDateTime": "2018-03-05T11:27:41"
},
"comment": "Text comment",
"customFields": {
"cf1": "Some data",
"FROM_MERCHANT_CONTRACT_ID": "1234"
},
"creationDateTime": "2018-03-05T11:27:41",
"expirationDateTime": "2018-04-13T14:30:00",
"payUrl": "https://oplata.qiwi.com/form/?invoice_uid=d875277b-6f0f-445d-8a83-f62c7c07be77"
}`)
}))
serv.Start()
defer serv.Close()
// Route request to mocked http server
pay := New("billId", "SiteID", "TOKEN", serv.URL)
amount := 500 // 5.00RUB
err := pay.GooglePay(context.TODO(), amount, googlePayToken)
if pay.PaymentMethod.Type != GooglePayPayment {
t.Errorf("Wrong payment type %s", pay.PaymentMethod.Type)
}
if err != nil {
t.Errorf("GooglePay method error: %s", err)
}
}