solidus_me 3.3.0 → 3.4.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: 67eaf235ef7a480092aac742313cb9f1746a56329e9a406b307768aef24f4422
4
- data.tar.gz: fd2becf059939f163cb8337a0df9ff89a623f219bd1383a28ebd679ac2453d2c
3
+ metadata.gz: ac3b73ea48e4461bf029a3882594562aaeffd9e1ac0617b92642f1c32bb731e5
4
+ data.tar.gz: 8570fb55a1623c0c13e721b7b7a5fb7085a6fb42a2411b5266549e032478966e
5
5
  SHA512:
6
- metadata.gz: dc32f4f9aff29da78361eb4fedca2103c7fe96c6766cd142a9ba0cc83a4a5887404186c23a96ddbb5f2316164d9114d9e267778010624af8333b14bea74aaef8
7
- data.tar.gz: 9167dc1d2b02eb20ede01003a079e40fdafeec9f554547baa0896ce734bd3ea40d0ee6f1e4160fff952090e4ec2b41255fda1c9a3a8bdaf74a4756d519542905
6
+ metadata.gz: d71249d412670d8d4a2af11164a9ab2180abcd8022a88b77ebd925c53557ccd54c71f8b5ff12a2b684c7df8b11d0b2f8f49fdda145bed1b48582b4974e01df3c
7
+ data.tar.gz: 0ac0090dd26a8002a7f2c57a19a6685779caaf0b65133f6c9893de6ac44e3b760fdd311eba90219316a4ffef6b77431a3cb2aef4e7d421ac30096942e2bbb719
@@ -40,7 +40,6 @@ module SolidusMe
40
40
 
41
41
  @me_account.check_token
42
42
  @me_client = MeApi::Client.new(@me_account.access_token)
43
-
44
43
  melhor_envio_rates = get_rates_from_melhor_envio(package)
45
44
  shipping_rates = melhor_envio_rates.map do |melhor_envio_rate|
46
45
  build_shipping_rate(melhor_envio_rate, package)
@@ -49,19 +48,90 @@ module SolidusMe
49
48
  shipping_rates
50
49
  end
51
50
 
51
+ def insert_package_into_cart from
52
+ me_client = set_me_client
53
+ order = package.order
54
+ variants = order.variants
55
+ line_items = order.line_items
56
+ address = order.ship_address
57
+ weight = package.weight
58
+ dimensions = estimate_dimensions_from_package(package)
59
+
60
+ shipping_method_name = @shipping_methods.sole.name
61
+ service_id = SHIPPING_METHODS.filter { _1[:name] == shipping_method_name }.sole[:external_id]
62
+
63
+ to = {
64
+ name: address.name,
65
+ phone: address.phone,
66
+ email: order.email,
67
+ document: order.tax_id,
68
+ address: address.address1,
69
+ complement: address.address2,
70
+ number: address.number,
71
+ district: address.district,
72
+ city: address.city,
73
+ postal_code: address.zipcode,
74
+ state_abbr: address.state.abbr
75
+ }
76
+
77
+ products = []
78
+ line_items.each do |line_item|
79
+ products << {
80
+ name: line_item.name,
81
+ quantity: line_item.quantity,
82
+ unitary_value: line_item.price
83
+ }
84
+ end
85
+
86
+ volumes = [{
87
+ width: dimensions[0].to_i.round(2),
88
+ height: dimensions[1].to_i.round(2),
89
+ length: dimensions[2].to_i.round(2),
90
+ weight: weight
91
+ }]
92
+
93
+ me_client.insert_package_into_cart(service_id:, from:, to:, products:, volumes:)
94
+ end
95
+
96
+ def payment_ticket(ticket_id)
97
+ me_client = set_me_client
98
+ me_client.payment_ticket ticket_id
99
+ end
100
+
101
+ def generate_ticket(ticket_id)
102
+ me_client = set_me_client
103
+ me_client.generate_ticket ticket_id
104
+ end
105
+
106
+ def print_ticket(ticket_id)
107
+ me_client = set_me_client
108
+ me_client.print_ticket ticket_id
109
+ end
110
+
52
111
  private
53
112
 
113
+ def set_me_client
114
+ me_account = Account.first
115
+ return if me_account.blank?
116
+
117
+ me_account.check_token
118
+ MeApi::Client.new(me_account.access_token)
119
+ end
120
+
54
121
  def get_rates_from_melhor_envio(package)
55
122
  price = package.contents.map { |content| content.price }.sum
56
123
  weight = package.weight
124
+ dimensions = estimate_dimensions_from_package(package)
57
125
 
58
126
  zipcode = package.order.ship_address.zipcode
59
127
  available_shipping_method_codes = @shipping_methods.pluck(:code)
60
-
61
128
  rates = @me_client.rates(
62
129
  from: @me_account.postal_code_from,
63
130
  to: zipcode,
64
131
  weight_kg: (weight / 1000.00),
132
+ width_cm: dimensions[0].to_i.round(2),
133
+ height_cm: dimensions[1].to_i.round(2),
134
+ length_cm: dimensions[2].to_i.round(2),
65
135
  contents_value_brl: price
66
136
  )
67
137
  rates.select { |rate| rate.price > 0 && available_shipping_method_codes.include?(self.class.generate_code(rate.id)) }
@@ -69,6 +139,19 @@ module SolidusMe
69
139
  []
70
140
  end
71
141
 
142
+ def estimate_dimensions_from_package(package)
143
+ volume = package.contents.sum { |item| dimensions_from_item(item).reduce(:*) }
144
+ cube_side = Math.cbrt(volume)
145
+ [cube_side, cube_side, cube_side]
146
+ end
147
+
148
+ def dimensions_from_item(item)
149
+ width = item.variant.width || 20
150
+ height = item.variant.height || 10
151
+ depth = item.variant.depth || 10
152
+ [width, height, depth]
153
+ end
154
+
72
155
  def build_shipping_rate(melhor_envio_rate, package)
73
156
  return unless melhor_envio_rate.max_delivery_time
74
157
  shipping_method = Spree::ShippingMethod.find_by(code: self.class.generate_code(melhor_envio_rate.id))
@@ -1,10 +1,10 @@
1
1
  <div class="field">
2
2
  <div style="display: flex; align-items: center; margin-bottom: 6px">
3
3
  <label style="margin-right: 5px; margin-bottom: 0" for="me_url">URL de autorização</label>
4
- <a target="_blank" 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">
4
+ <a target="_blank" 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 cart-read cart-write companies-read companies-write coupons-read coupons-write notifications-read orders-read products-read products-write purchases-read shipping-calculate shipping-cancel shipping-checkout shipping-companies shipping-generate shipping-preview shipping-print shipping-share shipping-tracking ecommerce-shipping transactions-read users-read users-write">
5
5
  <svg style="width: 18px; height: 18px; color: black" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
6
6
  <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>
7
7
  </a>
8
8
  </div>
9
9
  <input id="me_url" class="fullwidth" 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">
10
- </div>
10
+ </div>
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusMe
4
- VERSION = "3.3.0"
4
+ VERSION = "3.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_me
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hamilton Tumenas Borges
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-12-18 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: me_api
@@ -81,7 +80,6 @@ homepage: https://github.com/ulysses-bull/solidus_me#readme
81
80
  licenses:
82
81
  - BSD-3-Clause
83
82
  metadata: {}
84
- post_install_message:
85
83
  rdoc_options: []
86
84
  require_paths:
87
85
  - lib
@@ -96,8 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
94
  - !ruby/object:Gem::Version
97
95
  version: '0'
98
96
  requirements: []
99
- rubygems_version: 3.5.3
100
- signing_key:
97
+ rubygems_version: 3.6.9
101
98
  specification_version: 4
102
99
  summary: Gem para integração com a API do Melhor Envio
103
100
  test_files: []