Skip to content

Commit

Permalink
Merge pull request #20 from BoltApp/yhsu/AddClientCredentialClient
Browse files Browse the repository at this point in the history
Add NewWithClientCredentialsAndHttpClient
  • Loading branch information
ellenwei authored Feb 2, 2022
2 parents e4c53fa + f8e9261 commit 13fb927
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
16 changes: 15 additions & 1 deletion braintree.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ func NewWithHttpClient(env Environment, merchantId, publicKey, privateKey string
return &Braintree{credentials: newAPIKey(env, merchantId, publicKey, privateKey), HttpClient: client}
}

func NewWithClientCredentialsAndHttpClient(
env Environment,
clientId string,
clientSecret string,
client *http.Client,
) *Braintree {
return &Braintree{credentials: newClientAPIKey(env, clientId, clientSecret), HttpClient: client}
}

// NewWithAccessToken creates a Braintree client with an Access Token and customized http client.
func NewWithAccessTokenAndCustomizedHttpClient(accessToken string, client *http.Client) (*Braintree, error) {
c, err := newAccessToken(accessToken)
Expand Down Expand Up @@ -115,7 +124,12 @@ func (g *Braintree) executeVersion(ctx context.Context, method, path string, xml
}
}

url := g.MerchantURL() + "/" + path
baseUrl := g.Environment().BaseURL()
if g.MerchantID() != "" {
baseUrl = g.MerchantURL()
}

url := baseUrl + "/" + path

if g.Logger != nil {
g.Logger.Printf("> %s %s\n%s", method, url, buf.String())
Expand Down
30 changes: 30 additions & 0 deletions credentials_client_api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package braintree

import "encoding/base64"

type clientApiKey struct {
env Environment
clientId string
clientSecret string
}

func newClientAPIKey(env Environment, clientId, clientSecret string) credentials {
return clientApiKey{
env: env,
clientId: clientId,
clientSecret: clientSecret,
}
}

func (k clientApiKey) Environment() Environment {
return k.env
}

func (k clientApiKey) MerchantID() string {
return ""
}

func (k clientApiKey) AuthorizationHeader() string {
auth := k.clientId + ":" + k.clientSecret
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}
2 changes: 1 addition & 1 deletion oauth_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (g *OauthGateway) CreateTokenFromCode(ctx context.Context, oAuthCredentialR
return nil, err
}
switch resp.StatusCode {
case 201:
case 200:
return resp.oauth()
}
return nil, &invalidResponseError{resp}
Expand Down

0 comments on commit 13fb927

Please sign in to comment.