Method: Transbank::Onepay::Transaction.create

Defined in:
lib/transbank/sdk/onepay/models/transaction.rb

.create(shopping_cart:, channel: nil, external_unique_number: nil, options: nil) ⇒ TransactionCreateResponse

Create a [Transaction], initiating the purchase process. Includes data that you will need to #commit your [Transaction]

Parameters:

  • shopping_cart (ShoppingCart)

    contains the [Item]s to be purchased

  • channel (String) (defaults to: nil)

    The channel that the transaction is going to be done through. Valid values are contained on the [Transbank::Onepay::Channel] class

  • external_unique_number (String) (defaults to: nil)

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

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

    an optional Hash with configuration overrides

Returns:

Raises:

  • (ShoppingCartError)

    if shopping cart is nil or empty

  • (TransactionCreateError)

    if channel is not valid

  • (TransactionCreateError)

    if no response is gotten, or responseCode of the response is not ‘OK’



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/transbank/sdk/onepay/models/transaction.rb', line 23

def create(shopping_cart:, channel: nil, external_unique_number: nil,
           options: nil)
  if is_options_hash?(channel)
    options = channel
    channel = nil
  end

  if is_options_hash?(external_unique_number)
    options = external_unique_number
    external_unique_number = nil
  end

  validate_channel!(channel)
  validate_shopping_cart!(shopping_cart)

  options = complete_options(options)
  create_request = create_transaction(shopping_cart: shopping_cart,
                                      channel: channel,
                                      external_unique_number: external_unique_number,
                                      options: options)
  response = http_post(uri_string: transaction_create_path, body: create_request.to_h)
  validate_create_response!(response)
  transaction_create_response = TransactionCreateResponse.new JSON.parse(response.body)
  signature_is_valid = transaction_create_response.valid_signature?(options.fetch(:shared_secret))
  unless signature_is_valid
    raise Errors::SignatureError, "The response's signature is not valid."
  end
  transaction_create_response
end