Class: Finswitz::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/finswitz.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, dashboard_token: nil, base_url: DEFAULT_BASE_URL) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
# File 'lib/finswitz.rb', line 23

def initialize(api_key: nil, dashboard_token: nil, base_url: DEFAULT_BASE_URL)
  @api_key = api_key
  @dashboard_token = dashboard_token
  @base_url = base_url.sub(%r{/+$}, "")
end

Class Method Details

.parse_webhook(raw_body) ⇒ Object



111
112
113
# File 'lib/finswitz.rb', line 111

def self.parse_webhook(raw_body)
  raw_body.is_a?(String) ? JSON.parse(raw_body) : raw_body
end

Instance Method Details

#card_pricingObject



72
# File 'lib/finswitz.rb', line 72

def card_pricing = get("/cards/pricing")

#convert_wallet(payload) ⇒ Object



89
# File 'lib/finswitz.rb', line 89

def convert_wallet(payload) = post("/developers/wallets/convert", payload, **dashboard_options)

#create_api_key(payload) ⇒ Object



81
# File 'lib/finswitz.rb', line 81

def create_api_key(payload) = post("/developers/keys", payload, **dashboard_options)

#create_collection(payload) ⇒ Object



100
# File 'lib/finswitz.rb', line 100

def create_collection(payload) = post("/developers/collections", payload, **dashboard_options)

#create_escrow_hold(payload, idempotency_key: nil) ⇒ Object



95
# File 'lib/finswitz.rb', line 95

def create_escrow_hold(payload, idempotency_key: nil) = post("/developers/treasury/escrow", payload, **dashboard_options(idempotency_key: idempotency_key))


64
# File 'lib/finswitz.rb', line 64

def create_payment_link(payload, idempotency_key: nil) = post("/payments/payment-links", payload, idempotency_key: idempotency_key)

#create_split_rule(payload, idempotency_key: nil) ⇒ Object



92
# File 'lib/finswitz.rb', line 92

def create_split_rule(payload, idempotency_key: nil) = post("/developers/treasury/split-rules", payload, **dashboard_options(idempotency_key: idempotency_key))

#dashboard_options(options = {}) ⇒ Object



78
# File 'lib/finswitz.rb', line 78

def dashboard_options(options = {}) = options.merge(token_type: :dashboard)

#execute_vendor_settlement(id, payload = {}, idempotency_key: nil) ⇒ Object



98
# File 'lib/finswitz.rb', line 98

def execute_vendor_settlement(id, payload = {}, idempotency_key: nil) = post("/developers/treasury/settlements/#{URI.encode_www_form_component(id)}/execute", payload, **dashboard_options(idempotency_key: idempotency_key))

#fund_wallet(payload) ⇒ Object



87
# File 'lib/finswitz.rb', line 87

def fund_wallet(payload) = post("/developers/wallets/fund", payload, **dashboard_options)

#get(path, **options) ⇒ Object



58
# File 'lib/finswitz.rb', line 58

def get(path, **options) = request(:get, path, **options)

#get_card_transaction(card_id, transaction_id) ⇒ Object



76
# File 'lib/finswitz.rb', line 76

def get_card_transaction(card_id, transaction_id) = get("/cards/#{URI.encode_www_form_component(card_id)}/transactions/#{URI.encode_www_form_component(transaction_id)}")

#get_transaction(reference) ⇒ Object



67
# File 'lib/finswitz.rb', line 67

def get_transaction(reference) = get("/payments/#{URI.encode_www_form_component(reference)}")

#issue_card(payload, idempotency_key: nil) ⇒ Object



73
# File 'lib/finswitz.rb', line 73

def issue_card(payload, idempotency_key: nil) = post("/cards/issue", payload, idempotency_key: idempotency_key)

#kycObject



107
# File 'lib/finswitz.rb', line 107

def kyc = get("/developers/kyc", **dashboard_options)

#list_api_keysObject



80
# File 'lib/finswitz.rb', line 80

def list_api_keys = get("/developers/keys", **dashboard_options)

#list_banks(query: nil) ⇒ Object



70
# File 'lib/finswitz.rb', line 70

def list_banks(query: nil) = get("/payments/banks", query: query)

#list_card_transactions(card_id, query: nil) ⇒ Object



75
# File 'lib/finswitz.rb', line 75

def list_card_transactions(card_id, query: nil) = get("/cards/#{URI.encode_www_form_component(card_id)}/transactions", query: query)

#list_cards(query: nil) ⇒ Object



74
# File 'lib/finswitz.rb', line 74

def list_cards(query: nil) = get("/cards", query: query)

#list_collection_payments(id) ⇒ Object



102
# File 'lib/finswitz.rb', line 102

def list_collection_payments(id) = get("/developers/collections/#{URI.encode_www_form_component(id)}/payments", **dashboard_options)

#list_collectionsObject



99
# File 'lib/finswitz.rb', line 99

def list_collections = get("/developers/collections", **dashboard_options)

#list_escrow_holdsObject



94
# File 'lib/finswitz.rb', line 94

def list_escrow_holds = get("/developers/treasury/escrow", **dashboard_options)

#list_split_rulesObject



91
# File 'lib/finswitz.rb', line 91

def list_split_rules = get("/developers/treasury/split-rules", **dashboard_options)

#list_transactions(query: nil) ⇒ Object



68
# File 'lib/finswitz.rb', line 68

def list_transactions(query: nil) = get("/payments", query: query)

#list_vendor_settlementsObject



97
# File 'lib/finswitz.rb', line 97

def list_vendor_settlements = get("/developers/treasury/settlements", **dashboard_options)

#list_webhook_logs(query: nil) ⇒ Object



84
# File 'lib/finswitz.rb', line 84

def list_webhook_logs(query: nil) = get("/developers/webhooks/logs", **dashboard_options(query: query))

#meObject



79
# File 'lib/finswitz.rb', line 79

def me = get("/developers/me", **dashboard_options)

#mobile_money(payload, idempotency_key: nil) ⇒ Object



65
# File 'lib/finswitz.rb', line 65

def mobile_money(payload, idempotency_key: nil) = post("/payments/mobile-money", payload, idempotency_key: idempotency_key)

#mock_charge(payload) ⇒ Object



66
# File 'lib/finswitz.rb', line 66

def mock_charge(payload) = post("/payments/mock/charge", payload)

#patch(path, body = {}, **options) ⇒ Object



60
# File 'lib/finswitz.rb', line 60

def patch(path, body = {}, **options) = request(:patch, path, body: body, **options)

#pay_collection(slug, payload) ⇒ Object



104
# File 'lib/finswitz.rb', line 104

def pay_collection(slug, payload) = post("/public/collections/#{URI.encode_www_form_component(slug)}/pay", payload, token_type: :none)

#post(path, body = {}, **options) ⇒ Object



59
# File 'lib/finswitz.rb', line 59

def post(path, body = {}, **options) = request(:post, path, body: body, **options)

#public_collection(slug) ⇒ Object



103
# File 'lib/finswitz.rb', line 103

def public_collection(slug) = get("/public/collections/#{URI.encode_www_form_component(slug)}", token_type: :none)

#refund(reference, payload = {}, idempotency_key: nil) ⇒ Object



69
# File 'lib/finswitz.rb', line 69

def refund(reference, payload = {}, idempotency_key: nil) = post("/payments/#{URI.encode_www_form_component(reference)}/refund", payload, idempotency_key: idempotency_key)

#release_escrow_hold(id, payload = {}, idempotency_key: nil) ⇒ Object



96
# File 'lib/finswitz.rb', line 96

def release_escrow_hold(id, payload = {}, idempotency_key: nil) = post("/developers/treasury/escrow/#{URI.encode_www_form_component(id)}/release", payload, **dashboard_options(idempotency_key: idempotency_key))

#request(method, path, body: nil, query: nil, idempotency_key: nil, token_type: :api, token: nil) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/finswitz.rb', line 29

def request(method, path, body: nil, query: nil, idempotency_key: nil, token_type: :api, token: nil)
  bearer = token || (token_type == :dashboard ? @dashboard_token : @api_key)
  raise ArgumentError, token_type == :dashboard ? "Missing dashboard_token." : "Missing api_key." if token_type != :none && !bearer

  uri = URI(@base_url + path)
  uri.query = URI.encode_www_form(query.reject { |_key, value| value.nil? }) if query && !query.empty?

  klass = Net::HTTP.const_get(method.to_s.capitalize)
  req = klass.new(uri)
  req["Accept"] = "application/json"
  req["Authorization"] = "Bearer #{bearer}" unless token_type == :none
  req["Idempotency-Key"] = idempotency_key if idempotency_key
  if body
    req["Content-Type"] = "application/json"
    req.body = JSON.generate(body)
  end

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https", read_timeout: 30) do |http|
    http.request(req)
  end

  data = res.body && !res.body.empty? ? JSON.parse(res.body) : nil
  unless res.code.to_i.between?(200, 299)
    message = data.is_a?(Hash) && data["error"] ? data["error"] : "Finswitz API request failed with #{res.code}"
    raise ApiError.new(message, status: res.code.to_i, response: data)
  end
  data
end

#request_compliance_review(payload = {}) ⇒ Object



109
# File 'lib/finswitz.rb', line 109

def request_compliance_review(payload = {}) = post("/developers/compliance/request-review", payload, **dashboard_options)

#revoke_api_key(id) ⇒ Object



82
# File 'lib/finswitz.rb', line 82

def revoke_api_key(id) = post("/developers/keys/#{URI.encode_www_form_component(id)}/revoke", {}, **dashboard_options)

#simulate_webhook(payload) ⇒ Object



85
# File 'lib/finswitz.rb', line 85

def simulate_webhook(payload) = post("/developers/webhooks/simulate", payload, **dashboard_options)

#submit_kyc(payload) ⇒ Object



108
# File 'lib/finswitz.rb', line 108

def submit_kyc(payload) = post("/developers/kyc", payload, **dashboard_options)

#transfer(payload, idempotency_key: nil) ⇒ Object



62
# File 'lib/finswitz.rb', line 62

def transfer(payload, idempotency_key: nil) = post("/payments/transfer", payload, idempotency_key: idempotency_key)

#treasuryObject



90
# File 'lib/finswitz.rb', line 90

def treasury = get("/developers/treasury", **dashboard_options)

#update_collection(id, payload) ⇒ Object



101
# File 'lib/finswitz.rb', line 101

def update_collection(id, payload) = patch("/developers/collections/#{URI.encode_www_form_component(id)}", payload, **dashboard_options)

#update_settlement(payload) ⇒ Object



105
# File 'lib/finswitz.rb', line 105

def update_settlement(payload) = post("/developers/settlement", payload, **dashboard_options)

#update_split_rule(id, payload) ⇒ Object



93
# File 'lib/finswitz.rb', line 93

def update_split_rule(id, payload) = patch("/developers/treasury/split-rules/#{URI.encode_www_form_component(id)}", payload, **dashboard_options)

#update_webhooks(payload) ⇒ Object



83
# File 'lib/finswitz.rb', line 83

def update_webhooks(payload) = request(:put, "/developers/webhooks", body: payload, **dashboard_options)

#verify_account(payload) ⇒ Object



63
# File 'lib/finswitz.rb', line 63

def (payload) = post("/payments/verify-account", payload)

#verify_settlement_account(payload) ⇒ Object



106
# File 'lib/finswitz.rb', line 106

def (payload) = post("/developers/verify-account", payload, **dashboard_options)

#wallet_funding_status(query: nil) ⇒ Object



88
# File 'lib/finswitz.rb', line 88

def wallet_funding_status(query: nil) = get("/developers/wallets/fund/status", **dashboard_options(query: query))

#walletsObject



86
# File 'lib/finswitz.rb', line 86

def wallets = get("/developers/wallets", **dashboard_options)