-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuthauthorizeMock.go
247 lines (239 loc) · 9.39 KB
/
AuthauthorizeMock.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"fmt"
"net/http"
"encoding/json"
"strconv"
"github.com/patrickmn/go-cache"
)
//
//
//
func AuthauthorizeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("AuthauthorizeHandler : INIT")
//build the form
r.ParseForm()
fmt.Println("-----")
fmt.Println(r.Form)
fmt.Println("-----")
httpStatus := http.StatusBadRequest
//
// Set all Headers
//
header := w.Header()
requestId := CreateRequestId()
header.Set("content-type", "application/json")
header.Set("stripe-version", "mock-1.0")
header.Set("request-id", requestId)
//copy the idempotency key to response
idempotencyKey := r.Header.Get("idempotency-key")
header.Set("idempotency-key", idempotencyKey)
//make hash of form this will help maintain idempotency
formHash := MD5Hash(r.Form)
header.Set("request-md5", formHash)
//
// check for auth idempotency_key
//
idempotencyObj, found := idempotencyCache.Get(idempotencyKey)
//found
if found {
fmt.Println("AuthauthorizeHandler : idempotency found for key: " + idempotencyKey)
//response object
errorObjects := ErrorResponse{
Error: ErrorObject{
Type: "idempotency_error",
},
}
//
idempotency := idempotencyObj.(Idempotency)
//
exit := true
if idempotency.Type == "capture" {
fmt.Println("AuthauthorizeHandler : idempotency capture key found")
//end user is trying to access capture with same idempotency as of capture.
errorObjects.Error.Message = "Keys for idempotent requests can only be used for the same endpoint they were first used for ('/v1/charges' vs '/v1/charges/" + idempotency.ChargeId + "/capture'). Try using a key other than '" + idempotencyKey + "' if you meant to execute a different request."
} else if idempotency.Type == "void" {
fmt.Println("AuthauthorizeHandler : idempotency void key found")
//end user is trying to access capture with same idempotency as of void with different form parameters.
errorObjects.Error.Message = "Keys for idempotent requests can only be used for the same endpoint they were first used for ('/v1/charges' vs '/v1/charges/" + idempotency.ChargeId + "/refunds'). Try using a key other than '" + idempotencyKey + "' if you meant to execute a different request."
} else if idempotency.Type == "auth" && idempotency.RequestHash != formHash {
fmt.Println("AuthauthorizeHandler : idempotency auth key found with md5:" + formHash)
//end user is trying to access capture with same idempotency as of auth.
errorObjects.Error.Message = "Keys for idempotent requests can only be used with the same parameters they were first used with. Try using a key other than '" + idempotencyKey + "' if you meant to execute a different request."
} else {
fmt.Println("AuthauthorizeHandler : idempotency auth key cache")
//this would be a request for cached auth
exit = false
}
// idempotency error so exit
if exit {
fmt.Fprintln(w, json.NewEncoder(w).Encode(errorObjects))
//final http status code
w.WriteHeader(httpStatus)
//exit
fmt.Println("AuthauthorizeHandler : IDP_EXIT")
return
}
} else {
//new request
fmt.Println("AuthauthorizeHandler : new request with idempotency key: " + idempotencyKey)
idempotency := Idempotency{
Type: "auth",
RequestId: requestId,
RequestHash: formHash,
}
//set cache for next use
idempotencyCache.Set(idempotencyKey, idempotency, cache.DefaultExpiration)
}
//
//check for cached void object
//
chargeObj, found := authCache.Get(idempotencyKey)
if found {
fmt.Println("AuthauthorizeHandler : cache fault:" + idempotencyKey)
//get capture object from cache
cacheObject := chargeObj.(CacheObject)
//copy original request id
header.Set("original-request", cacheObject.RequestId)
//write to stream
if cacheObject.Status == 200 {
json.NewEncoder(w).Encode(cacheObject.Charge)
} else {
//this will never happen
json.NewEncoder(w).Encode(cacheObject.Error)
}
//should be the last
w.WriteHeader(cacheObject.Status)
//print and exit
fmt.Println("AuthauthorizeHandler : cache exit with http_status:" + strconv.Itoa(cacheObject.Status))
return
}
//
// First time request
//
fmt.Println("AuthauthorizeHandler :First time request")
//original request id and request id will be same this case
header.Set("original-request", requestId)
//
fmt.Println("AuthauthorizeHandler : Start")
//evaluate auth & capture
chargeRequest, errorObject, status := ValidateAndMapAuth(r)
chargeObject := ChargeObject{}
//declined
if status != http.StatusOK {
//write to stream
json.NewEncoder(w).Encode(errorObject)
//
fmt.Println("AuthauthorizeHandler : exiting with validation error")
} else {
//new charge id
newId := CreateChargeId()
chargeId := "ch_" + newId
cardId := "card_" + CreateCardId()
//
// create mock response objects
//
source := Source{
ID: cardId,
Object: "card",
Fingerprint: CreateFingerPrint(),
Funding: "credit", //TODO Test card to funding type mapping
Last4: LastFour(chargeRequest.Source.Number),
//
Brand: "Visa", //TODO support test card id to brand mapping
Country: "US", // TODO support test card to country mapping
//
AddressCity: chargeRequest.Source.AddressCity,
AddressCountry: chargeRequest.Source.AddressCountry,
AddressLine1: chargeRequest.Source.AddressLine1,
AddressLine2: chargeRequest.Source.AddressLine2,
AddressState: chargeRequest.Source.AddressState,
AddressZip: chargeRequest.Source.AddressZip,
}
//
//card to test flows https://stripe.com/docs/testing
//
//address line check
if chargeRequest.Source.AddressLine1 != "" {
if chargeRequest.Source.Number == 4000000000000028 {
source.AddressLine1Check = "fail"
} else if chargeRequest.Source.Number == 4000000000000044 {
source.AddressLine1Check = "unavailable"
} else {
source.AddressLine1Check = "pass"
}
}
//zip code check
if chargeRequest.Source.AddressZip != "" {
if chargeRequest.Source.Number == 4000000000000036 {
source.AddressZipCheck = "fail"
} else if chargeRequest.Source.Number == 4000000000000044 {
source.AddressZipCheck = "unavailable"
} else {
source.AddressZipCheck = "pass"
}
}
//If a CVC number is provided, the cvc_check fails.
if chargeRequest.Source.CVV > 0 {
if chargeRequest.Source.Number == 4000000000000101 {
source.CvcCheck = "fail"
} else {
source.CvcCheck = "pass"
}
}
//mock refunds
refunds := Refunds{
Object: "list",
HasMore: false,
TotalCount: 0,
URL: "/v1/charges/" + chargeId + "/refunds",
}
//create mock charge response
chargeObject = ChargeObject{
ID: chargeId,
Status: "succeeded",
Object: "charge",
Amount: chargeRequest.Amount,
AmountRefunded: 0,
Captured: false, //todo based on the capture request flag,
Created: Timestamp(),
Currency: chargeRequest.Currency,
Description: chargeRequest.Description,
Livemode: false,
Paid: true, // in case if there are no error
Refunded: false,
Outcome: successOutcome,
Refunds: refunds,
Source: source,
}
//Charge succeeds with a risk_level of elevated
if chargeRequest.Source.Number == 4000000000000036 {
chargeObject.Review = "prv_" + newId
chargeObject.Outcome = elevatedOutcome
}
//return write to stream
json.NewEncoder(w).Encode(chargeObject)
//should be the last
status = http.StatusOK
//
fmt.Println("AuthauthorizeHandler : mock object created")
}
//put object into cache
cacheableObject := CacheObject{
Type: "auth",
Status: status,
RequestId: requestId,
Charge: chargeObject,
Error: errorObject,
}
//set item
authCache.Set(idempotencyKey, cacheableObject, cache.DefaultExpiration)
//add to charge cache in case of success event
if status == http.StatusOK{
chargeCache.Set(chargeObject.ID, cacheableObject, cache.DefaultExpiration)
}
//should be the last
w.WriteHeader(status)
//the end
fmt.Println(" AuthauthorizeHandler : END")
}