solidus_mp_dois 2.1.0 → 2.2.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: 8afbc9842d9b54323fe860bc58ca1d143f64a686d88d738e8bd3c23fd6248cf7
4
- data.tar.gz: d01bbc2d93bb493404e551b3e944ed148ac33569920c0381967486a29cd5f1d2
3
+ metadata.gz: b4ead0fc00f11a0e32c267a9056e87692ff2db687a4ecfc5c200f714a7cb7d2a
4
+ data.tar.gz: c8cac11c17a01ee837c6e0f6de0bb4f497e0ae85550c7676a7477c5c88fe5b7c
5
5
  SHA512:
6
- metadata.gz: 76eaf1e1f21c75ac37296ce711b3ad2cb588e8155ee9306cd519caeaf3a9f5f915cd7d1a6725011607b4c67873b5be353aa7585a8faec565ec589b732beb1b39
7
- data.tar.gz: ce7d3fe8a2a73a07c56228e79ea5f29a362947ed08fc5e09477d460f3602eacdb560f23a581b231b1d154697da72aea05d75635bb5d77e470ed68fea4922958e
6
+ metadata.gz: a110a136c54b7fab4626fe1d0ef2f071a6d88068cbf6d756c46326ef7e903db57a1db701a37fe888ca75a13a5660123dabc835c2f46fe1a5788eb77972347f44
7
+ data.tar.gz: 7f5df2e59d581d2b28113460535d3027b26586da7ea6a02cca32a7eb3a79c607806a3f303e9a8198429885dd69402d7380c1f3ee22069b54563301d9257940cd
@@ -94,24 +94,32 @@ module SolidusMpDois
94
94
  order_user = order.try(:store_user) || order&.user # Para suportar lojas que usam Spree::User e User
95
95
  return unless order_user
96
96
 
97
- customer = SolidusMpDois::Customer.find_by(spree_user_id: order_user.id)
98
- if customer.blank?
99
- mp_customer = create_mp_customer(order, source_params, order_user)
100
- if mp_customer.error_detail&.dig(0, "code") == "101" # Não achou no banco de dados mas está criado na API
101
- mp_customer = find_mp_customer(order_user)
102
- end
103
- if mp_customer.error.blank?
104
- customer = SolidusMpDois::Customer.create(
97
+ mp_customer = find_mp_customer(order_user)
98
+ if mp_customer
99
+ # Pode ser que exista na API mas nao no banco de dados
100
+ SolidusMpDois::Customer.upsert({
105
101
  external_id: mp_customer.external_id,
106
102
  email: mp_customer.email,
107
103
  first_name: mp_customer.first_name,
108
104
  identification_type: mp_customer.identification_type,
109
105
  tax_id: mp_customer.identification_number,
110
106
  spree_user_id: order_user.id
107
+ }, unique_by: [:external_id]
108
+ )
109
+ else
110
+ created_mp_customer = create_mp_customer(order, source_params, order_user)
111
+ if created_mp_customer.error.blank?
112
+ SolidusMpDois::Customer.create(
113
+ external_id: created_mp_customer.external_id,
114
+ email: created_mp_customer.email,
115
+ first_name: created_mp_customer.first_name,
116
+ identification_type: created_mp_customer.identification_type,
117
+ tax_id: created_mp_customer.identification_number,
118
+ spree_user_id: order_user.id
111
119
  )
112
120
  end
113
121
  end
114
- customer
122
+ SolidusMpDois::Customer.find_by(spree_user_id: order_user.id)
115
123
  end
116
124
 
117
125
  def create_mp_customer(order, source_params, user)
@@ -133,7 +141,7 @@ module SolidusMpDois
133
141
  search_req = Typhoeus.get("https://api.mercadopago.com/v1/customers/search?email=#{email}", headers: { "Authorization" => "Bearer #{preferences[:access_token]}" })
134
142
  search_response = JSON.parse(search_req.body)
135
143
  customer_json = search_response["results"].find { |r| r.dig("email") == email }
136
- MpApi::Customer.new(**MpApi::Customer.build_hash(customer_json))
144
+ MpApi::Customer.new(**MpApi::Customer.build_hash(customer_json)) if customer_json
137
145
  end
138
146
 
139
147
  def save_credit_card(card_token, customer)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_mp_dois
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todas Essas Coisas