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.

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:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/payabli/subscription/client.rb', line 25

def get_subscription(request_options: {}, **params)
  params = Payabli::Internal::Types::Utils.normalize_keys(params)
  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)}",
    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::Subscription::Types::AddSubscriptionResponse

Creates a subscription or scheduled payment to run at a specified time and frequency.

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:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/payabli/subscription/client.rb', line 60

def new_subscription(request_options: {}, **params)
  params = Payabli::Internal::Types::Utils.normalize_keys(params)
  query_param_names = %i[force_customer_creation]
  query_params = {}
  query_params["forceCustomerCreation"] = params[:force_customer_creation] if params.key?(:force_customer_creation)
  params = params.except(*query_param_names)

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

  request = Payabli::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "Subscription/add",
    headers: headers,
    query: query_params,
    body: Payabli::Subscription::Types::SubscriptionRequestBody.new(params).to_h,
    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::Subscription::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::Subscription::Types::RemoveSubscriptionResponse

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

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:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/payabli/subscription/client.rb', line 105

def remove_subscription(request_options: {}, **params)
  params = Payabli::Internal::Types::Utils.normalize_keys(params)
  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)}",
    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::Subscription::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::Subscription::Types::UpdateSubscriptionResponse

Updates a subscription’s details.

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:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/payabli/subscription/client.rb', line 139

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 = ["subId"]
  body = request_data.except(*non_body_param_names)

  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)}",
    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::Subscription::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