github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/Integration_test.go (about) 1 package connectsdk 2 3 import ( 4 "crypto/rand" 5 "fmt" 6 "net/url" 7 "os" 8 "testing" 9 10 "github.com/Ingenico-ePayments/connect-sdk-go/communicator" 11 "github.com/Ingenico-ePayments/connect-sdk-go/configuration" 12 "github.com/Ingenico-ePayments/connect-sdk-go/defaultimpl" 13 "github.com/Ingenico-ePayments/connect-sdk-go/domain/definitions" 14 "github.com/Ingenico-ePayments/connect-sdk-go/domain/payment" 15 "github.com/Ingenico-ePayments/connect-sdk-go/domain/riskassessments" 16 "github.com/Ingenico-ePayments/connect-sdk-go/domain/sessions" 17 "github.com/Ingenico-ePayments/connect-sdk-go/domain/token" 18 sdkErrors "github.com/Ingenico-ePayments/connect-sdk-go/errors" 19 "github.com/Ingenico-ePayments/connect-sdk-go/merchant/productgroups" 20 "github.com/Ingenico-ePayments/connect-sdk-go/merchant/products" 21 "github.com/Ingenico-ePayments/connect-sdk-go/merchant/services" 22 "github.com/Ingenico-ePayments/connect-sdk-go/merchant/tokens" 23 ) 24 25 var envMerchantID = os.Getenv("connect.api.merchantId") 26 var envAPIKeyID = os.Getenv("connect.api.apiKeyId") 27 var envSecretAPIKey = os.Getenv("connect.api.secretApiKey") 28 var envProxyURL = os.Getenv("connect.api.proxyUrl") 29 30 func TestIntegratedConvertAmount(t *testing.T) { 31 skipTestIfNeeded(t) 32 33 client, err := getClientIntegration() 34 if err != nil { 35 t.Fatal(err) 36 } 37 defer client.Close() 38 39 var query services.ConvertAmountParams 40 query.Amount = newInt64(123) 41 query.Source = newString("USD") 42 query.Target = newString("EUR") 43 44 response, err := client.Merchant(envMerchantID).Services().ConvertAmount(query, nil) 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 if response.ConvertedAmount == nil { 50 t.Fatal("nil converted amount") 51 } 52 } 53 54 func TestIntegratedConnection(t *testing.T) { 55 skipTestIfNeeded(t) 56 57 client, err := getClientIntegration() 58 if err != nil { 59 t.Fatal(err) 60 } 61 defer client.Close() 62 63 response, err := client.Merchant(envMerchantID).Services().Testconnection(nil) 64 if err != nil { 65 t.Fatal(err) 66 } 67 68 if response.Result == nil { 69 t.Fatal("nil result") 70 } 71 } 72 73 func TestIntegratedPayment(t *testing.T) { 74 skipTestIfNeeded(t) 75 76 client, err := getClientIntegration() 77 if err != nil { 78 t.Fatal(err) 79 } 80 defer client.Close() 81 82 var query payment.CreateRequest 83 var order payment.Order 84 85 var amountOfMoney definitions.AmountOfMoney 86 amountOfMoney.Amount = newInt64(100) 87 amountOfMoney.CurrencyCode = newString("EUR") 88 order.AmountOfMoney = &amountOfMoney 89 90 var customer payment.Customer 91 customer.Locale = newString("en") 92 93 var address definitions.Address 94 address.CountryCode = newString("NL") 95 customer.BillingAddress = &address 96 97 order.Customer = &customer 98 query.Order = &order 99 100 var paymentMethodSpecificInput payment.RedirectPaymentMethodSpecificInput 101 paymentMethodSpecificInput.ReturnURL = newString("http://example.com/") 102 paymentMethodSpecificInput.PaymentProductID = newInt32(809) 103 104 var paymentProductSpecificInput payment.RedirectPaymentProduct809SpecificInput 105 paymentProductSpecificInput.IssuerID = newString("INGBNL2A") 106 paymentMethodSpecificInput.PaymentProduct809SpecificInput = &paymentProductSpecificInput 107 108 query.RedirectPaymentMethodSpecificInput = &paymentMethodSpecificInput 109 110 idempotenceKey, err := pseudoUUID() 111 if err != nil { 112 t.Fatal(err) 113 } 114 115 context, err := NewCallContext(idempotenceKey) 116 if err != nil { 117 t.Fatal(err) 118 } 119 120 result, err := doCreatePayment(client, query, context) 121 if err != nil { 122 t.Fatal(err) 123 } 124 paymentID := result.Payment.ID 125 status := result.Payment.Status 126 127 if idempotenceKey != context.IdempotenceKey { 128 t.Fatalf("idempotence key mismatch") 129 } 130 if context.IdempotenceRequestTimestamp != nil { 131 t.Fatalf("timestamp not nil") 132 } 133 134 secondResult, err := doCreatePayment(client, query, context) 135 if idempotenceKey != context.IdempotenceKey { 136 t.Fatalf("idempotence key mismatch") 137 } 138 if context.IdempotenceRequestTimestamp == nil { 139 t.Fatalf("timestamp nil") 140 } 141 142 if *secondResult.Payment.ID != *paymentID { 143 t.Fatalf("payment id mismatch") 144 } 145 if *secondResult.Payment.Status != *status { 146 t.Fatalf("status mismatch") 147 } 148 } 149 150 func TestIntegratedPaymentProducts(t *testing.T) { 151 skipTestIfNeeded(t) 152 153 client, err := getClientIntegration() 154 if err != nil { 155 t.Fatal(err) 156 } 157 defer client.Close() 158 159 lParams := products.NewFindParams() 160 lParams.CountryCode = newString("NL") 161 lParams.CurrencyCode = newString("EUR") 162 163 _, err = client.Merchant(envMerchantID).Products().Find(*lParams, nil) 164 if err != nil { 165 t.Fatal(err) 166 } 167 } 168 169 func TestIntegratedPaymentProductDirectories(t *testing.T) { 170 skipTestIfNeeded(t) 171 172 client, err := getClientIntegration() 173 if err != nil { 174 t.Fatal(err) 175 } 176 defer client.Close() 177 178 lParams := products.NewDirectoryParams() 179 lParams.CountryCode = newString("NL") 180 lParams.CurrencyCode = newString("EUR") 181 182 _, err = client.Merchant(envMerchantID).Products().Directory(809, *lParams, nil) 183 if err != nil { 184 t.Fatal(err) 185 } 186 } 187 188 func TestIntegratedPaymentProductsGroups(t *testing.T) { 189 skipTestIfNeeded(t) 190 191 client, err := getClientIntegration() 192 if err != nil { 193 t.Fatal(err) 194 } 195 defer client.Close() 196 197 lParams := productgroups.NewGetParams() 198 lParams.CountryCode = newString("NL") 199 lParams.CurrencyCode = newString("EUR") 200 201 _, err = client.Merchant(envMerchantID).Productgroups().Get("cards", *lParams, nil) 202 if err != nil { 203 t.Fatal(err) 204 } 205 } 206 207 func TestIntegratedCreateSession(t *testing.T) { 208 skipTestIfNeeded(t) 209 210 client, err := getClientIntegration() 211 if err != nil { 212 t.Fatal(err) 213 } 214 defer client.Close() 215 216 lParams := sessions.NewSessionRequest() 217 218 _, err = client.Merchant(envMerchantID).Sessions().Create(*lParams, nil) 219 if err != nil { 220 t.Fatal(err) 221 } 222 } 223 224 func TestIntegratedCreateToken(t *testing.T) { 225 skipTestIfNeeded(t) 226 227 client, err := getClientIntegration() 228 if err != nil { 229 t.Fatal(err) 230 } 231 defer client.Close() 232 233 var query token.CreateRequest 234 query.PaymentProductID = newInt32(1) 235 236 tokenCard := token.NewCard() 237 query.Card = tokenCard 238 239 customerToken := token.NewCustomerToken() 240 tokenCard.Customer = customerToken 241 242 address := definitions.NewAddress() 243 customerToken.BillingAddress = address 244 address.CountryCode = newString("NL") 245 246 mandate := token.NewCardData() 247 tokenCard.Data = mandate 248 249 cardWithoutCVV := definitions.NewCardWithoutCvv() 250 mandate.CardWithoutCvv = cardWithoutCVV 251 cardWithoutCVV.CardholderName = newString("Jan") 252 cardWithoutCVV.IssueNumber = newString("12") 253 cardWithoutCVV.CardNumber = newString("4567350000427977") 254 cardWithoutCVV.ExpiryDate = newString("1225") 255 256 response, err := client.Merchant(envMerchantID).Tokens().Create(query, nil) 257 if err != nil { 258 t.Fatal(err) 259 } 260 261 if response.Token == nil { 262 t.Fatal("nil token") 263 } 264 265 var query2 tokens.DeleteParams 266 267 err = client.Merchant(envMerchantID).Tokens().Delete(*response.Token, query2, nil) 268 if err != nil { 269 t.Fatal(err) 270 } 271 } 272 273 func TestIntegratedRiskAssessments(t *testing.T) { 274 skipTestIfNeeded(t) 275 276 client, err := getClientIntegration() 277 if err != nil { 278 t.Fatal(err) 279 } 280 defer client.Close() 281 282 var query riskassessments.RiskAssessmentBankAccount 283 284 bankAccountBBan := definitions.NewBankAccountBban() 285 bankAccountBBan.CountryCode = newString("DE") 286 bankAccountBBan.AccountNumber = newString("0532013000") 287 bankAccountBBan.BankCode = newString("37040044") 288 query.BankAccountBban = bankAccountBBan 289 290 order := riskassessments.NewOrderRiskAssessment() 291 292 amountOfMoney := definitions.NewAmountOfMoney() 293 amountOfMoney.Amount = newInt64(100) 294 amountOfMoney.CurrencyCode = newString("EUR") 295 order.AmountOfMoney = amountOfMoney 296 297 customer := riskassessments.NewCustomerRiskAssessment() 298 customer.Locale = newString("en_GB") 299 order.Customer = customer 300 301 query.Order = order 302 303 response, err := client.Merchant(envMerchantID).Riskassessments().Bankaccounts(query, nil) 304 if err != nil { 305 t.Fatal(err) 306 } 307 308 if response.Results == nil { 309 t.Fatal("nil results") 310 } 311 if len(*response.Results) == 0 { 312 t.Fatal("empty results") 313 } 314 } 315 316 func TestIntegratedMultilineHeader(t *testing.T) { 317 //skipTestIfNeeded(t) 318 t.Skip("fails inside sandbox") 319 320 configuration := configuration.DefaultConfiguration(envAPIKeyID, envSecretAPIKey, "Ingenico") 321 configuration.APIEndpoint.Host = "eu.sandbox.api-ingenico.com" 322 configuration.APIKeyID = envAPIKeyID 323 configuration.SecretAPIKey = envSecretAPIKey 324 325 connection, err := defaultimpl.NewDefaultConnection(configuration.SocketTimeout, 326 configuration.ConnectTimeout, 327 configuration.KeepAliveTimeout, 328 configuration.IdleTimeout, 329 configuration.MaxConnections, 330 configuration.Proxy) 331 if err != nil { 332 t.Fatal(err) 333 } 334 335 multiLineHeader := newHeader("X-GCS-MultiLineHeader", "some\nvalue") 336 337 metaDataProviderBuilder := communicator.NewMetaDataProviderBuilder(configuration.Integrator) 338 metaDataProviderBuilder.ShoppingCartExtension = configuration.ShoppingCartExtension 339 metaDataProviderBuilder.AdditionalRequestHeaders = append(metaDataProviderBuilder.AdditionalRequestHeaders, multiLineHeader) 340 341 metaDataProvider, err := metaDataProviderBuilder.Build() 342 if err != nil { 343 t.Fatal(err) 344 } 345 346 authenticator, err := defaultimpl.NewDefaultAuthenticator(configuration.AuthorizationType, 347 configuration.APIKeyID, 348 configuration.SecretAPIKey) 349 if err != nil { 350 t.Fatal(err) 351 } 352 353 session, err := communicator.NewSession(&configuration.APIEndpoint, connection, authenticator, metaDataProvider) 354 if err != nil { 355 t.Fatal(err) 356 } 357 358 client, err := CreateClientFromSession(session) 359 if err != nil { 360 t.Fatal(err) 361 } 362 defer client.Close() 363 364 _, err = client.Merchant(envMerchantID).Services().Testconnection(nil) 365 if err != nil { 366 t.Fatal(err) 367 } 368 } 369 370 func getClientIntegration() (*Client, error) { 371 configuration := configuration.DefaultConfiguration(envAPIKeyID, envSecretAPIKey, "Ingenico") 372 configuration.APIEndpoint.Host = "eu.sandbox.api-ingenico.com" 373 374 if len(envProxyURL) > 0 { 375 proxy, err := url.Parse(envProxyURL) 376 if err != nil { 377 return nil, err 378 } 379 configuration.Proxy = proxy 380 } 381 382 client, err := CreateClientFromConfiguration(&configuration) 383 if err != nil { 384 return nil, err 385 } 386 387 return client.WithClientMetaInfo("{\"test\":\"test\"}") 388 } 389 390 func doCreatePayment(client *Client, query payment.CreateRequest, context *CallContext) (payment.CreateResult, error) { 391 // For this test it doesn't matter if the response is successful or declined, 392 // as long as idempotence is handled correctly 393 394 response, err := client.Merchant(envMerchantID).Payments().Create(query, context) 395 if err == nil { 396 result := payment.CreateResult{ 397 CreationOutput: response.CreationOutput, 398 MerchantAction: response.MerchantAction, 399 Payment: response.Payment, 400 } 401 return result, nil 402 } 403 switch realError := err.(type) { 404 case *sdkErrors.DeclinedPaymentError: 405 return *realError.PaymentResult(), nil 406 default: 407 return payment.CreateResult{}, nil 408 } 409 } 410 411 func pseudoUUID() (string, error) { 412 b := make([]byte, 16) 413 _, err := rand.Read(b) 414 if err != nil { 415 return "", err 416 } 417 418 return fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]), nil 419 } 420 421 func skipTestIfNeeded(t *testing.T) { 422 if len(envMerchantID) == 0 { 423 t.Skip("empty env connect.api.merchantId") 424 } 425 if len(envAPIKeyID) == 0 { 426 t.Skip("empty env connect.api.apiKeyId") 427 } 428 if len(envSecretAPIKey) == 0 { 429 t.Skip("empty env connect.api.secretApiKey") 430 } 431 }