solidus_bling 3.5.2 → 4.0.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: 839a34b72b6e7f33d03e5eeb6de8ff0e18b923902b72b465fa8e460369abe9dd
4
- data.tar.gz: e9c8d2e89b243a07d8b6e3349d3bbe364b6f3cfd2dc6a7c02bc66f24984c9c38
3
+ metadata.gz: 7efd0c005698628f0d0471a083c711277abd72af98f17aaf9e83ffcea7691890
4
+ data.tar.gz: 25aed12e345d68a9815f58a3eed202166c919892e3e6a6e32900bf929e628461
5
5
  SHA512:
6
- metadata.gz: d1576e931cf2b22a7c843d311086052d3224288cff30c99945f833ac93623ecf7e3b9a67bd64a783f8327023baec72f3fc2961720be24d7d47f5ae2707ff398f
7
- data.tar.gz: fe7d2e29ec0e3997457908d09ae821eca889689ad491a3417d1567322d63c35065b57b7f17239ecf56bc22fefe5d14592321130cf7d987b5d108bdb59c35bfb3
6
+ metadata.gz: da4d6eec527a7fe1e41910611d2ce3c2ae4334e7cbcbc6aa9777bf9d33d9bebae2de5de8048c8a5b904caf13a6fe0894b223cc62d3b592469bc0f3a2a7dcb51c
7
+ data.tar.gz: 0f30f0daf84d487e3a5fc9001a6b94a230f83ca7352d2e0f73c9ab6c97b0fb28760903571ead67049543c0d68594143770325b766d9ae2952074aa9d1022b131
@@ -55,7 +55,7 @@ module Spree
55
55
  def authorize
56
56
  bling_account = ::SolidusBling::Account.first
57
57
  code = params[:code]
58
- unless ::SolidusBling::Token.new.authorize(bling_account, code)
58
+ unless bling_account.authorize(code: code)
59
59
  bling_account.errors.add(:base, "Erro ao autorizar aplicativo 🦆")
60
60
  flash[:error] = bling_account.errors.full_messages.join("\n")
61
61
  end
@@ -2,19 +2,16 @@ module Spree
2
2
  module Admin
3
3
  module SolidusBling
4
4
  class OrdersController < BaseController
5
-
6
5
  def update
7
6
  order = Spree::Order.find(params[:id])
8
7
  if order.bling_order_id
9
8
  flash[:error] = "Pedido já enviado"
10
- redirect_to edit_admin_order_customer_path(order)
11
9
  else
12
10
  ::SolidusBling::SendOrderJob.perform_later(order)
13
11
  flash[:success] = "O pedido está sendo enviado"
14
- redirect_to edit_admin_order_customer_path(order)
15
12
  end
13
+ redirect_to edit_admin_order_customer_path(order)
16
14
  end
17
-
18
15
  end
19
16
  end
20
17
  end
@@ -3,16 +3,7 @@ module SolidusBling
3
3
  queue_as :default
4
4
 
5
5
  def perform(order)
6
- bling_event = SolidusBling::Event.new(internal_id: order.id, body: order.to_json)
7
- begin
8
- SolidusBling::Order.new(order).send
9
- bling_event.status = "success"
10
- rescue => e
11
- bling_event.status = "error"
12
- bling_event.message = e.to_json
13
- ensure
14
- bling_event.save!
15
- end
6
+ SolidusBling::Order.new(order).send
16
7
  end
17
8
  end
18
9
  end
@@ -5,10 +5,14 @@ module SolidusBling
5
5
  has_many :sellers, dependent: :destroy
6
6
  accepts_nested_attributes_for :payment_methods, :shipping_methods, :sellers
7
7
 
8
- def check_token
9
- return if client_id.blank? || client_secret.blank? || refresh_token.blank?
10
- return if (token_expires_in - 1.hour).utc > DateTime.now.utc
11
- Token.new.refresh self
8
+ def authorize(code:)
9
+ client = BlingApi::Client.new(client_id:, client_secret:)
10
+ client.authorize(code:)
11
+ update(
12
+ refresh_token: client.get_refresh_token,
13
+ access_token: client.access_token,
14
+ token_expires_in: client.token_expires_at
15
+ )
12
16
  end
13
17
  end
14
18
  end
@@ -0,0 +1,34 @@
1
+ module SolidusBling
2
+ class BlingClient < ::BlingApi::Client
3
+ def self.for_account(account)
4
+ self.new(account)
5
+ end
6
+
7
+ attr_accessor :account
8
+ def initialize(account)
9
+ @account = account
10
+ super(
11
+ client_id: account.client_id,
12
+ client_secret: account.client_secret,
13
+ refresh_token: account.refresh_token,
14
+ access_token: account.access_token,
15
+ token_expires_at: account.token_expires_in
16
+ )
17
+ end
18
+
19
+ def refresh_token
20
+ super
21
+ sync_account_tokens
22
+ end
23
+
24
+ private
25
+
26
+ def sync_account_tokens
27
+ account.update!(
28
+ access_token: access_token,
29
+ refresh_token: get_refresh_token,
30
+ token_expires_in: token_expires_at
31
+ )
32
+ end
33
+ end
34
+ end
@@ -6,94 +6,83 @@ module SolidusBling
6
6
 
7
7
  def send
8
8
  @account = SolidusBling::Account.first
9
- @account.check_token
10
- BlingApi.configure { |config| config.access_token = @account.access_token }
9
+ @client = SolidusBling::BlingClient.for_account(@account)
11
10
 
12
- contato = customer
13
- items = products
14
- parcelas = installments
11
+ contato = upsert_customer
12
+ itens = build_bling_items
13
+ parcelas = build_bling_installments
15
14
  observacoes_internas = internal_observations
16
- transporte = shipment
15
+ transporte = build_bling_shipment
17
16
  id_vendedor = seller_external_id
18
17
  id_categoria_receita = @account.payment_methods.find_by(spree_payment_method_id: @order.payments.last.payment_method_id).incoming_category_id
19
18
 
20
- bling_order = BlingApi::Order.new(
19
+ response = @client.create_order(
20
+ data: @order.completed_at.strftime("%Y-%m-%d"),
21
21
  id_contato: contato.id,
22
+ itens: itens,
23
+ numero_pedido: @order.number,
24
+ id_loja: @account.external_store_id,
25
+ parcelas: parcelas,
26
+ transporte: transporte,
27
+ id_vendedor: id_vendedor,
22
28
  pessoa_juridica: @order.tax_id.length > 14,
23
29
  numero_documento: @order.tax_id,
24
- items: items,
25
- parcelas: parcelas,
26
- numero_loja: @order.number,
27
- data: @order.completed_at.strftime("%Y-%m-%d"),
28
- total_produtos: @order.item_total.to_f,
29
- total: @order.total.to_f,
30
- id_loja: @account.external_store_id,
30
+ id_categoria_receita: id_categoria_receita,
31
31
  observacoes: "Número do pedido loja: #{@order.number}",
32
32
  observacoes_internas: observacoes_internas,
33
- valor_desconto: @order.adjustment_total.abs,
34
- id_categoria: id_categoria_receita,
35
- transporte: transporte,
36
- id_vendedor: id_vendedor
37
- ).create
33
+ valor_desconto: @order.adjustment_total.abs
34
+ )
38
35
 
39
- if bling_order
40
- @order.update(bling_order_id: bling_order.id)
41
- end
36
+ @order.update(bling_order_id: response.data.id)
37
+ puts "Pedido #{@order.number} enviado"
42
38
  end
43
39
 
44
- def customer
45
- contato = BlingApi::Customer.find_by_tax_id(@order.tax_id)
46
- if contato
47
- contato.update(
40
+ def upsert_customer
41
+ search_contato = @client.find_contact_by_document_number(@order.tax_id).data
42
+ contact_id = if search_contato.any?
43
+ @client.update_contact(
44
+ id: search_contato[0].id,
48
45
  pessoa_juridica: @order.tax_id.length > 14,
49
46
  numero_documento: @order.tax_id,
50
47
  situacao: "A",
51
- codigo: contato.codigo,
52
- vendedor: contato.vendedor,
53
- dados_adicionais: contato.dados_adicionais,
54
- financeiro: contato.financeiro,
55
- pais: contato.pais,
56
- tipos_contato: contato.tipos_contato,
57
- pessoa_contato: contato.pessoa_contato,
58
48
  **build_customer
59
49
  )
50
+ search_contato[0].id
60
51
  else
61
- contato = BlingApi::Customer.new(
52
+ response = @client.create_contact(
62
53
  pessoa_juridica: @order.tax_id.length > 14,
63
54
  numero_documento: @order.tax_id,
64
- cod_contribuinte: (@order.tax_id.length > 14) ? nil : "9",
65
55
  situacao: "A",
56
+ cod_contribuinte: (@order.tax_id.length > 14) ? nil : "9",
66
57
  **build_customer
67
- ).create
58
+ )
59
+ response.data.id
68
60
  end
69
- contato
61
+ @client.get_contact(contact_id).data
70
62
  end
71
63
 
72
- def products
64
+ def build_bling_items
73
65
  @order.line_items.map do |item|
74
- product = BlingApi::Product.find_by_sku(item.sku)
75
- {
76
- id: product.id,
66
+ product = @client.find_product_by_sku(item.sku).data
67
+ raise "Produto com SKU #{item.sku} não encontrado no Bling" if product.blank?
68
+ BlingApi::Item.new(
69
+ id_produto: product[0].id,
70
+ codigo: item.sku,
77
71
  quantidade: item.quantity,
78
- valor: item.price.to_f,
79
- produto: {
80
- id: product.id
81
- }
82
- }
72
+ valor: item.price.to_f
73
+ )
83
74
  end.compact
84
75
  end
85
76
 
86
- def installments
77
+ def build_bling_installments
87
78
  @parcelas = @order.payments.last.source.try(:installments) || 1
88
79
  valor_parcela = @order.total.to_f / @parcelas
89
80
  1.upto(@parcelas).map do |parcela|
90
- {
91
- dataVencimento: (@order.completed_at + parcela.months).strftime("%Y-%m-%d"),
81
+ BlingApi::Installment.new(
82
+ id_forma_pagamento: @account.payment_methods.find_by(spree_payment_method_id: @order.payments.last.payment_method_id).external_id,
92
83
  valor: valor_parcela,
93
- formaPagamento: {
94
- id: @account.payment_methods.find_by(spree_payment_method_id: @order.payments.last.payment_method_id).external_id
95
- }
96
- }
84
+ data_vencimento: (@order.completed_at + parcela.months).strftime("%Y-%m-%d")
85
+ )
97
86
  end
98
87
  end
99
88
 
@@ -107,40 +96,32 @@ module SolidusBling
107
96
  build_order_url.to_s
108
97
  end
109
98
 
110
- def shipment
99
+ def build_bling_shipment
111
100
  shipping_method = @account.shipping_methods.find_by(spree_shipping_method_id: @order.shipments.last.shipping_method.id)
112
101
  servico = shipping_method.nil? ? "" : shipping_method.alias
113
102
  transportadora = shipping_method.nil? ? "" : shipping_method.company
114
- {
115
- fretePorConta: 1,
116
- frete: @order.shipment_total.to_f,
117
- quantidadeVolumes: 1,
118
- contato: {
119
- nome: transportadora
120
- },
121
- etiqueta: {
122
- nome: @order.ship_address.name.strip,
123
- endereco: @order.ship_address.address1.strip,
124
- numero: @order.ship_address.number.strip,
125
- complemento: @order.ship_address.address2.strip,
126
- municipio: @order.ship_address.city.strip,
127
- uf: @order.ship_address.state.abbr.strip,
128
- bairro: @order.ship_address.district.strip,
129
- cep: @order.ship_address.zipcode.strip,
130
- nomePais: "Brasil"
131
- },
132
- volumes: [
133
- {
134
- servico: servico,
135
- codigoRastreamento: ""
136
- }
137
- ]
138
- }
103
+
104
+ BlingApi::Shipment.new(
105
+ valor_frete: @order.shipment_total.to_f,
106
+ nome_transportadora: transportadora,
107
+ frete_por_conta: 1,
108
+ quantidade_volumes: 1,
109
+ nome_etiqueta: @order.ship_address.name.strip,
110
+ endereco: @order.ship_address.address1.strip,
111
+ numero: @order.ship_address.number.strip,
112
+ complemento: @order.ship_address.address2.strip,
113
+ municipio: @order.ship_address.city.strip,
114
+ uf: @order.ship_address.state.abbr.strip,
115
+ bairro: @order.ship_address.district.strip,
116
+ cep: @order.ship_address.zipcode.strip,
117
+ pais: "Brasil",
118
+ nome_servico: servico
119
+ )
139
120
  end
140
121
 
141
122
  def seller_external_id
142
123
  seller = SolidusBling::Seller.find_by_id(@order.solidus_bling_seller_id)
143
- if seller&.active_from_api?
124
+ if seller && @client.get_seller(seller.external_id).data.contato.situacao == "A"
144
125
  seller.external_id
145
126
  else
146
127
  SolidusBling::Seller.default.external_id
@@ -149,27 +130,23 @@ module SolidusBling
149
130
 
150
131
  def build_customer
151
132
  {
152
- nome: @order.nf_name.strip,
133
+ nome: @order.nf_name&.strip || @order.bill_address.name.strip,
153
134
  telefone: @order.bill_address.phone.strip,
154
135
  email: @order.email.strip,
155
- endereco: {
156
- "endereco" => @order.bill_address.address1.strip,
157
- "cep" => @order.bill_address.zipcode.strip,
158
- "bairro" => @order.bill_address.district.strip,
159
- "municipio" => @order.bill_address.city.strip,
160
- "uf" => @order.bill_address.state.abbr.strip,
161
- "numero" => @order.bill_address.number.strip,
162
- "complemento" => @order.bill_address.address2.strip
163
- },
164
- endereco_cobranca: {
165
- "endereco" => @order.bill_address.address1.strip,
166
- "cep" => @order.bill_address.zipcode.strip,
167
- "bairro" => @order.bill_address.district.strip,
168
- "municipio" => @order.bill_address.city.strip,
169
- "uf" => @order.bill_address.state.abbr.strip,
170
- "numero" => @order.bill_address.number.strip,
171
- "complemento" => @order.bill_address.address2.strip
172
- }
136
+ endereco: @order.bill_address.address1.strip,
137
+ endereco_cep: @order.bill_address.zipcode.strip,
138
+ endereco_bairro: @order.bill_address.district.strip,
139
+ endereco_municipio: @order.bill_address.city.strip,
140
+ endereco_uf: @order.bill_address.state.abbr.strip,
141
+ endereco_numero: @order.bill_address.number.strip,
142
+ endereco_complemento: @order.bill_address.address2.strip,
143
+ endereco_cob: @order.bill_address.address1.strip,
144
+ endereco_cob_cep: @order.bill_address.zipcode.strip,
145
+ endereco_cob_bairro: @order.bill_address.district.strip,
146
+ endereco_cob_municipio: @order.bill_address.city.strip,
147
+ endereco_cob_uf: @order.bill_address.state.abbr.strip,
148
+ endereco_cob_numero: @order.bill_address.number.strip,
149
+ endereco_cob_complemento: @order.bill_address.address2.strip
173
150
  }
174
151
  end
175
152
 
@@ -6,11 +6,15 @@ module SolidusBling
6
6
  after_save :falsify_all_others
7
7
 
8
8
  def self.upsert_sellers account
9
- account.check_token
10
- BlingApi.configure { |config| config.access_token = account.access_token }
11
-
12
- sellers = BlingApi::Seller.get_sellers
13
- upsert_sellers = sellers.map { |seller| {name: seller[:nome], external_id: seller[:id], account_id: account.id, active: true} }
9
+ client = BlingApi::Client.new(
10
+ client_id: account.client_id,
11
+ client_secret: account.client_secret,
12
+ refresh_token: account.refresh_token,
13
+ access_token: account.access_token,
14
+ token_expires_at: account.token_expires_in
15
+ )
16
+ sellers = client.get_sellers(1, 100, "A").data
17
+ upsert_sellers = sellers.map { |seller| {name: seller.contato.nome, external_id: seller.id, account_id: account.id, active: true} }
14
18
  SolidusBling::Seller.upsert_all(upsert_sellers, unique_by: :external_id)
15
19
  inactivate_sellers(sellers)
16
20
  end
@@ -23,10 +27,6 @@ module SolidusBling
23
27
  where(active: true)
24
28
  end
25
29
 
26
- def active_from_api?
27
- BlingApi::Seller.find_by_id(external_id).try(:situacao) == "A"
28
- end
29
-
30
30
  def self.inactivate_sellers sellers_bling
31
31
  ids_sellers_bling = sellers_bling.pluck(:id)
32
32
  ids_sellers_store = pluck(:external_id).map(&:to_i)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusBling
4
- VERSION = "3.5.2"
4
+ VERSION = "4.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_bling
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.5.2
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todas Essas Coisas
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-01-29 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: solidus_brazilian_adaptations
@@ -38,7 +37,6 @@ dependencies:
38
37
  - - ">="
39
38
  - !ruby/object:Gem::Version
40
39
  version: '0'
41
- description:
42
40
  email: hamiltontubo@gmail.com
43
41
  executables: []
44
42
  extensions: []
@@ -53,12 +51,12 @@ files:
53
51
  - app/jobs/solidus_bling/send_order_job.rb
54
52
  - app/jobs/solidus_bling/upsert_sellers_job.rb
55
53
  - app/models/solidus_bling/account.rb
54
+ - app/models/solidus_bling/bling_client.rb
56
55
  - app/models/solidus_bling/event.rb
57
56
  - app/models/solidus_bling/order.rb
58
57
  - app/models/solidus_bling/payment_method.rb
59
58
  - app/models/solidus_bling/seller.rb
60
59
  - app/models/solidus_bling/shipping_method.rb
61
- - app/models/solidus_bling/token.rb
62
60
  - app/models/spree/permission_sets/seller_update.rb
63
61
  - app/subscribers/solidus_bling/subscriber.rb
64
62
  - app/views/spree/admin/orders/sellers/edit.html.erb
@@ -93,7 +91,6 @@ files:
93
91
  homepage: https://github.com/todasessascoisas/solidus_bling
94
92
  licenses: []
95
93
  metadata: {}
96
- post_install_message:
97
94
  rdoc_options: []
98
95
  require_paths:
99
96
  - lib
@@ -111,8 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
108
  - !ruby/object:Gem::Version
112
109
  version: '0'
113
110
  requirements: []
114
- rubygems_version: 3.5.10
115
- signing_key:
111
+ rubygems_version: 3.6.9
116
112
  specification_version: 4
117
113
  summary: Solidus extension to integrate with the Bling
118
114
  test_files: []
@@ -1,40 +0,0 @@
1
- module SolidusBling
2
- class Token < ::Ac::Base
3
- BASE_URL = "https://www.bling.com.br/Api/v3"
4
-
5
- def authorize bling_account, code
6
- basic_encoded = Base64.strict_encode64("#{bling_account.client_id}:#{bling_account.client_secret}")
7
- headers = {
8
- "Content-Type": "application/x-www-form-urlencoded",
9
- Authorization: "Basic #{basic_encoded}"
10
- }
11
- body = {
12
- code: code,
13
- grant_type: "authorization_code"
14
- }
15
- response_json = post("/oauth/token", headers:, body:).json
16
- bling_account.update(
17
- access_token: response_json["access_token"],
18
- token_expires_in: DateTime.now.utc + response_json["expires_in"].seconds,
19
- refresh_token: response_json["refresh_token"]
20
- )
21
- rescue
22
- false
23
- end
24
-
25
- def refresh bling_account
26
- response_json = BlingApi::Client.new.refresh_token(
27
- client_id: bling_account.client_id,
28
- client_secret: bling_account.client_secret,
29
- refresh_token: bling_account.refresh_token
30
- )
31
- bling_account.update(
32
- access_token: response_json["access_token"],
33
- token_expires_in: DateTime.now.utc + response_json["expires_in"].seconds,
34
- refresh_token: response_json["refresh_token"]
35
- )
36
- rescue
37
- false
38
- end
39
- end
40
- end