Class: Tosspayments2::Rails::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tosspayments2/rails/client.rb

Overview

Lightweight client for TossPayments REST endpoints (confirm/cancel only).

Instance Method Summary collapse

Constructor Details

#initialize(secret_key: nil, api_base: nil, timeout: nil, retries: 2, backoff: 0.2) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tosspayments2/rails/client.rb', line 16

def initialize(secret_key: nil, api_base: nil, timeout: nil, retries: 2, backoff: 0.2)
  cfg = ::Tosspayments2::Rails.configuration
  @secret_key = secret_key || cfg.secret_key
  @api_base = api_base || cfg.api_base
  @timeout = timeout || cfg.timeout
  @retries = retries
  @backoff = backoff
  return if @secret_key

  raise ::Tosspayments2::Rails::ConfigurationError,
        'secret_key required – configure with Tosspayments2::Rails.configure'
end

Instance Method Details

#cancel(payment_key:, cancel_reason:, amount: nil) ⇒ Hash

Cancel payment (full or partial).

Raises:



43
44
45
46
47
# File 'lib/tosspayments2/rails/client.rb', line 43

def cancel(payment_key:, cancel_reason:, amount: nil)
  body = { cancelReason: cancel_reason }
  body[:cancelAmount] = amount if amount
  post_json("/v1/payments/#{payment_key}/cancel", body)
end

#confirm(payment_key:, order_id:, amount:) ⇒ Hash

Confirm payment after success redirect.

Raises:



32
33
34
35
36
37
38
# File 'lib/tosspayments2/rails/client.rb', line 32

def confirm(payment_key:, order_id:, amount:)
  post_json('/v1/payments/confirm', {
              paymentKey: payment_key,
              orderId: order_id,
              amount: amount
            })
end