Module: Transbank::Onepay::Utils::RequestBuilder

Included in:
Transaction
Defined in:
lib/transbank/sdk/utils/request_builder.rb

Instance Method Summary collapse

Instance Method Details

#commit_transaction(occ:, external_unique_number:, options: nil) ⇒ Object

Used internally by [Transaction]#commit

Parameters:

  • occ (String)

    Merchant purchase order

  • external_unique_number (String)

    a unique value (per Merchant, not global) that is used to identify a Transaction

  • options (Hash, nil) (defaults to: nil)

    a hash with config overrides



39
40
41
42
43
44
45
46
# File 'lib/transbank/sdk/utils/request_builder.rb', line 39

def commit_transaction(occ:, external_unique_number:, options: nil)
  options = complete_options(options)
  issued_at = Time.now.to_i
  request = TransactionCommitRequest.new(occ, external_unique_number, issued_at)
  request.set_keys_from_options(options)
  request.app_key = Base::current_integration_type_app_key
  request.sign(options.fetch(:shared_secret))
end

#complete_options(options = {}) ⇒ Object

Fill options with default values



75
76
77
78
# File 'lib/transbank/sdk/utils/request_builder.rb', line 75

def complete_options(options = {})
  options = {} if options.nil?
  default_options.merge(options)
end

#create_transaction(shopping_cart:, channel:, external_unique_number: nil, options: nil) ⇒ Object

Create a [Transaction] request. Used internally by [Transaction]#create he/she intends to purchase are on the [Channel] class

Parameters:

  • shopping_cart (ShoppingCart)

    the user’s ShoppingCart, with [Item]s

  • channel (String)

    The channel the operation is made on. Valid values

  • external_unique_number (String, nil) (defaults to: nil)

    a unique value (per Merchant, not global) that is used to identify a Transaction

  • options (Hash, nil) (defaults to: nil)

    a hash with config overrides



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/transbank/sdk/utils/request_builder.rb', line 12

def create_transaction(shopping_cart:, channel:, external_unique_number: nil, options: nil)
  channel = Base.default_channel if channel.nil?
  external_unique_number = time_as_number if external_unique_number.nil?
  options = complete_options(options)
  issued_at = Time.now.to_i

  request = TransactionCreateRequest.new(
    external_unique_number: external_unique_number,
    total: shopping_cart.total,
    items_quantity: shopping_cart.items_quantity,
    issued_at: issued_at,
    items: shopping_cart.items,
    callback_url: Base.callback_url,
    channel: channel,
    app_scheme: Base.app_scheme,
    commerce_logo_url: options[:commerce_logo_url],
    width_height: options[:qr_width_height]
  )
  request.set_keys_from_options(options)
  request.app_key = Base::current_integration_type_app_key
  request.sign(options.fetch(:shared_secret))
end

#default_optionsHash

Return the default options values: api_key: Base::api_key app_key: Base::current_integration_type_app_key shared_secret: Base::shared_secret

Returns:

  • (Hash)

    a hash with the aforementioned keys/values



85
86
87
88
89
90
91
92
# File 'lib/transbank/sdk/utils/request_builder.rb', line 85

def default_options
  {
    api_key: Base::api_key,
    shared_secret: Base::shared_secret,
    commerce_logo_url: Base::commerce_logo_url,
    qr_width_height: Base::qr_width_height
  }
end

#refund_transaction(refund_amount:, occ:, external_unique_number:, authorization_code:, options: nil) ⇒ Object

Used internally by [Refund]#create This is given when you successfully #commit a [Transaction]

Parameters:

  • refund_amount (Integer)

    the full amount of the [Transaction] to refund. No partial refunds allowed.

  • occ (String)

    Merchant purchase order of the order to refund

  • external_unique_number (String)

    external unique number of the [Transaction] to refund

  • authorization_code (String)

    authorization code for the [Transaction] to refund.

  • options (Hash, nil) (defaults to: nil)

    a hash with config overrides



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/transbank/sdk/utils/request_builder.rb', line 55

def refund_transaction(refund_amount:, occ:, external_unique_number:, authorization_code:, options: nil)
  options = complete_options(options)
  issued_at = Time.now.to_i
  request = RefundCreateRequest.new(nullify_amount: refund_amount,
                                    occ: occ,
                                    external_unique_number: external_unique_number,
                                    authorization_code: authorization_code,
                                    issued_at: issued_at)
  request.set_keys_from_options(options)
  request.app_key = Base::current_integration_type_app_key
  request.sign(options.fetch(:shared_secret))
end

#time_as_numberObject



68
69
70
71
72
# File 'lib/transbank/sdk/utils/request_builder.rb', line 68

def time_as_number
  # Float#truncate(number_of_digits_to_leave) is from Ruby 2.4 onwards
  number, decimals = Time.now.to_f.to_s.split('.')
  (number + decimals[0..2]).to_i
end