This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
types.go
307 lines (269 loc) · 12.7 KB
/
types.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package sleet
import (
"context"
"net/http"
"time"
)
// Client defines the Sleet interface which takes in a generic request and returns a generic response
// The translations for each specific PsP takes place in the corresponding gateways/<PsP> folders
// The four supported methods are Auth, Capture, Void, Refund
type Client interface {
Authorize(request *AuthorizationRequest) (*AuthorizationResponse, error)
Capture(request *CaptureRequest) (*CaptureResponse, error)
Void(request *VoidRequest) (*VoidResponse, error)
Refund(request *RefundRequest) (*RefundResponse, error)
}
// ClientWithContext is a superset of `Client` that includes addtional methods that take
// `context.Context` as parameters.
type ClientWithContext interface {
Client // supset includes the normal Client interface
AuthorizeWithContext(ctx context.Context, request *AuthorizationRequest) (*AuthorizationResponse, error)
CaptureWithContext(ctx context.Context, request *CaptureRequest) (*CaptureResponse, error)
VoidWithContext(ctx context.Context, request *VoidRequest) (*VoidResponse, error)
RefundWithContext(ctx context.Context, request *RefundRequest) (*RefundResponse, error)
}
// Amount specifies both quantity and currency
type Amount struct {
Amount int64
Currency string
}
// Address generic address to represent billing address, shipping address, etc.
// used for AVS checks for auth calls
type Address struct {
StreetAddress1 *string
StreetAddress2 *string
Locality *string
RegionCode *string
PostalCode *string
CountryCode *string // ISO 2-digit code
Company *string
Email *string
PhoneNumber *string
}
// BillingAddress for backwards compatibility
type BillingAddress = Address
// CreditCard represents raw credit card information
type CreditCard struct {
FirstName string
LastName string
Number string
ExpirationMonth int
ExpirationYear int
CVV string
Network CreditCardNetwork
Save bool // indicates if customer wants to save their credit card details
}
// LineItem is used for Level3 Processing if enabled (not default). Specifies information per item in the order
type LineItem struct {
Description string
ProductCode string
UnitPrice Amount
Quantity int64
TotalAmount Amount
ItemTaxAmount Amount
ItemDiscountAmount Amount
UnitOfMeasure string
CommodityCode string
}
// Level3Data contains all of the information needed for Level3 processing including LineItems
type Level3Data struct {
CustomerReference string
TaxAmount Amount
DiscountAmount Amount
ShippingAmount Amount
DutyAmount Amount
DestinationPostalCode string
DestinationCountryCode string
DestinationAdminArea string
LineItems []LineItem
}
const (
// ResponseHeaderOption will return in the response the value for each HTTP header key listed in the option.
// Value type: []string
ResponseHeaderOption string = "ResponseHeader"
// GooglePayTokenOption will use the provided Google Pay token to authorize the payment.
// Value type: string
GooglePayTokenOption string = "GooglePayToken"
// ApplePayTokenOption will use the provided Apple Pay token to authorize the payment.
// Value type: string
ApplePayTokenOption string = "ApplePayToken"
// CyberSourceTokenizeOption will cause tokens to be requested for each token type listed in the option.
// Value type: []TokenType
CyberSourceTokenizeOption string = "CyberSourceTokenize"
)
// AuthorizationRequest specifies needed information for request to authorize by PsPs
// Note: Only credit cards are supported
// Note: Options is a generic key-value pair that can be used to provide additional information to PsP
type AuthorizationRequest struct {
Amount Amount
BillingAddress *Address
Channel string // for PSPs that track the sales channel
ClientTransactionReference *string // Custom transaction reference metadata that will be associated with this request
CreditCard *CreditCard
Cryptogram string // for Network Tokenization methods
ECI string // E-Commerce Indicator (can be used for Network Tokenization as well)
Level3Data *Level3Data
MerchantOrderReference string // Similar to ClientTransactionReference but specifically if we want to store the shopping cart order id
PreviousExternalTransactionID *string // If we are in a recurring situation, then we can use the PreviousExternalTransactionID as part of the auth request
ProcessingInitiator *ProcessingInitiatorType // For Card on File transactions we want to store the various different types (initial cof, initial recurring, etc)
ShippingAddress *Address
ShopperReference string // ShopperReference Unique reference to a shopper (shopperId, etc.)
ThreeDS *ThreeDS
Options map[string]interface{}
}
const (
AuthCodeMetadata string = "authCode"
ApprovalCodeMetadata string = "approvalCode"
ResponseCodeMetadata string = "responseCode"
)
// AuthorizationResponse is a generic response returned back to client after data massaging from PsP Response.
// The raw AVS and CVV are included if applicable.
// Raw fields contain the untranslated responses from processors, while
// the non-raw fields are the best parsings to a single standard, with
// loss of granularity minimized. The latter should be preferred when
// treating Sleet as a black box.
type AuthorizationResponse struct {
// Success is true if Auth went through successfully
Success bool
TransactionReference string
ExternalTransactionID string
AvsResult AVSResponse
CvvResult CVVResponse
Response string
ErrorCode string
// Message is from the gateway describing the reason for the response code, for example a failed auth.
Message string
ResultType ResultType
AvsResultRaw string
CvvResultRaw string
RTAUResult *RTAUResponse
// AdyenAdditionalData stores additional recurring info (will be refactored to general naming on next major version upgrade)
AdyenAdditionalData map[string]string
// Metadata stores additional data that might be unique to PSP.
Metadata map[string]string
// CreatedTokens stores the tokens that were created as a result of the request, if any.
CreatedTokens map[TokenType]string
// StatusCode is the HTTP status code from the header of the PSP response.
StatusCode int
// Header is the HTTP header from the PSP response, filtered by the list of headers in the ResponseHeaderOption.
Header http.Header
}
// CaptureRequest specifies the authorized transaction to capture and also an amount for partial capture use cases
type CaptureRequest struct {
Amount *Amount
TransactionReference string
ClientTransactionReference *string // Custom transaction reference metadata that will be associated with this request
MerchantOrderReference *string // Custom merchant order reference that will be associated with this request
Options map[string]interface{} // For additional options that need to be passed in
}
// CaptureResponse will have Success be true if transaction is captured and also a reference to be used for subsequent operations
type CaptureResponse struct {
Success bool
TransactionReference string
ErrorCode *string
}
// VoidRequest cancels an authorized transaction
type VoidRequest struct {
TransactionReference string
ClientTransactionReference *string // Custom transaction reference metadata that will be associated with this request
MerchantOrderReference *string // Custom merchant order reference that will be associated with this request
}
// VoidResponse also specifies a transaction reference if PsP uses different transaction references for different states
type VoidResponse struct {
Success bool
TransactionReference string
ErrorCode *string
}
// RefundRequest for refunding a captured transaction with generic Options and amount to be refunded
type RefundRequest struct {
Amount *Amount
TransactionReference string
ClientTransactionReference *string // Custom transaction reference metadata that will be associated with this request
MerchantOrderReference *string // Custom merchant order reference that will be associated with this request
Last4 string
Options map[string]interface{}
}
// RefundResponse indicating if request went through successfully
type RefundResponse struct {
Success bool
TransactionReference string
ErrorCode *string
}
// TransactionDetailsRequest for fetching a transaction's details
type TransactionDetailsRequest struct {
TransactionReference string
}
// TransactionDetailsResponse indicating the transaction details. Currently, only the last 4 digits of credit card is returned.
type TransactionDetailsResponse struct {
ResultCode string
CardNumber string
}
// GetHTTPResponseHeader returns the http response headers specified in the given options.
func GetHTTPResponseHeader(options map[string]interface{}, httpResp http.Response) http.Header {
var responseHeader http.Header
if headers, ok := options[ResponseHeaderOption].([]string); ok {
responseHeader = make(http.Header)
for _, header := range headers {
if headerValue := httpResp.Header.Get(header); len(headerValue) > 0 {
responseHeader.Add(header, headerValue)
}
}
}
return responseHeader
}
// Currency maps to the CURRENCIES list in currency.go specifying the symbol and precision for the currency
type Currency struct {
Precision int
Symbol string
}
// RTAUStatus represents the Real Time Account Updater response from a processor, if applicable
type RTAUStatus string
const (
RTAUStatusUnknown RTAUStatus = "Unknown" // when a processor has RTAU capability, but returns an unexpected status
RTAUStatusNoResponse RTAUStatus = "NoResponse" // when a processor has RTAU capability, but doesn't return any additional info
RTAUStatusCardChanged RTAUStatus = "CardChanged"
RTAUStatusCardExpired RTAUStatus = "CardExpiryChanged"
RTAUStatusContactCardAccountHolder RTAUStatus = "ContactCardAccountHolder"
RTAUStatusCloseAccount RTAUStatus = "CloseAccount"
)
type RTAUResponse struct {
RealTimeAccountUpdateStatus RTAUStatus
UpdatedExpiry *time.Time
UpdatedBIN string
UpdatedLast4 string
}
// ThreeDS holds results from a 3DS verification challenge.
type ThreeDS struct {
Frictionless bool // Whether the 3DS flow for this transaction was frictionless
ACSTransactionID string // Transaction ID assigned by ACS
CAVV string // Cardholder Authentication Value
CAVVAlgorithm string // Algorithm used to calculate CAVV
DSTransactionID string // Directory Server (DS) Transaction ID (for 3DS2)
PAResStatus string // Transaction status result
UCAFIndicator string // Universal Cardholder Authentication Field Indicator value provided by issuer
Version string // 3DS Version
XID string // Transaction ID from authentication processing (for 3DS1)
}
type ResultType string
const (
ResultTypeSuccess ResultType = "Approved"
ResultTypeUnknownError ResultType = "Unknown"
ResultTypePaymentError ResultType = "PaymentError" // payment or credit card related error
ResultTypeAPIError ResultType = "APIError" // error related to the PSPs API (validation error, authentication, idempotency, etc)
ResultTypeServerError ResultType = "ServerError" // network, connection, timeout etc. errors
)
// TokenType defines the type of token, used either as input to complete a transaction, or as output to be saved for
// future transactions.
type TokenType string
const (
// TokenTypeCustomer points to a token that can be mapped to information about a customer.
// Examples of mapped data: name + shipping address + list of preferred payment methods.
TokenTypeCustomer TokenType = "customerToken"
// TokenTypePayment points to a token that can be mapped to information about a payment method and its auxiliary data.
// Examples of mapped data: payment account number + expiration + billing address.
TokenTypePayment TokenType = "paymentToken"
// TokenTypePaymentIdentifier points to a token that can be mapped to only a payment account number.
TokenTypePaymentIdentifier TokenType = "paymentIdentifierToken"
// TokenTypeShippingAddress points to a token that can be mapped to a shipping address.
TokenTypeShippingAddress TokenType = "shippingAddressToken"
)