Class: AbacatePay::Clients::SubscriptionClient
- Defined in:
- lib/abacate_pay/clients/subscription_client.rb
Overview
Client for recurring subscriptions in the AbacatePay API.
See Enums::Products::Cycles for the supported billing cycles.
Constant Summary collapse
- URI =
"subscriptions"
Constants inherited from Client
Client::RETRIABLE_EXCEPTIONS, Client::RETRIABLE_METHODS, Client::RETRIABLE_STATUSES
Instance Method Summary collapse
-
#cancel(id) ⇒ Resources::Subscriptions
Cancels an active subscription immediately.
-
#change_plan(id, product_id:, quantity: 1) ⇒ Hash
Changes the main product of an active subscription.
- #create(data) ⇒ Resources::Subscriptions
-
#initialize(client = nil) ⇒ SubscriptionClient
constructor
A new instance of SubscriptionClient.
- #list(**params) ⇒ Array<Resources::Subscriptions>
-
#record_usage(id, product_id:, units:, action: "add") ⇒ Hash
Records usage of a pay-as-you-go product on an active subscription.
Methods inherited from Client
Constructor Details
#initialize(client = nil) ⇒ SubscriptionClient
Returns a new instance of SubscriptionClient.
11 12 13 |
# File 'lib/abacate_pay/clients/subscription_client.rb', line 11 def initialize(client = nil) super(URI, client) end |
Instance Method Details
#cancel(id) ⇒ Resources::Subscriptions
Cancels an active subscription immediately. Pending future instalments are cancelled along with it.
49 50 51 52 |
# File 'lib/abacate_pay/clients/subscription_client.rb', line 49 def cancel(id) response = request("POST", "cancel", json: { id: id }) Resources::Subscriptions.new(response) end |
#change_plan(id, product_id:, quantity: 1) ⇒ Hash
Changes the main product of an active subscription. The new price takes effect on the next billing cycle — the current cycle is untouched.
62 63 64 65 66 |
# File 'lib/abacate_pay/clients/subscription_client.rb', line 62 def change_plan(id, product_id:, quantity: 1) raise ArgumentError, "quantity must be at least 1, got #{quantity.inspect}" if quantity.to_i < 1 request("POST", "change-plan", json: { id: id, productId: product_id, quantity: quantity.to_i }) end |
#create(data) ⇒ Resources::Subscriptions
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/abacate_pay/clients/subscription_client.rb', line 24 def create(data) request_data = { methods: data.methods, externalId: data.external_id, customerId: data.customer&.id, items: data.products&.map do |product| { externalId: product.external_id, name: product.name, description: product.description, quantity: product.quantity, price: product.price } end } response = request("POST", "create", json: request_data) Resources::Subscriptions.new(response) end |
#list(**params) ⇒ Array<Resources::Subscriptions>
17 18 19 20 |
# File 'lib/abacate_pay/clients/subscription_client.rb', line 17 def list(**params) response = request("GET", "list", params: params.empty? ? nil : params) build_list(response, Resources::Subscriptions) end |
#record_usage(id, product_id:, units:, action: "add") ⇒ Hash
Records usage of a pay-as-you-go product on an active subscription. The amount is added to the next pending instalment of the cycle.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/abacate_pay/clients/subscription_client.rb', line 78 def record_usage(id, product_id:, units:, action: "add") raise ArgumentError, "units must be at least 1, got #{units.inspect}" if units.to_i < 1 unless %w[add subtract].include?(action.to_s) raise ArgumentError, "action must be \"add\" or \"subtract\", got #{action.inspect}" end request("POST", "record-usage", json: { id: id, productId: product_id, units: units.to_i, action: action.to_s }) end |