Skip to content

Restore easypost functionality #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,55 @@ orbs:
solidusio_extensions: solidusio/extensions@volatile

jobs:
run-specs-with-postgres:
executor: solidusio_extensions/postgres
run-specs:
parameters:
solidus:
type: string
default: main
db:
type: string
default: "postgres"
ruby:
type: string
default: "3.2"
executor:
name: solidusio_extensions/<< parameters.db >>
ruby_version: << parameters.ruby >>
steps:
- solidusio_extensions/run-tests
run-specs-with-mysql:
executor: solidusio_extensions/mysql
steps:
- solidusio_extensions/run-tests
lint-code:
executor: solidusio_extensions/sqlite-memory
steps:
- solidusio_extensions/lint-code
- checkout
- solidusio_extensions/run-tests-solidus-<< parameters.solidus >>

workflows:
"Run specs on supported Solidus versions":
jobs:
- run-specs-with-postgres
- run-specs-with-mysql
- lint-code
- run-specs:
name: &name "run-specs-solidus-<< matrix.solidus >>-ruby-<< matrix.ruby >>-db-<< matrix.db >>"
matrix:
parameters: { solidus: ["main"], ruby: ["3.2"], db: ["postgres"] }
- run-specs:
name: *name
matrix:
parameters: { solidus: ["current"], ruby: ["3.1"], db: ["mysql"] }
- run-specs:
name: *name
matrix:
parameters: { solidus: ["older"], ruby: ["3.0"], db: ["sqlite"] }

"Weekly run specs against master":
"Weekly run specs against main":
triggers:
- schedule:
cron: "0 0 * * 4" # every Thursday
filters:
branches:
only:
- master
- main
jobs:
- run-specs-with-postgres
- run-specs-with-mysql
- run-specs:
name: *name
matrix:
parameters: { solidus: ["main"], ruby: ["3.2"], db: ["postgres"] }
- run-specs:
name: *name
matrix:
parameters: { solidus: ["current"], ruby: ["3.1"], db: ["mysql"] }

28 changes: 19 additions & 9 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

branch = ENV.fetch('SOLIDUS_BRANCH', 'master')
solidus_git, solidus_frontend_git = if (branch == 'master') || (branch >= 'v3.2')
%w[solidusio/solidus solidusio/solidus_frontend]
else
%w[solidusio/solidus] * 2
end
gem 'solidus', github: solidus_git, branch: branch
gem 'solidus_frontend', github: solidus_frontend_git, branch: branch
solidus_branch = ENV.fetch('SOLIDUS_BRANCH', 'main')
gem 'solidus', github: 'solidusio/solidus', branch: solidus_branch

# The solidus_frontend gem has been pulled out since v3.2
if solidus_branch >= 'v3.2'
gem 'solidus_frontend'
elsif solidus_branch == 'main'
gem 'solidus_frontend', github: 'solidusio/solidus_frontend'
else
gem 'solidus_frontend', github: 'solidusio/solidus', branch: solidus_branch
end

# Needed to help Bundler figure out how to resolve dependencies,
# otherwise it takes forever to resolve them.
Expand All @@ -26,7 +29,14 @@ when 'mysql'
when 'postgresql'
gem 'pg'
else
gem 'sqlite3'
gem 'sqlite3', '~> 1.4'
end

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3')
# Fix for Rails 7+ / Ruby 3+, see https://stackoverflow.com/a/72474475
gem 'net-imap', require: false
gem 'net-pop', require: false
gem 'net-smtp', require: false
end

gemspec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def easypost_tracker
return @easypost_tracker if @easypost_tracker

if easy_post_tracker_id.present?
@easypost_tracker = EasyPost::Tracker.retrieve(easy_post_tracker_id)
@easypost_tracker = SolidusEasypost.client.tracker.retrieve(easy_post_tracker_id)
else
@easypost_tracker = EasyPost::Tracker.create(
@easypost_tracker = SolidusEasypost.client.tracker.create(
tracking_code: tracking,
carrier: shipping_method.carrier,
)
Expand Down
21 changes: 18 additions & 3 deletions app/decorators/models/solidus_easypost/spree/shipment_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,37 @@ def self.prepended(base)
def easypost_shipment
return unless selected_easy_post_shipment_id

@easypost_shipment ||= ::EasyPost::Shipment.retrieve(selected_easy_post_shipment_id)
@easypost_shipment ||= SolidusEasypost.client.shipment.retrieve(selected_easy_post_shipment_id)
end

def easypost_postage_label_url
easypost_shipment&.postage_label&.label_url
end

def select_shipping_method(shipping_method)
estimator = ::Spree::Config.stock.estimator_class.new
rates = estimator.shipping_rates(to_package, false)
rate = rates.detect { |detected| detected.shipping_method_id == shipping_method.id }
deselect_other_shipping_rates(rate.id)
rate.update(selected: true)
end

private

def deselect_other_shipping_rates(selected_rate_id)
shipping_rates.where.not(id: selected_rate_id).update_all(selected: false)
end

def buy_easypost_rate
return if tracking

easypost_shipment_id = easypost_shipment.id

rate = easypost_shipment.rates.find do |easypost_rate|
easypost_rate.id == selected_easy_post_rate_id
end

easypost_shipment.buy(rate)

SolidusEasypost.client.shipment.buy(easypost_shipment_id, rate: { id: rate.id })
self.tracking = easypost_shipment.tracking_code
end

Expand Down
6 changes: 4 additions & 2 deletions app/models/solidus_easypost/return_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ def easypost_shipment
end

def return_label(rate)
easypost_shipment.buy(rate) unless easypost_shipment.postage_label
return if easypost_shipment.postage_label

easypost_shipment.postage_label
return_shipment = SolidusEasypost.client.shipment.buy(easypost_shipment.id, rate: { id: rate.id } )

return_shipment.postage_label
end
end
end
2 changes: 1 addition & 1 deletion bin/sandbox
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if [ ! -z $SOLIDUS_BRANCH ]
then
BRANCH=$SOLIDUS_BRANCH
else
BRANCH="master"
BRANCH="main"
fi

extension_name="solidus_easypost"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

EasyPost.api_key = 'YOUR_API_KEY_HERE'

SolidusEasypost.configure do |config|
# API Key
config.api_key = 'YOUR_API_KEY_HERE'

# Purchase labels from EasyPost when shipping shipments in Solidus?
# config.purchase_labels = true

Expand Down
3 changes: 3 additions & 0 deletions lib/solidus_easypost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@

module SolidusEasypost
class << self
attr_reader :client

def configuration
@configuration ||= Configuration.new
end

def configure
yield configuration
@client = ::EasyPost::Client.new(api_key: @configuration.api_key)
end
end
end
4 changes: 2 additions & 2 deletions lib/solidus_easypost/address_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def from_address(address, options = {})
attributes[:state] = address.state ? address.state.abbr : address.state_name
attributes[:country] = address.country&.iso

::EasyPost::Address.create attributes.merge(options)
SolidusEasypost.client.address.create attributes.merge(options)
end

def from_stock_location(stock_location, options = {})
Expand All @@ -40,7 +40,7 @@ def from_stock_location(stock_location, options = {})
attributes[:state] = stock_location.state ? stock_location.state.abbr : stock_location.state_name
attributes[:country] = stock_location.country&.iso

::EasyPost::Address.create attributes.merge(options)
SolidusEasypost.client.address.create attributes.merge(options)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/solidus_easypost/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module SolidusEasypost
class Configuration
attr_accessor :purchase_labels, :track_all_cartons
attr_accessor :api_key, :purchase_labels, :track_all_cartons
attr_writer :shipping_rate_calculator_class, :shipping_method_selector_class, :parcel_dimension_calculator_class,
:webhook_handler_class

Expand Down
12 changes: 9 additions & 3 deletions lib/solidus_easypost/estimator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
module SolidusEasypost
class Estimator
def shipping_rates(package, _frontend_only = true)
easypost_rates = ShipmentBuilder.from_package(package).rates.sort_by(&:rate)
shipment = package.shipment

shipping_rates = easypost_rates.map { |rate| build_shipping_rate(rate) }.compact
shipping_rates.min_by(&:cost)&.selected = true
if shipment.easypost_shipment
shipping_rates = shipment.shipping_rates
else
easypost_rates = ShipmentBuilder.from_package(package).rates.sort_by(&:rate)

shipping_rates = easypost_rates.map { |rate| build_shipping_rate(rate) }.compact
shipping_rates.min_by(&:cost)&.selected = true
end

shipping_rates
end
Expand Down
4 changes: 2 additions & 2 deletions lib/solidus_easypost/parcel_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def from_package(package)
.new
.compute(package)

::EasyPost::Parcel.create(parcel_dimension.to_h)
SolidusEasypost.client.parcel.create(parcel_dimension.to_h)
end

def from_return_authorization(return_authorization)
Expand All @@ -20,7 +20,7 @@ def from_return_authorization(return_authorization)
.new
.compute(return_authorization)

::EasyPost::Parcel.create(parcel_dimension.to_h)
SolidusEasypost.client.parcel.create(parcel_dimension.to_h)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/solidus_easypost/shipment_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ShipmentBuilder
class << self
def from_package(package, options = {})
from_address, to_address = address_options(options)
::EasyPost::Shipment.create(
SolidusEasypost.client.shipment.create(
to_address: AddressBuilder.from_address(package.order.ship_address, to_address || {}),
from_address: AddressBuilder.from_stock_location(package.stock_location, from_address || {}),
parcel: ParcelBuilder.from_package(package),
Expand All @@ -15,7 +15,7 @@ def from_package(package, options = {})

def from_shipment(shipment, options = {})
from_address, to_address = address_options(options)
::EasyPost::Shipment.create(
SolidusEasypost.client.shipment.create(
to_address: AddressBuilder.from_address(shipment.order.ship_address, to_address || {}),
from_address: AddressBuilder.from_stock_location(shipment.stock_location, from_address || {}),
parcel: ParcelBuilder.from_package(shipment.to_package),
Expand All @@ -25,7 +25,7 @@ def from_shipment(shipment, options = {})

def from_return_authorization(return_authorization, options = {})
from_address, to_address = address_options(options)
::EasyPost::Shipment.create(
SolidusEasypost.client.shipment.create(
from_address: AddressBuilder.from_stock_location(return_authorization.stock_location, from_address || {}),
to_address: AddressBuilder.from_address(return_authorization.order.ship_address, to_address || {}),
parcel: ParcelBuilder.from_return_authorization(return_authorization),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
factory :variant do
weight { 10.0 }
end
end
end
4 changes: 2 additions & 2 deletions solidus_easypost.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Gem::Specification.new do |spec|

spec.add_dependency 'deface'
spec.add_dependency 'easypost'
spec.add_dependency 'solidus_core', '>= 2.0.0'
spec.add_dependency 'solidus_support', '~> 0.9'
spec.add_dependency 'solidus_core', ['>= 2.0.0', '< 5']
spec.add_dependency 'solidus_support', '~> 0.13'

spec.add_development_dependency 'solidus_dev_support', '~> 2.1'
spec.add_development_dependency 'vcr'
Expand Down
Loading