Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow returning responses as json. #805

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/stripe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ defmodule Stripe do
from Stripe. See [https://stripe.com/docs/api/expanding_objects](https://stripe.com/docs/api/expanding_objects)
* `:idempotency_key` - A string that is passed through as the "Idempotency-Key" header on all POST requests. This is used by Stripe's idempotency layer to manage
duplicate requests to the stripe API. See [https://stripe.com/docs/api/idempotent_requests](https://stripe.com/docs/api/idempotent_requests)
* `:response_as_json` - If set to `true`, the response will be returned as a
JSON string instead of a struct. This is useful for persisting the original
response from stripe. You can use Stripe.Converter.convert_result/1 to get structs
from it.
Comment on lines +36 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there, I need this myself, but instead, I want maps.

I like what you are going, but I would instead use an enum and avoid booleans.

It could be (just an idea, please share your thoughts on the naming):

@type response_as :: :map | :struct | :raw

What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is reasonable. I will make the changes when I next get a few moments.


### HTTP Connection Pool

Expand Down
16 changes: 13 additions & 3 deletions lib/stripe/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ defmodule Stripe.Request do
with {:ok, params} <- do_cast_to_id(params, request.cast_to_id),
{:ok, endpoint} <- consolidate_endpoint(endpoint, params),
{:ok, result} <- API.request(params, method, endpoint, headers, opts) do
{:ok, Converter.convert_result(result)}
if Keyword.get(opts, :response_as_json, false) do
{:ok, result}
else
{:ok, Converter.convert_result(result)}
end
end
end

Expand All @@ -210,11 +214,17 @@ defmodule Stripe.Request do
@spec make_file_upload_request(t) :: {:ok, struct} | {:error, Stripe.Error.t()}
def make_file_upload_request(
%Request{params: params, endpoint: endpoint, method: method, opts: opts} = request
) do
) do


with {:ok, params} <- do_cast_to_id(params, request.cast_to_id),
{:ok, endpoint} <- consolidate_endpoint(endpoint, params),
{:ok, result} <- API.request_file_upload(params, method, endpoint, %{}, opts) do
{:ok, Converter.convert_result(result)}
if Keyword.get(opts, :response_as_json, false) do
{:ok, result}
else
{:ok, Converter.convert_result(result)}
end
end
end

Expand Down
19 changes: 12 additions & 7 deletions lib/stripe/webhook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ defmodule Stripe.Webhook do
# Reject webhook by responding with non-2XX
end
"""
@spec construct_event(String.t(), String.t(), String.t(), integer) ::
@spec construct_event(String.t(), String.t(), String.t(), integer, opts :: Keyword.t()) ::
{:ok, Stripe.Event.t()} | {:error, any}
def construct_event(payload, signature_header, secret, tolerance \\ @default_tolerance) do
def construct_event(payload, signature_header, secret, tolerance \\ @default_tolerance, opts \\ []) do
case verify_header(payload, signature_header, secret, tolerance) do
:ok ->
{:ok, convert_to_event!(payload)}

if Keyword.get(opts, :response_as_json, false) do
{:ok, convert_to_json!(payload)}
else
{:ok, convert_to_event!(payload)}
end
error ->
error
end
Expand Down Expand Up @@ -146,9 +149,11 @@ defmodule Stripe.Webhook do
|> secure_compare(input, expected)
end

defp convert_to_json!(payload) do
payload |> Stripe.API.json_library().decode!()
end

defp convert_to_event!(payload) do
payload
|> Stripe.API.json_library().decode!()
|> Stripe.Converter.convert_result()
payload |> convert_to_json!() |> Stripe.Converter.convert_result()
end
end