tbk-sdk 1.5.0.pre.ruby2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.built +0 -0
- data/.bundled +0 -0
- data/.github/ISSUE_TEMPLATE/reporte-de-error.md +40 -0
- data/.gitignore +16 -0
- data/.rakeTasks +7 -0
- data/.travis.yml +10 -0
- data/CHANGELOG.md +63 -0
- data/Dockerfile +5 -0
- data/Gemfile +6 -0
- data/LICENSE.md +11 -0
- data/Makefile +24 -0
- data/README.md +87 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/docker-compose.yml +20 -0
- data/lib/transbank/sdk/oneclick/base.rb +64 -0
- data/lib/transbank/sdk/oneclick/errors/oneclick_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/inscription/errors/mall_inscription_delete_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/inscription/errors/mall_inscription_finish_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/inscription/errors/mall_inscription_start_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/inscription/errors/oneclick_mall_inscription_errors.rb +3 -0
- data/lib/transbank/sdk/oneclick/inscription/inscription.rb +3 -0
- data/lib/transbank/sdk/oneclick/inscription/responses/mall_inscription_delete_response.rb +18 -0
- data/lib/transbank/sdk/oneclick/inscription/responses/mall_inscription_finish_response.rb +15 -0
- data/lib/transbank/sdk/oneclick/inscription/responses/mall_inscription_start_response.rb +14 -0
- data/lib/transbank/sdk/oneclick/inscription/responses/oneclick_mall_inscription_responses.rb +3 -0
- data/lib/transbank/sdk/oneclick/mall/errors/mall_transaction_authorize_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall/errors/mall_transaction_refund_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall/errors/mall_transaction_status_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall/errors/oneclick_mall_errors.rb +4 -0
- data/lib/transbank/sdk/oneclick/mall/mall_inscription.rb +73 -0
- data/lib/transbank/sdk/oneclick/mall/mall_transaction.rb +97 -0
- data/lib/transbank/sdk/oneclick/mall/oneclick_mall.rb +4 -0
- data/lib/transbank/sdk/oneclick/mall/responses/mall_transaction_authorize_response.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall/responses/mall_transaction_refund_response.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall/responses/mall_transaction_status_response.rb +15 -0
- data/lib/transbank/sdk/oneclick/mall/responses/oneclick_mall_responses.rb +4 -0
- data/lib/transbank/sdk/oneclick/mall_deferred/errors/mall_deferred_transaction_capture_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall_deferred/mall_deferred_inscription.rb +20 -0
- data/lib/transbank/sdk/oneclick/mall_deferred/mall_deferred_transaction.rb +42 -0
- data/lib/transbank/sdk/oneclick/mall_deferred/responses/mall_deferred_transaction_capture_response.rb +16 -0
- data/lib/transbank/sdk/oneclick/oneclick.rb +12 -0
- data/lib/transbank/sdk/onepay/base.rb +125 -0
- data/lib/transbank/sdk/onepay/errors/errors.rb +17 -0
- data/lib/transbank/sdk/onepay/errors/integration_type_error.rb +8 -0
- data/lib/transbank/sdk/onepay/errors/invalid_options_error.rb +8 -0
- data/lib/transbank/sdk/onepay/errors/item_error.rb +8 -0
- data/lib/transbank/sdk/onepay/errors/refund_create_error.rb +8 -0
- data/lib/transbank/sdk/onepay/errors/response_error.rb +8 -0
- data/lib/transbank/sdk/onepay/errors/shopping_cart_error.rb +8 -0
- data/lib/transbank/sdk/onepay/errors/signature_error.rb +8 -0
- data/lib/transbank/sdk/onepay/errors/transaction_commit_error.rb +8 -0
- data/lib/transbank/sdk/onepay/errors/transaction_create_error.rb +8 -0
- data/lib/transbank/sdk/onepay/errors/transbank_error.rb +9 -0
- data/lib/transbank/sdk/onepay/models/channels.rb +15 -0
- data/lib/transbank/sdk/onepay/models/item.rb +103 -0
- data/lib/transbank/sdk/onepay/models/models.rb +10 -0
- data/lib/transbank/sdk/onepay/models/refund.rb +50 -0
- data/lib/transbank/sdk/onepay/models/shopping_cart.rb +65 -0
- data/lib/transbank/sdk/onepay/models/transaction.rb +143 -0
- data/lib/transbank/sdk/onepay/requests/refund_create_request.rb +45 -0
- data/lib/transbank/sdk/onepay/requests/request.rb +18 -0
- data/lib/transbank/sdk/onepay/requests/requests.rb +9 -0
- data/lib/transbank/sdk/onepay/requests/transaction_commit_request.rb +48 -0
- data/lib/transbank/sdk/onepay/requests/transaction_create_request.rb +85 -0
- data/lib/transbank/sdk/onepay/responses/refund_create_response.rb +24 -0
- data/lib/transbank/sdk/onepay/responses/response.rb +18 -0
- data/lib/transbank/sdk/onepay/responses/responses.rb +9 -0
- data/lib/transbank/sdk/onepay/responses/transaction_commit_response.rb +39 -0
- data/lib/transbank/sdk/onepay/responses/transaction_create_response.rb +32 -0
- data/lib/transbank/sdk/patpass/errors/integration_type_error.rb +8 -0
- data/lib/transbank/sdk/patpass/errors/patpass_error.rb +15 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/base.rb +47 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/errors/patpass_by_webpay_errors.rb +12 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/errors/transaction_commit_error.rb +15 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/errors/transaction_create_error.rb +15 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/errors/transaction_status_error.rb +15 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/options.rb +9 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/patpass_by_webpay.rb +16 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/responses/patpass_by_webpay_responses.rb +3 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/responses/transaction_commit_response.rb +20 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/responses/transaction_create_response.rb +15 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/responses/transaction_status_response.rb +21 -0
- data/lib/transbank/sdk/patpass/patpass_by_webpay/transaction.rb +93 -0
- data/lib/transbank/sdk/patpass/patpass_comercio/base.rb +47 -0
- data/lib/transbank/sdk/patpass/patpass_comercio/errors/inscription_start_error.rb +15 -0
- data/lib/transbank/sdk/patpass/patpass_comercio/errors/inscription_status_error.rb +15 -0
- data/lib/transbank/sdk/patpass/patpass_comercio/errors/patpass_comercio_errors.rb +2 -0
- data/lib/transbank/sdk/patpass/patpass_comercio/inscription.rb +79 -0
- data/lib/transbank/sdk/patpass/patpass_comercio/options.rb +9 -0
- data/lib/transbank/sdk/patpass/patpass_comercio/patpass_comercio.rb +5 -0
- data/lib/transbank/sdk/patpass/patpass_comercio/responses/inscription_start_response.rb +13 -0
- data/lib/transbank/sdk/patpass/patpass_comercio/responses/inscription_status_response.rb +13 -0
- data/lib/transbank/sdk/patpass/patpass_comercio/responses/patpass_comercio_responses.rb +2 -0
- data/lib/transbank/sdk/transaccion_completa/base.rb +51 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaccion_completa_error.rb +16 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaccion_completa_errors.rb +7 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_capture_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_commit_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_create_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_installments_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_refund_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_status_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/mall/mall_transaction.rb +197 -0
- data/lib/transbank/sdk/transaccion_completa/mall/responses/mall_transaction_capture_response.rb +12 -0
- data/lib/transbank/sdk/transaccion_completa/mall/responses/mall_transaction_commit_response.rb +15 -0
- data/lib/transbank/sdk/transaccion_completa/mall/responses/mall_transaction_installments_response.rb +11 -0
- data/lib/transbank/sdk/transaccion_completa/mall/responses/mall_transaction_responses.rb +4 -0
- data/lib/transbank/sdk/transaccion_completa/mall/responses/mall_transaction_status_response.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/options.rb +7 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaccion_completa_responses.rb +6 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_capture_response.rb +12 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_commit_response.rb +16 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_create_response.rb +10 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_installments_response.rb +13 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_refund_response.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_status_response.rb +16 -0
- data/lib/transbank/sdk/transaccion_completa/transaccion_completa.rb +7 -0
- data/lib/transbank/sdk/transaccion_completa/transaction.rb +144 -0
- data/lib/transbank/sdk/transbank_error.rb +6 -0
- data/lib/transbank/sdk/utils/json_utils.rb +75 -0
- data/lib/transbank/sdk/utils/net_helper.rb +82 -0
- data/lib/transbank/sdk/utils/request_builder.rb +96 -0
- data/lib/transbank/sdk/utils/signature_utils.rb +49 -0
- data/lib/transbank/sdk/utils/utils.rb +9 -0
- data/lib/transbank/sdk/version.rb +5 -0
- data/lib/transbank/sdk/webpay/errors/integration_type_error.rb +8 -0
- data/lib/transbank/sdk/webpay/errors/webpay_error.rb +14 -0
- data/lib/transbank/sdk/webpay/webpay_plus/base.rb +74 -0
- data/lib/transbank/sdk/webpay/webpay_plus/deferred/deferred_transaction.rb +108 -0
- data/lib/transbank/sdk/webpay/webpay_plus/deferred/errors/transaction_capture_error.rb +16 -0
- data/lib/transbank/sdk/webpay/webpay_plus/deferred/errors/webpay_plus_deferred_errors.rb +1 -0
- data/lib/transbank/sdk/webpay/webpay_plus/deferred/responses/transaction_capture_response.rb +22 -0
- data/lib/transbank/sdk/webpay/webpay_plus/deferred/responses/webpay_plus_deferred_responses.rb +1 -0
- data/lib/transbank/sdk/webpay/webpay_plus/errors/transaction_commit_error.rb +17 -0
- data/lib/transbank/sdk/webpay/webpay_plus/errors/transaction_create_error.rb +16 -0
- data/lib/transbank/sdk/webpay/webpay_plus/errors/transaction_refund_error.rb +16 -0
- data/lib/transbank/sdk/webpay/webpay_plus/errors/transaction_status_error.rb +16 -0
- data/lib/transbank/sdk/webpay/webpay_plus/errors/webpay_plus_errors.rb +13 -0
- data/lib/transbank/sdk/webpay/webpay_plus/mall/mall_transaction.rb +88 -0
- data/lib/transbank/sdk/webpay/webpay_plus/mall/responses/mall_transaction_commit_response.rb +23 -0
- data/lib/transbank/sdk/webpay/webpay_plus/mall/responses/mall_transaction_status_response.rb +21 -0
- data/lib/transbank/sdk/webpay/webpay_plus/mall/responses/webpay_plus_mall_responses.rb +2 -0
- data/lib/transbank/sdk/webpay/webpay_plus/mall_deferred/mall_deferred_transaction.rb +109 -0
- data/lib/transbank/sdk/webpay/webpay_plus/options.rb +9 -0
- data/lib/transbank/sdk/webpay/webpay_plus/responses/transaction_commit_response.rb +20 -0
- data/lib/transbank/sdk/webpay/webpay_plus/responses/transaction_create_response.rb +15 -0
- data/lib/transbank/sdk/webpay/webpay_plus/responses/transaction_refund_response.rb +19 -0
- data/lib/transbank/sdk/webpay/webpay_plus/responses/transaction_status_response.rb +21 -0
- data/lib/transbank/sdk/webpay/webpay_plus/responses/webpay_plus_responses.rb +4 -0
- data/lib/transbank/sdk/webpay/webpay_plus/transaction.rb +89 -0
- data/lib/transbank/sdk/webpay/webpay_plus/webpay_plus.rb +24 -0
- data/lib/transbank/sdk.rb +27 -0
- data/sdk_test.sh +2 -0
- data/transbank-sdk.gemspec +35 -0
- metadata +340 -0
@@ -0,0 +1,103 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
class Item
|
4
|
+
include Transbank::Utils::JSONUtils
|
5
|
+
# An Item to be purchased by the user, and to be added to a [ShoppingCart]
|
6
|
+
|
7
|
+
# @return [String] An item's description
|
8
|
+
attr_reader :description
|
9
|
+
|
10
|
+
# @return quantity [Integer] How many of units of [Item]
|
11
|
+
attr_reader :quantity
|
12
|
+
|
13
|
+
# @return amount [Integer] The value of each unit of [Item]
|
14
|
+
attr_reader :amount
|
15
|
+
|
16
|
+
# @return additional_data [String] A string with whatever additional data the
|
17
|
+
# Merchant might want to add
|
18
|
+
attr_reader :additional_data
|
19
|
+
|
20
|
+
# @return expire [Integer] Expiry for the Item
|
21
|
+
attr_reader :expire
|
22
|
+
# @param [Hash] opts options Hash
|
23
|
+
# @param description [String] The item's description
|
24
|
+
# @param quantity [Integer] How many of units of [Item]
|
25
|
+
# @param amount [Integer] The value of each unit of [Item]
|
26
|
+
# @param additional_data [String] A string with whatever additional data the
|
27
|
+
# Merchant might want to add
|
28
|
+
# @param expire [Integer] Expiry for the Item
|
29
|
+
# @raise [ItemError] when opts is not a [Hash]
|
30
|
+
def initialize(opts = {})
|
31
|
+
raise Errors::ItemError, 'Item must be a Hash' unless opts.is_a? Hash
|
32
|
+
opts = transform_hash_keys opts
|
33
|
+
self.description = opts.fetch(:description)
|
34
|
+
self.quantity = opts.fetch(:quantity)
|
35
|
+
self.amount = opts.fetch(:amount)
|
36
|
+
self.additional_data = opts.fetch(:additional_data, nil)
|
37
|
+
self.expire = opts.fetch(:expire, nil)
|
38
|
+
end
|
39
|
+
|
40
|
+
# @param description [String] An item's description
|
41
|
+
# @raise [ItemError] when description is null
|
42
|
+
def description=(description)
|
43
|
+
raise Errors::ItemError, "Description cannot be null" if description.nil?
|
44
|
+
@description = description
|
45
|
+
end
|
46
|
+
|
47
|
+
# @param quantity [Integer] How many of units of [Item]
|
48
|
+
# @raise [ItemError] when given quantity is nil or less than zero.
|
49
|
+
def quantity=(quantity)
|
50
|
+
raise Errors::ItemError, "Quantity cannot be null" if quantity.nil?
|
51
|
+
if quantity < 0
|
52
|
+
raise Errors::ItemError, "Quantity cannot be less than zero"
|
53
|
+
end
|
54
|
+
@quantity = quantity
|
55
|
+
end
|
56
|
+
|
57
|
+
# @param amount [Integer] The value of each unit of [Item]
|
58
|
+
# @raise [ItemError] when amount cannot be null.
|
59
|
+
# @raise [ArgumentError] when amount is not an Integer.
|
60
|
+
def amount=(amount)
|
61
|
+
raise Errors::ItemError, "Amount cannot be null" if amount.nil?
|
62
|
+
if amount != Integer(amount)
|
63
|
+
raise ArgumentError, "Amount is not an Integer"
|
64
|
+
end
|
65
|
+
@amount = amount
|
66
|
+
end
|
67
|
+
|
68
|
+
# @param additional_data [String] A string with whatever additional data the
|
69
|
+
# Merchant might want to add
|
70
|
+
def additional_data=(additional_data)
|
71
|
+
additional_data = '' if additional_data.nil?
|
72
|
+
@additional_data = additional_data
|
73
|
+
end
|
74
|
+
|
75
|
+
# @param expire [Integer] Expiry for the Item
|
76
|
+
def expire=(expire)
|
77
|
+
expire = expire || 0
|
78
|
+
@expire = expire
|
79
|
+
end
|
80
|
+
|
81
|
+
# Return the total amount to pay for the Item, that is, amount * quantity
|
82
|
+
# @return [Numeric] the total amount to pay for the [Item]
|
83
|
+
def total
|
84
|
+
self.quantity * self.amount
|
85
|
+
end
|
86
|
+
|
87
|
+
# Override == to allow comparison between [Item]s
|
88
|
+
# @return [boolean] true if equal, false otherwise
|
89
|
+
def ==(another_item)
|
90
|
+
self.description == another_item.description &&
|
91
|
+
self.quantity == another_item.quantity &&
|
92
|
+
self.amount == another_item.amount &&
|
93
|
+
self.additional_data == another_item.additional_data &&
|
94
|
+
self.expire == another_item.expire
|
95
|
+
end
|
96
|
+
|
97
|
+
# Alias for #==
|
98
|
+
def eql?(another_item)
|
99
|
+
self.==(another_item)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'transbank/sdk/onepay/models/item'
|
2
|
+
require 'transbank/sdk/onepay/models/channels'
|
3
|
+
require 'transbank/sdk/onepay/models/refund'
|
4
|
+
require 'transbank/sdk/onepay/models/shopping_cart'
|
5
|
+
require 'transbank/sdk/onepay/models/transaction'
|
6
|
+
|
7
|
+
module Transbank
|
8
|
+
module Onepay
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
class Refund
|
4
|
+
extend Transbank::Utils::NetHelper, Utils::RequestBuilder
|
5
|
+
# Manages Refunds
|
6
|
+
REFUND_TRANSACTION = 'nullifytransaction'.freeze
|
7
|
+
TRANSACTION_BASE_PATH = '/ewallet-plugin-api-services/services/transactionservice/'.freeze
|
8
|
+
|
9
|
+
class << self
|
10
|
+
# Create a request for a Refund
|
11
|
+
# @param amount [Integer] Amount to be refunded. Must be the full amount of the
|
12
|
+
# [Transaction] to be refunded
|
13
|
+
# @param occ [String] Merchant purchase order
|
14
|
+
# @param external_unique_number [String] Unique identifier (per Merchant) of the [Transaction] that
|
15
|
+
# will be refunded
|
16
|
+
# @param authorization_code [String] Authorization code. This is given when the [Transaction]
|
17
|
+
# is successfully committed (with the #commit method of [Transaction])
|
18
|
+
# @param options[Hash, nil] an optional Hash with configuration overrides
|
19
|
+
# @raise [RefundCreateError] if the response is nil or has no response code
|
20
|
+
# @raise [RefundCreateError] if response from the service is not equal to 'OK'
|
21
|
+
def create(amount:, occ:, external_unique_number:, authorization_code:, options: nil)
|
22
|
+
refund_request = refund_transaction(refund_amount: amount,
|
23
|
+
occ: occ,
|
24
|
+
external_unique_number: external_unique_number,
|
25
|
+
authorization_code: authorization_code,
|
26
|
+
options: options)
|
27
|
+
response = http_post(uri_string: refund_path, body: refund_request.to_h)
|
28
|
+
if response.nil? || !JSON.parse(response.body)['responseCode']
|
29
|
+
raise Errors::RefundCreateError, 'Could not obtain a response from the service.'
|
30
|
+
end
|
31
|
+
|
32
|
+
refund_create_response = RefundCreateResponse.new(JSON.parse(response.body))
|
33
|
+
|
34
|
+
unless refund_create_response.response_ok?
|
35
|
+
raise Errors::RefundCreateError, refund_create_response.full_description
|
36
|
+
end
|
37
|
+
|
38
|
+
refund_create_response
|
39
|
+
end
|
40
|
+
|
41
|
+
# Return the string url to POST to
|
42
|
+
# @return [String] the url to POST to
|
43
|
+
def refund_path
|
44
|
+
Base.current_integration_type_url + TRANSACTION_BASE_PATH + REFUND_TRANSACTION
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
# Represents a Shopping Cart, which contains [Item]s that the user wants to buy
|
4
|
+
class ShoppingCart
|
5
|
+
include Transbank::Utils::JSONUtils
|
6
|
+
|
7
|
+
# @return [Array<Item>] An [Array<Item>] with the [ShoppingCart] contents
|
8
|
+
attr_reader :items
|
9
|
+
|
10
|
+
# @param items [Array, nil] an array of Hashes that can be converted to [Item]
|
11
|
+
# if nil, an empty shopping cart is created
|
12
|
+
def initialize(items = [])
|
13
|
+
# An [Array<Item>] with the [ShoppingCart] contents
|
14
|
+
@items = []
|
15
|
+
return if items.nil? || items.empty?
|
16
|
+
|
17
|
+
items.each do |it|
|
18
|
+
it = transform_hash_keys it
|
19
|
+
item = Item.new it
|
20
|
+
self << item
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# @param item [Item] an instance of [Item]
|
25
|
+
# @return [boolean] return true if item is successfully added
|
26
|
+
def add(item)
|
27
|
+
new_total = total + item.total
|
28
|
+
if new_total < 0
|
29
|
+
raise Errors::ShoppingCartError, "Total amount cannot be less than zero."
|
30
|
+
end
|
31
|
+
@items << item
|
32
|
+
end
|
33
|
+
|
34
|
+
# Alias for #add
|
35
|
+
def << item
|
36
|
+
add item
|
37
|
+
end
|
38
|
+
|
39
|
+
# Remove an [Item] from self
|
40
|
+
# @raise [ShoppingCartError] if item is not found
|
41
|
+
def remove(item)
|
42
|
+
if @items.delete(item).nil?
|
43
|
+
raise Errors::ShoppingCartError, "Item not found"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Clear the cart, setting @items to []
|
48
|
+
def remove_all
|
49
|
+
@items = []
|
50
|
+
end
|
51
|
+
|
52
|
+
# @return [Integer] The amount in CLP of the [Item]s included in the [ShoppingCart]
|
53
|
+
def total
|
54
|
+
# Array#sum is Ruby 2.4+
|
55
|
+
@items.reduce(0) { |total, item| total + item.total }
|
56
|
+
end
|
57
|
+
|
58
|
+
# Sum the quantity of items in the cart
|
59
|
+
def items_quantity
|
60
|
+
# Array#sum is Ruby 2.4+
|
61
|
+
@items.reduce(0) { |total, item| total + item.quantity }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
## Class Transaction
|
4
|
+
# This class creates or commits a Transaction (that is, a purchase)
|
5
|
+
class Transaction
|
6
|
+
extend Transbank::Utils::NetHelper, Utils::RequestBuilder
|
7
|
+
|
8
|
+
SEND_TRANSACTION = 'sendtransaction'.freeze
|
9
|
+
COMMIT_TRANSACTION = 'gettransactionnumber'.freeze
|
10
|
+
TRANSACTION_BASE_PATH = '/ewallet-plugin-api-services/services/transactionservice/'.freeze
|
11
|
+
|
12
|
+
class << self
|
13
|
+
# Create a [Transaction], initiating the purchase process.
|
14
|
+
# @param shopping_cart [ShoppingCart] contains the [Item]s to be purchased
|
15
|
+
# @param channel [String] The channel that the transaction is going to be done through. Valid values are contained on the [Transbank::Onepay::Channel] class
|
16
|
+
# @param external_unique_number [String] a unique value (per Merchant, not global) that is used to identify a Transaction
|
17
|
+
# @param options[Hash, nil] an optional Hash with configuration overrides
|
18
|
+
# @return [TransactionCreateResponse] the response to your request.
|
19
|
+
# Includes data that you will need to #commit your [Transaction]
|
20
|
+
# @raise [ShoppingCartError] if shopping cart is nil or empty
|
21
|
+
# @raise [TransactionCreateError] if channel is not valid
|
22
|
+
# @raise [TransactionCreateError] if no response is gotten, or responseCode of the response is not 'OK'
|
23
|
+
def create(shopping_cart:, channel: nil, external_unique_number: nil,
|
24
|
+
options: nil)
|
25
|
+
if is_options_hash?(channel)
|
26
|
+
options = channel
|
27
|
+
channel = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
if is_options_hash?(external_unique_number)
|
31
|
+
options = external_unique_number
|
32
|
+
external_unique_number = nil
|
33
|
+
end
|
34
|
+
|
35
|
+
validate_channel!(channel)
|
36
|
+
validate_shopping_cart!(shopping_cart)
|
37
|
+
|
38
|
+
options = complete_options(options)
|
39
|
+
create_request = create_transaction(shopping_cart: shopping_cart,
|
40
|
+
channel: channel,
|
41
|
+
external_unique_number: external_unique_number,
|
42
|
+
options: options)
|
43
|
+
response = http_post(uri_string: transaction_create_path, body: create_request.to_h)
|
44
|
+
validate_create_response!(response)
|
45
|
+
transaction_create_response = TransactionCreateResponse.new JSON.parse(response.body)
|
46
|
+
signature_is_valid = transaction_create_response.valid_signature?(options.fetch(:shared_secret))
|
47
|
+
unless signature_is_valid
|
48
|
+
raise Errors::SignatureError, "The response's signature is not valid."
|
49
|
+
end
|
50
|
+
transaction_create_response
|
51
|
+
end
|
52
|
+
|
53
|
+
# Commit a [Transaction]. It is MANDATORY for this to be done, and you have
|
54
|
+
# 30 seconds to do so, otherwise the [Transaction] is automatically REVERSED
|
55
|
+
# @param occ [String] Merchant purchase order
|
56
|
+
# @param external_unique_number [String] a unique value (per Merchant, not global) that is used to identify a Transaction
|
57
|
+
# @param options[Hash, nil] an optional Hash with configuration overrides
|
58
|
+
# @return [TransactionCommitResponse] The response to your commit request.
|
59
|
+
# @raise [TransactionCommitError] if response is nil or responseCode of the response is not 'OK'
|
60
|
+
def commit(occ:, external_unique_number:, options: nil)
|
61
|
+
options = complete_options(options)
|
62
|
+
commit_request = commit_transaction(occ: occ,
|
63
|
+
external_unique_number: external_unique_number,
|
64
|
+
options: options)
|
65
|
+
response = http_post(uri_string: transaction_commit_path, body: commit_request.to_h)
|
66
|
+
validate_commit_response!(response)
|
67
|
+
transaction_commit_response = TransactionCommitResponse.new(JSON.parse(response.body))
|
68
|
+
signature_is_valid = transaction_commit_response.valid_signature?(options.fetch(:shared_secret))
|
69
|
+
unless signature_is_valid
|
70
|
+
raise Errors::SignatureError, "The response's signature is not valid."
|
71
|
+
end
|
72
|
+
transaction_commit_response
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def is_options_hash?(hash)
|
78
|
+
return false unless hash.respond_to? :keys
|
79
|
+
# Intersection of the two arrays
|
80
|
+
([:api_key, :shared_secret] & hash.keys).any?
|
81
|
+
end
|
82
|
+
|
83
|
+
def validate_channel!(channel)
|
84
|
+
if channel_is_app?(channel) && Base::app_scheme.nil?
|
85
|
+
raise Errors::TransactionCreateError, 'You need to set an app_scheme if you want to use the APP channel'
|
86
|
+
end
|
87
|
+
|
88
|
+
if channel_is_mobile?(channel) && Base::callback_url.nil?
|
89
|
+
raise Errors::TransactionCreateError, 'You need to set a valid callback if you want to use the MOBILE channel'
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def validate_shopping_cart!(shopping_cart)
|
94
|
+
if shopping_cart.items.nil? || shopping_cart.items.empty?
|
95
|
+
raise Errors::ShoppingCartError, 'Shopping cart is null or empty.'
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def validate_commit_response!(response)
|
100
|
+
unless response
|
101
|
+
raise Errors::TransactionCommitError, 'Could not obtain a response from the service.'
|
102
|
+
end
|
103
|
+
|
104
|
+
unless JSON.parse(response.body).fetch('responseCode') == 'OK'
|
105
|
+
body = JSON.parse(response.body)
|
106
|
+
msg = "#{body.fetch('responseCode')} : #{body.fetch('description')}"
|
107
|
+
raise Errors::TransactionCommitError, msg
|
108
|
+
end
|
109
|
+
response
|
110
|
+
end
|
111
|
+
|
112
|
+
def validate_create_response!(response)
|
113
|
+
unless response
|
114
|
+
raise Errors::TransactionCreateError, 'Could not obtain a response from the service.'
|
115
|
+
end
|
116
|
+
|
117
|
+
unless JSON.parse(response.body).fetch('responseCode') == 'OK'
|
118
|
+
body = JSON.parse(response.body)
|
119
|
+
msg = "#{body.fetch('responseCode')} : #{body['description']}"
|
120
|
+
raise Errors::TransactionCreateError, msg
|
121
|
+
end
|
122
|
+
response
|
123
|
+
end
|
124
|
+
|
125
|
+
def channel_is_app?(channel)
|
126
|
+
channel && channel == Transbank::Onepay::Channel::APP
|
127
|
+
end
|
128
|
+
|
129
|
+
def channel_is_mobile?(channel)
|
130
|
+
channel && channel == Transbank::Onepay::Channel::MOBILE
|
131
|
+
end
|
132
|
+
|
133
|
+
def transaction_create_path
|
134
|
+
Base.current_integration_type_url + TRANSACTION_BASE_PATH + SEND_TRANSACTION
|
135
|
+
end
|
136
|
+
|
137
|
+
def transaction_commit_path
|
138
|
+
Base.current_integration_type_url + TRANSACTION_BASE_PATH + COMMIT_TRANSACTION
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
# Creates a Refund request
|
4
|
+
class RefundCreateRequest
|
5
|
+
include Request
|
6
|
+
attr_accessor :nullify_amount, :occ, :external_unique_number,
|
7
|
+
:authorization_code, :issued_at, :signature
|
8
|
+
|
9
|
+
# These are the params used to build this class's @signature
|
10
|
+
SIGNATURE_PARAMS = [:occ,
|
11
|
+
:external_unique_number,
|
12
|
+
:authorization_code,
|
13
|
+
:issued_at,
|
14
|
+
:nullify_amount].freeze
|
15
|
+
|
16
|
+
# @param nullify_amount [Integer, nil] The total amount of the [Transaction] to Refund.
|
17
|
+
# No partial refunds are possible
|
18
|
+
# @param external_unique_number [String] a unique value (per Merchant, not global) that is used to identify a Transaction
|
19
|
+
# @param occ [String] Merchant purchase order
|
20
|
+
# @param authorization_code [String] a string returned when [Transaction]#commit completes correctly
|
21
|
+
# @param issued_at [Integer, nil] a timestamp
|
22
|
+
# @param signature [String, nil] a hashed string to verify the data
|
23
|
+
def initialize(nullify_amount: nil,
|
24
|
+
occ: nil,
|
25
|
+
external_unique_number: nil,
|
26
|
+
authorization_code: nil,
|
27
|
+
issued_at: nil,
|
28
|
+
signature: nil)
|
29
|
+
@nullify_amount = nullify_amount
|
30
|
+
@occ = occ
|
31
|
+
@external_unique_number = external_unique_number
|
32
|
+
@authorization_code = authorization_code
|
33
|
+
@issued_at = issued_at
|
34
|
+
@signature = signature
|
35
|
+
end
|
36
|
+
|
37
|
+
# Create and set the signature for this instance of RefundCreateRequest
|
38
|
+
# @return [RefundCreateRequest] returns self
|
39
|
+
def sign(secret)
|
40
|
+
@signature = signature_for(to_data, secret)
|
41
|
+
self
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
# Base module with methods & attributes common to Requests
|
4
|
+
module Request
|
5
|
+
include Transbank::Utils::JSONUtils, Utils::SignatureUtils
|
6
|
+
attr_accessor :api_key
|
7
|
+
attr_accessor :app_key
|
8
|
+
|
9
|
+
# Set the request's @api_key overriding the one in
|
10
|
+
# the [Base] class
|
11
|
+
def set_keys_from_options(options)
|
12
|
+
transform_hash_keys(options)
|
13
|
+
new_api_key = options.fetch(:api_key, nil)
|
14
|
+
self.api_key = new_api_key unless new_api_key.nil?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'transbank/sdk/onepay/requests/request'
|
2
|
+
require 'transbank/sdk/onepay/requests/transaction_create_request'
|
3
|
+
require 'transbank/sdk/onepay/requests/transaction_commit_request'
|
4
|
+
require 'transbank/sdk/onepay/requests/refund_create_request'
|
5
|
+
|
6
|
+
module Transbank
|
7
|
+
module Onepay
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
# Creates a request to Transbank attempting to commit a [Transaction]
|
4
|
+
class TransactionCommitRequest
|
5
|
+
include Request
|
6
|
+
attr_reader :occ, :external_unique_number, :issued_at, :signature
|
7
|
+
|
8
|
+
SIGNATURE_PARAMS = [:occ,
|
9
|
+
:external_unique_number,
|
10
|
+
:issued_at].freeze
|
11
|
+
# @param occ [String] Merchant purchase order
|
12
|
+
# @param external_unique_number [String] a unique value (per Merchant, not global) that is used to identify a Transaction
|
13
|
+
# @param issued_at [Integer] timestamp for when the transaction commit request was created
|
14
|
+
def initialize(occ, external_unique_number, issued_at)
|
15
|
+
self.occ = occ
|
16
|
+
self.external_unique_number = external_unique_number
|
17
|
+
self.issued_at = issued_at
|
18
|
+
@signature = nil
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param occ [String] Merchant purchase order
|
22
|
+
def occ=(occ)
|
23
|
+
raise Errors::TransactionCommitError, 'occ cannot be null.' if occ.nil?
|
24
|
+
@occ = occ
|
25
|
+
end
|
26
|
+
|
27
|
+
# @param external_unique_number [String] a unique value (per Merchant, not global) that is used to identify a Transaction
|
28
|
+
def external_unique_number=(external_unique_number)
|
29
|
+
raise Errors::TransactionCommitError, 'external_unique_number cannot be null.' if external_unique_number.nil?
|
30
|
+
@external_unique_number = external_unique_number
|
31
|
+
end
|
32
|
+
|
33
|
+
# @param issued_at [Integer] timestamp for when the transaction commit request was created
|
34
|
+
def issued_at=(issued_at)
|
35
|
+
raise Errors::TransactionCommitError, 'issued_at cannot be null.' if issued_at.nil?
|
36
|
+
@issued_at = issued_at
|
37
|
+
end
|
38
|
+
|
39
|
+
# Create a signature string and assign it to @signature
|
40
|
+
# @return [TransactionCommitRequest] self
|
41
|
+
def sign(secret)
|
42
|
+
@signature = signature_for(to_data, secret)
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
# Create a payload to create [Transaction] on Transbank
|
4
|
+
class TransactionCreateRequest
|
5
|
+
include Request
|
6
|
+
|
7
|
+
attr_accessor :external_unique_number, :total, :items_quantity, :issued_at,
|
8
|
+
:items, :callback_url, :channel, :app_scheme, :signature,
|
9
|
+
:commerce_logo_url, :width_height
|
10
|
+
attr_reader :generate_ott_qr_code
|
11
|
+
|
12
|
+
SIGNATURE_PARAMS = [:external_unique_number,
|
13
|
+
:total,
|
14
|
+
:items_quantity,
|
15
|
+
:issued_at,
|
16
|
+
:callback_url].freeze
|
17
|
+
# @param opts [Hash] options hash with params needed to initialize this
|
18
|
+
# @param external_unique_number [String] Unique identifier (per Merchant) of the [Transaction] that
|
19
|
+
# @param total [Numeric] the total amount to pay for the items
|
20
|
+
# @param items_quantity [Numeric] the quantity of items on the shopping cart
|
21
|
+
# @param items [Array<Item] the items on the shopping cart
|
22
|
+
# @param issued_at [Numeric] timestamp at the moment the transaction is created
|
23
|
+
# @param callback_url [String] used when the channel is mobile, to be able to finish the [Transaction]
|
24
|
+
# @param channel [String] The channel the operation is made on. Valid values
|
25
|
+
# are on the [Channel] class
|
26
|
+
# @param app_scheme [String] identificator for the Merchant's app
|
27
|
+
# @param signature [String, nil] a hashstring created for verification purposes
|
28
|
+
# @param commerce_logo_url [String] The URL pointing to the Merchant's logo
|
29
|
+
# @param width_height [Numeric] qr width and height used by the frontend JS SDK.
|
30
|
+
def initialize(opts = {})
|
31
|
+
self.external_unique_number = opts.fetch(:external_unique_number)
|
32
|
+
self.total = opts.fetch(:total)
|
33
|
+
self.items_quantity = opts.fetch(:items_quantity)
|
34
|
+
self.items = opts.fetch(:items)
|
35
|
+
self.issued_at = opts.fetch(:issued_at)
|
36
|
+
self.callback_url = opts.fetch(:callback_url)
|
37
|
+
channel = opts.fetch(:channel, Channel::WEB)
|
38
|
+
self.channel = channel
|
39
|
+
self.app_scheme = opts.fetch(:app_scheme, '')
|
40
|
+
self.signature = nil
|
41
|
+
self.commerce_logo_url = opts.fetch(:commerce_logo_url)
|
42
|
+
self.width_height = opts.fetch(:width_height)
|
43
|
+
# This is never anything but true, but it is required by the server
|
44
|
+
@generate_ott_qr_code = true
|
45
|
+
end
|
46
|
+
|
47
|
+
def external_unique_number=(external_unique_number)
|
48
|
+
raise Errors::TransactionCreateError, 'External Unique Number cannot be null.' if external_unique_number.nil?
|
49
|
+
@external_unique_number = external_unique_number
|
50
|
+
end
|
51
|
+
|
52
|
+
def total=(total)
|
53
|
+
raise Errors::TransactionCreateError, 'Total cannot be null.' if total.nil?
|
54
|
+
raise Errors::TransactionCreateError, 'Total cannot be less than zero.' if total < 0
|
55
|
+
@total = total
|
56
|
+
end
|
57
|
+
|
58
|
+
def items_quantity=(items_quantity)
|
59
|
+
raise Errors::TransactionCreateError, 'Items quantity cannot be null.' if items_quantity.nil?
|
60
|
+
raise Errors::TransactionCreateError, 'Items quantity cannot be less than zero.' if items_quantity < 0
|
61
|
+
@items_quantity = items_quantity
|
62
|
+
end
|
63
|
+
|
64
|
+
def items=(items)
|
65
|
+
raise Errors::TransactionCreateError, 'Items must not be empty.' if items.empty?
|
66
|
+
@items = items
|
67
|
+
end
|
68
|
+
|
69
|
+
def callback_url=(callback_url)
|
70
|
+
raise Errors::TransactionCreateError, 'Callback url cannot be null.' if callback_url.nil?
|
71
|
+
@callback_url = callback_url
|
72
|
+
end
|
73
|
+
|
74
|
+
def channel=(channel)
|
75
|
+
raise Errors::TransactionCreateError, 'Channel cannot be null.' if channel.nil?
|
76
|
+
@channel = channel
|
77
|
+
end
|
78
|
+
|
79
|
+
def sign(secret)
|
80
|
+
@signature = signature_for(to_data, secret)
|
81
|
+
self
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
# Serializes the response to a RefundCreateRequest
|
4
|
+
class RefundCreateResponse
|
5
|
+
include Response
|
6
|
+
attr_accessor :occ, :external_unique_number, :reverse_code, :issued_at,
|
7
|
+
:signature
|
8
|
+
# @raise []RefundCreateError] if the responseCode from the service is not 'OK'
|
9
|
+
def initialize(json)
|
10
|
+
unless json.fetch('responseCode').downcase == 'ok'
|
11
|
+
raise Errors::RefundCreateError, "#{json.fetch('responseCode')} : #{json.fetch('description')}"
|
12
|
+
end
|
13
|
+
result = json.fetch('result')
|
14
|
+
@response_code = json.fetch('responseCode')
|
15
|
+
@description = json.fetch('description')
|
16
|
+
@occ = result.fetch('occ')
|
17
|
+
@external_unique_number = result.fetch('externalUniqueNumber')
|
18
|
+
@reverse_code = result.fetch('reverseCode')
|
19
|
+
@issued_at = result.fetch('issuedAt')
|
20
|
+
@signature = result.fetch('signature')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
# Shared methods and attributes between all types of Responses
|
4
|
+
module Response
|
5
|
+
include Utils::SignatureUtils, Transbank::Utils::JSONUtils
|
6
|
+
attr_accessor :response_code
|
7
|
+
attr_accessor :description
|
8
|
+
|
9
|
+
def response_ok?
|
10
|
+
response_code.downcase == 'ok'
|
11
|
+
end
|
12
|
+
|
13
|
+
def full_description
|
14
|
+
"#{ response_code } : #{ description }"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'transbank/sdk/onepay/responses/response'
|
2
|
+
require 'transbank/sdk/onepay/responses/refund_create_response'
|
3
|
+
require 'transbank/sdk/onepay/responses/transaction_commit_response'
|
4
|
+
require 'transbank/sdk/onepay/responses/transaction_create_response'
|
5
|
+
|
6
|
+
module Transbank
|
7
|
+
module Onepay
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Onepay
|
3
|
+
# Serializes the response to a TransactionCommitRequest
|
4
|
+
class TransactionCommitResponse
|
5
|
+
include Response
|
6
|
+
|
7
|
+
attr_accessor :occ, :authorization_code, :signature, :transaction_desc,
|
8
|
+
:buy_order, :issued_at, :amount, :installments_amount,
|
9
|
+
:installments_number
|
10
|
+
|
11
|
+
SIGNATURE_PARAMS = [:occ,
|
12
|
+
:authorization_code,
|
13
|
+
:issued_at,
|
14
|
+
:amount,
|
15
|
+
:installments_amount,
|
16
|
+
:installments_number,
|
17
|
+
:buy_order].freeze
|
18
|
+
# @raise [KeyError] upon missing a response parameter
|
19
|
+
def initialize(json)
|
20
|
+
result = json.fetch('result')
|
21
|
+
@response_code = json.fetch('responseCode')
|
22
|
+
@description = json.fetch('description')
|
23
|
+
@occ = result.fetch('occ')
|
24
|
+
@authorization_code = result.fetch('authorizationCode')
|
25
|
+
@signature = result.fetch('signature')
|
26
|
+
@transaction_desc = result.fetch('transactionDesc')
|
27
|
+
@buy_order = result.fetch('buyOrder')
|
28
|
+
@issued_at = result.fetch('issuedAt')
|
29
|
+
@amount = result.fetch('amount')
|
30
|
+
@installments_amount = result.fetch('installmentsAmount')
|
31
|
+
@installments_number = result.fetch('installmentsNumber')
|
32
|
+
end
|
33
|
+
|
34
|
+
def sign(secret)
|
35
|
+
@signature = signature_for(to_data, secret)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|