Class: Transbank::TransaccionCompleta::MallTransaction

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

Constant Summary collapse

CREATE_TRANSACTION_ENDPOINT =
'rswebpaytransaction/api/webpay/v1.0/transactions'
TRANSACTION_INSTALLMENTS_ENDPOINT =
'rswebpaytransaction/api/webpay/v1.0/transactions/:token/installments'
COMMIT_TRANSACTION_ENDPOINT =
'rswebpaytransaction/api/webpay/v1.0/transactions/:token'
TRANSACTION_STATUS_ENDPOINT =
'rswebpaytransaction/api/webpay/v1.0/transactions/:token'
REFUND_TRANSACTION_ENDPOINT =
'rswebpaytransaction/api/webpay/v1.0/transactions/:token/refunds'

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

.commit(token:, details:, options: nil) ⇒ Object



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

def commit(token:,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? ? TransaccionCompleta::Base::integration_type[:TEST] : TransaccionCompleta::Base.integration_type_url(integration_type)

  url = base_url + COMMIT_TRANSACTION_ENDPOINT.gsub(':token', token)
  headers = webpay_headers(commerce_code: commerce_code, api_key: api_key)

  detail = commit_details(details)
  body = {  details: detail }

  resp = http_put(uri_string: url, body: body,  headers: headers)
  body = JSON.parse(resp.body)
  return ::Transbank::TransaccionCompleta::MallTransactionCommitResponse.new(body) if resp.kind_of? Net::HTTPSuccess
  raise Errors::TransactionCommitError.new(body['error_message'], resp.code)
end

.create(buy_order:, session_id:, card_number:, card_expiration_date:, 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
# File 'lib/transbank/sdk/transaccion_completa/mall/mall_transaction.rb', line 13

def create(buy_order:, session_id:,card_number:, card_expiration_date:,
           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? ? TransaccionCompleta::Base::integration_type[:TEST] : TransaccionCompleta::Base.integration_type_url(integration_type)

  detail = create_details(details)
  body = {
    buy_order: buy_order, session_id: session_id,
    card_number: card_number, card_expiration_date: card_expiration_date,
    details: detail
  }

  url = base_url + CREATE_TRANSACTION_ENDPOINT
  headers = webpay_headers(commerce_code: commerce_code, api_key: api_key)
  resp = http_post(uri_string: url, body: body, headers: headers, camel_case_keys: false)
  body = JSON.parse(resp.body)
  return ::Transbank::TransaccionCompleta::TransactionCreateResponse.new(body) if resp.kind_of? Net::HTTPSuccess
  raise Errors::TransactionCreateError.new(body['error_message'], resp.code)
end

.default_integration_paramsObject



117
118
119
120
121
122
123
124
# File 'lib/transbank/sdk/transaccion_completa/mall/mall_transaction.rb', line 117

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

.installments(token:, details:, options: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/transbank/sdk/transaccion_completa/mall/mall_transaction.rb', line 35

def installments(token:, details:, options:nil)
  api_key = options&.api_key || default_integration_params[:api_key]
  base_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? ? TransaccionCompleta::Base::integration_type[:TEST] : TransaccionCompleta::Base.integration_type_url(integration_type)

  url = base_url + TRANSACTION_INSTALLMENTS_ENDPOINT.gsub(':token', token)
  headers = webpay_headers(commerce_code: base_commerce_code, api_key: api_key)

  detail = installments_details(details)

  resp = detail.map do |det|
      body =  {
        commerce_code: det[:commerce_code],
        buy_order: det[:buy_order],
        installments_number: det[:installments_number]
      }
      http_post(uri_string: url, body: body, headers: headers, camel_case_keys: false)
  end

  if resp.all? { |res| res.kind_of? Net::HTTPSuccess }
    return resp.map do |res|
      body = JSON.parse(res.body)
      ::Transbank::TransaccionCompleta::TransactionInstallmentsResponse.new(body)
    end
  end

  raise Errors::TransactionInstallmentsError.new(resp, resp.code)
end

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/transbank/sdk/transaccion_completa/mall/mall_transaction.rb', line 97

def refund(token:, child_buy_order:, child_commerce_code:, 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? ? TransaccionCompleta::Base::integration_type[:TEST] : TransaccionCompleta::Base.integration_type_url(integration_type)

  body = {
    buy_order: child_buy_order,
    commerce_code: child_commerce_code,
    amount: amount
  }

  url = base_url + REFUND_TRANSACTION_ENDPOINT.gsub(':token', token)
  headers = webpay_headers(commerce_code: commerce_code, api_key: api_key)
  resp = http_post(uri_string: url, body: body, headers: headers, camel_case_keys: false)
  body = JSON.parse(resp.body)
  return ::Transbank::TransaccionCompleta::TransactionRefundResponse.new(body) if resp.kind_of? Net::HTTPSuccess
  raise Errors::TransactionRefundError.new(body['error_message'], resp.code)
end

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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/transbank/sdk/transaccion_completa/mall/mall_transaction.rb', line 83

def status(token:, 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? ? TransaccionCompleta::Base::integration_type[:TEST] : TransaccionCompleta::Base.integration_type_url(integration_type)

  url = base_url + TRANSACTION_STATUS_ENDPOINT.gsub(':token', token)
  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::TransaccionCompleta::MallTransactionStatusResponse.new(body) if resp.kind_of? Net::HTTPSuccess
  raise Errors::TransactionStatusError.new(body['error_message'], resp.code)
end