solidus_mp 1.0.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 317ace149d7d821b01bf295b4883a8b51966a77b9bd58f39607abeff96c6602a
4
- data.tar.gz: d9f670db89f5bd072dab697a37cb3b15446966b93dd71762bffd5c4c35fb76ce
3
+ metadata.gz: 33ef4a77689e3e5f47c6f63c5cb1bd23d54faa4381509cb0896bf99613096c0f
4
+ data.tar.gz: 2840a3539a70733aa59bc9290a19a6e86db43a14e0b9974b0adfd116c32e9e63
5
5
  SHA512:
6
- metadata.gz: 8f9d7f20d1db7a1e432aa91cfe54328d9f8a8f0aa01ba907fa9cd92b0dcd0bbe493a9944d1cdefa9b910b660f29a680c8ef758032deaa1048da0acd580ea9d1f
7
- data.tar.gz: 808b7b32bd0f4776180af0f6bcd1124a14879b39947d73dfd493ceb796066a6351b59874ceac72abd0a39d7a62b26082053ff5e90bb2a0670c9700157cc7ad8d
6
+ metadata.gz: 9d7edfdf6d41100e7d72daab3cd7c41d98a48ce98ea855ce0cd37ada07efb4daead926b513af5600380203703b84f99f58b83ed2fb6d68b141c27b879791b9f5
7
+ data.tar.gz: b49afdafc986df3105fd8f36f501d1bb2323e5c44d43eb69821cad7dbf3e10c6947a97c6417e28572a1594b978f11c2e7c67da20d7d0f7ca0def5b7866aa7a3d
@@ -0,0 +1,5 @@
1
+ module SolidusMp
2
+ class Card < ApplicationRecord
3
+ belongs_to :customer
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module SolidusMp
2
+ class Customer < ApplicationRecord
3
+ has_many :cards
4
+ belongs_to :spree_user, class_name: "Spree::User"
5
+ end
6
+ end
@@ -10,7 +10,7 @@ module SolidusMp
10
10
  end
11
11
 
12
12
  def gateway_class
13
- "MpCard"
13
+ MpGateway
14
14
  end
15
15
 
16
16
  # Pra nao dar problema de pagamento pendente
@@ -22,8 +22,12 @@ module SolidusMp
22
22
  source.is_a?(payment_source_class)
23
23
  end
24
24
 
25
- def create_card_payment payment
26
- mp_payment = create_mp_payment(payment)
25
+ def create_card_payment payment, user
26
+ if user
27
+ customer = find_or_create_customer(payment, user)
28
+ save_credit_card(payment, customer)
29
+ end
30
+ mp_payment = create_mp_payment(payment, customer.try(:external_id))
27
31
  payment.source.update(external_id: mp_payment.id, three_ds_url: mp_payment.three_ds_info_external_resource_url, three_ds_creq: mp_payment.three_ds_info_creq, last_four_digits: mp_payment.last_four_digits)
28
32
  if mp_payment.error || mp_payment.internal_error
29
33
  payment.invalidate
@@ -39,9 +43,66 @@ module SolidusMp
39
43
  payment.source
40
44
  end
41
45
 
42
- def create_mp_payment payment
46
+ def find_or_create_customer(payment, user)
47
+ customer = SolidusMp::Customer.find_by(spree_user_id: user.id)
48
+ if customer.blank?
49
+ mp_customer = create_mp_customer(payment)
50
+ if mp_customer.error.blank?
51
+ customer = SolidusMp::Customer.create(
52
+ external_id: mp_customer.external_id,
53
+ email: mp_customer.email,
54
+ first_name: mp_customer.first_name,
55
+ identification_type: mp_customer.identification_type,
56
+ tax_id: mp_customer.identification_number,
57
+ spree_user_id: user.id
58
+ )
59
+ end
60
+ end
61
+ customer
62
+ end
63
+
64
+ def save_credit_card(payment, customer)
65
+ return if customer.nil?
66
+ MpApi.configuration.access_token = preferences[:access_token]
67
+ mp_credit_card = MpApi::Card.new(token: payment.source.token, customer_id: customer.external_id).create
68
+ SolidusMp::Card.upsert(
69
+ {
70
+ external_id: mp_credit_card.external_id,
71
+ last_four_digits: mp_credit_card.last_four_digits,
72
+ mp_payment_method_id: mp_credit_card.mp_payment_method_id,
73
+ customer_id: customer.id
74
+ }, unique_by: :external_id
75
+ )
76
+ end
77
+
78
+ def create_mp_customer(payment)
79
+ MpApi.configuration.access_token = preferences[:access_token]
80
+ MpApi::Customer.new(
81
+ email: payment.source.email,
82
+ first_name: payment.order.ship_address.name,
83
+ identification_type: payment.source.payer_identification_type,
84
+ identification_number: payment.source.tax_id
85
+ ).create
86
+ end
87
+
88
+ def create_mp_payment payment, customer_id
43
89
  MpApi.configuration.access_token = preferences[:access_token]
44
- MpApi::Payment.new(amount: payment.amount.to_f, payment_method: payment.source.credit_card_brand, payer_email: payment.source.email, payer_identification_type: payment.source.payer_identification_type, payer_identification_number: payment.source.tax_id, token: payment.source.token, issuer_id: payment.source.issuer_id, installments: payment.source.installments, three_d_secure_mode: true, statement_descriptor: payment.source.statement_descriptor, description: payment.source.description, items: payment.source.items).create
90
+ MpApi::Payment.new(
91
+ amount: payment.amount.to_f,
92
+ payment_method: payment.source.credit_card_brand,
93
+ payer_email: payment.source.email,
94
+ payer_identification_type: payment.source.payer_identification_type,
95
+ payer_identification_number: payment.source.tax_id,
96
+ token: payment.source.token,
97
+ issuer_id: payment.source.issuer_id,
98
+ installments: payment.source.installments,
99
+ three_d_secure_mode: true,
100
+ statement_descriptor: payment.source.statement_descriptor,
101
+ description: payment.source.description,
102
+ items: payment.source.items,
103
+ saved_card: payment.source.saved_card,
104
+ customer_id: customer_id
105
+ ).create
45
106
  end
46
107
 
47
108
  def cancel_payment payment
@@ -0,0 +1,22 @@
1
+ module SolidusMp
2
+ class MpGateway
3
+
4
+ def initialize(options)
5
+ MpApi.configuration.access_token = options[:access_token]
6
+ end
7
+
8
+ def void(transaction_id, options = {})
9
+ # Respondendo sempre com successful_response para funcionar o botão de "Cancelar" do pedido. Reembolso deve ser feito por fora.
10
+ successful_response("Pedido cancelado. Se necessário, realize o reembolso.", transaction_id)
11
+ end
12
+
13
+ def successful_response message, transaction_id
14
+ ActiveMerchant::Billing::Response.new(
15
+ true,
16
+ message,
17
+ {},
18
+ authorization: transaction_id
19
+ )
20
+ end
21
+ end
22
+ end
@@ -10,7 +10,7 @@ module SolidusMp
10
10
  end
11
11
 
12
12
  def gateway_class
13
- "MpPix"
13
+ MpGateway
14
14
  end
15
15
 
16
16
  def supports?(source)
@@ -0,0 +1,17 @@
1
+ class CreateSolidusMpCustomers < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :solidus_mp_customers do |t|
4
+ t.string :external_id
5
+ t.string :email
6
+ t.string :first_name
7
+ t.string :identification_type
8
+ t.string :tax_id
9
+ t.integer :spree_user_id, null: false
10
+
11
+ t.timestamps
12
+ t.foreign_key :spree_users, column: :spree_user_id
13
+ end
14
+
15
+ add_index :solidus_mp_customers, :external_id, unique: true
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ class CreateSolidusMpCards < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :solidus_mp_cards do |t|
4
+ t.string :external_id
5
+ t.string :last_four_digits
6
+ t.string :mp_payment_method_id
7
+ t.integer :customer_id, null: false
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ add_index :solidus_mp_cards, :external_id, unique: true
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ class AddSavedCardColumnToCardSource < ActiveRecord::Migration[7.1]
2
+ def change
3
+ add_column :solidus_mp_card_payment_sources, :saved_card, :boolean, default: false
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusMp
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.1'
5
5
  end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusMp
4
+ RSpec.describe Card, type: :model do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusMp
4
+ RSpec.describe Customer, type: :model do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_mp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - caio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-10 00:00:00.000000000 Z
11
+ date: 2024-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core
@@ -90,8 +90,11 @@ files:
90
90
  - LICENSE
91
91
  - README.md
92
92
  - Rakefile
93
+ - app/models/solidus_mp/card.rb
93
94
  - app/models/solidus_mp/card_payment_source.rb
95
+ - app/models/solidus_mp/customer.rb
94
96
  - app/models/solidus_mp/mp_card.rb
97
+ - app/models/solidus_mp/mp_gateway.rb
95
98
  - app/models/solidus_mp/mp_pix.rb
96
99
  - app/models/solidus_mp/pix_payment_source.rb
97
100
  - app/views/spree/admin/payments/source_forms/_mercado_pago_card.html.erb
@@ -116,6 +119,9 @@ files:
116
119
  - db/migrate/20240105143500_add_status_to_payment_source.rb
117
120
  - db/migrate/20240109171955_add_error_message_to_payment_source.rb
118
121
  - db/migrate/20240510185743_add_last_four_digits_to_credit_card_source.rb
122
+ - db/migrate/20240715124647_create_solidus_mp_customers.rb
123
+ - db/migrate/20240715125709_create_solidus_mp_cards.rb
124
+ - db/migrate/20240715131005_add_saved_card_column_to_card_source.rb
119
125
  - lib/generators/solidus_mp/install/install_generator.rb
120
126
  - lib/generators/solidus_mp/install/templates/app/javascript/controllers/credit_card_controller.js
121
127
  - lib/generators/solidus_mp/install/templates/app/javascript/controllers/three_ds_controller.js
@@ -131,6 +137,8 @@ files:
131
137
  - lib/solidus_mp/version.rb
132
138
  - solidus_mp.gemspec
133
139
  - spec/models/solidus_mp/card_payment_source_spec.rb
140
+ - spec/models/solidus_mp/card_spec.rb
141
+ - spec/models/solidus_mp/customer_spec.rb
134
142
  - spec/models/solidus_mp/pix_payment_source_spec.rb
135
143
  - spec/spec_helper.rb
136
144
  homepage: https://github.com/CaioGarcia1/solidus_mp
@@ -158,11 +166,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
166
  - !ruby/object:Gem::Version
159
167
  version: '0'
160
168
  requirements: []
161
- rubygems_version: 3.5.10
169
+ rubygems_version: 3.5.11
162
170
  signing_key:
163
171
  specification_version: 4
164
172
  summary: ''
165
173
  test_files:
166
174
  - spec/models/solidus_mp/card_payment_source_spec.rb
175
+ - spec/models/solidus_mp/card_spec.rb
176
+ - spec/models/solidus_mp/customer_spec.rb
167
177
  - spec/models/solidus_mp/pix_payment_source_spec.rb
168
178
  - spec/spec_helper.rb