Skip to content

Commit

Permalink
Add GET transaction info api call (#5)
Browse files Browse the repository at this point in the history
* Add intial steps for transaction info

* Adding some test resources from the python lib

Available at: https://github.com/apple/app-store-server-library-python

* Add 200 response for get_transacion_info test

* Remove support files

* Remove decode function. Not within the scope of this PR

* Bump minor version to 0.2.0

* Revert to v0.1.0, add details to CHANGELOG

* remove empty line
  • Loading branch information
mrnovalles authored Jun 25, 2024
1 parent fea3d2e commit 933658f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
## v0.1.0

Initial release

## v0.2.0

- Added a call to GET transaction info by transaction ID `AppStore.API.TransactionInfo`
10 changes: 9 additions & 1 deletion lib/app_store/api/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ defmodule AppStore.API do
The main module to interact with the App Store Server APIs
"""

alias AppStore.API.{TransactionHistory, SubscriptionStatus, ConsumptionInformation}
alias AppStore.API.{
TransactionHistory,
TransactionInfo,
SubscriptionStatus,
ConsumptionInformation
}

defdelegate get_transaction_history(
api_config,
Expand All @@ -13,6 +18,9 @@ defmodule AppStore.API do
),
to: TransactionHistory

defdelegate get_transaction_info(api_config, token, transaction_id),
to: TransactionInfo

defdelegate get_subscription_statuses(api_config, token, original_transaction_id),
to: SubscriptionStatus

Expand Down
24 changes: 24 additions & 0 deletions lib/app_store/api/transaction_info.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule AppStore.API.TransactionInfo do
@moduledoc """
The module for Transaction Info
"""

alias AppStore.API.{Config, Response, Error, HTTP}

@type transaction_id :: String.t()

@path_prefix "/inApps/v1/transactions/"

@doc """
Get information about a single transaction for your app.
Official documentation: https://developer.apple.com/documentation/appstoreserverapi/get_transaction_info
"""
@spec get_transaction_info(Config.t(), String.t(), transaction_id) ::
{:error, Error.t()} | {:ok, Response.t()}
def get_transaction_info(%Config{} = api_config, token, transaction_id) do
path = "#{@path_prefix}/#{transaction_id}"

HTTP.get(api_config, token, path)
end
end
3 changes: 0 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ defmodule AppStore.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}

# runtime deps
{:finch, "~> 0.6"},
{:jason, "~> 1.0"},
Expand Down
45 changes: 45 additions & 0 deletions test/app_store/api/transaction_info_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule AppStore.API.TransactionInfoTest do
use AppStore.TestCase, async: false

alias AppStore.API.TransactionInfo

describe "get_transaction_info/3" do
test "returns 200 and an expected response body", %{bypass: bypass, app_store: app_store} do
expected_body = %{signedTransactionInfo: "signed_transaction_info_value"} |> Jason.encode!()

Bypass.expect_once(bypass, "GET", "/inApps/v1/transactions/1234", fn conn ->
conn
|> Plug.Conn.put_resp_header("server", "daiquiri/3.0.0")
|> Plug.Conn.resp(200, expected_body)
end)

{:ok, %AppStore.API.Response{body: body, status: status}} =
TransactionInfo.get_transaction_info(
app_store.api_config,
"token",
"1234"
)

assert status === 200
assert body === expected_body
end

test "returns 401 for unauthenticated request", %{bypass: bypass, app_store: app_store} do
Bypass.expect_once(bypass, "GET", "/inApps/v1/transactions/transaction-id", fn conn ->
conn
|> Plug.Conn.put_resp_header("server", "daiquiri/3.0.0")
|> Plug.Conn.resp(401, "Unauthenticated\n\nRequest ID: PXYVB35MOBBC5TL6UOXY6DGJGY.0.0\n")
end)

{:ok, %AppStore.API.Response{body: body, status: status}} =
TransactionInfo.get_transaction_info(
app_store.api_config,
"token",
"transaction-id"
)

assert status === 401
assert body === "Unauthenticated\n\nRequest ID: PXYVB35MOBBC5TL6UOXY6DGJGY.0.0\n"
end
end
end

0 comments on commit 933658f

Please sign in to comment.