Class: XenditApi::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/xendit_api/client.rb', line 10

def initialize(api_key:)
  @api_key = api_key
  # Xendit requires us to use token in every request
  # This is how to get the token, appending colon at the end then encode it
  @token = Base64.strict_encode64(api_key + ':')
  setup_connection
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



8
9
10
# File 'lib/xendit_api/client.rb', line 8

def error
  @error
end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/xendit_api/client.rb', line 8

def token
  @token
end

Instance Method Details

#charge_credit_card(external_id:, token:, amount:) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/xendit_api/client.rb', line 159

def charge_credit_card(external_id:, token:, amount:)
  return nil if @api_key.empty?

  data = { 
    external_id: external_id, 
    token: token, 
    amount: amount
  }
  
  response = make_request('credit_card_charges', 'post', data)

  attrs = JSON.parse(response.body)
  XenditApi::Entities::CardCharge.new(attrs)
end

#create_disbursement(idempotency_key: nil, external_id:, bank_code:, account_holder_name:, account_number:, description:, amount:) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/xendit_api/client.rb', line 135

def create_disbursement(idempotency_key: nil, external_id:, bank_code:, account_holder_name:, account_number:, description:, amount:)
  return nil if @api_key.empty?

  data = { 
    external_id: external_id, 
    bank_code: bank_code, 
    account_holder_name: ,
    account_number: ,
    description: description,
    amount: amount
  }

  if idempotency_key.nil?
    headers = {}
  else
    headers = { 'X-IDEMPOTENCY-KEY' => idempotency_key } 
  end

  response = make_request('disbursements', 'post', data, headers)

  attrs = JSON.parse(response.body)
  XenditApi::Entities::Disbursement.new(attrs)
end

#create_fixed_virtual_account(external_id:, bank_code:, name:, virtual_account_number: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/xendit_api/client.rb', line 65

def (external_id:, bank_code:, name:, virtual_account_number: nil)
  return nil if @api_key.empty?

  data = { 
    external_id:  external_id, 
    bank_code:    bank_code, 
    name:         name
  }
  data[:virtual_account_number] =  unless .nil?

  response = make_request('callback_virtual_accounts', 'post', data)

  attrs = JSON.parse(response.body)
  XenditApi::Entities::VirtualAccount.new(attrs)
end

#create_invoice(external_id:, payer_email:, description:, amount:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xendit_api/client.rb', line 49

def create_invoice(external_id:, payer_email:, description:, amount:)
  return nil if @api_key.empty?

  data = { 
    external_id: external_id, 
    payer_email: payer_email, 
    description: description, 
    amount: amount
  }

  response = make_request('v2/invoices', 'post', data)

  attrs = JSON.parse(response.body)
  XenditApi::Entities::Invoice.new(attrs)
end

#get_bank_account_data(account_number:, bank_code:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/xendit_api/client.rb', line 27

def (account_number:, bank_code:)
  return nil if @api_key.empty?

  response = make_request(
    'bank_account_data_requests', 
    'post', 
    { bank_account_number: , bank_code: bank_code }
  )

  JSON.parse(response.body)
end

#get_banks_for_disbursementObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/xendit_api/client.rb', line 108

def get_banks_for_disbursement
  return nil if @api_key.empty?

  response = make_request(
    'available_disbursements_banks', 'get'
  )

  elements = JSON.parse(response.body)
  banks = []

  elements.each do |element| 
    banks << XenditApi::Entities::Bank.new(element)
  end

  banks
end

#get_banks_for_virtual_accountObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/xendit_api/client.rb', line 91

def 
  return nil if @api_key.empty?

  response = make_request(
    'available_virtual_account_banks', 'get'
  )

  elements = JSON.parse(response.body)
  banks = []

  elements.each do |element| 
    banks << XenditApi::Entities::Bank.new(element)
  end

  banks
end

#get_cash_balanceObject



18
19
20
21
22
23
24
25
# File 'lib/xendit_api/client.rb', line 18

def get_cash_balance
  return nil if @api_key.empty?

  response = make_request('balance', 'get', {})

  attrs = JSON.parse(response.body)
  XenditApi::Entities::CashAccount.new(attrs)
end

#get_disbursement(id:) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/xendit_api/client.rb', line 125

def get_disbursement(id:)
  return nil if @api_key.empty?

  path = 'v2/disbursements/' + id.to_s
  response = make_request(path, 'get')

  attrs = JSON.parse(response.body)
  XenditApi::Entities::Disbursement.new(attrs)
end

#get_invoice(id:) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/xendit_api/client.rb', line 39

def get_invoice(id:)
  return nil if @api_key.empty?

  path = 'v2/invoices/' + id.to_s
  response = make_request(path, 'get')

  attrs = JSON.parse(response.body)
  XenditApi::Entities::Invoice.new(attrs)
end

#get_virtual_account(id:) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/xendit_api/client.rb', line 81

def (id:)
  return nil if @api_key.empty?

  path = 'callback_virtual_accounts/' + id.to_s
  response = make_request(path, 'get')

  attrs = JSON.parse(response.body)
  XenditApi::Entities::VirtualAccount.new(attrs)
end