Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Add reporting entities (beam-community#751)
Browse files Browse the repository at this point in the history
* Add reporting entities

* Add tests for report runs
  • Loading branch information
mattnenterprise authored Sep 27, 2022
1 parent 879338a commit 1c34ec9
Show file tree
Hide file tree
Showing 23 changed files with 233 additions and 38 deletions.
3 changes: 2 additions & 1 deletion lib/stripe/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ defmodule Stripe.API do
end

@spec add_idempotency_headers(headers, method) :: headers
defp add_idempotency_headers(existing_headers, method) when method in [:get, :head, :put, :delete] do
defp add_idempotency_headers(existing_headers, method)
when method in [:get, :head, :put, :delete] do
existing_headers
end

Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/billing_portal/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Stripe.BillingPortal.Session do

@type create_params :: %{
:customer => String.t(),
optional(:return_url) => String.t(),
optional(:return_url) => String.t(),
optional(:locale) => String.t()
}

Expand Down
14 changes: 7 additions & 7 deletions lib/stripe/checkout/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ defmodule Stripe.Session do
optional(:dynamic_tax_rates) => list(String.t()),
optional(:price) => String.t(),
optional(:price_data) => price_data,
optional(:taxes) => list(map),
optional(:taxes) => list(map)
},
optional(:has_more) => boolean,
optional(:url) => String.t(),
optional(:url) => String.t()
}

@type adjustable_quantity :: %{
Expand Down Expand Up @@ -277,13 +277,13 @@ defmodule Stripe.Session do
@type payment_status :: String.t()

@type phone_number_collection :: %{
:enabled => boolean()
}
:enabled => boolean()
}

@type shipping_option :: %{
:shipping_amount => non_neg_integer(),
:shipping_rate => String.t()
}
:shipping_amount => non_neg_integer(),
:shipping_rate => String.t()
}

@typedoc """
One of `"open"`, `"complete"`, or `"expired"`.
Expand Down
2 changes: 2 additions & 0 deletions lib/stripe/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ defmodule Stripe.Converter do
promotion_code
recipient
refund
reporting.report_run
reporting.report_type
review
search_result
setup_intent
Expand Down
4 changes: 1 addition & 3 deletions lib/stripe/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ defmodule Stripe.Error do
source: :network,
code: :network_error,
message:
"An error occurred while making the network request. The HTTP client returned the following reason: #{
inspect(reason)
}",
"An error occurred while making the network request. The HTTP client returned the following reason: #{inspect(reason)}",
extra: %{
hackney_reason: reason
}
Expand Down
89 changes: 89 additions & 0 deletions lib/stripe/reporting/report_run.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
defmodule Stripe.Reporting.ReportRun do
@moduledoc """
Work with Stripe Report Run objects.
You can:
- Create a report run
- Retrieve a report run
- List all report runs
Stripe API reference: https://stripe.com/docs/api/reporting/report_run
"""

use Stripe.Entity
import Stripe.Request

@typedoc """
One of `"pending"`, `"succeeded"`, or `"failed"`.
"""
@type status :: String.t()

@type t :: %__MODULE__{
id: Stripe.id(),
object: String.t(),
parameters: map(),
report_type: String.t(),
result: map(),
status: status(),
created: Stripe.timestamp(),
error: String.t(),
livemode: boolean(),
succeeded_at: Stripe.timestamp()
}

defstruct [
:id,
:object,
:parameters,
:report_type,
:result,
:status,
:created,
:error,
:livemode,
:succeeded_at
]

@plural_endpoint "reporting/report_runs"

@spec create(params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params:
%{
:report_type => String.t(),
optional(:parameters) => map()
}
| %{}
def create(params, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint)
|> put_params(params)
|> put_method(:post)
|> make_request()
end

@spec retrieve(Stripe.id() | t, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def retrieve(id, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:get)
|> make_request()
end

@spec list(params, Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()}
when params: %{
optional(:created) => Stripe.date_query(),
optional(:ending_before) => t | Stripe.id(),
optional(:limit) => 1..100,
optional(:starting_after) => t | Stripe.id()
}
def list(params \\ %{}, opts \\ []) do
new_request(opts)
|> prefix_expansions()
|> put_endpoint(@plural_endpoint)
|> put_method(:get)
|> put_params(params)
|> cast_to_id([:ending_before, :starting_after])
|> make_request()
end
end
58 changes: 58 additions & 0 deletions lib/stripe/reporting/report_type.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule Stripe.Reporting.ReportType do
@moduledoc """
Work with Stripe Report Type objects.
You can:
- Retrieve a report type
- List all report types
Stripe API reference: https://stripe.com/docs/api/reporting/report_type
"""

use Stripe.Entity
import Stripe.Request

@type t :: %__MODULE__{
id: Stripe.id(),
object: String.t(),
data_available_end: Stripe.timestamp(),
data_available_start: Stripe.timestamp(),
name: String.t(),
default_columns: list(String.t()),
livemode: boolean(),
updated: Stripe.timestamp(),
version: integer()
}

defstruct [
:id,
:object,
:data_available_end,
:data_available_start,
:name,
:default_columns,
:livemode,
:updated,
:version
]

@plural_endpoint "reporting/report_types"

@spec retrieve(Stripe.id() | t, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def retrieve(id, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:get)
|> make_request()
end

@spec list(Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()}
def list(opts \\ []) do
new_request(opts)
|> prefix_expansions()
|> put_endpoint(@plural_endpoint)
|> put_method(:get)
|> make_request()
end
end
2 changes: 1 addition & 1 deletion lib/stripe/subscriptions/credit_note.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Stripe.CreditNote do

@type discount :: %{
amount: integer,
discount: String.t(),
discount: String.t()
}

@type t :: %__MODULE__{
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/subscriptions/credit_note_line_item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Stripe.CreditNoteLineItem do

@type discount :: %{
amount: integer,
discount: String.t(),
discount: String.t()
}

@type t :: %__MODULE__{
Expand Down
9 changes: 6 additions & 3 deletions lib/stripe/subscriptions/invoice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ defmodule Stripe.Invoice do
optional(:metadata) => Stripe.Types.metadata(),
optional(:payment_settings) => map,
optional(:statement_descriptor) => String.t(),
optional(:subscription) => Stripe.id() | Stripe.Subscription.t(),
optional(:subscription) => Stripe.id() | Stripe.Subscription.t()
}
| %{}
def create(params, opts \\ []) do
Expand Down Expand Up @@ -280,7 +280,7 @@ defmodule Stripe.Invoice do
optional(:metadata) => Stripe.Types.metadata(),
optional(:paid) => boolean,
optional(:payment_settings) => map,
optional(:statement_descriptor) => String.t(),
optional(:statement_descriptor) => String.t()
}
| %{}
def update(id, params, opts \\ []) do
Expand All @@ -304,7 +304,10 @@ defmodule Stripe.Invoice do
@spec upcoming(map, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def upcoming(params, opts \\ [])
def upcoming(params = %{customer: _customer}, opts), do: get_upcoming(params, opts)
def upcoming(params = %{customer_details: _customer_details}, opts), do: get_upcoming(params, opts)

def upcoming(params = %{customer_details: _customer_details}, opts),
do: get_upcoming(params, opts)

def upcoming(params = %{subscription: _subscription}, opts), do: get_upcoming(params, opts)

defp get_upcoming(params, opts) do
Expand Down
12 changes: 7 additions & 5 deletions lib/stripe/subscriptions/line_item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ defmodule Stripe.LineItem do
}

@type proration_details :: %{
credited_items: %{
invoice: String.t(),
invoice_line_items: [String.t()]
} | nil
}
credited_items:
%{
invoice: String.t(),
invoice_line_items: [String.t()]
}
| nil
}

@type t :: %__MODULE__{
id: Stripe.id(),
Expand Down
10 changes: 6 additions & 4 deletions lib/stripe/subscriptions/subscription_schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Stripe.SubscriptionSchedule do
billing_thresholds: Stripe.Types.collection_method_thresholds(),
price: String.t(),
quantity: pos_integer,
tax_rates: [Stripe.TaxRate.t()],
tax_rates: [Stripe.TaxRate.t()]
}

@type phases :: %{
Expand Down Expand Up @@ -78,7 +78,7 @@ defmodule Stripe.SubscriptionSchedule do
:metadata,
:end_behavior,
:revision,
:test_clock,
:test_clock
]

@plural_endpoint "subscription_schedules"
Expand Down Expand Up @@ -107,7 +107,8 @@ defmodule Stripe.SubscriptionSchedule do
%{
:items => [
%{
optional(:billing_thresholds) => Stripe.Types.collection_method_thresholds(),
optional(:billing_thresholds) =>
Stripe.Types.collection_method_thresholds(),
optional(:price) => Stripe.id() | Stripe.Price.t(),
optional(:quantity) => non_neg_integer,
optional(:tax_rates) => [Stripe.TaxRate.t()]
Expand Down Expand Up @@ -161,7 +162,8 @@ defmodule Stripe.SubscriptionSchedule do
%{
:items => [
%{
optional(:billing_thresholds) => Stripe.Types.collection_method_thresholds(),
optional(:billing_thresholds) =>
Stripe.Types.collection_method_thresholds(),
optional(:price) => Stripe.id() | Stripe.Price.t(),
optional(:quantity) => non_neg_integer,
optional(:tax_rates) => [Stripe.TaxRate.t()]
Expand Down
6 changes: 6 additions & 0 deletions lib/stripe/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ defmodule Stripe.Util do
def object_name_to_module("identity.verification_report"),
do: Stripe.Identity.VerificationReport

def object_name_to_module("reporting.report_type"),
do: Stripe.Reporting.ReportType

def object_name_to_module("reporting.report_run"),
do: Stripe.Reporting.ReportRun

def object_name_to_module("issuing.authorization"), do: Stripe.Issuing.Authorization
def object_name_to_module("issuing.card"), do: Stripe.Issuing.Card
def object_name_to_module("issuing.cardholder"), do: Stripe.Issuing.Cardholder
Expand Down
3 changes: 2 additions & 1 deletion test/stripe/converter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ defmodule Stripe.ConverterTest do
%Stripe.Customer{
address: nil,
balance: 0,
created: 1656364912,
created: 1_656_364_912,
currency: "usd",
default_source: nil,
deleted: nil,
Expand Down Expand Up @@ -213,6 +213,7 @@ defmodule Stripe.ConverterTest do
total_count: nil,
url: "/v1/customers/search"
}

fixture = Helper.load_fixture("customer_search.json")
result = Converter.convert_result(fixture)

Expand Down
1 change: 1 addition & 0 deletions test/stripe/core_resources/charge_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Stripe.ChargeTest do

test "is searchable" do
search_query = "amount>999 AND metadata['order_id']:'6735'"

assert {:ok, %Stripe.SearchResult{data: charges}} =
Stripe.Charge.search(%{query: search_query})

Expand Down
1 change: 1 addition & 0 deletions test/stripe/core_resources/customer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ defmodule Stripe.CustomerTest do

test "is searchable" do
search_query = "name:'fakename' AND metadata['foo']:'bar'"

assert {:ok, %Stripe.SearchResult{data: customers}} =
Stripe.Customer.search(%{query: search_query})

Expand Down
1 change: 1 addition & 0 deletions test/stripe/core_resources/payment_intent_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Stripe.PaymentIntentTest do

test "is searchable" do
search_query = "status:'succeeded' AND metadata['order_id']:'6735'"

assert {:ok, %Stripe.SearchResult{data: payment_intents}} =
Stripe.PaymentIntent.search(%{query: search_query})

Expand Down
4 changes: 2 additions & 2 deletions test/stripe/core_resources/price_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ defmodule Stripe.PriceTest do
end
end


describe "search/2" do
test "searches Prices" do
search_query = "active:'true' AND metadata['order_id']:'6735'"

assert {:ok, %Stripe.SearchResult{data: prices}} =
Stripe.Price.search(%{query: search_query})
Stripe.Price.search(%{query: search_query})

assert_stripe_requested(:get, "/v1/prices/search", query: [query: search_query])
assert is_list(prices)
Expand Down
Loading

0 comments on commit 1c34ec9

Please sign in to comment.