Skip to content

Commit

Permalink
Add subdomain for merchants
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko committed Feb 27, 2024
1 parent 293ebd4 commit a60c0bc
Show file tree
Hide file tree
Showing 14 changed files with 373 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET }}
CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID }}
CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET }}
CHECKOUT_MERCHANT_SUBDOMAIN: ${{ secrets.IT_CHECKOUT_MERCHANT_SUBDOMAIN }}
run:
go build && go test -v -timeout 5m ./...

1 change: 1 addition & 0 deletions .github/workflows/build-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ jobs:
CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET }}
CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID }}
CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET }}
CHECKOUT_MERCHANT_SUBDOMAIN: ${{ secrets.IT_CHECKOUT_MERCHANT_SUBDOMAIN }}
run:
go build && go test -v -timeout 5m ./...
1 change: 1 addition & 0 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET }}
CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID }}
CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET }}
CHECKOUT_MERCHANT_SUBDOMAIN: ${{ secrets.IT_CHECKOUT_MERCHANT_SUBDOMAIN }}
run:
go build && go test -v -timeout 5m ./...
- id: read-version
Expand Down
10 changes: 10 additions & 0 deletions abc/checkout_previous_sdk_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func (b *CheckoutPreviousSdkBuilder) WithEnvironment(environment configuration.E
return b
}

func (b *CheckoutPreviousSdkBuilder) WithEnvironmentSubdomain(subdomain string) *CheckoutPreviousSdkBuilder {
b.EnvironmentSubdomain = configuration.NewEnvironmentSubdomain(b.Environment, subdomain)
return b
}

func (b *CheckoutPreviousSdkBuilder) WithHttpClient(client *http.Client) *CheckoutPreviousSdkBuilder {
b.HttpClient = client
return b
Expand Down Expand Up @@ -47,7 +52,12 @@ func (b *CheckoutPreviousSdkBuilder) Build() (*Api, error) {
}

sdkCredentials := configuration.NewPreviousKeysSdkCredentials(b.SecretKey, b.PublicKey)

newConfiguration := configuration.NewConfiguration(sdkCredentials, b.Environment, b.HttpClient, b.Logger)

if b.EnvironmentSubdomain != nil {
newConfiguration = configuration.NewConfigurationWithSubdomain(sdkCredentials, b.Environment, b.EnvironmentSubdomain, b.HttpClient, b.Logger)
}

return CheckoutApi(newConfiguration), nil
}
37 changes: 33 additions & 4 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
)

type Configuration struct {
Credentials SdkCredentials
Environment Environment
HttpClient http.Client
Logger StdLogger
Credentials SdkCredentials
Environment Environment
EnvironmentSubdomain *EnvironmentSubdomain
HttpClient http.Client
Logger StdLogger
}

func NewConfiguration(
Expand Down Expand Up @@ -38,3 +39,31 @@ func NewConfiguration(
Logger: logger,
}
}

func NewConfigurationWithSubdomain(
credentials SdkCredentials,
environment Environment,
environmentSubdomain *EnvironmentSubdomain,
client *http.Client,
logger StdLogger,
) *Configuration {
if environment == nil {
environment = Sandbox()
}

if client == nil {
client = common.BuildDefaultClient()
}

if logger == nil {
logger = DefaultLogger()
}

return &Configuration{
Credentials: credentials,
Environment: environment,
EnvironmentSubdomain: environmentSubdomain,
HttpClient: *client,
Logger: logger,
}
}
31 changes: 31 additions & 0 deletions configuration/environment.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package configuration

import (
"net/url"
"regexp"
)

type Environment interface {
BaseUri() string
AuthorizationUri() string
Expand All @@ -9,6 +14,32 @@ type Environment interface {
IsSandbox() bool
}

type EnvironmentSubdomain struct {
ApiUrl string
}

func NewEnvironmentSubdomain(environment Environment, subdomain string) *EnvironmentSubdomain {
apiUrl := addSubdomainToApiUrlEnvironment(environment, subdomain)
return &EnvironmentSubdomain{ApiUrl: apiUrl}
}

func addSubdomainToApiUrlEnvironment(environment Environment, subdomain string) string {
apiUrl := environment.BaseUri()

newEnvironment := apiUrl

regex := regexp.MustCompile("^[0-9a-z]{8}$")

if regex.MatchString(subdomain) {
merchantApiUrl, _ := url.Parse(apiUrl)
merchantApiUrl.Host = subdomain + "." + merchantApiUrl.Host

newEnvironment = merchantApiUrl.String()
}

return newEnvironment
}

type CheckoutEnv struct {
baseUri string
authorizationUri string
Expand Down
7 changes: 4 additions & 3 deletions configuration/sdk_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package configuration
import "net/http"

type SdkBuilder struct {
Environment Environment
HttpClient *http.Client
Logger StdLogger
Environment Environment
EnvironmentSubdomain *EnvironmentSubdomain
HttpClient *http.Client
Logger StdLogger
}

func (s *SdkBuilder) GetConfiguration(string, string) *Configuration {
Expand Down
3 changes: 3 additions & 0 deletions nas/checkout_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func CheckoutApi(configuration *configuration.Configuration) *Api {
}

func buildBaseClient(configuration *configuration.Configuration) client.HttpClient {
if configuration.EnvironmentSubdomain != nil {
return client.NewApiClient(configuration, configuration.EnvironmentSubdomain.ApiUrl)
}
return client.NewApiClient(configuration, configuration.Environment.BaseUri())
}

Expand Down
10 changes: 10 additions & 0 deletions nas/checkout_default_sdk_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func (b *CheckoutDefaultSdkBuilder) WithEnvironment(environment configuration.En
return b
}

func (b *CheckoutDefaultSdkBuilder) WithEnvironmentSubdomain(subdomain string) *CheckoutDefaultSdkBuilder {
b.EnvironmentSubdomain = configuration.NewEnvironmentSubdomain(b.Environment, subdomain)
return b
}

func (b *CheckoutDefaultSdkBuilder) WithHttpClient(client *http.Client) *CheckoutDefaultSdkBuilder {
b.HttpClient = client
return b
Expand Down Expand Up @@ -47,7 +52,12 @@ func (b *CheckoutDefaultSdkBuilder) Build() (*Api, error) {
}

sdkCredentials := configuration.NewDefaultKeysSdkCredentials(b.SecretKey, b.PublicKey)

newConfiguration := configuration.NewConfiguration(sdkCredentials, b.Environment, b.HttpClient, b.Logger)

if b.EnvironmentSubdomain != nil {
newConfiguration = configuration.NewConfigurationWithSubdomain(sdkCredentials, b.Environment, b.EnvironmentSubdomain, b.HttpClient, b.Logger)
}

return CheckoutApi(newConfiguration), nil
}
9 changes: 9 additions & 0 deletions nas/checkout_oauth_sdk_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func (b *CheckoutOAuthSdkBuilder) WithEnvironment(environment configuration.Envi
return b
}

func (b *CheckoutOAuthSdkBuilder) WithEnvironmentSubdomain(subdomain string) *CheckoutOAuthSdkBuilder {
b.EnvironmentSubdomain = configuration.NewEnvironmentSubdomain(b.Environment, subdomain)
return b
}

func (b *CheckoutOAuthSdkBuilder) WithHttpClient(client *http.Client) *CheckoutOAuthSdkBuilder {
b.HttpClient = client
return b
Expand Down Expand Up @@ -67,5 +72,9 @@ func (b *CheckoutOAuthSdkBuilder) Build() (*Api, error) {

newConfiguration := configuration.NewConfiguration(sdkCredentials, b.Environment, b.HttpClient, b.Logger)

if b.EnvironmentSubdomain != nil {
newConfiguration = configuration.NewConfigurationWithSubdomain(sdkCredentials, b.Environment, b.EnvironmentSubdomain, b.HttpClient, b.Logger)
}

return CheckoutApi(newConfiguration), nil
}
54 changes: 54 additions & 0 deletions test/configuration_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package test

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"

"github.com/checkout/checkout-sdk-go/configuration"
"github.com/checkout/checkout-sdk-go/mocks"
)

func TestShouldCreateConfiguration(t *testing.T) {
credentials := new(mocks.CredentialsMock)
environment := configuration.Production()
subdomain := configuration.NewEnvironmentSubdomain(environment, "123dmain")
subdomainBad := configuration.NewEnvironmentSubdomain(environment, "baddomain")

cases := []struct {
name string
configuration *configuration.Configuration
checker func(*configuration.Configuration, error)
}{
{
name: "should create a configuration object",
configuration: configuration.NewConfiguration(credentials, environment, &http.Client{}, nil),
checker: func(configuration *configuration.Configuration, err error) {
assert.NotNil(t, configuration)
},
},
{
name: "should create a configuration object with a valid subdomain",
configuration: configuration.NewConfigurationWithSubdomain(credentials, environment, subdomain, &http.Client{}, nil),
checker: func(configuration *configuration.Configuration, err error) {
assert.NotNil(t, configuration)
assert.Equal(t, "https://123dmain.api.checkout.com", configuration.EnvironmentSubdomain.ApiUrl)
},
},
{
name: "should create a configuration object with a invalid subdomain",
configuration: configuration.NewConfigurationWithSubdomain(credentials, environment, subdomainBad, &http.Client{}, nil),
checker: func(configuration *configuration.Configuration, err error) {
assert.NotNil(t, configuration)
assert.Equal(t, "https://api.checkout.com", configuration.EnvironmentSubdomain.ApiUrl)
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tc.checker(tc.configuration, nil)
})
}
}
71 changes: 71 additions & 0 deletions test/default_sdk_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package test

import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/checkout/checkout-sdk-go"
"github.com/checkout/checkout-sdk-go/configuration"
"github.com/checkout/checkout-sdk-go/nas"
)

func TestDefaultCheckoutSdks(t *testing.T) {
var defaultApi, _ = checkout.Builder().
StaticKeys().
WithSecretKey(os.Getenv("CHECKOUT_DEFAULT_SECRET_KEY")).
WithPublicKey(os.Getenv("CHECKOUT_DEFAULT_PUBLIC_KEY")).
WithEnvironment(configuration.Sandbox()).
Build()

var defaultApiSubdomain, _ = checkout.Builder().
StaticKeys().
WithSecretKey(os.Getenv("CHECKOUT_DEFAULT_SECRET_KEY")).
WithPublicKey(os.Getenv("CHECKOUT_DEFAULT_PUBLIC_KEY")).
WithEnvironment(configuration.Sandbox()).
WithEnvironmentSubdomain("123dmain").
Build()

var defaultApiBad, _ = checkout.Builder().
StaticKeys().
WithSecretKey("error").
WithPublicKey("error").
WithEnvironment(configuration.Sandbox()).
Build()

cases := []struct {
name string
defaultApi *nas.Api
checker func(*nas.Api, error)
}{
{
name: "should create a default checkout sdk api object",
defaultApi: defaultApi,
checker: func(token *nas.Api, err error) {
assert.NotNil(t, defaultApi)
},
},
{
name: "should create a default checkout sdk api object with valid subdomain",
defaultApi: defaultApiSubdomain,
checker: func(token *nas.Api, err error) {
assert.NotNil(t, defaultApiSubdomain)
},
},
{
name: "should fail a default checkout sdk api object",
defaultApi: defaultApiBad,
checker: func(token *nas.Api, err error) {
assert.Nil(t, defaultApiBad)
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tc.checker(tc.defaultApi, nil)
})
}

}
Loading

0 comments on commit a60c0bc

Please sign in to comment.