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
Changes from 1 commit
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
Next Next commit
Allow returning responses as json.
mindreader committed Jun 9, 2023
commit 16ad9fdc2386b9b034e3df12df0010f580700c40
4 changes: 4 additions & 0 deletions lib/stripe.ex
Original file line number Diff line number Diff line change
@@ -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 structs
from it.

### HTTP Connection Pool

16 changes: 13 additions & 3 deletions lib/stripe/request.ex
Original file line number Diff line number Diff line change
@@ -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

@@ -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

19 changes: 12 additions & 7 deletions lib/stripe/webhook.ex
Original file line number Diff line number Diff line change
@@ -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
@@ -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