Class: Transbank::Onepay::Transaction

Inherits:
Object
  • Object
show all
Extended by:
Utils::RequestBuilder, Utils::NetHelper
Defined in:
lib/transbank/sdk/onepay/models/transaction.rb

Overview

Class Transaction

This class creates or commits a Transaction (that is, a purchase)

Constant Summary collapse

SEND_TRANSACTION =
'sendtransaction'.freeze
COMMIT_TRANSACTION =
'gettransactionnumber'.freeze
TRANSACTION_BASE_PATH =
'/ewallet-plugin-api-services/services/transactionservice/'.freeze

Class Method Summary collapse

Methods included from Utils::RequestBuilder

commit_transaction, complete_options, create_transaction, default_options, refund_transaction, time_as_number

Methods included from Utils::NetHelper

http_delete, http_get, http_post, http_put, keys_to_camel_case, patpass_comercio_headers, snake_to_camel_case, webpay_headers

Class Method Details

.commit(occ:, external_unique_number:, options: nil) ⇒ TransactionCommitResponse

Commit a [Transaction]. It is MANDATORY for this to be done, and you have 30 seconds to do so, otherwise the [Transaction] is automatically REVERSED

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)

    an optional Hash with configuration overrides

Returns:

Raises:

  • (TransactionCommitError)

    if response is nil or responseCode of the response is not ‘OK’



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/transbank/sdk/onepay/models/transaction.rb', line 60

def commit(occ:, external_unique_number:, options: nil)
  options = complete_options(options)
  commit_request = commit_transaction(occ: occ,
                                      external_unique_number: external_unique_number,
                                      options: options)
  response = http_post(uri_string: transaction_commit_path, body: commit_request.to_h)
  validate_commit_response!(response)
  transaction_commit_response = TransactionCommitResponse.new(JSON.parse(response.body))
  signature_is_valid = transaction_commit_response.valid_signature?(options.fetch(:shared_secret))
  unless signature_is_valid
    raise Errors::SignatureError, "The response's signature is not valid."
  end
  transaction_commit_response
end

.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