solidus_inter 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6880a50bb4064d065f42b193d4eca5f279a69e26a53f5e8e98cc4aa1f2ba1f25
4
- data.tar.gz: f6f455ab7c640fb55bcee39bf46e0d93e3c1bb0ca93fe5191ae0e83292900dd1
3
+ metadata.gz: ae346a932b747ab2ca304f73324d30cfa84fabbdd5fbfd80d7a6c0aa7532d174
4
+ data.tar.gz: 32ce13c0e6b3c630031fe7625d5b12fdc399811cdf54f35efea0225a74d56ea3
5
5
  SHA512:
6
- metadata.gz: cc31d973df48f0b66e5913fcc9b0bb71df492e09db7d90470cb029fb3bf183a2b97d7e45f1a587844ceee8a340ebc6d6a86b4722a6c30bd98d3172714b030b24
7
- data.tar.gz: e3fdbe911fc408488f990597aee8d218c4408daa33ec02678595c751b22572722e17baf7ef6da09d0393fa9706faf0f865723d34a01deb42988936cb8ef7d57a
6
+ metadata.gz: a68aec24ba88bece094de83fc396004f46fb18e7fa4fef15b3210aac7f314590bb7562806d604abc02caf1bffaa7731dc85fa2435515b77e5d59892637c7d9a7
7
+ data.tar.gz: 4b96483f66dd64894b71a794f542f96b099288b069cbaf311ebb1abf446cebcd22d130630c2cae4535510cae6679ec0cf21a1efd80b5af1eff665440420704be
@@ -1,26 +1,25 @@
1
1
  module SolidusInter
2
2
  class Gateway
3
- def initialize options
4
- end
3
+ def initialize(options) end
5
4
 
6
- def purchase money, source, options = {}
5
+ def purchase(_money, source, _options = {})
7
6
  inter_payment = source.retrieve_from_api
8
7
  if inter_payment.paid?
9
- source.update(status: "approved", e2e_id: inter_payment.end_to_end_id)
8
+ source.update(status: "approved", paid_amount: inter_payment.valor_pago, e2e_id: inter_payment.end_to_end_id)
10
9
  successful_response("Pagamento realizado", inter_payment.txid)
11
10
  else
12
11
  failure_response(inter_payment.internal_error || "Ocorreu um erro no pagamento.")
13
12
  end
14
13
  end
15
14
 
16
- def void(transaction_id, options = {})
15
+ def void(transaction_id, _options = {})
17
16
  # Respondendo sempre com successful_response para funcionar o botão de "Cancelar" do pedido. Reembolso deve ser feito por fora.
18
- successful_response("Pedido cancelado. Se necessário, realize o reembolso.", transaction_id)
17
+ successful_response("Pagamento cancelado. Se necessário, realize o reembolso.", transaction_id)
19
18
  end
20
19
 
21
20
  private
22
21
 
23
- def successful_response message, transaction_id
22
+ def successful_response(message, transaction_id)
24
23
  ActiveMerchant::Billing::Response.new(
25
24
  true,
26
25
  message,
@@ -29,7 +28,7 @@ module SolidusInter
29
28
  )
30
29
  end
31
30
 
32
- def failure_response message
31
+ def failure_response(message)
33
32
  ActiveMerchant::Billing::Response.new(
34
33
  false,
35
34
  message
@@ -27,45 +27,41 @@ module SolidusInter
27
27
  "inter_pix"
28
28
  end
29
29
 
30
- def create_payment(payment)
31
- client = set_api_client
32
- inter_payment = client.create_payment(
33
- amount: payment.amount,
34
- payer_tax_id: payment.source.payer_tax_id,
35
- payer_name: payment.source.payer_name,
36
- expiration: 600
37
- )
38
- payment.response_code = inter_payment.txid
39
- payment.source.assign_attributes(
40
- txid: inter_payment.txid,
41
- pix_code: inter_payment.copia_e_cola,
42
- qr_code_svg: inter_payment.qr_code,
43
- status: inter_payment.status,
44
- expiration: inter_payment.expiracao,
45
- internal_error: inter_payment.internal_error
46
- )
47
- payment.log_entries.new(parsed_payment_response_details_with_fallback: failure_response(inter_payment.internal_error)) if inter_payment.internal_error
48
- inter_payment
49
- end
50
-
51
30
  def find_payment(txid)
52
31
  client = set_api_client
53
32
  client.get_payment(txid)
54
33
  end
55
34
 
35
+ def create_payment(order)
36
+ existing_payment = find_existing_payment(order)
37
+ return existing_payment if payment_is_usable?(existing_payment, order)
38
+
39
+ payment = order.payments.new(amount: order.total, payment_method: self)
40
+ payment.source = init_source(order)
41
+ payment.save
42
+
43
+ inter_payment = create_inter_payment(payment.source)
44
+ process_payment_response(payment, inter_payment)
45
+ payment
46
+ end
47
+
56
48
  def invalidate_payment(payment_source)
57
49
  return false unless payment_source&.txid
50
+
58
51
  inter_payment = find_payment(payment_source.txid)
59
52
  return false if inter_payment.paid?
53
+
60
54
  inter_payment.invalidate!
61
55
  payment_source.payments[0].log_entries.create!(parsed_payment_response_details_with_fallback: failure_response("Pagamento cancelado"))
62
56
  true
63
57
  end
64
58
 
65
- def pay_test_payment payment
66
- return false unless payment && payment.source.txid
67
- inter_payment = find_payment(payment.source.txid)
59
+ def pay_test_payment(payment_source)
60
+ return false unless payment_source&.txid
61
+
62
+ inter_payment = find_payment(payment_source.txid)
68
63
  return false if inter_payment.paid?
64
+
69
65
  client = set_api_client
70
66
  client.pay_pix(inter_payment.txid, inter_payment.valor_original)
71
67
  end
@@ -81,6 +77,70 @@ module SolidusInter
81
77
 
82
78
  private
83
79
 
80
+ def init_source(order)
81
+ PixPaymentSource.new(
82
+ amount: order.total,
83
+ payer_name: order.ship_address.name,
84
+ payer_tax_id: order.tax_id,
85
+ payment_method: self
86
+ )
87
+ end
88
+
89
+ def create_inter_payment(payment_source)
90
+ client = set_api_client
91
+ client.create_payment(
92
+ amount: payment_source.amount,
93
+ payer_tax_id: payment_source.payer_tax_id,
94
+ payer_name: payment_source.payer_name,
95
+ expiration: 600
96
+ )
97
+ end
98
+
99
+ def find_existing_payment(order)
100
+ pix_payments = order.payments.checkout
101
+ raise "More than one valid payment for #{order.number}" if pix_payments.count > 1
102
+
103
+ pix_payments.first
104
+ end
105
+
106
+ def payment_is_usable?(payment, order)
107
+ return false unless payment
108
+
109
+ payment.source.active? && payment.amount == order.total
110
+ end
111
+
112
+ def process_payment_response(payment, inter_payment)
113
+ payment.update(response_code: inter_payment.txid)
114
+ payment.source.update(
115
+ txid: inter_payment.txid,
116
+ pix_code: inter_payment.copia_e_cola,
117
+ qr_code_svg: inter_payment.qr_code,
118
+ status: inter_payment.status,
119
+ expiration: inter_payment.expiracao
120
+ )
121
+
122
+ if inter_payment.internal_error
123
+ handle_payment_error(payment, inter_payment)
124
+ else
125
+ update_payment_status(payment, inter_payment)
126
+ end
127
+ end
128
+
129
+ def update_payment_status(payment, inter_payment)
130
+ status = case inter_payment.status
131
+ when "ATIVA" then "pending"
132
+ end
133
+ payment.source.update(status: status)
134
+ end
135
+
136
+ def handle_payment_error(payment, inter_payment)
137
+ payment.invalidate
138
+ error_message = inter_payment.internal_error || "Erro ao criar o pagamento"
139
+ response = failure_response(error_message)
140
+ payment.log_entries.create(parsed_payment_response_details_with_fallback: response)
141
+ payment.source.update(internal_error: error_message, status: "error")
142
+ end
143
+
84
144
  def set_api_client
85
145
  account = SolidusInter::Account.find_by(chave_pix: preferences[:chave_pix])
86
146
  client = ::InterApi::Client.new(
@@ -94,18 +154,21 @@ module SolidusInter
94
154
  token_expires_at: account&.expires_at,
95
155
  test_mode: preferences[:test_mode]
96
156
  )
97
- SolidusInter::Account.upsert({token: client.token, expires_at: client.token_expires_at, chave_pix: client.chave_pix, spree_payment_method_id: id}, unique_by: :chave_pix)
157
+ SolidusInter::Account.upsert(
158
+ { token: client.token, expires_at: client.token_expires_at, chave_pix: client.chave_pix,
159
+ spree_payment_method_id: id }, unique_by: :chave_pix
160
+ )
98
161
  client
99
162
  end
100
163
 
101
- def temp_file content
164
+ def temp_file(content)
102
165
  t = Tempfile.new
103
166
  t << content
104
167
  t.close
105
168
  t
106
169
  end
107
170
 
108
- def successful_response message, transaction_id
171
+ def successful_response(message, transaction_id)
109
172
  ActiveMerchant::Billing::Response.new(
110
173
  true,
111
174
  message,
@@ -114,7 +177,7 @@ module SolidusInter
114
177
  )
115
178
  end
116
179
 
117
- def failure_response message
180
+ def failure_response(message)
118
181
  ActiveMerchant::Billing::Response.new(
119
182
  false,
120
183
  message
@@ -16,14 +16,14 @@ module SolidusInter
16
16
  payment.completed? && payment.credit_allowed > 0
17
17
  end
18
18
 
19
- def usable?
20
- txid.present? && expiration.future?
21
- end
22
-
23
19
  def expired?
24
20
  expiration.past?
25
21
  end
26
22
 
23
+ def active?
24
+ expiration.future?
25
+ end
26
+
27
27
  def retrieve_from_api
28
28
  payment_method.find_payment(txid)
29
29
  end
@@ -1,5 +1,5 @@
1
1
  <div class="content-wrapper">
2
- <fieldset class="no-border-top">
2
+ <fieldset class="no-border-top">
3
3
  <legend>
4
4
  Pagamento Inter Pix
5
5
  </legend>
@@ -40,7 +40,7 @@
40
40
 
41
41
  <div class="field">
42
42
  <%= label_tag :expiration, "Expiração" %>
43
- <%= text_field_tag :expiration, payment.source.expiration.in_time_zone('America/Sao_Paulo').strftime("%d/%m/%Y às %H:%M"), class:"fullwidth", disabled: true %>
43
+ <%= text_field_tag :expiration, payment.source.expiration&.in_time_zone('America/Sao_Paulo')&.strftime("%d/%m/%Y às %H:%M"), class:"fullwidth", disabled: true %>
44
44
  </div>
45
45
 
46
46
  <div class="field">
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusInter
4
- VERSION = "1.5.0"
4
+ VERSION = "1.6.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_inter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ulysses
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-12 00:00:00.000000000 Z
11
+ date: 2024-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core