Skip to content
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

Replace order simulator with something simpler #82

Merged
merged 2 commits into from
Aug 4, 2020
Merged
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
15 changes: 0 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,6 @@ end

The instances of your wizard class should respond to `#name` and `#partial_name`, where `partial_name` will return the path to the partial you'd like to display on the wizard setup section. In our case, we just display a button to direct the user to PayPal.

## Customization

You can customize some of the settings in this app with the `configure`
method in an initializer. For instance, if you'd prefer to use your own order
simulator to simulate taxes & shipping rates from a custom address, you can set
the order_simulator_class like this:

```ruby
# config/initializers/use_my_simulator.rb

SolidusPaypalCommercePlatform.configure do |config|
config.order_simulator_class = "MyApp::MyOrderSimulator"
end
```

## Backend Payments

PayPals API does not allow for admin-side payments. Instead, backend users taking payments for customers will need to use the PayPal Virtual Terminal to take payments. [More info is available on the PayPal website.](https://www.paypal.com/merchantapps/appcenter/acceptpayments/virtualterminal?locale.x=en_US)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def update_address
paypal_address = SolidusPaypalCommercePlatform::PaypalAddress.new(@order)

if paypal_address.update(paypal_address_params).valid?
@order.ensure_updated_shipments
@order.contents.advance
render json: {}, status: :ok
else
render json: paypal_address.errors.full_messages, status: :unprocessable_entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ class ShippingRatesController < ::Spree::Api::BaseController

def simulate_shipping_rates
authorize! :show, @order, order_token
order_simulator = SolidusPaypalCommercePlatform.config.order_simulator_class.new(@order)
simulated_order = order_simulator.simulate_with_address(params[:address])

if simulated_order.ship_address.valid?
render json: SolidusPaypalCommercePlatform::PaypalOrder.new(simulated_order).to_replace_json, status: :ok
else
render json: simulated_order.ship_address.errors.full_messages, status: :unprocessable_entity
@order.transaction do
SolidusPaypalCommercePlatform::PaypalAddress.new(@order).simulate_update(params[:address])
@paypal_order = SolidusPaypalCommercePlatform::PaypalOrder.new(@order).to_replace_json
raise ActiveRecord::Rollback
end

render json: @paypal_order, status: :ok
end

private
Expand Down
68 changes: 0 additions & 68 deletions app/models/solidus_paypal_commerce_platform/order_simulator.rb

This file was deleted.

22 changes: 22 additions & 0 deletions app/models/solidus_paypal_commerce_platform/paypal_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ def initialize(order)
@order = order
end

def simulate_update(paypal_address)
@order.update(ship_address: format_simulated_address(paypal_address))
@order.ensure_updated_shipments
@order.email = "[email protected]" unless @order.email
@order.contents.advance
end

def update(paypal_address)
formatted_address = format_address(paypal_address)
new_address = @order.ship_address.dup || ::Spree::Address.new
Expand All @@ -32,6 +39,21 @@ def add_email_to_order(recipient)
@order.update(email: recipient[:email_address])
end

def format_simulated_address(paypal_address)
country = ::Spree::Country.find_by(iso: paypal_address[:country_code])
# Also adds fake information for a few fields, so validations can run
::Spree::Address.new(
city: paypal_address[:city],
state: country.states.find_by(abbr: paypal_address[:state]),
state_name: paypal_address[:state],
zipcode: paypal_address[:postal_code],
country: country,
address1: "123 Fake St.",
phone: "123456789",
firstname: "Fake"
)
end

def format_address(paypal_address)
address = paypal_address[:updated_address]
recipient = paypal_address[:recipient]
Expand Down
7 changes: 0 additions & 7 deletions lib/solidus_paypal_commerce_platform/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

module SolidusPaypalCommercePlatform
class Configuration
attr_writer :order_simulator_class
InvalidEnvironment = Class.new(StandardError)

DEFAULT_NONCE = {
Expand All @@ -22,12 +21,6 @@ class Configuration
live: "TBD",
}.freeze

def order_simulator_class
self.order_simulator_class = "SolidusPaypalCommercePlatform::OrderSimulator" unless @order_simulator_class

@order_simulator_class.constantize
end

def env=(value)
unless %w[live sandbox].include? value
raise InvalidEnvironment, "#{value} is not a valid environment"
Expand Down
18 changes: 0 additions & 18 deletions spec/lib/solidus_paypal_commerce_platform/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,4 @@
expect(subject.env_domain).to eq("www.sandbox.paypal.com")
end
end

describe "#order_simulator_class" do
before do
stub_const('SolidusPaypalCommercePlatform::BetterOrderSimulator', Class.new)
end

it "returns a class" do
expect(subject.order_simulator_class).to be_kind_of(Class)
end

it "is settable" do
expect(subject.order_simulator_class).to eq(SolidusPaypalCommercePlatform::OrderSimulator)

subject.order_simulator_class = "SolidusPaypalCommercePlatform::BetterOrderSimulator"

expect(subject.order_simulator_class).to eq(SolidusPaypalCommercePlatform::BetterOrderSimulator)
end
end
end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper'

RSpec.describe SolidusPaypalCommercePlatform::ShippingRatesController, type: :request do
stub_authorization!
let(:new_country) { create(:country, iso: "IT") }
let(:new_state) { create(:state, country: new_country) }
let(:new_address) { create(:address, country: new_country, state: new_state) }
let(:order) { Spree::TestingSupport::OrderWalkthrough.up_to(:payment) }
let(:paypal_address) {
{
country_code: new_country.iso,
city: new_address.city,
state: new_state.abbr,
postal_code: new_address.zipcode
}
}

describe "GET /simulate_shipping_rates" do
before do
get solidus_paypal_commerce_platform.shipping_rates_path, params: {
order_id: order.number,
address: paypal_address
}
end

it "returns a paypal_order with the new address" do
expect(response.body).to include new_address.state.abbr
expect(response.body).not_to include order.ship_address.state.abbr
end

it "does not modify original address" do
expect(order.ship_address).not_to eq new_address
end
end
end