solidus_inter 1.4.4 → 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: 9dc7fbc6f9ea037df1750a2c1202a04e662ebf499e8b4803285c0450cf1019a7
4
- data.tar.gz: 89535c6e6b7730aee3c62410f8ca1e9ae5d7bb2963ab8ba7833fc47c534c4795
3
+ metadata.gz: ae346a932b747ab2ca304f73324d30cfa84fabbdd5fbfd80d7a6c0aa7532d174
4
+ data.tar.gz: 32ce13c0e6b3c630031fe7625d5b12fdc399811cdf54f35efea0225a74d56ea3
5
5
  SHA512:
6
- metadata.gz: 4e85e15f6c3b026b601a972cb87f578ef71c56f8032def1d4d1255124b5f516e7dcc34035769ee663a2cbcb54be51713d0b439988b508b176a42ebadddefa39d
7
- data.tar.gz: 890539c6c22b52f763273859d7006c6a0d393f1c245fc76ace19acd5fcb5bf771653187617c46720fcfd410bc4e21cfb05e956ab0fefeeaf805f6631166bde37
6
+ metadata.gz: a68aec24ba88bece094de83fc396004f46fb18e7fa4fef15b3210aac7f314590bb7562806d604abc02caf1bffaa7731dc85fa2435515b77e5d59892637c7d9a7
7
+ data.tar.gz: 4b96483f66dd64894b71a794f542f96b099288b069cbaf311ebb1abf446cebcd22d130630c2cae4535510cae6679ec0cf21a1efd80b5af1eff665440420704be
@@ -1,14 +1,25 @@
1
1
  module SolidusInter
2
2
  class Gateway
3
- def initialize options
3
+ def initialize(options) end
4
+
5
+ def purchase(_money, source, _options = {})
6
+ inter_payment = source.retrieve_from_api
7
+ if inter_payment.paid?
8
+ source.update(status: "approved", paid_amount: inter_payment.valor_pago, e2e_id: inter_payment.end_to_end_id)
9
+ successful_response("Pagamento realizado", inter_payment.txid)
10
+ else
11
+ failure_response(inter_payment.internal_error || "Ocorreu um erro no pagamento.")
12
+ end
4
13
  end
5
14
 
6
- def void(transaction_id, options = {})
15
+ def void(transaction_id, _options = {})
7
16
  # Respondendo sempre com successful_response para funcionar o botão de "Cancelar" do pedido. Reembolso deve ser feito por fora.
8
- 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)
9
18
  end
10
19
 
11
- def successful_response message, transaction_id
20
+ private
21
+
22
+ def successful_response(message, transaction_id)
12
23
  ActiveMerchant::Billing::Response.new(
13
24
  true,
14
25
  message,
@@ -16,5 +27,12 @@ module SolidusInter
16
27
  authorization: transaction_id
17
28
  )
18
29
  end
30
+
31
+ def failure_response(message)
32
+ ActiveMerchant::Billing::Response.new(
33
+ false,
34
+ message
35
+ )
36
+ end
19
37
  end
20
38
  end
@@ -27,57 +27,47 @@ 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
 
56
- def invalidate_payment(payment)
57
- return false unless payment && payment.source.txid
58
- inter_payment = find_payment(payment.source.txid)
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
+
48
+ def invalidate_payment(payment_source)
49
+ return false unless payment_source&.txid
50
+
51
+ inter_payment = find_payment(payment_source.txid)
59
52
  return false if inter_payment.paid?
53
+
60
54
  inter_payment.invalidate!
61
- payment.log_entries.create!(parsed_payment_response_details_with_fallback: failure_response("Pagamento cancelado"))
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
72
68
 
73
69
  def purchase(money, source, options = {})
74
- inter_payment = find_payment(source.txid)
75
- if inter_payment.paid?
76
- source.update(status: "approved", e2e_id: inter_payment.end_to_end_id)
77
- successful_response("Pagamento realizado", inter_payment.txid)
78
- else
79
- failure_response(inter_payment.internal_error || "Ocorreu um erro no pagamento.")
80
- end
70
+ gateway.purchase(money, source, options)
81
71
  end
82
72
 
83
73
  def should_skip_processing?(source)
@@ -87,6 +77,70 @@ module SolidusInter
87
77
 
88
78
  private
89
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
+
90
144
  def set_api_client
91
145
  account = SolidusInter::Account.find_by(chave_pix: preferences[:chave_pix])
92
146
  client = ::InterApi::Client.new(
@@ -100,18 +154,21 @@ module SolidusInter
100
154
  token_expires_at: account&.expires_at,
101
155
  test_mode: preferences[:test_mode]
102
156
  )
103
- 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
+ )
104
161
  client
105
162
  end
106
163
 
107
- def temp_file content
164
+ def temp_file(content)
108
165
  t = Tempfile.new
109
166
  t << content
110
167
  t.close
111
168
  t
112
169
  end
113
170
 
114
- def successful_response message, transaction_id
171
+ def successful_response(message, transaction_id)
115
172
  ActiveMerchant::Billing::Response.new(
116
173
  true,
117
174
  message,
@@ -120,7 +177,7 @@ module SolidusInter
120
177
  )
121
178
  end
122
179
 
123
- def failure_response message
180
+ def failure_response(message)
124
181
  ActiveMerchant::Billing::Response.new(
125
182
  false,
126
183
  message
@@ -16,16 +16,25 @@ 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
30
+
31
+ def paid?
32
+ inter_payment = retrieve_from_api
33
+ inter_payment.paid?
34
+ end
35
+
36
+ def invalidate
37
+ payment_method.invalidate_payment(self)
38
+ end
30
39
  end
31
40
  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.4.4"
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.4.4
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-02 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