solidus_me 1.0.7 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -5
- data/app/controller/spree/admin/solidus_me/accounts_controller.rb +69 -0
- data/app/models/solidus_me/account.rb +23 -0
- data/app/models/solidus_me/shipping_estimator.rb +19 -16
- data/app/views/spree/admin/solidus_me/accounts/_form.html.erb +58 -0
- data/app/views/spree/admin/solidus_me/accounts/edit.html.erb +1 -0
- data/app/views/spree/admin/solidus_me/accounts/index.html.erb +12 -0
- data/app/views/spree/admin/solidus_me/accounts/new.html.erb +1 -0
- data/bin/meu_sandbox +67 -0
- data/config/routes.rb +5 -2
- data/db/migrate/20231109124213_create_solidus_me_accounts.rb +17 -0
- data/lib/solidus_me/engine.rb +1 -1
- data/lib/solidus_me/version.rb +1 -1
- data/spec/models/solidus_me/account_spec.rb +7 -0
- metadata +12 -7
- data/app/controller/spree/admin/melhor_envio_controller.rb +0 -43
- data/app/models/solidus_me/preferences.rb +0 -30
- data/app/overrides/store_preferences.rb +0 -23
- data/app/views/spree/admin/melhor_envio/index.html.erb +0 -58
- data/db/migrate/20231103133348_add_preferences_to_spree_store.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8110b4363983ec1861ecd1a447f4667aab0189a5ad7610e4d5848383f5980fe
|
4
|
+
data.tar.gz: 400e05caa5f7bc61f5f25633d5e487fa8fdfd075f9640c4b73ab88da4c2514a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3641ad7b24395e36ed8bdac1c6afe29cc119018883dc2e076ead540874e59d3ae684da7fafb57b041859e8034db2c6d890e9adbb761bbcbb1d7e1278d3eb3153
|
7
|
+
data.tar.gz: 36787597b09bbb862d1b40d2f8c2b936b1693516bf286321f951d3802136d8b742633f394ac57d478f853b4d1fc4fbd19b2d403137212a7f8bf8f8479cada9ab
|
data/README.md
CHANGED
@@ -7,24 +7,34 @@ Gem para cotar empresas e valores de entrega junto à Api do Melhor Envio.
|
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
10
|
-
|
10
|
+
Adicionar solidus_me ao Gemfile:
|
11
11
|
|
12
12
|
```ruby
|
13
13
|
gem 'solidus_me'
|
14
14
|
```
|
15
15
|
|
16
|
-
|
16
|
+
Executar:
|
17
17
|
|
18
18
|
```shell
|
19
|
+
bundle
|
19
20
|
bin/rails generate solidus_me:install
|
20
21
|
```
|
22
|
+
|
23
|
+
Executar o comando para instalar a gem de dependências `solidus_brasilian_adaptations`:
|
24
|
+
|
25
|
+
```shell
|
26
|
+
bin/rails generate solidus_brazilian_adaptations:install
|
27
|
+
```
|
28
|
+
|
29
|
+
Após, seguir o [tutorial](https://github.com/ulysses-bull/solidus_brazilian_adaptations) da gem `soluds_brazilian_adaptations`
|
30
|
+
|
21
31
|
Adicionar no arquivo `/config/initilizers/spree.rb`
|
22
32
|
|
23
33
|
```ruby
|
24
34
|
Spree.config do |config|
|
25
|
-
|
35
|
+
+++
|
26
36
|
config.stock.estimator_class = "SolidusMe::ShippingEstimator"
|
27
|
-
|
37
|
+
+++
|
28
38
|
end
|
29
39
|
```
|
30
40
|
|
@@ -34,8 +44,10 @@ No painel do admin será adiconado o item do `Melhor Envio` no menu lateral. Nes
|
|
34
44
|
CEP de origem:
|
35
45
|
Client ID:
|
36
46
|
Client Secret:
|
47
|
+
Redirect Url:
|
48
|
+
State:
|
37
49
|
```
|
38
|
-
Após salvar, o link completo para autenticação do aplicativo ficará disponível abaixo. Uma vez autorizado, são gerados os `refresh_token` e `access_token`, e o sistema estará pronto para cotação dos fretes junto ao Melhor Envio.
|
50
|
+
Após salvar, o link completo para autenticação do aplicativo ficará disponível abaixo. A gem disponibiliza um endpoint (`HOST/admin/melhor_envio/authorize`) para receber o code e realizar a autenticação automaticamente. Uma vez autorizado, são gerados os `refresh_token` e `access_token`, e o sistema estará pronto para cotação dos fretes junto ao Melhor Envio.
|
39
51
|
|
40
52
|
Por padrão, os serviços de entrega são os `SEDEX`, `PAC` e `MiniEnvios` dos CORREIOS. Porém, os serviços podem ser customizados de acordo com a necessidade. Basta adicioná-los aos `preferences` da `Spree::Store`:
|
41
53
|
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Spree
|
2
|
+
module Admin
|
3
|
+
module SolidusMe
|
4
|
+
|
5
|
+
class AccountsController < BaseController
|
6
|
+
|
7
|
+
def index
|
8
|
+
@accounts = ::SolidusMe::Account.all
|
9
|
+
end
|
10
|
+
|
11
|
+
def new
|
12
|
+
@me_account = ::SolidusMe::Account.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def create
|
16
|
+
me_account = ::SolidusMe::Account.new account_params
|
17
|
+
if me_account.save
|
18
|
+
flash[:success] = "Conta criada com sucesso"
|
19
|
+
redirect_to admin_solidus_me_accounts_path
|
20
|
+
else
|
21
|
+
flash[:error] = me_account.errors.full_messages.join("\n")
|
22
|
+
redirect_to new_admin_solidus_me_account_path
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def edit
|
27
|
+
@me_account = ::SolidusMe::Account.find(params[:id])
|
28
|
+
end
|
29
|
+
|
30
|
+
def update
|
31
|
+
me_account = ::SolidusMe::Account.find(params[:id])
|
32
|
+
me_account.update account_params
|
33
|
+
redirect_to edit_admin_solidus_me_account_path(me_account.id)
|
34
|
+
end
|
35
|
+
|
36
|
+
def destroy
|
37
|
+
me_account = ::SolidusMe::Account.find(params[:id])
|
38
|
+
me_account.delete
|
39
|
+
redirect_to admin_solidus_me_accounts_path
|
40
|
+
end
|
41
|
+
|
42
|
+
def authorize
|
43
|
+
code = params[:code]
|
44
|
+
me_account = ::SolidusMe::Account.first
|
45
|
+
authorize_json = MeApi::Client.new.authorize(
|
46
|
+
client_id: me_account.client_id,
|
47
|
+
client_secret: me_account.client_secret,
|
48
|
+
code: code,
|
49
|
+
redirect_url: me_account.redirect_url
|
50
|
+
).json
|
51
|
+
me_account.update(
|
52
|
+
access_token: authorize_json["access_token"],
|
53
|
+
refresh_token: authorize_json["refresh_token"],
|
54
|
+
token_expires_in: (DateTime.now + authorize_json["expires_in"].seconds).to_s
|
55
|
+
)
|
56
|
+
redirect_to edit_admin_solidus_me_account_path me_account
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def account_params
|
62
|
+
params.require(:account).permit(
|
63
|
+
:client_id, :client_secret, :access_token, :refresh_token, :redirect_url, :state, :token_expires_in, :services, :postal_code_from
|
64
|
+
)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SolidusMe
|
2
|
+
class Account < ApplicationRecord
|
3
|
+
|
4
|
+
def check_token
|
5
|
+
return if client_id.blank? || client_secret.blank? || refresh_token.blank?
|
6
|
+
return if (token_expires_in - 1.day) > DateTime.now
|
7
|
+
refresh
|
8
|
+
end
|
9
|
+
|
10
|
+
def refresh
|
11
|
+
refresh_json = MeApi::Client.new.refresh_token(
|
12
|
+
client_id: client_id,
|
13
|
+
client_secret: client_secret,
|
14
|
+
refresh_token: refresh_token
|
15
|
+
).json
|
16
|
+
access_token = refresh_json["access_token"]
|
17
|
+
refresh_token = refresh_json["refresh_token"]
|
18
|
+
token_expires_in = DateTime.now.utc + refresh_json["expires_in"].seconds
|
19
|
+
self.update(access_token: access_token, refresh_token: refresh_token, token_expires_in: token_expires_in)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module SolidusMe
|
2
2
|
class ShippingEstimator
|
3
3
|
|
4
|
-
def initialize
|
5
|
-
store = Spree::Store.first
|
6
|
-
@preferences = Preferences.new(store).preferences
|
7
|
-
@me_client = MeApi::Client.new(@preferences[:access_token])
|
8
|
-
end
|
9
|
-
|
10
4
|
def shipping_rates(package, _frontend_only = true)
|
5
|
+
@me_account = Account.first
|
6
|
+
return [] if @me_account.blank?
|
7
|
+
|
8
|
+
@me_account.check_token
|
9
|
+
@me_client = MeApi::Client.new(@me_account.access_token)
|
10
|
+
|
11
11
|
melhor_envio_rates = get_rates_from_melhor_envio(package)
|
12
12
|
shipping_rates = melhor_envio_rates.map do |melhor_envio_rate|
|
13
13
|
build_shipping_rate(melhor_envio_rate, package)
|
@@ -28,24 +28,17 @@ module SolidusMe
|
|
28
28
|
weight = package.contents.map {|content| content.weight }.sum
|
29
29
|
price = package.contents.map {|content| content.price }.sum
|
30
30
|
zipcode = package.order.ship_address.zipcode
|
31
|
-
services = @
|
31
|
+
services = @me_account.services.blank? ? ["SEDEX", "PAC", "Mini Envios", ".Package", ".Com"] : @me_account.services
|
32
32
|
|
33
33
|
rates = @me_client.rates(
|
34
|
-
from: @
|
34
|
+
from: @me_account.postal_code_from,
|
35
35
|
to: zipcode,
|
36
36
|
weight_kg: (weight/1000),
|
37
37
|
contents_value_brl: price
|
38
38
|
)
|
39
39
|
rates.select { |rate| rate.price > 0 && services.include?(rate.service_name) }
|
40
40
|
rescue
|
41
|
-
[
|
42
|
-
OpenStruct.new({
|
43
|
-
price: 15.00,
|
44
|
-
service_name: "",
|
45
|
-
carrier_name: "Frete padrão",
|
46
|
-
delivery_range: { "min" => 5, "max" => 5 }
|
47
|
-
})
|
48
|
-
]
|
41
|
+
[default_rate]
|
49
42
|
end
|
50
43
|
end
|
51
44
|
|
@@ -67,5 +60,15 @@ module SolidusMe
|
|
67
60
|
max_delivery_time: melhor_envio_rate.delivery_range["max"]
|
68
61
|
)
|
69
62
|
end
|
63
|
+
|
64
|
+
def default_rate
|
65
|
+
default_rate = Data.define(:price, :service_name, :carrier_name, :delivery_range)
|
66
|
+
default_rate.new(
|
67
|
+
price: 15.00,
|
68
|
+
service_name: "",
|
69
|
+
carrier_name: "Frete padrão",
|
70
|
+
delivery_range: { "min" => 5, "max" => 5 }
|
71
|
+
)
|
72
|
+
end
|
70
73
|
end
|
71
74
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
<%= form_with model: me_account, url: form_url do |form| %>
|
2
|
+
<h3>Melhor Envio</h3>
|
3
|
+
|
4
|
+
<div style="margin-top: 10px;">
|
5
|
+
<%= form.label :postal_code_from, "CEP de Origem: " %>
|
6
|
+
<%= form.text_field :postal_code_from, value: me_account.postal_code_from %>
|
7
|
+
</div>
|
8
|
+
|
9
|
+
<div style="margin-top: 10px;">
|
10
|
+
<%= form.hidden_field :services, value: me_account.services %>
|
11
|
+
<span>Serviços Disponíveis: <%= me_account.services %></span>
|
12
|
+
</div>
|
13
|
+
|
14
|
+
<div style="margin-top: 10px;">
|
15
|
+
<%= form.label :client_id, "Client ID: " %>
|
16
|
+
<%= form.text_field :client_id, value: me_account.client_id %>
|
17
|
+
</div>
|
18
|
+
|
19
|
+
<div style="margin-top: 10px;">
|
20
|
+
<%= form.label :client_secret, "Client Secret: " %>
|
21
|
+
<%= form.text_field :client_secret, value: me_account.client_secret %>
|
22
|
+
</div>
|
23
|
+
|
24
|
+
<div style="margin-top: 10px;">
|
25
|
+
<%= form.label :redirect_url, "Redirect URL: " %>
|
26
|
+
<%= form.text_field :redirect_url, value: me_account.redirect_url %>
|
27
|
+
</div>
|
28
|
+
|
29
|
+
<div style="margin-top: 10px;">
|
30
|
+
<%= form.label :state, "State: " %>
|
31
|
+
<%= form.text_field :state, value: me_account.state %>
|
32
|
+
</div>
|
33
|
+
|
34
|
+
<div style="margin-top: 10px;">
|
35
|
+
<%= form.label :access_token, "Access Token: " %>
|
36
|
+
<%= form.text_field :access_token, value: me_account.access_token %>
|
37
|
+
</div>
|
38
|
+
|
39
|
+
<div style="margin-top: 10px;">
|
40
|
+
<%= form.label :refresh_token, "Refresh Token: " %>
|
41
|
+
<%= form.text_field :refresh_token, value: me_account.refresh_token %>
|
42
|
+
</div>
|
43
|
+
|
44
|
+
<div style="margin-top: 10px;">
|
45
|
+
<%= form.hidden_field :token_expires_in, value: me_account.token_expires_in %>
|
46
|
+
<span>Expiração do token: <%= me_account.token_expires_in %></span>
|
47
|
+
</div>
|
48
|
+
|
49
|
+
<%= form.submit "Salvar", style: "margin-top: 10px;" %>
|
50
|
+
<% end %>
|
51
|
+
|
52
|
+
<div>
|
53
|
+
<input value="https://www.melhorenvio.com.br/oauth/authorize?client_id=<%= me_account.client_id %>&redirect_uri=<%= me_account.redirect_url %>&response_type=code&state=<%= me_account.state %>&scope=shipping-calculate">
|
54
|
+
<a target="_blank" class="" href="https://www.melhorenvio.com.br/oauth/authorize?client_id=<%= me_account.client_id %>&redirect_uri=<%= me_account.redirect_url %>&response_type=code&state=<%= me_account.state %>&scope=shipping-calculate">
|
55
|
+
<svg style="width: 24px; height: 24px;" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
56
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 6H5.25A2.25 2.25 0 003 8.25v10.5A2.25 2.25 0 005.25 21h10.5A2.25 2.25 0 0018 18.75V10.5m-10.5 6L21 3m0 0h-5.25M21 3v5.25"></path></svg>
|
57
|
+
</a>
|
58
|
+
</div>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= render "form", me_account: @me_account, form_url: admin_solidus_me_account_path(@me_account.id) %>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<%= link_to("Criar", new_admin_solidus_me_account_path) if @accounts.count < 1 %>
|
2
|
+
<div>
|
3
|
+
<% @accounts.each do |account| %>
|
4
|
+
<div>
|
5
|
+
<% account.attributes.each do |attribute| %>
|
6
|
+
<div style="overflow-wrap: break-word;"> <%= attribute[0] %>: <%= attribute[1] %></div>
|
7
|
+
<% end %>
|
8
|
+
<%= link_to "Editar", edit_admin_solidus_me_account_path(account.id) %>
|
9
|
+
<%= link_to "Excluir", admin_solidus_me_account_path(account.id), method: :delete %>
|
10
|
+
</div>
|
11
|
+
<% end %>
|
12
|
+
</div>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= render "form", me_account: @me_account, form_url: admin_solidus_me_accounts_path %>
|
data/bin/meu_sandbox
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
bin/rails db:environment:set RAILS_ENV=development
|
4
|
+
|
5
|
+
set -e
|
6
|
+
test -z "${DEBUG+empty_string}" || set -x
|
7
|
+
|
8
|
+
if [ -z "$SOLIDUS_BRANCH" ]
|
9
|
+
then
|
10
|
+
echo "~~> Use 'export SOLIDUS_BRANCH=[main|v3.2|...]' to control the Solidus branch"
|
11
|
+
SOLIDUS_BRANCH="main"
|
12
|
+
fi
|
13
|
+
echo "~~> Using branch $SOLIDUS_BRANCH of solidus"
|
14
|
+
|
15
|
+
extension_name="solidus_me"
|
16
|
+
|
17
|
+
# Stay away from the bundler env of the containing extension.
|
18
|
+
function unbundled {
|
19
|
+
ruby -rbundler -e'b = proc {system *ARGV}; Bundler.respond_to?(:with_unbundled_env) ? Bundler.with_unbundled_env(&b) : Bundler.with_clean_env(&b)' -- $@
|
20
|
+
}
|
21
|
+
|
22
|
+
rm -rf ./sandbox
|
23
|
+
unbundled bundle exec rails new sandbox \
|
24
|
+
--database=postgresql \
|
25
|
+
--skip-bundle \
|
26
|
+
--skip-git \
|
27
|
+
--skip-keeps \
|
28
|
+
--skip-rc \
|
29
|
+
--skip-spring \
|
30
|
+
--skip-test \
|
31
|
+
--skip-javascript
|
32
|
+
|
33
|
+
if [ ! -d "sandbox" ]; then
|
34
|
+
echo 'sandbox rails application failed'
|
35
|
+
exit 1
|
36
|
+
fi
|
37
|
+
|
38
|
+
cd ./sandbox
|
39
|
+
cat <<RUBY >> Gemfile
|
40
|
+
gem 'solidus', github: 'solidusio/solidus', branch: '$SOLIDUS_BRANCH'
|
41
|
+
gem 'rails-i18n'
|
42
|
+
gem 'solidus_i18n'
|
43
|
+
|
44
|
+
gem '$extension_name', path: '..'
|
45
|
+
|
46
|
+
group :test, :development do
|
47
|
+
platforms :mri do
|
48
|
+
gem 'pry-byebug'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
RUBY
|
52
|
+
|
53
|
+
unbundled bundle install --gemfile Gemfile
|
54
|
+
|
55
|
+
unbundled bundle exec rake db:drop db:create
|
56
|
+
|
57
|
+
unbundled bundle exec rails generate solidus:install \
|
58
|
+
--auto-accept \
|
59
|
+
--payment-method=none \
|
60
|
+
--frontend=starter \
|
61
|
+
$@
|
62
|
+
|
63
|
+
unbundled bundle exec rails generate ${extension_name}:install --auto-run-migrations
|
64
|
+
|
65
|
+
echo
|
66
|
+
echo "🚀 Sandbox app successfully created for $extension_name!"
|
67
|
+
echo "🧪 This app is intended for test purposes."
|
data/config/routes.rb
CHANGED
@@ -2,7 +2,10 @@
|
|
2
2
|
|
3
3
|
Spree::Core::Engine.routes.draw do
|
4
4
|
namespace :admin do
|
5
|
-
|
6
|
-
|
5
|
+
namespace :solidus_me do
|
6
|
+
resources :accounts do
|
7
|
+
get "authorize", on: :collection
|
8
|
+
end
|
9
|
+
end
|
7
10
|
end
|
8
11
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateSolidusMeAccounts < ActiveRecord::Migration[7.0]
|
2
|
+
def change
|
3
|
+
create_table :solidus_me_accounts do |t|
|
4
|
+
t.string :client_id
|
5
|
+
t.string :client_secret
|
6
|
+
t.string :access_token
|
7
|
+
t.string :refresh_token
|
8
|
+
t.string :redirect_url
|
9
|
+
t.string :state
|
10
|
+
t.datetime :token_expires_in
|
11
|
+
t.string :services
|
12
|
+
t.string :postal_code_from
|
13
|
+
|
14
|
+
t.timestamps
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/solidus_me/engine.rb
CHANGED
data/lib/solidus_me/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: solidus_me
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hamilton Tumenas Borges
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-11-
|
11
|
+
date: 2023-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: solidus_core
|
@@ -104,12 +104,15 @@ files:
|
|
104
104
|
- LICENSE
|
105
105
|
- README.md
|
106
106
|
- Rakefile
|
107
|
-
- app/controller/spree/admin/
|
108
|
-
- app/models/solidus_me/
|
107
|
+
- app/controller/spree/admin/solidus_me/accounts_controller.rb
|
108
|
+
- app/models/solidus_me/account.rb
|
109
109
|
- app/models/solidus_me/shipping_estimator.rb
|
110
|
-
- app/
|
111
|
-
- app/views/spree/admin/
|
110
|
+
- app/views/spree/admin/solidus_me/accounts/_form.html.erb
|
111
|
+
- app/views/spree/admin/solidus_me/accounts/edit.html.erb
|
112
|
+
- app/views/spree/admin/solidus_me/accounts/index.html.erb
|
113
|
+
- app/views/spree/admin/solidus_me/accounts/new.html.erb
|
112
114
|
- bin/console
|
115
|
+
- bin/meu_sandbox
|
113
116
|
- bin/rails
|
114
117
|
- bin/rails-engine
|
115
118
|
- bin/rails-sandbox
|
@@ -119,7 +122,7 @@ files:
|
|
119
122
|
- config/locales/en.yml
|
120
123
|
- config/locales/pt-BR.yml
|
121
124
|
- config/routes.rb
|
122
|
-
- db/migrate/
|
125
|
+
- db/migrate/20231109124213_create_solidus_me_accounts.rb
|
123
126
|
- lib/generators/solidus_me/install/install_generator.rb
|
124
127
|
- lib/generators/solidus_me/install/templates/initializer.rb
|
125
128
|
- lib/solidus_me.rb
|
@@ -128,6 +131,7 @@ files:
|
|
128
131
|
- lib/solidus_me/testing_support/factories.rb
|
129
132
|
- lib/solidus_me/version.rb
|
130
133
|
- solidus_me.gemspec
|
134
|
+
- spec/models/solidus_me/account_spec.rb
|
131
135
|
- spec/spec_helper.rb
|
132
136
|
homepage: https://github.com/solidusio-contrib/solidus_me#readme
|
133
137
|
licenses:
|
@@ -159,4 +163,5 @@ signing_key:
|
|
159
163
|
specification_version: 4
|
160
164
|
summary: Gem para integração com a API do Melhor Envio
|
161
165
|
test_files:
|
166
|
+
- spec/models/solidus_me/account_spec.rb
|
162
167
|
- spec/spec_helper.rb
|
@@ -1,43 +0,0 @@
|
|
1
|
-
module Spree
|
2
|
-
module Admin
|
3
|
-
class MelhorEnvioController < BaseController
|
4
|
-
before_action :set_melhor_envio_preferences
|
5
|
-
|
6
|
-
def index
|
7
|
-
end
|
8
|
-
|
9
|
-
def update
|
10
|
-
@me_preferences = preferences_params
|
11
|
-
Spree::Store.first.update(preferences: { melhor_envio: @me_preferences })
|
12
|
-
redirect_to admin_melhor_envio_index_path
|
13
|
-
end
|
14
|
-
|
15
|
-
def authorize
|
16
|
-
code = params[:code]
|
17
|
-
authorize_json = MeApi::Client.new.authorize(
|
18
|
-
client_id: @me_preferences[:client_id],
|
19
|
-
client_secret: @me_preferences[:client_secret],
|
20
|
-
code: code,
|
21
|
-
redirect_url: @me_preferences[:redirect_url]
|
22
|
-
).json
|
23
|
-
@me_preferences[:access_token] = authorize_json["access_token"]
|
24
|
-
@me_preferences[:refresh_token] = authorize_json["refresh_token"]
|
25
|
-
@me_preferences[:token_expires_in] = (DateTime.now + authorize_json["expires_in"].seconds).to_s
|
26
|
-
Spree::Store.first.update(preferences: { melhor_envio: @me_preferences })
|
27
|
-
redirect_to admin_melhor_envio_index_path
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def set_melhor_envio_preferences
|
33
|
-
@me_preferences = Spree::Store.first.preferences[:melhor_envio]
|
34
|
-
end
|
35
|
-
|
36
|
-
def preferences_params
|
37
|
-
hash_preferences = params.permit(:postal_code_from, :services, :client_id, :client_secret, :redirect_url, :state, :access_token, :refresh_token, :token_expires_in)
|
38
|
-
hash_preferences[:services] = hash_preferences[:services].split(" ")
|
39
|
-
hash_preferences.to_h
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module SolidusMe
|
2
|
-
class Preferences
|
3
|
-
|
4
|
-
attr_reader :preferences
|
5
|
-
def initialize store
|
6
|
-
@store = store
|
7
|
-
@preferences = store.preferences[:melhor_envio]
|
8
|
-
check
|
9
|
-
end
|
10
|
-
|
11
|
-
def check
|
12
|
-
return if @preferences[:client_id].blank? || @preferences[:client_secret].blank? || @preferences[:refresh_token].blank?
|
13
|
-
return if (@preferences[:token_expires_in]&.to_datetime - 1.day) > DateTime.now
|
14
|
-
refresh
|
15
|
-
end
|
16
|
-
|
17
|
-
def refresh
|
18
|
-
refresh_json = MeApi::Client.new.refresh_token(
|
19
|
-
client_id: @preferences[:client_id],
|
20
|
-
client_secret: @preferences[:client_secret],
|
21
|
-
refresh_token: @preferences[:refresh_token]
|
22
|
-
).json
|
23
|
-
@preferences[:access_token] = refresh_json["access_token"]
|
24
|
-
@preferences[:refresh_token] = refresh_json["refresh_token"]
|
25
|
-
@preferences[:token_expires_in] = (DateTime.now.utc + refresh_json["expires_in"].seconds).to_s
|
26
|
-
@store.update(preferences: { melhor_envio: @preferences })
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module StorePreferences
|
2
|
-
extend ActiveSupport::Concern
|
3
|
-
|
4
|
-
prepended do
|
5
|
-
require 'spree/preferences/persistable'
|
6
|
-
|
7
|
-
include Spree::Preferences::Persistable
|
8
|
-
|
9
|
-
preference :melhor_envio, :json, default: {
|
10
|
-
postal_code_from: "13870320",
|
11
|
-
services: ["SEDEX", "PAC", "Mini Envios"],
|
12
|
-
client_id: nil,
|
13
|
-
client_secret: nil,
|
14
|
-
redirect_url: nil,
|
15
|
-
state: nil,
|
16
|
-
access_token: nil,
|
17
|
-
refresh_token: nil,
|
18
|
-
token_expires_in: DateTime.now.to_s
|
19
|
-
}
|
20
|
-
end
|
21
|
-
|
22
|
-
::Spree::Store.prepend self
|
23
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
<%= form_with url: admin_melhor_envio_path("melhor_envio"), method: :put do |form| %>
|
2
|
-
<h3>Melhor Envio</h3>
|
3
|
-
|
4
|
-
<div style="margin-top: 10px;">
|
5
|
-
<%= form.label :postal_code_from, "CEP de Origem: " %>
|
6
|
-
<%= form.text_field :postal_code_from, value: @me_preferences[:postal_code_from] %>
|
7
|
-
</div>
|
8
|
-
|
9
|
-
<div style="margin-top: 10px;">
|
10
|
-
<%= form.hidden_field :services, value: @me_preferences[:services] %>
|
11
|
-
<span>Serviços Disponíveis: <%= @me_preferences[:services]&.join(", ") %></span>
|
12
|
-
</div>
|
13
|
-
|
14
|
-
<div style="margin-top: 10px;">
|
15
|
-
<%= form.label :client_id, "Client ID: " %>
|
16
|
-
<%= form.text_field :client_id, value: @me_preferences[:client_id] %>
|
17
|
-
</div>
|
18
|
-
|
19
|
-
<div style="margin-top: 10px;">
|
20
|
-
<%= form.label :client_secret, "Client Secret: " %>
|
21
|
-
<%= form.text_field :client_secret, value: @me_preferences[:client_secret] %>
|
22
|
-
</div>
|
23
|
-
|
24
|
-
<div style="margin-top: 10px;">
|
25
|
-
<%= form.label :redirect_url, "Redirect URL: " %>
|
26
|
-
<%= form.text_field :redirect_url, value: @me_preferences[:redirect_url] %>
|
27
|
-
</div>
|
28
|
-
|
29
|
-
<div style="margin-top: 10px;">
|
30
|
-
<%= form.label :state, "State: " %>
|
31
|
-
<%= form.text_field :state, value: @me_preferences[:state] %>
|
32
|
-
</div>
|
33
|
-
|
34
|
-
<div style="margin-top: 10px;">
|
35
|
-
<%= form.label :access_token, "Access Token: " %>
|
36
|
-
<%= form.text_field :access_token, value: @me_preferences[:access_token] %>
|
37
|
-
</div>
|
38
|
-
|
39
|
-
<div style="margin-top: 10px;">
|
40
|
-
<%= form.label :refresh_token, "Refresh Token: " %>
|
41
|
-
<%= form.text_field :refresh_token, value: @me_preferences[:refresh_token] %>
|
42
|
-
</div>
|
43
|
-
|
44
|
-
<div style="margin-top: 10px;">
|
45
|
-
<%= form.hidden_field :token_expires_in, value: @me_preferences[:token_expires_in] %>
|
46
|
-
<span>Expiração do token: <%= @me_preferences[:token_expires_in] %></span>
|
47
|
-
</div>
|
48
|
-
|
49
|
-
<%= form.submit "Salvar", style: "margin-top: 10px;" %>
|
50
|
-
<% end %>
|
51
|
-
|
52
|
-
<div>
|
53
|
-
<input value="https://www.melhorenvio.com.br/oauth/authorize?client_id=<%= @me_preferences[:client_id] %>&redirect_uri=<%= @me_preferences[:redirect_url] %>&response_type=code&state=<%= @me_preferences[:state] %>&scope=shipping-calculate">
|
54
|
-
<a target="_blank" class="" href="https://www.melhorenvio.com.br/oauth/authorize?client_id=<%= @me_preferences[:client_id] %>&redirect_uri=<%= @me_preferences[:redirect_url] %>&response_type=code&state=<%= @me_preferences[:state] %>&scope=shipping-calculate">
|
55
|
-
<svg style="width: 24px; height: 24px;" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
56
|
-
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 6H5.25A2.25 2.25 0 003 8.25v10.5A2.25 2.25 0 005.25 21h10.5A2.25 2.25 0 0018 18.75V10.5m-10.5 6L21 3m0 0h-5.25M21 3v5.25"></path></svg>
|
57
|
-
</a>
|
58
|
-
</div>
|