diff --git a/lib/stripe/api.ex b/lib/stripe/api.ex index 88d0b2d3e..813733fe6 100644 --- a/lib/stripe/api.ex +++ b/lib/stripe/api.ex @@ -171,6 +171,10 @@ defmodule Stripe.API do base_url = get_upload_url() req_url = base_url <> endpoint + req_body = + body + |> Stripe.Util.map_keys_to_atoms() + |> Stripe.URI.encode_query() req_headers = headers |> add_multipart_form_headers() @@ -183,7 +187,7 @@ defmodule Stripe.API do |> add_default_options() |> add_pool_option() - @http_module.request(method, req_url, req_headers, body, req_opts) + @http_module.request(method, req_url, req_headers, req_body, req_opts) |> handle_response() end diff --git a/lib/stripe/core_resources/file_upload.ex b/lib/stripe/core_resources/file_upload.ex index 4098972dc..ae2cab1e4 100644 --- a/lib/stripe/core_resources/file_upload.ex +++ b/lib/stripe/core_resources/file_upload.ex @@ -47,7 +47,7 @@ defmodule Stripe.FileUpload do |> put_endpoint(@plural_endpoint) |> put_method(:post) |> put_params(params) - |> make_request() + |> make_file_upload_request() end @doc """ @@ -58,7 +58,7 @@ defmodule Stripe.FileUpload do new_request(opts) |> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}") |> put_method(:get) - |> make_request() + |> make_file_upload_request() end @doc """ @@ -71,6 +71,6 @@ defmodule Stripe.FileUpload do |> put_method(:get) |> put_params(params) |> cast_to_id([:ending_before, :starting_after, :limit, :purpose]) - |> make_request() + |> make_file_upload_request() end end diff --git a/lib/stripe/request.ex b/lib/stripe/request.ex index 0fb41fd74..01fa14569 100644 --- a/lib/stripe/request.ex +++ b/lib/stripe/request.ex @@ -156,6 +156,20 @@ defmodule Stripe.Request do end end + @doc """ + Executes the request and returns the response for file uploads + """ + @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 + 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)} + end + end + defp do_cast_to_id(params, cast_to_id) do to_cast = MapSet.to_list(cast_to_id) params = Enum.reduce(to_cast, params, fn diff --git a/test/test_helper.exs b/test/test_helper.exs index f7acc1800..0f9eb2f41 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -8,6 +8,7 @@ Logger.configure level: :info {:ok, pid} = Stripe.StripeMock.start_link port: 12123, global: true Process.sleep(250) Application.put_env(:stripity_stripe, :api_base_url, "http://localhost:12123/v1/") +Application.put_env(:stripity_stripe, :api_upload_url, "http://localhost:12123/v1/") Application.put_env(:stripity_stripe, :api_key, "sk_test_123") defmodule Helper do