class Transbank::Onepay::Transaction
Class Transaction
This class creates or commits a Transaction
(that is, a purchase)
Constants
- COMMIT_TRANSACTION
- SEND_TRANSACTION
- TRANSACTION_BASE_PATH
Public Class Methods
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 @param occ [String] Merchant purchase order @param external_unique_number [String] a unique value (per Merchant, not global) that is used to identify a Transaction
@param options[Hash, nil] an optional Hash with configuration overrides @return [TransactionCommitResponse] The response to your commit request. @raise [TransactionCommitError] if response is nil or responseCode of the response is not 'OK'
# 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 a [Transaction], initiating the purchase process. @param shopping_cart [ShoppingCart] contains the [Item]s to be purchased @param channel [String] The channel that the transaction is going to be done through. Valid values are contained on the [Transbank::Onepay::Channel] class @param external_unique_number [String] a unique value (per Merchant, not global) that is used to identify a Transaction
@param options[Hash, nil] an optional Hash with configuration overrides @return [TransactionCreateResponse] the response to your request. Includes data that you will need to commit your [Transaction] @raise [ShoppingCartError] if shopping cart is nil or empty @raise [TransactionCreateError] if channel is not valid @raise [TransactionCreateError] if no response is gotten, or responseCode of the response is not 'OK'
# 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
Private Class Methods
# File lib/transbank/sdk/onepay/models/transaction.rb, line 125 def channel_is_app?(channel) channel && channel == Transbank::Onepay::Channel::APP end
# File lib/transbank/sdk/onepay/models/transaction.rb, line 129 def channel_is_mobile?(channel) channel && channel == Transbank::Onepay::Channel::MOBILE end
# File lib/transbank/sdk/onepay/models/transaction.rb, line 77 def is_options_hash?(hash) return false unless hash.respond_to? :keys # Intersection of the two arrays ([:api_key, :shared_secret] & hash.keys).any? end
# File lib/transbank/sdk/onepay/models/transaction.rb, line 137 def transaction_commit_path Base.current_integration_type_url + TRANSACTION_BASE_PATH + COMMIT_TRANSACTION end
# File lib/transbank/sdk/onepay/models/transaction.rb, line 133 def transaction_create_path Base.current_integration_type_url + TRANSACTION_BASE_PATH + SEND_TRANSACTION end
# File lib/transbank/sdk/onepay/models/transaction.rb, line 83 def validate_channel!(channel) if channel_is_app?(channel) && Base::app_scheme.nil? raise Errors::TransactionCreateError, 'You need to set an app_scheme if you want to use the APP channel' end if channel_is_mobile?(channel) && Base::callback_url.nil? raise Errors::TransactionCreateError, 'You need to set a valid callback if you want to use the MOBILE channel' end end
# File lib/transbank/sdk/onepay/models/transaction.rb, line 99 def validate_commit_response!(response) unless response raise Errors::TransactionCommitError, 'Could not obtain a response from the service.' end unless JSON.parse(response.body).fetch('responseCode') == 'OK' body = JSON.parse(response.body) msg = "#{body.fetch('responseCode')} : #{body.fetch('description')}" raise Errors::TransactionCommitError, msg end response end
# File lib/transbank/sdk/onepay/models/transaction.rb, line 112 def validate_create_response!(response) unless response raise Errors::TransactionCreateError, 'Could not obtain a response from the service.' end unless JSON.parse(response.body).fetch('responseCode') == 'OK' body = JSON.parse(response.body) msg = "#{body.fetch('responseCode')} : #{body['description']}" raise Errors::TransactionCreateError, msg end response end
# File lib/transbank/sdk/onepay/models/transaction.rb, line 93 def validate_shopping_cart!(shopping_cart) if shopping_cart.items.nil? || shopping_cart.items.empty? raise Errors::ShoppingCartError, 'Shopping cart is null or empty.' end end