Skip to content

Commit

Permalink
Merge pull request #65 from TransbankDevelopers/feat/add-tx-completa-…
Browse files Browse the repository at this point in the history
…capture

Add TX Completa capture in normal and mall
  • Loading branch information
Felipe Fiebig authored Apr 12, 2021
2 parents 497f1e6 + 2dc7986 commit f1a01db
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 68 deletions.
16 changes: 9 additions & 7 deletions lib/transbank/sdk/transaccion_completa/base.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Transbank
module TransaccionCompleta
class Base

DEFAULT_API_KEY = '579B532A7440BB0C9079DED94D31EA1615BACEB56610332264630D42D0A36B1C'.freeze
DEFAULT_COMMERCE_CODE = '597055555530'.freeze

Expand All @@ -12,19 +11,20 @@ class Base
@commerce_code = DEFAULT_COMMERCE_CODE
@integration_type = :TEST
@integration_types = {
LIVE: "https://webpay3g.transbank.cl/",
TEST: "https://webpay3gint.transbank.cl/"
LIVE: 'https://webpay3g.transbank.cl/',
TEST: 'https://webpay3gint.transbank.cl/'
}
class << self
attr_reader :integration_types
attr_accessor :api_key, :integration_type, :commerce_code


def integration_type_url(integration_type)
type = integration_type.upcase.to_sym
return @integration_types[type] unless @integration_types[type].nil?

valid_values = @integration_types.keys.join(', ')
raise Transbank::Webpay::Errors::IntegrationTypeError, "Invalid integration type, valid values are #{valid_values}"
raise Transbank::Webpay::Errors::IntegrationTypeError,
"Invalid integration type, valid values are #{valid_values}"
end

def current_integration_type_url
Expand All @@ -34,8 +34,10 @@ def current_integration_type_url
def integration_type=(integration_type)
type = integration_type.upcase.to_sym
return @integration_type = type unless @integration_types[type].nil?

valid_values = @integration_types.keys.join(', ')
raise Transbank::Webpay::Errors::IntegrationTypeError, "Invalid integration type, valid values are #{valid_values}"
raise Transbank::Webpay::Errors::IntegrationTypeError,
"Invalid integration type, valid values are #{valid_values}"
end

def configure_for_testing
Expand All @@ -46,4 +48,4 @@ def configure_for_testing
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
require 'transbank/sdk/transaccion_completa/errors/transaction_installments_error'
require 'transbank/sdk/transaccion_completa/errors/transaction_refund_error'
require 'transbank/sdk/transaccion_completa/errors/transaction_status_error'
require 'transbank/sdk/transaccion_completa/errors/transaction_capture_error'
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Transbank
module TransaccionCompleta
module Errors
class TransactionCaptureError < TransaccionCompletaError
attr_accessor :message, :code
def initialize(message, code = nil)
self.message = message
self.code = code
super(message)
end
end
end
end
end
99 changes: 66 additions & 33 deletions lib/transbank/sdk/transaccion_completa/mall/mall_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,55 @@ class MallTransaction
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'
CAPTURE_TRANSACTION_ENDPOINT = 'rswebpaytransaction/api/webpay/v1.0/transactions/:token/capture'

class << self
def create(buy_order:, session_id:,card_number:, card_expiration_date:,
details:, options:nil)
def create(
buy_order:, session_id:, card_number:, card_expiration_date:, details:, cvv: nil, 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)
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
}
body[:cvv] = cvv unless cvv.nil?

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
return ::Transbank::TransaccionCompleta::TransactionCreateResponse.new(body) if resp.is_a? Net::HTTPSuccess

raise Errors::TransactionCreateError.new(body['error_message'], resp.code)
end

def installments(token:, details:, options:nil)
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)
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)
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 }
if resp.all? { |res| res.is_a? Net::HTTPSuccess }
return resp.map do |res|
body = JSON.parse(res.body)
::Transbank::TransaccionCompleta::TransactionInstallmentsResponse.new(body)
Expand All @@ -62,43 +66,49 @@ def installments(token:, details:, options:nil)
raise Errors::TransactionInstallmentsError.new(resp, resp.code)
end

def commit(token:,details:, options:nil)
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)
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 }
body = { details: detail }

resp = http_put(uri_string: url, body: body, headers: headers)
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
if resp.is_a? Net::HTTPSuccess
return ::Transbank::TransaccionCompleta::MallTransactionCommitResponse.new(body)
end

raise Errors::TransactionCommitError.new(body['error_message'], resp.code)
end

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)
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
if resp.is_a? Net::HTTPSuccess
return ::Transbank::TransaccionCompleta::MallTransactionStatusResponse.new(body)
end

raise Errors::TransactionStatusError.new(body['error_message'], resp.code)
end

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

body = {
buy_order: child_buy_order,
Expand All @@ -110,20 +120,46 @@ def refund(token:, child_buy_order:, child_commerce_code:, amount:, options:nil)
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
return ::Transbank::TransaccionCompleta::TransactionRefundResponse.new(body) if resp.is_a? Net::HTTPSuccess

raise Errors::TransactionRefundError.new(body['error_message'], resp.code)
end

def capture(token:, commerce_code:, buy_order:, authorization_code:, capture_amount:, options: nil)
api_key = options&.api_key || default_integration_params[:api_key]
parent_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 = {
commerce_code: commerce_code,
buy_order: buy_order,
authorization_code: authorization_code,
capture_amount: capture_amount
}

url = base_url + CAPTURE_TRANSACTION_ENDPOINT.gsub(':token', token)
headers = webpay_headers(commerce_code: parent_commerce_code, api_key: api_key)
resp = http_put(uri_string: url, body: body, headers: headers)
body = JSON.parse(resp.body)
if resp.is_a? Net::HTTPSuccess
return ::Transbank::TransaccionCompleta::MallTransactionCaptureResponse.new(body)
end

raise Errors::TransactionCaptureError.new(body['error_message'], resp.code)
end

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
integration_type: TransaccionCompleta::Base.integration_type,
base_url: TransaccionCompleta::Base.current_integration_type_url
}
end

private

def create_details(details)
details.map do |det|
{
Expand All @@ -137,11 +173,11 @@ def create_details(details)
def commit_details(details)
details.map do |det|
{
commerce_code: det.fetch('commerce_code'){ det.fetch(:commerce_code) },
buy_order: det.fetch('buy_order'){ det.fetch(:buy_order) },
id_query_installments: det.fetch('id_query_installments'){ det.fetch(:id_query_installments) },
deferred_period_index: det.fetch('deferred_period_index'){ det.fetch(:deferred_period_index) },
grace_period: det.fetch('grace_period'){ det.fetch(:grace_period) }
commerce_code: det.fetch('commerce_code') { det.fetch(:commerce_code) },
buy_order: det.fetch('buy_order') { det.fetch(:buy_order) },
id_query_installments: det.fetch('id_query_installments') { det.fetch(:id_query_installments) },
deferred_period_index: det.fetch('deferred_period_index') { det.fetch(:deferred_period_index) },
grace_period: det.fetch('grace_period') { det.fetch(:grace_period) }
}
end
end
Expand All @@ -155,10 +191,7 @@ def installments_details(details)
}
end
end

end
end
end
end


Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Transbank
module TransaccionCompleta
class MallTransactionCaptureResponse
FIELDS = %i[token authorization_code authorization_date captured_amount response_code].freeze
attr_accessor(*FIELDS)

def initialize(json)
FIELDS.each { |field| send("#{field}=", json[field.to_s]) }
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module Transbank
module TransaccionCompleta
class MallTransactionCommitResponse
FIELDS = %i(
buy_order session_id card_detail expiration_date
accounting_date transaction_date details
)
attr_accessor *FIELDS
FIELDS = %i[
buy_order session_id card_detail expiration_date
accounting_date transaction_date details
]
attr_accessor(*FIELDS)

def initialize(json)
FIELDS.each { |field| send("#{field}=", json["#{field}"])}
FIELDS.each { |field| send("#{field}=", json[field.to_s]) }
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'transbank/sdk/transaccion_completa/mall/responses/mall_transaction_commit_response'
require 'transbank/sdk/transaccion_completa/mall/responses/mall_transaction_status_response'
require 'transbank/sdk/transaccion_completa/mall/responses/mall_transaction_installments_response'
require 'transbank/sdk/transaccion_completa/mall/responses/mall_transaction_capture_response'
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
require 'transbank/sdk/transaccion_completa/responses/transaction_create_response'
require 'transbank/sdk/transaccion_completa/responses/transaction_installments_response'
require 'transbank/sdk/transaccion_completa/responses/transaction_status_response'
require 'transbank/sdk/transaccion_completa/responses/transaction_refund_response'
require 'transbank/sdk/transaccion_completa/responses/transaction_refund_response'
require 'transbank/sdk/transaccion_completa/responses/transaction_capture_response'
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Transbank
module TransaccionCompleta
class TransactionCaptureResponse
FIELDS = %i[token authorization_code authorization_date captured_amount response_code].freeze
attr_accessor(*FIELDS)

def initialize(json)
FIELDS.each { |field| send("#{field}=", json[field.to_s]) }
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
require 'transbank/sdk/transaccion_completa/responses/transaccion_completa_responses'
require 'transbank/sdk/transaccion_completa/options'
require 'transbank/sdk/transaccion_completa/base'
require 'transbank/sdk/transaccion_completa/transaction'
require 'transbank/sdk/transaccion_completa/transaction'
require 'transbank/sdk/transaccion_completa/mall/mall_transaction'
require 'transbank/sdk/transaccion_completa/mall/responses/mall_transaction_responses'
Loading

0 comments on commit f1a01db

Please sign in to comment.