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

Add configuration options to SolidusPaypalCommercePlatform #50

Merged
merged 2 commits into from
Jul 10, 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
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ bundle exec rails g solidus_paypal_commerce_platform:install

This extension will automatically selecte a PayPal environment based on Rails environment so that "production" will be mapped to the "live" PayPal environment, and everything else will be routed to the "sandbox" PayPal environment.

If you want to override these values you can either set `SolidusPaypalCommercePlatform.env` to `"live"` or `"sandbox"` inside an initializer. Or, alternatively, you can set the `PAYPAL_ENV` environment variable to one of the same two values.
If you want to override these values you can either set `SolidusPaypalCommercePlatform.config.env` to `"live"` or `"sandbox"` inside an initializer. Or, alternatively, you can set the `PAYPAL_ENV` environment variable to one of the same two values.

### Custom PartnerID, PartnerClientID, and Nonce

You can declare your PayPal Partner-ID and Partner-Client-ID as environment
variables or set their values directly on `SolidusPaypalCommercePlatform`.
variables or set their values directly on `SolidusPaypalCommercePlatform.config`
or by using the configuration method detailed in the Customization section.

#### Use ENV variables

Expand All @@ -96,9 +97,9 @@ export PAYPAL_PARTNER_CLIENT_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#### Set them directly

```ruby
SolidusPaypalCommercePlatform.nonce = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbdb84"
SolidusPaypalCommercePlatform.partner_id = "xxxxxxxxxxKG2"
SolidusPaypalCommercePlatform.partner_client_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxVSX"
SolidusPaypalCommercePlatform.config.nonce = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbdb84"
SolidusPaypalCommercePlatform.config.partner_id = "xxxxxxxxxxKG2"
SolidusPaypalCommercePlatform.config.partner_client_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxVSX"
```

Address Phone Number Validation
Expand All @@ -112,6 +113,22 @@ B) Collect the users phone number seperately

and then override the `Spree::Address` method `require_phone?` to return `true`.

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
```

Testing
-------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ShippingRatesController < ::Spree::Api::BaseController

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

if simulated_order.ship_address.valid?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def payment_method_params
type: SolidusPaypalCommercePlatform::PaymentMethod,
preferred_client_id: api_credentials.client_id,
preferred_client_secret: api_credentials.client_secret,
preferred_test_mode: SolidusPaypalCommercePlatform.env.sandbox?
preferred_test_mode: SolidusPaypalCommercePlatform.config.env.sandbox?
}
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/solidus_paypal_commerce_platform/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def trade_tokens(auth_code:, nonce:)

@client.execute(FetchMerchantCredentialsRequest.new(
access_token: access_token,
partner_merchant_id: SolidusPaypalCommercePlatform.partner_id,
partner_merchant_id: SolidusPaypalCommercePlatform.config.partner_id,
)).result
end

Expand Down
8 changes: 4 additions & 4 deletions app/models/solidus_paypal_commerce_platform/wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ def partial_name
def button_url
parameters = {
product: "addipmt",
partnerId: SolidusPaypalCommercePlatform.partner_id,
partnerClientId: SolidusPaypalCommercePlatform.partner_client_id,
partnerId: SolidusPaypalCommercePlatform.config.partner_id,
partnerClientId: SolidusPaypalCommercePlatform.config.partner_client_id,
features: "PAYMENT,REFUND",
partnerLogoUrl: logo,
integrationType: "FO",
displayMode: "minibrowser",
sellerNonce: SolidusPaypalCommercePlatform.nonce
sellerNonce: SolidusPaypalCommercePlatform.config.nonce
}

URI("https://#{SolidusPaypalCommercePlatform.env_domain}/bizsignup/partner/entry?#{parameters.to_query}")
URI("https://#{SolidusPaypalCommercePlatform.config.env_domain}/bizsignup/partner/entry?#{parameters.to_query}")
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<%= t('sign_up_for_paypal') %>
</button>
</a>
<script id="paypal-js" src="https://<%= SolidusPaypalCommercePlatform.env_domain %>/webapps/merchantboarding/js/lib/lightbox/partner.js"></script>
<script id="paypal-js" src="https://<%= SolidusPaypalCommercePlatform.config.env_domain %>/webapps/merchantboarding/js/lib/lightbox/partner.js"></script>

<script>
window.SolidusPaypalCommercePlatform.nonce = "<%= SolidusPaypalCommercePlatform.nonce %>"
window.SolidusPaypalCommercePlatform.nonce = "<%= SolidusPaypalCommercePlatform.config.nonce %>"
window.SolidusPaypalCommercePlatform_onboardCallback = SolidusPaypalCommercePlatform.onboardCallback
</script>
14 changes: 13 additions & 1 deletion lib/solidus_paypal_commerce_platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
require 'solidus_support'

require 'solidus_paypal_commerce_platform/version'
require 'solidus_paypal_commerce_platform/environment'
require 'solidus_paypal_commerce_platform/configuration'
require 'solidus_paypal_commerce_platform/client'
require 'solidus_paypal_commerce_platform/engine'

module SolidusPaypalCommercePlatform
class << self
def config
@config ||= Configuration.new
end

def configure
yield config
end
end
end
2 changes: 1 addition & 1 deletion lib/solidus_paypal_commerce_platform/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Client
attr_reader :environment

def initialize(test_mode: nil, client_id:, client_secret: "")
test_mode = SolidusPaypalCommercePlatform.env.sandbox? if test_mode.nil?
test_mode = SolidusPaypalCommercePlatform.config.env.sandbox? if test_mode.nil?
env_class = test_mode ? PayPal::SandboxEnvironment : PayPal::LiveEnvironment

@environment = env_class.new(client_id, client_secret)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
require 'paypal-checkout-sdk'

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

DEFAULT_NONCE = {
Expand All @@ -21,6 +22,12 @@ module Environment
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 Expand Up @@ -70,6 +77,4 @@ def partner_client_id
@partner_client_id ||= ENV['PAYPAL_PARTNER_CLIENT_ID'] || DEFAULT_PARTNER_CLIENT_ID[env.to_sym]
end
end

extend Environment
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

RSpec.describe SolidusPaypalCommercePlatform::Environment do
let(:subject) { Class.new.extend described_class }
RSpec.describe SolidusPaypalCommercePlatform::Configuration do
let(:subject) { described_class.new }

describe '#default_env' do
it "uses ENV['PAYPAL_ENV'] when present" do
Expand Down Expand Up @@ -64,4 +64,22 @@
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