Skip to content

Commit

Permalink
Allow configuration of sandbox endpoint for testing (#86)
Browse files Browse the repository at this point in the history
* Allows sandbox endpoint configuration, uses `@sandbox_endpoint` as
  default
* Builds keyword list at runtime to prevent stale config
* Updates readme to inform library users of new config
  • Loading branch information
cjfreeze authored May 11, 2020
1 parent cc47c76 commit fcc1e36
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ testing][plp].
[dtc]: https://articles.braintreepayments.com/control-panel/transactions/duplicate-checking
[plp]: https://developers.braintreepayments.com/guides/paypal/testing-go-live/php#linked-paypal-testing

### Testing Using Only `localhost`

You can optionally configure the sandbox endpoint url to point towards a local url and
port for testing which does not need to call out to the Braintree sandbox API.
For example, in your `config.exs`
```
config :braintree, :sandbox_endpoint, "localhost:4001"
```
In conjuction with a libary such as [`Bypass`](https://github.com/PSPDFKit-labs/bypass)
you can use this config to define test-specific returns from `Braintree` calls without
hitting the Braintree sandbox API.

## License

MIT License, see [LICENSE.txt](LICENSE.txt) for details.
20 changes: 15 additions & 5 deletions lib/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ defmodule Braintree.HTTP do
| {:error, Error.t()}
| {:error, binary}

@endpoints [
production: "https://api.braintreegateway.com/merchants/",
sandbox: "https://api.sandbox.braintreegateway.com/merchants/"
]
@production_endpoint "https://api.braintreegateway.com/merchants/"
@sandbox_endpoint "https://api.sandbox.braintreegateway.com/merchants/"

@cacertfile "/certs/api_braintreegateway_com.ca.crt"

Expand Down Expand Up @@ -131,7 +129,7 @@ defmodule Braintree.HTTP do
environment = opts |> get_lazy_env(:environment) |> maybe_to_atom()
merchant_id = get_lazy_env(opts, :merchant_id)

Keyword.fetch!(@endpoints, environment) <> merchant_id <> "/" <> path
Keyword.fetch!(endpoints(), environment) <> merchant_id <> "/" <> path
end

defp maybe_to_atom(value) when is_binary(value), do: String.to_existing_atom(value)
Expand Down Expand Up @@ -199,4 +197,16 @@ defmodule Braintree.HTTP do
defp resolve_error_response(%{"unprocessable_entity" => _}) do
Error.new(%{message: "Unprocessable Entity"})
end

defp endpoints do
[production: @production_endpoint, sandbox: sandbox_endpoint()]
end

defp sandbox_endpoint do
Application.get_env(
:braintree,
:sandbox_endpoint,
@sandbox_endpoint
)
end
end

0 comments on commit fcc1e36

Please sign in to comment.