Class: Transbank::Webpay::Oneclick::MallTransaction

Inherits:
Object
  • Object
show all
Extended by:
Utils::NetHelper
Defined in:
lib/transbank/sdk/oneclick/mall/mall_transaction.rb

Direct Known Subclasses

MallDeferredTransaction

Constant Summary collapse

AUTHORIZE_TRANSACTION_ENDPOINT =
'rswebpaytransaction/api/oneclick/v1.0/transactions'.freeze
TRANSACTION_STATUS_ENDPOINT =
'rswebpaytransaction/api/oneclick/v1.0/transactions/:buy_order'.freeze
TRANSACTION_REFUND_ENDPOINT =
'rswebpaytransaction/api/oneclick/v1.0/transactions/:buy_order/refunds'.freeze

Class Method Summary collapse

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

.authorize(username:, tbk_user:, parent_buy_order:, details:, options: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
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
# File 'lib/transbank/sdk/oneclick/mall/mall_transaction.rb', line 13

def authorize(username:, tbk_user:, parent_buy_order:, details:,
              options: nil)
  api_key = options&.api_key || default_integration_params[:api_key]
  commerce_code = options&.commerce_code || default_integration_params[:commerce_code]
  integration_type = options&.integration_type || default_integration_params[:integration_type]
  base_url = integration_type.nil? ? Oneclick::Base::integration_type[:TEST] : Oneclick::Base.integration_type_url(integration_type)

  url = base_url + AUTHORIZE_TRANSACTION_ENDPOINT
  headers = webpay_headers(commerce_code: commerce_code, api_key: api_key)

  details_array = details.map do |det|
    hash = det.reduce({}) do |acc, (k, v)|
      acc[k.to_s] = v
      acc
    end
    # We need to make sure the hash's keys are strings (or symbols)
    # since Ruby doesnt have hahes with indifferent access
    # I chose to make them strings
    details_hash = {}
    details_hash[:amount] = hash.fetch('amount')
    details_hash[:buy_order] = hash.fetch('buy_order')
    details_hash[:commerce_code] = hash.fetch('commerce_code')
    details_hash[:installments_number] = hash.fetch('installments_number')
    details_hash
  end

  body = {
    username: username,
    tbk_user: tbk_user,
    buy_order: parent_buy_order,
    details: details_array
  }
  resp = http_post(uri_string: url, body: body, headers: headers, camel_case_keys: false)
  body = JSON.parse(resp.body)
  return ::Transbank::Webpay::Oneclick::MallTransactionAuthorizeResponse.new(body) if resp.kind_of? Net::HTTPSuccess
  raise Oneclick::Errors::MallTransactionAuthorizeError.new(body['error_message'], resp.code)
end

.default_integration_paramsObject



85
86
87
88
89
90
91
92
# File 'lib/transbank/sdk/oneclick/mall/mall_transaction.rb', line 85

def default_integration_params
  {
    api_key: Oneclick::Base.api_key,
    commerce_code: Oneclick::Base.commerce_code,
    integration_type: Oneclick::Base::integration_type,
    base_url: Oneclick::Base::current_integration_type_url
  }
end

.refund(buy_order:, child_commerce_code:, child_buy_order:, amount:, options: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/transbank/sdk/oneclick/mall/mall_transaction.rb', line 65

def refund(buy_order:, child_commerce_code:, child_buy_order:, amount:,
           options: nil)
  api_key = options&.api_key || default_integration_params[:api_key]
  commerce_code = options&.commerce_code || default_integration_params[:commerce_code]
  integration_type = options&.integration_type || default_integration_params[:integration_type]
  base_url = integration_type.nil? ? Oneclick::Base::integration_type[:TEST] : Oneclick::Base.integration_type_url(integration_type)

  url = base_url + TRANSACTION_REFUND_ENDPOINT.gsub(':buy_order', buy_order)
  headers = webpay_headers(commerce_code: commerce_code, api_key: api_key)
  body = {
    detail_buy_order: child_buy_order,
    commerce_code: child_commerce_code,
    amount: amount
  }
  resp = http_post(uri_string: url, body: body, headers: headers, camel_case_keys: false)
  body = JSON.parse(resp.body)
  return ::Transbank::Webpay::Oneclick::MallTransactionRefundResponse.new(body) if resp.kind_of? Net::HTTPSuccess
  raise Oneclick::Errors::MallTransactionRefundError.new(body['error_message'], resp.code)
end

.status(buy_order:, options: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/transbank/sdk/oneclick/mall/mall_transaction.rb', line 51

def status(buy_order:, options: nil)
  api_key = options&.api_key || default_integration_params[:api_key]
  commerce_code = options&.commerce_code || default_integration_params[:commerce_code]
  integration_type = options&.integration_type || default_integration_params[:integration_type]
  base_url = integration_type.nil? ? Oneclick::Base::integration_type[:TEST] : Oneclick::Base.integration_type_url(integration_type)

  url = base_url + TRANSACTION_STATUS_ENDPOINT.gsub(':buy_order', buy_order)
  headers = webpay_headers(commerce_code: commerce_code, api_key: api_key)
  resp = http_get(uri_string: url, headers: headers)
  body = JSON.parse(resp.body)
  return ::Transbank::Webpay::Oneclick::MallTransactionStatusResponse.new(body) if resp.kind_of? Net::HTTPSuccess
  raise Oneclick::Errors::MallTransactionStatusError.new(body['error_message'], resp.code)
end