From d3fe344dc531419b1d8c469107d88ff21dfa371f Mon Sep 17 00:00:00 2001 From: Peter Ullrich Date: Sat, 6 Jan 2024 13:21:19 +0100 Subject: [PATCH] Add first_subscription_item to Subscriptions. Bump version --- CHANGELOG.md | 4 ++ README.md | 2 +- lib/lemon_ex/subscriptions/subscription.ex | 17 ++++++++ mix.exs | 2 +- .../subscriptions/subscriptions_test.exs | 23 ++++++++-- .../fixtures/api/v1/subscriptions_get.json | 43 +++++++++---------- .../fixtures/api/v1/subscriptions_list.json | 13 +----- .../fixtures/subscription_created_event.json | 10 ++++- 8 files changed, 75 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f03e2d..7b6159a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v0.2.2 + +* Add `first_subscription_item` to Subscriptions + ## v0.2.1 * Set all dependencies to version `>= 0.0.0` to prevent version conflicts with applications that use the library. diff --git a/README.md b/README.md index c6d02d4..bc17894 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ An Elixir client for the API and Webhooks of LemonSqueezy. ```elixir def deps do [ - {:lemon_ex, "~> 0.2.1"} + {:lemon_ex, "~> 0.2.2"} ] end ``` diff --git a/lib/lemon_ex/subscriptions/subscription.ex b/lib/lemon_ex/subscriptions/subscription.ex index e10f9d0..6edff4f 100644 --- a/lib/lemon_ex/subscriptions/subscription.ex +++ b/lib/lemon_ex/subscriptions/subscription.ex @@ -17,6 +17,7 @@ defmodule LemonEx.Subscriptions.Subscription do :cancelled, :trial_ends_at, :billing_anchor, + :first_subscription_item, :urls, :renews_at, :ends_at, @@ -48,6 +49,7 @@ defmodule LemonEx.Subscriptions.Subscription do pause: attributes["pause"], cancelled: attributes["cancelled"], billing_anchor: attributes["billing_anchor"], + first_subscription_item: first_subscription_item(attributes), trial_ends_at: attributes["trial_ends_at"], urls: %{ update_payment_method: urls["update_payment_method"], @@ -62,4 +64,19 @@ defmodule LemonEx.Subscriptions.Subscription do card_last_four: attributes["card_last_four"] } end + + defp first_subscription_item(%{"first_subscription_item" => nil}) do + nil + end + + defp first_subscription_item(%{"first_subscription_item" => item}) do + %{ + id: item["id"], + subscription_id: item["subscription_id"], + price_id: item["price_id"], + quantity: item["quantity"], + created_at: item["created_at"], + updated_at: item["updated_at"] + } + end end diff --git a/mix.exs b/mix.exs index d4369ae..471a729 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule LemonEx.MixProject do use Mix.Project - @version "0.2.1" + @version "0.2.2" @source_url "https://github.com/PJUllrich/lemon_ex" def project do diff --git a/test/lemon_ex/subscriptions/subscriptions_test.exs b/test/lemon_ex/subscriptions/subscriptions_test.exs index 9f02349..dd6800e 100644 --- a/test/lemon_ex/subscriptions/subscriptions_test.exs +++ b/test/lemon_ex/subscriptions/subscriptions_test.exs @@ -5,18 +5,18 @@ defmodule LemonEx.SubscriptionsTest do import Support.Fixtures, only: [load_json: 1] test "list" do - with_mock HTTPoison, get: succesful_response("api/v1/subscriptions_list") do + with_mock HTTPoison, get: successful_response("api/v1/subscriptions_list") do assert LemonEx.Subscriptions.list() == expected_subscription_list() end end test "get" do - with_mock HTTPoison, get: succesful_response("api/v1/subscriptions_get") do + with_mock HTTPoison, get: successful_response("api/v1/subscriptions_get") do assert LemonEx.Subscriptions.get(169_590) == expected_subscription() end end - defp succesful_response(json_file) do + defp successful_response(json_file) do fn _url, _params, _headers -> {:ok, %HTTPoison.Response{ @@ -59,6 +59,7 @@ defmodule LemonEx.SubscriptionsTest do cancelled: false, trial_ends_at: nil, billing_anchor: 27, + first_subscription_item: nil, urls: %{ customer_portal: "https:\\/\\/mystore.lemonsqueezy.com\\/billing?expires=1698434430&user=1461055&signature=27b0921cc4b6a37bc5caed624bc5dbaa44d374e26e892544421668561b8ff0f5", @@ -91,6 +92,14 @@ defmodule LemonEx.SubscriptionsTest do cancelled: false, trial_ends_at: nil, billing_anchor: 27, + first_subscription_item: %{ + id: 132_211, + subscription_id: 169_586, + price_id: 127_630, + quantity: 1, + created_at: "2023-10-27T07:27:30.000000Z", + updated_at: "2023-10-27T07:27:30.000000Z" + }, urls: %{ customer_portal: "https:\\/\\/mystore.lemonsqueezy.com\\/billing?expires=1698434430&user=1461055&signature=27b0921cc4b6a37bc5caed624bc5dbaa44d374e26e892544421668561b8ff0f5", @@ -129,6 +138,14 @@ defmodule LemonEx.SubscriptionsTest do cancelled: false, trial_ends_at: nil, billing_anchor: 27, + first_subscription_item: %{ + id: 132_215, + subscription_id: 169_590, + price_id: 127_630, + quantity: 1, + created_at: "2023-10-27T07:32:24.000000Z", + updated_at: "2023-10-27T07:32:24.000000Z" + }, urls: %{ customer_portal: "https://mystore.lemonsqueezy.com/billing?expires=1698445809&user=1461055&signature=d0accab17db6cd5fbd9243dd5b57afd131972d61ad81284d26b0dbfcc5e91119", diff --git a/test/support/fixtures/api/v1/subscriptions_get.json b/test/support/fixtures/api/v1/subscriptions_get.json index c9d95a9..af256cd 100644 --- a/test/support/fixtures/api/v1/subscriptions_get.json +++ b/test/support/fixtures/api/v1/subscriptions_get.json @@ -3,7 +3,7 @@ "version": "1.0" }, "links": { - "self": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590" + "self": "https://api.lemonsqueezy.com/v1/subscriptions/169590" }, "data": { "type": "subscriptions", @@ -32,13 +32,12 @@ "subscription_id": 169590, "price_id": 127630, "quantity": 1, - "is_usage_based": false, "created_at": "2023-10-27T07:32:24.000000Z", "updated_at": "2023-10-27T07:32:24.000000Z" }, "urls": { - "update_payment_method": "https:\/\/mystore.lemonsqueezy.com\/subscription\/169590\/payment-details?expires=1698510609&signature=09c4d3759f6e0c5124e3ab9714e0c87ba0a9ca3f19d73d30c8f81622ac15d9f2", - "customer_portal": "https:\/\/mystore.lemonsqueezy.com\/billing?expires=1698445809&user=1461055&signature=d0accab17db6cd5fbd9243dd5b57afd131972d61ad81284d26b0dbfcc5e91119" + "update_payment_method": "https://mystore.lemonsqueezy.com/subscription/169590/payment-details?expires=1698510609&signature=09c4d3759f6e0c5124e3ab9714e0c87ba0a9ca3f19d73d30c8f81622ac15d9f2", + "customer_portal": "https://mystore.lemonsqueezy.com/billing?expires=1698445809&user=1461055&signature=d0accab17db6cd5fbd9243dd5b57afd131972d61ad81284d26b0dbfcc5e91119" }, "renews_at": "2023-11-27T07:32:22.000000Z", "ends_at": null, @@ -49,55 +48,55 @@ "relationships": { "store": { "links": { - "related": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/store", - "self": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/relationships\/store" + "related": "https://api.lemonsqueezy.com/v1/subscriptions/169590/store", + "self": "https://api.lemonsqueezy.com/v1/subscriptions/169590/relationships/store" } }, "customer": { "links": { - "related": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/customer", - "self": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/relationships\/customer" + "related": "https://api.lemonsqueezy.com/v1/subscriptions/169590/customer", + "self": "https://api.lemonsqueezy.com/v1/subscriptions/169590/relationships/customer" } }, "order": { "links": { - "related": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/order", - "self": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/relationships\/order" + "related": "https://api.lemonsqueezy.com/v1/subscriptions/169590/order", + "self": "https://api.lemonsqueezy.com/v1/subscriptions/169590/relationships/order" } }, "order-item": { "links": { - "related": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/order-item", - "self": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/relationships\/order-item" + "related": "https://api.lemonsqueezy.com/v1/subscriptions/169590/order-item", + "self": "https://api.lemonsqueezy.com/v1/subscriptions/169590/relationships/order-item" } }, "product": { "links": { - "related": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/product", - "self": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/relationships\/product" + "related": "https://api.lemonsqueezy.com/v1/subscriptions/169590/product", + "self": "https://api.lemonsqueezy.com/v1/subscriptions/169590/relationships/product" } }, "variant": { "links": { - "related": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/variant", - "self": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/relationships\/variant" + "related": "https://api.lemonsqueezy.com/v1/subscriptions/169590/variant", + "self": "https://api.lemonsqueezy.com/v1/subscriptions/169590/relationships/variant" } }, "subscription-items": { "links": { - "related": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/subscription-items", - "self": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/relationships\/subscription-items" + "related": "https://api.lemonsqueezy.com/v1/subscriptions/169590/subscription-items", + "self": "https://api.lemonsqueezy.com/v1/subscriptions/169590/relationships/subscription-items" } }, "subscription-invoices": { "links": { - "related": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/subscription-invoices", - "self": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590\/relationships\/subscription-invoices" + "related": "https://api.lemonsqueezy.com/v1/subscriptions/169590/subscription-invoices", + "self": "https://api.lemonsqueezy.com/v1/subscriptions/169590/relationships/subscription-invoices" } } }, "links": { - "self": "https:\/\/api.lemonsqueezy.com\/v1\/subscriptions\/169590" + "self": "https://api.lemonsqueezy.com/v1/subscriptions/169590" } } -} \ No newline at end of file +} diff --git a/test/support/fixtures/api/v1/subscriptions_list.json b/test/support/fixtures/api/v1/subscriptions_list.json index 4d2ba47..3fe8901 100644 --- a/test/support/fixtures/api/v1/subscriptions_list.json +++ b/test/support/fixtures/api/v1/subscriptions_list.json @@ -39,15 +39,7 @@ "cancelled": false, "trial_ends_at": null, "billing_anchor": 27, - "first_subscription_item": { - "id": 132215, - "subscription_id": 169590, - "price_id": 127630, - "quantity": 1, - "is_usage_based": false, - "created_at": "2023-10-27T07:32:24.000000Z", - "updated_at": "2023-10-27T07:32:24.000000Z" - }, + "first_subscription_item": null, "urls": { "update_payment_method": "https:\\/\\/mystore.lemonsqueezy.com\\/subscription\\/169590\\/payment-details?expires=1698499230&signature=18cbb5bfc703292df67d0f3528e533e396115096a404bb1876c895805956a959", "customer_portal": "https:\\/\\/mystore.lemonsqueezy.com\\/billing?expires=1698434430&user=1461055&signature=27b0921cc4b6a37bc5caed624bc5dbaa44d374e26e892544421668561b8ff0f5" @@ -139,7 +131,6 @@ "subscription_id": 169586, "price_id": 127630, "quantity": 1, - "is_usage_based": false, "created_at": "2023-10-27T07:27:30.000000Z", "updated_at": "2023-10-27T07:27:30.000000Z" }, @@ -208,4 +199,4 @@ } } ] -} \ No newline at end of file +} diff --git a/test/support/fixtures/subscription_created_event.json b/test/support/fixtures/subscription_created_event.json index a6b4ba3..0708f4b 100644 --- a/test/support/fixtures/subscription_created_event.json +++ b/test/support/fixtures/subscription_created_event.json @@ -38,6 +38,14 @@ "order_item_id": 1, "trial_ends_at": null, "billing_anchor": 14, + "first_subscription_item": { + "id": 1, + "subscription_id": 1, + "price_id": 1, + "quantity": 5, + "created_at": "2021-08-11T13:47:28.000000Z", + "updated_at": "2021-08-11T13:47:28.000000Z" + }, "card_last_four": "4242", "status_formatted": "Active" }, @@ -86,4 +94,4 @@ } } } -} \ No newline at end of file +}