Class: Payabli::Subscription::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/payabli/subscription/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void

Parameters:



9
10
11
# File 'lib/payabli/subscription/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#get_subscription(request_options: {}, **params) ⇒ Payabli::Types::SubscriptionQueryRecords

Retrieves a single subscription's details.

Examples:

client.subscription.get_subscription(sub_id: 231)

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :sub_id (Integer)

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/payabli/subscription/client.rb', line 28

def get_subscription(request_options: {}, **params)
  params = Payabli::Internal::Types::Utils.normalize_keys(params)
  headers = @client.auth_headers_for_endpoint(security: [{ "BearerAuth" => [] }, { "APIKeyAuth" => [] }])
  request = Payabli::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "Subscription/#{URI.encode_uri_component(params[:sub_id].to_s)}",
    headers: headers,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Payabli::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Payabli::Types::SubscriptionQueryRecords.load(response.body)
  else
    error_class = Payabli::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#new_subscription(request_options: {}, **params) ⇒ Payabli::Types::AddSubscriptionResponse

Creates a subscription or scheduled payment to run at a specified time and frequency. You can use stored payment method tokens for card, ACH, and digital wallets by passing them into the paymentMethod.storedMethodId field.

Examples:

client.subscription.new_subscription(
  customer_data: {
    customer_id: 4440
  },
  entry_point: "8cfec329267",
  payment_details: {
    service_fee: 0,
    total_amount: 100
  },
  payment_method: {
    cardcvv: "123",
    cardexp: "12/29",
    card_holder: "John Cassian",
    cardnumber: "4111111111111111",
    cardzip: "37615",
    initiator: "payor",
    method_: "card"
  },
  schedule_details: {
    end_date: "2025-03-20",
    frequency: "weekly",
    plan_id: 1,
    start_date: "2024-09-20"
  }
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :force_customer_creation (Boolean, nil)
  • :idempotency_key (String, nil)

Returns:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/payabli/subscription/client.rb', line 179

def new_subscription(request_options: {}, **params)
  params = Payabli::Internal::Types::Utils.normalize_keys(params)
  request_data = Payabli::Subscription::Types::RequestSchedule.new(params).to_h
  non_body_param_names = %w[forceCustomerCreation idempotencyKey]
  body = request_data.except(*non_body_param_names)

  query_params = {}
  query_params["forceCustomerCreation"] = params[:force_customer_creation] if params.key?(:force_customer_creation)

  headers = {}
  headers["idempotencyKey"] = params[:idempotency_key] if params[:idempotency_key]

  headers = @client.auth_headers_for_endpoint(security: [{ "BearerAuth" => [] }, { "APIKeyAuth" => [] }]).merge(headers)
  request = Payabli::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "Subscription/add",
    headers: headers,
    query: query_params,
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Payabli::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Payabli::Types::AddSubscriptionResponse.load(response.body)
  else
    error_class = Payabli::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#remove_subscription(request_options: {}, **params) ⇒ Payabli::Types::RemoveSubscriptionResponse

Deletes a subscription, autopay, or recurring payment and prevents future charges.

Examples:

client.subscription.remove_subscription(sub_id: 231)

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :sub_id (Integer)

Returns:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/payabli/subscription/client.rb', line 114

def remove_subscription(request_options: {}, **params)
  params = Payabli::Internal::Types::Utils.normalize_keys(params)
  headers = @client.auth_headers_for_endpoint(security: [{ "BearerAuth" => [] }, { "APIKeyAuth" => [] }])
  request = Payabli::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "DELETE",
    path: "Subscription/#{URI.encode_uri_component(params[:sub_id].to_s)}",
    headers: headers,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Payabli::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Payabli::Types::RemoveSubscriptionResponse.load(response.body)
  else
    error_class = Payabli::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#update_subscription(request_options: {}, **params) ⇒ Payabli::Types::UpdateSubscriptionResponse

Updates a subscription's details.

Examples:

client.subscription.update_subscription(
  sub_id: 231,
  set_pause: true
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :sub_id (Integer)

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/payabli/subscription/client.rb', line 70

def update_subscription(request_options: {}, **params)
  params = Payabli::Internal::Types::Utils.normalize_keys(params)
  request_data = Payabli::Subscription::Types::RequestUpdateSchedule.new(params).to_h
  non_body_param_names = %w[subId]
  body = request_data.except(*non_body_param_names)

  headers = @client.auth_headers_for_endpoint(security: [{ "BearerAuth" => [] }, { "APIKeyAuth" => [] }])
  request = Payabli::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "PUT",
    path: "Subscription/#{URI.encode_uri_component(params[:sub_id].to_s)}",
    headers: headers,
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Payabli::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Payabli::Types::UpdateSubscriptionResponse.load(response.body)
  else
    error_class = Payabli::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end