Class: Whop_sdk::Payments::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/whop_sdk/payments/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void



9
10
11
# File 'lib/whop_sdk/payments/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#create(request_options: {}, **params) ⇒ Whop_sdk::Payments::Types::CreatePaymentsResponse

Charge an existing member off-session using one of their stored payment methods. You can provide an existing plan, or create a new one in-line. This endpoint will respond with a payment object immediately, but the payment is processed asynchronously in the background. Use webhooks to be notified when the payment succeeds or fails.

Required permissions:

  • payment:charge
  • plan:create
  • access_pass:create
  • access_pass:update
  • plan:basic:read
  • access_pass:basic:read
  • member:email:read
  • member:basic:read
  • member:phone:read
  • promo_code:basic:read
  • shipment:basic:read
  • payment:dispute:read
  • payment:resolution_center_case:read

Examples:

client.payments.create(
  company_id: "biz_xxxxxxxxxxxxxx",
  confirmation_token: "confirmation_token",
  plan: {
    currency: "usd"
  }
)

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)

Returns:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/whop_sdk/payments/client.rb', line 156

def create(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "payments",
    body: Whop_sdk::Payments::Types::CreatePaymentsRequest.new(params).to_h,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Payments::Types::CreatePaymentsResponse.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#list(request_options: {}, **params) ⇒ Whop_sdk::Payments::Types::ListPaymentsResponse

Returns a paginated list of payments for the actor in context, with optional filtering by product, plan, status, billing reason, currency, and creation date.

Required permissions:

  • payment:basic:read
  • plan:basic:read
  • access_pass:basic:read
  • member:email:read
  • member:basic:read
  • member:phone:read
  • promo_code:basic:read
  • shipment:basic:read

Examples:

client.payments.list(
  first: 42,
  last: 42,
  company_id: "biz_xxxxxxxxxxxxxx",
  created_before: "2023-12-01T05:00:00Z",
  created_after: "2023-12-01T05:00:00Z",
  updated_before: "2023-12-01T05:00:00Z",
  updated_after: "2023-12-01T05:00:00Z"
)

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):

Returns:



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/whop_sdk/payments/client.rb', line 66

def list(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["after"] = params[:after] if params.key?(:after)
  query_params["before"] = params[:before] if params.key?(:before)
  query_params["first"] = params[:first] if params.key?(:first)
  query_params["last"] = params[:last] if params.key?(:last)
  query_params["company_id"] = params[:company_id] if params.key?(:company_id)
  query_params["direction"] = params[:direction] if params.key?(:direction)
  query_params["order"] = params[:order] if params.key?(:order)
  query_params["product_ids"] = params[:product_ids] if params.key?(:product_ids)
  query_params["billing_reasons"] = params[:billing_reasons] if params.key?(:billing_reasons)
  query_params["currencies"] = params[:currencies] if params.key?(:currencies)
  query_params["plan_ids"] = params[:plan_ids] if params.key?(:plan_ids)
  query_params["statuses"] = params[:statuses] if params.key?(:statuses)
  query_params["substatuses"] = params[:substatuses] if params.key?(:substatuses)
  query_params["include_free"] = params[:include_free] if params.key?(:include_free)
  query_params["created_before"] = params[:created_before] if params.key?(:created_before)
  query_params["created_after"] = params[:created_after] if params.key?(:created_after)
  query_params["updated_before"] = params[:updated_before] if params.key?(:updated_before)
  query_params["updated_after"] = params[:updated_after] if params.key?(:updated_after)
  query_params["query"] = params[:query] if params.key?(:query)
  query_params["checkout_configuration_ids"] = params[:checkout_configuration_ids] if params.key?(:checkout_configuration_ids)

  Whop_sdk::Internal::CursorItemIterator.new(
    cursor_field: :end_cursor,
    item_field: :data,
    initial_cursor: query_params["after"]
  ) do |next_cursor|
    query_params["after"] = next_cursor
    request = Whop_sdk::Internal::JSON::Request.new(
      base_url: request_options[:base_url],
      method: "GET",
      path: "payments",
      query: query_params,
      request_options: request_options
    )
    begin
      response = @client.send(request)
    rescue Net::HTTPRequestTimeout
      raise Whop_sdk::Errors::TimeoutError
    end
    code = response.code.to_i
    if code.between?(200, 299)
      parsed_response = Whop_sdk::Payments::Types::ListPaymentsResponse.load(response.body)
      [parsed_response, response]
    else
      error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
      raise error_class.new(response.body, code: code)
    end
  end
end

#list_fees(request_options: {}, **params) ⇒ Whop_sdk::Payments::Types::ListFeesPaymentsResponse

Returns the list of fees associated with a specific payment, including platform fees and processing fees.

Required permissions:

  • payment:basic:read

Examples:

client.payments.list_fees(
  id: "pay_xxxxxxxxxxxxxx",
  first: 42,
  last: 42
)

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):

  • :id (String)
  • :after (String, nil)
  • :before (String, nil)
  • :first (Integer, nil)
  • :last (Integer, nil)

Returns:



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/whop_sdk/payments/client.rb', line 254

def list_fees(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["after"] = params[:after] if params.key?(:after)
  query_params["before"] = params[:before] if params.key?(:before)
  query_params["first"] = params[:first] if params.key?(:first)
  query_params["last"] = params[:last] if params.key?(:last)

  Whop_sdk::Internal::CursorItemIterator.new(
    cursor_field: :end_cursor,
    item_field: :data,
    initial_cursor: query_params["after"]
  ) do |next_cursor|
    query_params["after"] = next_cursor
    request = Whop_sdk::Internal::JSON::Request.new(
      base_url: request_options[:base_url],
      method: "GET",
      path: "payments/#{URI.encode_uri_component(params[:id].to_s)}/fees",
      query: query_params,
      request_options: request_options
    )
    begin
      response = @client.send(request)
    rescue Net::HTTPRequestTimeout
      raise Whop_sdk::Errors::TimeoutError
    end
    code = response.code.to_i
    if code.between?(200, 299)
      parsed_response = Whop_sdk::Payments::Types::ListFeesPaymentsResponse.load(response.body)
      [parsed_response, response]
    else
      error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
      raise error_class.new(response.body, code: code)
    end
  end
end

#refund(request_options: {}, **params) ⇒ Whop_sdk::Types::Payment

Issue a full or partial refund for a payment. The refund is processed through the original payment processor and the membership status is updated accordingly.

Required permissions:

  • payment:manage
  • plan:basic:read
  • access_pass:basic:read
  • member:email:read
  • member:basic:read
  • member:phone:read
  • promo_code:basic:read
  • shipment:basic:read
  • payment:dispute:read
  • payment:resolution_center_case:read

Examples:

client.payments.refund(id: "pay_xxxxxxxxxxxxxx")

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):

  • :id (String)

Returns:



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/whop_sdk/payments/client.rb', line 319

def refund(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request_data = Whop_sdk::Payments::Types::RefundPaymentsRequest.new(params).to_h
  non_body_param_names = %w[id]
  body = request_data.except(*non_body_param_names)

  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "payments/#{URI.encode_uri_component(params[:id].to_s)}/refund",
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::Payment.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#retrieve(request_options: {}, **params) ⇒ Whop_sdk::Payments::Types::RetrievePaymentsResponse

Retrieves the details of an existing payment.

Required permissions:

  • payment:basic:read
  • plan:basic:read
  • access_pass:basic:read
  • member:email:read
  • member:basic:read
  • member:phone:read
  • promo_code:basic:read
  • shipment:basic:read
  • payment:dispute:read
  • payment:resolution_center_case:read

Examples:

client.payments.retrieve(id: "pay_xxxxxxxxxxxxxx")

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):

  • :id (String)

Returns:



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/whop_sdk/payments/client.rb', line 206

def retrieve(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "payments/#{URI.encode_uri_component(params[:id].to_s)}",
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Payments::Types::RetrievePaymentsResponse.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#retrieve_status(request_options: {}, **params) ⇒ Whop_sdk::Types::PaymentStatus

Retrieves how far a payment has got and what the buyer must do next, if anything. A payment is collected in the background, so poll this rather than reading the create response. Accepts either a secret key or the payment's own client_secret, so the surface collecting the payment can poll it directly.

Examples:

client.payments.retrieve_status(payment_id: "payment_id")

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):

  • :payment_id (String)

Returns:



509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/whop_sdk/payments/client.rb', line 509

def retrieve_status(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "payments/#{URI.encode_uri_component(params[:payment_id].to_s)}/status",
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::PaymentStatus.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#retry_(request_options: {}, **params) ⇒ Whop_sdk::Types::Payment

Retry a failed or pending payment. This re-attempts the charge using the original payment method and plan details.

Required permissions:

  • payment:manage
  • plan:basic:read
  • access_pass:basic:read
  • member:email:read
  • member:basic:read
  • member:phone:read
  • promo_code:basic:read
  • shipment:basic:read
  • payment:dispute:read
  • payment:resolution_center_case:read

Examples:

client.payments.retry_(id: "pay_xxxxxxxxxxxxxx")

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):

  • :id (String)

Returns:



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/whop_sdk/payments/client.rb', line 374

def retry_(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "payments/#{URI.encode_uri_component(params[:id].to_s)}/retry",
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::Payment.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#update_return_url(request_options: {}, **params) ⇒ Whop_sdk::Types::PaymentStatus

Changes where the buyer lands after completing an off-site step, up until they return. Accepts either a secret key or the payment's own client_secret, so the surface that knows the final destination can set it.

Examples:

client.payments.update_return_url(
  payment_id: "payment_id",
  return_url: "https://shinetime.example/checkout/thanks"
)

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):

  • :payment_id (String)

Returns:



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/whop_sdk/payments/client.rb', line 465

def update_return_url(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request_data = Whop_sdk::Payments::Types::UpdateReturnURLPaymentsRequest.new(params).to_h
  non_body_param_names = %w[payment_id]
  body = request_data.except(*non_body_param_names)

  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "PATCH",
    path: "payments/#{URI.encode_uri_component(params[:payment_id].to_s)}/return_url",
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::PaymentStatus.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#void(request_options: {}, **params) ⇒ Whop_sdk::Types::Payment

Void a payment that has not yet been settled. Voiding cancels the payment before it is captured by the payment processor.

Required permissions:

  • payment:manage
  • plan:basic:read
  • access_pass:basic:read
  • member:email:read
  • member:basic:read
  • member:phone:read
  • promo_code:basic:read
  • shipment:basic:read
  • payment:dispute:read
  • payment:resolution_center_case:read

Examples:

client.payments.void(id: "pay_xxxxxxxxxxxxxx")

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):

  • :id (String)

Returns:



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/whop_sdk/payments/client.rb', line 424

def void(request_options: {}, **params)
  params = Whop_sdk::Internal::Types::Utils.normalize_keys(params)
  request = Whop_sdk::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "payments/#{URI.encode_uri_component(params[:id].to_s)}/void",
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Whop_sdk::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Whop_sdk::Types::Payment.load(response.body)
  else
    error_class = Whop_sdk::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end