solidus_bling 2.0.7 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +2 -0
  3. data/.gitignore +0 -1
  4. data/CHANGELOG.md +1 -3
  5. data/Gemfile +18 -19
  6. data/LICENSE +1 -1
  7. data/README.md +1 -1
  8. data/Rakefile +2 -2
  9. data/app/controllers/spree/admin/solidus_bling/accounts_controller.rb +80 -0
  10. data/app/jobs/solidus_bling/order_job.rb +4 -7
  11. data/app/models/solidus_bling/account.rb +6 -1
  12. data/app/models/solidus_bling/order.rb +121 -96
  13. data/app/models/solidus_bling/token.rb +44 -0
  14. data/app/subscribers/solidus_bling/subscriber.rb +2 -22
  15. data/app/views/spree/admin/solidus_bling/accounts/_form.html.erb +147 -0
  16. data/app/views/spree/admin/solidus_bling/accounts/_link_auth.html.erb +10 -0
  17. data/app/views/spree/admin/solidus_bling/accounts/edit.html.erb +3 -0
  18. data/app/views/spree/admin/solidus_bling/accounts/index.html.erb +67 -0
  19. data/app/views/spree/admin/solidus_bling/accounts/new.html.erb +3 -0
  20. data/bin/meu_sandbox +67 -0
  21. data/config/locales/pt-BR.yml +1 -1
  22. data/config/routes.rb +6 -2
  23. data/db/migrate/{20230918141419_create_solidus_bling_accounts.rb → 20231109210635_create_solidus_bling_accounts.rb} +4 -5
  24. data/db/migrate/{20230918142547_create_solidus_bling_events.rb → 20231110112819_create_solidus_bling_events.rb} +1 -2
  25. data/db/migrate/{20230918143057_create_solidus_bling_shipping_methods.rb → 20231110113221_create_solidus_bling_shipping_methods.rb} +3 -2
  26. data/lib/generators/solidus_bling/install/install_generator.rb +6 -6
  27. data/lib/solidus_bling/configuration.rb +1 -1
  28. data/lib/solidus_bling/engine.rb +12 -15
  29. data/lib/solidus_bling/version.rb +1 -1
  30. data/lib/solidus_bling.rb +5 -5
  31. data/solidus_bling.gemspec +16 -18
  32. metadata +32 -71
  33. data/app/controllers/spree/admin/accounts_controller.rb +0 -67
  34. data/app/controllers/spree/admin/bling_products_controller.rb +0 -20
  35. data/app/decorators/models/solidus_bling/spree/payment_method_decorator.rb +0 -13
  36. data/app/decorators/models/solidus_bling/spree/shipping_method_decorator.rb +0 -13
  37. data/app/helpers/solidus_bling/accounts_helper.rb +0 -4
  38. data/app/helpers/solidus_bling/bling_products_helper.rb +0 -4
  39. data/app/jobs/solidus_bling/contact_job.rb +0 -22
  40. data/app/jobs/solidus_bling/product_job.rb +0 -13
  41. data/app/jobs/solidus_bling/update_product_job.rb +0 -9
  42. data/app/models/solidus_bling/api.rb +0 -57
  43. data/app/models/solidus_bling/contact.rb +0 -83
  44. data/app/models/solidus_bling/product.rb +0 -36
  45. data/app/views/spree/admin/accounts/_form.html.erb +0 -76
  46. data/app/views/spree/admin/accounts/edit.html.erb +0 -11
  47. data/app/views/spree/admin/accounts/index.html.erb +0 -13
  48. data/app/views/spree/admin/accounts/new.html.erb +0 -2
  49. data/db/migrate/20230918141700_create_solidus_bling_products.rb +0 -12
  50. data/db/migrate/20231018141940_add_company_to_solidus_bling_shipping_methods.rb +0 -5
  51. data/spec/helpers/solidus_bling/accounts_helper_spec.rb +0 -17
  52. data/spec/helpers/solidus_bling/bling_products_helper_spec.rb +0 -17
  53. data/spec/jobs/solidus_bling/contact_job_spec.rb +0 -7
  54. data/spec/jobs/solidus_bling/order_job_spec.rb +0 -7
  55. data/spec/jobs/solidus_bling/product_job_spec.rb +0 -7
  56. data/spec/jobs/solidus_bling/update_product_job_spec.rb +0 -7
  57. data/spec/models/solidus_bling/account_spec.rb +0 -7
  58. data/spec/models/solidus_bling/event_spec.rb +0 -7
  59. data/spec/models/solidus_bling/payment_method_spec.rb +0 -7
  60. data/spec/models/solidus_bling/product_spec.rb +0 -7
  61. data/spec/models/solidus_bling/seller_spec.rb +0 -7
  62. data/spec/models/solidus_bling/shipping_method_spec.rb +0 -7
  63. data/spec/requests/solidus_bling/accounts_spec.rb +0 -7
  64. data/spec/requests/solidus_bling/bling_products_spec.rb +0 -7
  65. data/spec/spec_helper.rb +0 -32
  66. /data/app/views/spree/admin/{accounts → solidus_bling/accounts}/show.html.erb +0 -0
  67. /data/db/migrate/{20230918141505_add_bling_order_id_to_spree_orders.rb → 20231110112528_add_bling_order_id_to_spree_orders.rb} +0 -0
  68. /data/db/migrate/{20230918142742_create_solidus_bling_payment_methods.rb → 20231110112958_create_solidus_bling_payment_methods.rb} +0 -0
  69. /data/db/migrate/{20230918142953_create_solidus_bling_sellers.rb → 20231110113122_create_solidus_bling_sellers.rb} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6174ab1749491921d0e680b8b72635346b35cc38155d40b1ef0dc6a50a75ade
4
- data.tar.gz: 87b31e5d61269b0fe8999bee491d565853494aabbeebda3902d5c2336bafcd8f
3
+ metadata.gz: '06007929028aa91ff59c2aa725b53c6e68b177ac56d4e5c53a4443b645dabc17'
4
+ data.tar.gz: de6672cddf389140fdafb5b2580bc98e93d9c8a989b6769a6dcf5581d982948d
5
5
  SHA512:
6
- metadata.gz: e128a60e203a6bcc189a70152bc46d3a254616162cf06e1b1ba3e8722b7b61705a7af171081bd32d8fe7df4e044ba49325d6970350b3605b4b38d0ef2a388633
7
- data.tar.gz: 7b46c4aead25b783e38df50bc1c64f19e8797bd13f5ed470eaa8d5b006354e339c84f9d02d2639eda4371a3b1cbafe4f4c0c2a30ba38024d9343e48eab125376
6
+ metadata.gz: a55d2735828682b6ab1e53f25683e678fd33f3ec893ea568ed8f963430f908096da5c52b886c0f9905b87facf2909cc7144cd526578336803aff14c3fe5b688e
7
+ data.tar.gz: 9cd9a628c2ecdbd68a7f300ce0a6d2dc108ff3679c65812d532421232415c702bb0f80e618e08666a1ba7011ceab7f8729847d78f00b16241297c1a2a984bdf1
data/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
data/.gitignore CHANGED
@@ -19,4 +19,3 @@ spec/examples.txt
19
19
  .rvmrc
20
20
  .ruby-version
21
21
  .ruby-gemset
22
- release.sh
data/CHANGELOG.md CHANGED
@@ -1,5 +1,3 @@
1
1
  # Changelog
2
2
 
3
-
4
-
5
- \* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
3
+ See https://github.com/solidusio-contrib/solidus_bling/releases or OLD_CHANGELOG.md for older versions.
data/Gemfile CHANGED
@@ -1,42 +1,41 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source 'https://rubygems.org'
3
+ source "https://rubygems.org"
4
4
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
5
5
 
6
- branch = ENV.fetch('SOLIDUS_BRANCH', 'main')
7
- gem 'solidus', github: 'solidusio/solidus', branch: branch
6
+ branch = ENV.fetch("SOLIDUS_BRANCH", "main")
7
+ gem "solidus", github: "solidusio/solidus", branch: branch
8
8
 
9
9
  # The solidus_frontend gem has been pulled out since v3.2
10
- if branch >= 'v3.2'
11
- gem 'solidus_frontend'
12
- elsif branch == 'main'
13
- gem 'solidus_frontend', github: 'solidusio/solidus_frontend'
10
+ if branch >= "v3.2"
11
+ gem "solidus_frontend"
12
+ elsif branch == "main"
13
+ gem "solidus_frontend", github: "solidusio/solidus_frontend"
14
14
  else
15
- gem 'solidus_frontend', github: 'solidusio/solidus', branch: branch
15
+ gem "solidus_frontend", github: "solidusio/solidus", branch: branch
16
16
  end
17
17
 
18
18
  # Needed to help Bundler figure out how to resolve dependencies,
19
19
  # otherwise it takes forever to resolve them.
20
20
  # See https://github.com/bundler/bundler/issues/6677
21
- gem 'rails', '>0.a'
22
-
21
+ gem "rails", ">0.a"
23
22
 
24
23
  # Provides basic authentication functionality for testing parts of your engine
25
- gem 'solidus_auth_devise'
24
+ gem "solidus_auth_devise"
26
25
 
27
- case ENV.fetch('DB', nil)
28
- when 'mysql'
29
- gem 'mysql2'
30
- when 'postgresql'
31
- gem 'pg'
26
+ case ENV.fetch("DB", nil)
27
+ when "mysql"
28
+ gem "mysql2"
29
+ when "postgresql"
30
+ gem "pg"
32
31
  else
33
- gem 'sqlite3'
32
+ gem "sqlite3"
34
33
  end
35
34
 
36
35
  # While we still support Ruby < 3 we need to workaround a limitation in
37
36
  # the 'async' gem that relies on the latest ruby, since RubyGems doesn't
38
37
  # resolve gems based on the required ruby version.
39
- gem 'async', '< 3' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3')
38
+ gem "async", "< 3" if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3")
40
39
 
41
40
  gemspec
42
41
 
@@ -45,4 +44,4 @@ gemspec
45
44
  #
46
45
  # We use `send` instead of calling `eval_gemfile` to work around an issue with
47
46
  # how Dependabot parses projects: https://github.com/dependabot/dependabot-core/issues/1658.
48
- send(:eval_gemfile, 'Gemfile-local') if File.exist? 'Gemfile-local'
47
+ send(:eval_gemfile, "Gemfile-local") if File.exist? "Gemfile-local"
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2023 ulysses-bull
1
+ Copyright (c) 2023 Hamilton Tumenas Borges
2
2
  All rights reserved.
3
3
 
4
4
  Redistribution and use in source and binary forms, with or without modification,
data/README.md CHANGED
@@ -70,4 +70,4 @@ Please refer to the [dedicated page](https://github.com/solidusio/solidus/wiki/H
70
70
 
71
71
  ## License
72
72
 
73
- Copyright (c) 2023 ulysses-bull, released under the New BSD License.
73
+ Copyright (c) 2023 Hamilton Tumenas Borges, released under the New BSD License.
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
- require 'solidus_dev_support/rake_tasks'
4
+ require "solidus_dev_support/rake_tasks"
5
5
  SolidusDevSupport::RakeTasks.install
6
6
 
7
- task default: 'extension:specs'
7
+ task default: "extension:specs"
@@ -0,0 +1,80 @@
1
+ module Spree
2
+ module Admin
3
+ module SolidusBling
4
+ class AccountsController < BaseController
5
+ before_action :set_account, only: [:show, :edit, :update, :destroy]
6
+
7
+ def index
8
+ @accounts = ::SolidusBling::Account.all
9
+ end
10
+
11
+ def show
12
+ end
13
+
14
+ def new
15
+ if ::SolidusBling::Account.any?
16
+ flash[:error] = "Você só pode ter uma conta Bling cadastrada"
17
+ redirect_to admin_solidus_bling_accounts_path
18
+ end
19
+ @bling_account = ::SolidusBling::Account.new
20
+ @payment_methods = ::Spree::PaymentMethod.all.pluck(:name, :id)
21
+ @shipping_methods = ::Spree::ShippingMethod.all.pluck(:name, :id)
22
+ @bling_account.payment_methods.build
23
+ @bling_account.shipping_methods.build
24
+ @bling_account.sellers.build
25
+ end
26
+
27
+ def create
28
+ bling_account = ::SolidusBling::Account.new account_params
29
+ if bling_account.save
30
+ flash[:success] = "Conta criada com sucesso"
31
+ redirect_to admin_solidus_bling_accounts_path
32
+ else
33
+ flash[:error] = bling_account.errors.full_messages.join("\n")
34
+ redirect_to new_admin_solidus_bling_account_path
35
+ end
36
+ end
37
+
38
+ def edit
39
+ @payment_methods = ::Spree::PaymentMethod.all.pluck(:name, :id)
40
+ @shipping_methods = ::Spree::ShippingMethod.all.pluck(:name, :id)
41
+ end
42
+
43
+ def update
44
+ @bling_account.update account_params
45
+ redirect_to edit_admin_solidus_bling_account_path(@bling_account.id)
46
+ end
47
+
48
+ def destroy
49
+ @bling_account.delete
50
+ redirect_to admin_solidus_bling_accounts_path
51
+ end
52
+
53
+ def authorize
54
+ bling_account = ::SolidusBling::Account.first
55
+ code = params[:code]
56
+ unless ::SolidusBling::Token.new.authorize(bling_account, code)
57
+ bling_account.errors.add(:base, "Erro ao autorizar aplicativo 🦆")
58
+ flash[:error] = bling_account.errors.full_messages.join("\n")
59
+ end
60
+ redirect_to edit_admin_solidus_bling_account_path(bling_account.id)
61
+ end
62
+
63
+ private
64
+
65
+ def set_account
66
+ @bling_account = ::SolidusBling::Account.find(params[:id])
67
+ end
68
+
69
+ def account_params
70
+ params.require(:account).permit(
71
+ :client_id, :client_secret, :refresh_token, :redirect_url, :state, :access_token, :api_key, :external_store_id, :incoming_category_id,
72
+ payment_methods_attributes: [:id, :spree_payment_method_id, :external_id],
73
+ shipping_methods_attributes: [:id, :spree_shipping_method_id, :alias, :company],
74
+ sellers_attributes: [:id, :name, :external_id]
75
+ )
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -2,19 +2,16 @@ module SolidusBling
2
2
  class OrderJob < ApplicationJob
3
3
  queue_as :default
4
4
 
5
- def perform event:, method_name:
6
- bling_event = SolidusBling::Event.new
5
+ def perform(event:)
7
6
  order = event[:order]
8
- bling_event.internal_id = order.id
9
- bling_event.method = method_name
7
+ bling_event = SolidusBling::Event.new(internal_id: order.id, body: order.to_json)
10
8
  begin
11
9
  SolidusBling::Order.new(order).send
12
- bling_event.status = 'success'
10
+ bling_event.status = "success"
13
11
  rescue => e
14
- bling_event.status = 'error'
12
+ bling_event.status = "error"
15
13
  bling_event.message = e.to_json
16
14
  ensure
17
- bling_event.body = order.to_json
18
15
  bling_event.save!
19
16
  end
20
17
  end
@@ -2,8 +2,13 @@ module SolidusBling
2
2
  class Account < ApplicationRecord
3
3
  has_many :payment_methods, dependent: :destroy
4
4
  has_many :shipping_methods, dependent: :destroy
5
- has_many :products, dependent: :destroy
6
5
  has_many :sellers, dependent: :destroy
7
6
  accepts_nested_attributes_for :payment_methods, :shipping_methods, :sellers
7
+
8
+ def check_token
9
+ return if client_id.blank? || client_secret.blank? || refresh_token.blank?
10
+ return if (token_expires_in - 1.day) > DateTime.now
11
+ Token.new.refresh self
12
+ end
8
13
  end
9
14
  end
@@ -1,136 +1,161 @@
1
1
  module SolidusBling
2
- class Order < SolidusBling::Api
3
-
4
- def initialize order
5
- super(SolidusBling::Account.first)
6
- @order = order
2
+ class Order
3
+ def initialize spree_order
4
+ @order = spree_order
7
5
  end
8
6
 
9
7
  def send
10
- if @order.present?
11
- contato = SolidusBling::Contact.new(@order, @account)
12
- hash_order = build_hash_order(@order, contato.info)
13
- begin
14
- attempts ||= 1
15
- req = Typhoeus.post("#{@account.api_base_url}/pedidos/vendas", headers: @headers, body: JSON.dump(hash_order))
16
- raise if req.code == 429
17
- rescue
18
- retry if (attempts += 1) < 5
19
- ensure
20
- json = JSON.parse(req.body)
21
- response_has_error?(json)
22
- if json.include? "data"
23
- @order.bling_order_id = json["data"]["id"]
24
- @order.save
25
- end
26
- end
27
- end
28
- end
8
+ @account = SolidusBling::Account.first
9
+ @account.check_token
10
+ BlingApi.configuration.access_token = @account.access_token
29
11
 
30
- private
31
-
32
- def build_hash_order order, contato
33
- {
34
- "numeroLoja": order.number,
35
- "data": order.completed_at.strftime("%Y-%m-%d"),
36
- "loja": {
37
- "id": @account.external_store_id
38
- },
39
- "contato": {
40
- "id": contato["id"]
41
- },
42
- "itens": build_array_items(order.line_items),
43
- "vendedor": {
44
- "id": @account.sellers.first.external_id
45
- },
46
- "transporte": build_hash_transporte(order),
47
- "parcelas": build_array_parcelas(order),
48
- "desconto": {
49
- "valor": order.promo_total&.to_f.abs || 0,
50
- "unidade": "REAL"
51
- },
52
- "totalProdutos": order.item_total.to_f,
53
- "total": order.total.to_f,
54
- "observacoes": " Nº Pedido Loja: #{order.number}",
55
- "observacoesInternas": "Pagamento: #{@parcelas}x R$ #{(order.total.to_f / @parcelas).round(2)}\nForma de envio: #{order.shipments.last.selected_shipping_rate.name}\n#{build_cupons(order.promotions)}#{build_order_url(order)}",
56
- "categoria": {
57
- "id": @account.incoming_category_id
58
- }
59
- }
12
+ contato = costumer
13
+ items = products
14
+ parcelas = installments
15
+ observacoes_internas = internal_observations
16
+ transporte = shipment
17
+
18
+ bling_order = BlingApi::Order.new(
19
+ id_contato: contato.id,
20
+ items: items,
21
+ parcelas: parcelas,
22
+ numero_loja: @order.number,
23
+ data: @order.completed_at.strftime("%Y-%m-%d"),
24
+ total_produtos: @order.item_total.to_f,
25
+ total: @order.total.to_f,
26
+ id_loja: @account.external_store_id,
27
+ observacoes: "Número do pedido loja: #{@order.number}",
28
+ observacoes_internas: observacoes_internas,
29
+ valor_desconto: @order.adjustment_total,
30
+ id_categoria: @account.incoming_category_id,
31
+ transporte: transporte,
32
+ id_vendedor: @account.sellers.first.external_id
33
+ ).create
60
34
  end
61
35
 
62
- def build_order_url order
63
- "Link para o pedido: #{order.store.url}/orders/#{order.number}/token/#{order.guest_token}"
36
+ def costumer
37
+ contato = BlingApi::Customer.find_by_tax_id(@order.tax_id)
38
+ if contato
39
+ contato.update(
40
+ pessoa_juridica: @order.tax_id.length > 14,
41
+ numero_documento: @order.tax_id,
42
+ **build_customer
43
+ )
44
+ else
45
+ contato = BlingApi::Customer.new(
46
+ pessoa_juridica: @order.tax_id.length > 14,
47
+ numero_documento: @order.tax_id,
48
+ **build_customer
49
+ ).create
50
+ end
51
+ contato
64
52
  end
65
53
 
66
- def build_array_items items
67
- items.map do |item|
68
- bling_product = SolidusBling::Product.find_by(sku: item.sku)
54
+ def products
55
+ @order.line_items.map do |item|
56
+ product = BlingApi::Product.find_by_sku(item.sku)
69
57
  {
70
- "id": bling_product.external_id,
71
- "quantidade": item.quantity,
72
- "valor": item.price.to_f,
73
- "produto": {
74
- "id": bling_product.external_id
58
+ id: product.id,
59
+ quantidade: item.quantity,
60
+ valor: item.price.to_f,
61
+ produto: {
62
+ id: product.id
75
63
  }
76
64
  }
77
- end
65
+ end.compact
78
66
  end
79
67
 
80
- def build_array_parcelas order
81
- @parcelas = 1
82
- if order.payments.last.source
83
- @parcelas = order.payments.last.source.try(:installments) || 1
84
- end
85
- valor_parcela = order.total.to_f / @parcelas
68
+ def installments
69
+ @parcelas = @order.payments.last.source.try(:installments) || 1
70
+ valor_parcela = @order.total.to_f / @parcelas
86
71
  1.upto(@parcelas).map do |parcela|
87
72
  {
88
- "dataVencimento": (order.completed_at + parcela.months).strftime("%Y-%m-%d"),
89
- "valor": valor_parcela,
90
- "formaPagamento": {
91
- "id": @account.payment_methods.find_by(spree_payment_method_id: order.payments.last.payment_method_id).external_id
73
+ dataVencimento: (@order.completed_at + parcela.months).strftime("%Y-%m-%d"),
74
+ valor: valor_parcela,
75
+ formaPagamento: {
76
+ id: @account.payment_methods.find_by(spree_payment_method_id: @order.payments.last.payment_method_id).external_id
92
77
  }
93
78
  }
94
79
  end
95
80
  end
96
81
 
97
- def build_hash_transporte order
98
- shipping_method = @account.shipping_methods.find_by(spree_shipping_method_id: order.shipments.last.shipping_method.id)
82
+ def internal_observations
83
+ "Pagamento: #{@parcelas}x R$ #{(@order.total.to_f / @parcelas).round(2)}" +
84
+ "\n" +
85
+ "Forma de envio: #{@order.shipments.last.selected_shipping_rate.name}" +
86
+ "\n" +
87
+ "#{build_cupons(@order.promotions)}" +
88
+ "\n" +
89
+ "#{build_order_url}"
90
+ end
91
+
92
+ def shipment
93
+ shipping_method = @account.shipping_methods.find_by(spree_shipping_method_id: @order.shipments.last.shipping_method.id)
99
94
  servico = shipping_method.nil? ? "" : shipping_method.alias
100
95
  transportadora = shipping_method.nil? ? "" : shipping_method.company
101
96
  {
102
- "fretePorConta": 1,
103
- "frete": order.shipment_total.to_f,
104
- "quantidadeVolumes": 1,
105
- "contato": {
106
- "nome": transportadora,
97
+ fretePorConta: 1,
98
+ frete: @order.shipment_total.to_f,
99
+ quantidadeVolumes: 1,
100
+ contato: {
101
+ nome: transportadora
107
102
  },
108
- "etiqueta": {
109
- "nome": order.ship_address.name,
110
- "endereco": order.ship_address.address1,
111
- "numero": order.ship_address.number,
112
- "complemento": order.ship_address.address2,
113
- "municipio": order.ship_address.city,
114
- "uf": order.ship_address.state.abbr,
115
- "bairro": order.ship_address.district,
116
- "cep": order.ship_address.zipcode,
117
- "nomePais": "Brasil"
103
+ etiqueta: {
104
+ nome: @order.ship_address.name,
105
+ endereco: @order.ship_address.address1,
106
+ numero: @order.ship_address.number,
107
+ complemento: @order.ship_address.address2,
108
+ municipio: @order.ship_address.city,
109
+ uf: @order.ship_address.state.abbr,
110
+ bairro: @order.ship_address.district,
111
+ cep: @order.ship_address.zipcode,
112
+ nomePais: "Brasil"
118
113
  },
119
- "volumes": [
114
+ volumes: [
120
115
  {
121
- "servico": servico,
122
- "codigoRastreamento": ""
116
+ servico: servico,
117
+ codigoRastreamento: ""
123
118
  }
124
119
  ]
125
120
  }
126
121
  end
127
122
 
123
+ def build_customer
124
+ {
125
+ nome: @order.bill_address.name,
126
+ telefone: @order.bill_address.phone,
127
+ email: @order.email,
128
+ endereco: {
129
+ "endereco" => @order.bill_address.address1,
130
+ "cep" => @order.bill_address.zipcode,
131
+ "bairro" => @order.bill_address.district,
132
+ "municipio" => @order.bill_address.city,
133
+ "uf" => @order.bill_address.state.abbr,
134
+ "numero" => @order.bill_address.number,
135
+ "complemento" => @order.bill_address.address2
136
+ },
137
+ endereco_cobranca: {
138
+ "endereco" => @order.bill_address.address1,
139
+ "cep" => @order.bill_address.zipcode,
140
+ "bairro" => @order.bill_address.district,
141
+ "municipio" => @order.bill_address.city,
142
+ "uf" => @order.bill_address.state.abbr,
143
+ "numero" => @order.bill_address.number,
144
+ "complemento" => @order.bill_address.address2
145
+ }
146
+ }
147
+ end
148
+
128
149
  def build_cupons cupons
129
- string_cupons = ''
150
+ string_cupons = ""
130
151
  if !cupons.empty?
131
152
  string_cupons = "Cupons utilizados: #{cupons.pluck(:name).join(", ")}"
132
153
  end
133
154
  string_cupons
134
155
  end
156
+
157
+ def build_order_url
158
+ "Link para o pedido: #{@order.store.url}/orders/#{@order.number}/token/#{@order.guest_token}"
159
+ end
135
160
  end
136
161
  end
@@ -0,0 +1,44 @@
1
+ module SolidusBling
2
+ class Token < ::Ac::Base
3
+ BASE_URL = "https://www.bling.com.br/Api/v3"
4
+
5
+ def initialize
6
+ super
7
+ end
8
+
9
+ def authorize bling_account, code
10
+ basic_encoded = Base64.strict_encode64("#{bling_account.client_id}:#{bling_account.client_secret}")
11
+ headers = {
12
+ "Content-Type": "application/x-www-form-urlencoded",
13
+ Authorization: "Basic #{basic_encoded}"
14
+ }
15
+ body = {
16
+ code: code,
17
+ grant_type: "authorization_code"
18
+ }
19
+ response_json = post("/oauth/token", headers:, body:).json
20
+ bling_account.update(
21
+ access_token: response_json["access_token"],
22
+ token_expires_in: DateTime.now.utc + response_json["expires_in"].seconds,
23
+ refresh_token: response_json["refresh_token"]
24
+ )
25
+ rescue
26
+ false
27
+ end
28
+
29
+ def refresh bling_account
30
+ response_json = BlingApi::Client.new.refresh_token(
31
+ client_id: bling_account.client_id,
32
+ client_secret: bling_account.client_secret,
33
+ refresh_token: bling_account.refresh_token
34
+ )
35
+ bling_account.update(
36
+ access_token: response_json["access_token"],
37
+ token_expires_in: DateTime.now.utc + response_json["expires_in"].seconds,
38
+ refresh_token: response_json["refresh_token"]
39
+ )
40
+ rescue
41
+ false
42
+ end
43
+ end
44
+ end
@@ -3,30 +3,10 @@ module SolidusBling
3
3
  include Omnes::Subscriber
4
4
 
5
5
  handle :order_finalized,
6
- with: :send_order_to_bling,
7
- id: :send_order_to_bling
8
-
9
- handle :bling_products_updated,
10
- with: :products_updated,
11
- id: :products_updated
12
-
13
- handle :bling_contact_created,
14
- with: :contato_created,
15
- id: :contato_created
6
+ with: :send_order_to_bling
16
7
 
17
8
  def send_order_to_bling event, debug_info = {}
18
- method = __method__.to_s
19
- SolidusBling::OrderJob.perform_later(event: event.payload, method_name: method)
20
- end
21
-
22
- def contato_created event, debug_info = {}
23
- method = __method__.to_s
24
- SolidusBling::ContactJob.perform_later(event: event.payload, method_name: method)
25
- end
26
-
27
- def products_updated event, debug_info = {}
28
- method = __method__.to_s
29
- SolidusBling::ProductJob.perform_later(event: event.payload, method_name: method)
9
+ SolidusBling::OrderJob.perform_later(event: event.payload)
30
10
  end
31
11
  end
32
12
  end