solidus_paypal_commerce_platform 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +41 -0
  3. data/.gem_release.yml +5 -0
  4. data/.github/stale.yml +17 -0
  5. data/.gitignore +20 -0
  6. data/.rspec +2 -0
  7. data/.rubocop.yml +56 -0
  8. data/Gemfile +33 -0
  9. data/LICENSE +26 -0
  10. data/README.md +173 -0
  11. data/Rakefile +6 -0
  12. data/app/assets/javascripts/spree/backend/solidus_paypal_commerce_platform.js +2 -0
  13. data/app/assets/javascripts/spree/backend/solidus_paypal_commerce_platform/namespace.js +1 -0
  14. data/app/assets/javascripts/spree/backend/solidus_paypal_commerce_platform/wizard.js +28 -0
  15. data/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform.js +3 -0
  16. data/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/button_actions.js +191 -0
  17. data/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/buttons.js +26 -0
  18. data/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/namespace.js +1 -0
  19. data/app/assets/stylesheets/spree/backend/solidus_paypal_commerce_platform.css +4 -0
  20. data/app/assets/stylesheets/spree/frontend/solidus_paypal_commerce_platform.css +17 -0
  21. data/app/controllers/solidus_paypal_commerce_platform/orders_controller.rb +83 -0
  22. data/app/controllers/solidus_paypal_commerce_platform/payments_controller.rb +44 -0
  23. data/app/controllers/solidus_paypal_commerce_platform/paypal_orders_controller.rb +19 -0
  24. data/app/controllers/solidus_paypal_commerce_platform/shipping_rates_controller.rb +31 -0
  25. data/app/controllers/solidus_paypal_commerce_platform/wizard_controller.rb +47 -0
  26. data/app/decorators/solidus_paypal_commerce_platform/remove_required_phone_from_address.rb +13 -0
  27. data/app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb +21 -0
  28. data/app/jobs/solidus_paypal_commerce_platform/application_job.rb +6 -0
  29. data/app/jobs/solidus_paypal_commerce_platform/webhook_job.rb +22 -0
  30. data/app/models/solidus_paypal_commerce_platform/gateway.rb +88 -0
  31. data/app/models/solidus_paypal_commerce_platform/payment_method.rb +75 -0
  32. data/app/models/solidus_paypal_commerce_platform/payment_source.rb +27 -0
  33. data/app/models/solidus_paypal_commerce_platform/paypal_address.rb +81 -0
  34. data/app/models/solidus_paypal_commerce_platform/paypal_order.rb +107 -0
  35. data/app/models/solidus_paypal_commerce_platform/pricing_options.rb +9 -0
  36. data/app/models/solidus_paypal_commerce_platform/wizard.rb +40 -0
  37. data/app/overrides/spree/admin/payment_methods/form/insert_email_verification_notification.rb +9 -0
  38. data/app/overrides/spree/admin/payment_methods/index/insert_setup_wizards.rb +17 -0
  39. data/app/overrides/spree/admin/payments/log_entries/add_paypal_debug_id.rb +9 -0
  40. data/app/overrides/spree/orders/edit/insert_cart_buttons.rb +9 -0
  41. data/app/overrides/spree/payments/payment/add_paypal_email_to_payment.rb +9 -0
  42. data/app/overrides/spree/products/cart_form/insert_product_buttons.rb +10 -0
  43. data/app/views/solidus_paypal_commerce_platform/admin/payment_methods/_available_to_users.html.erb +6 -0
  44. data/app/views/solidus_paypal_commerce_platform/admin/payment_methods/_paypal_wizard.html.erb +16 -0
  45. data/app/views/solidus_paypal_commerce_platform/admin/payment_methods/_setup_wizards.html.erb +16 -0
  46. data/app/views/solidus_paypal_commerce_platform/admin/payments/_log_entries.html.erb +32 -0
  47. data/bin/console +17 -0
  48. data/bin/rails +7 -0
  49. data/bin/rails-engine +13 -0
  50. data/bin/rails-sandbox +18 -0
  51. data/bin/rake +7 -0
  52. data/bin/sandbox +86 -0
  53. data/bin/setup +8 -0
  54. data/codecov.yml +14 -0
  55. data/config/locales/en.yml +37 -0
  56. data/config/routes.rb +12 -0
  57. data/db/migrate/20200521190038_add_paypal_commerce_platform_sources.rb +13 -0
  58. data/lib/generators/solidus_paypal_commerce_platform/install/install_generator.rb +38 -0
  59. data/lib/solidus_paypal_commerce_platform.rb +21 -0
  60. data/lib/solidus_paypal_commerce_platform/access_token_authorization_request.rb +21 -0
  61. data/lib/solidus_paypal_commerce_platform/client.rb +84 -0
  62. data/lib/solidus_paypal_commerce_platform/configuration.rb +64 -0
  63. data/lib/solidus_paypal_commerce_platform/engine.rb +45 -0
  64. data/lib/solidus_paypal_commerce_platform/factories.rb +14 -0
  65. data/lib/solidus_paypal_commerce_platform/fetch_merchant_credentials_request.rb +17 -0
  66. data/lib/solidus_paypal_commerce_platform/version.rb +5 -0
  67. data/lib/views/api/spree/api/payments/source_views/_paypal_commerce_platform.json.jbuilder +3 -0
  68. data/lib/views/backend/spree/admin/payments/source_forms/_paypal_commerce_platform.html.erb +1 -0
  69. data/lib/views/backend/spree/admin/payments/source_views/_paypal_commerce_platform.html.erb +18 -0
  70. data/lib/views/backend/spree/admin/shared/preference_fields/_paypal_select.html.erb +13 -0
  71. data/lib/views/frontend/solidus_paypal_commerce_platform/cart/_cart_buttons.html.erb +3 -0
  72. data/lib/views/frontend/solidus_paypal_commerce_platform/payments/_payment.html.erb +4 -0
  73. data/lib/views/frontend/solidus_paypal_commerce_platform/product/_product_buttons.html.erb +3 -0
  74. data/lib/views/frontend/spree/checkout/payment/_paypal_commerce_platform.html.erb +13 -0
  75. data/lib/views/frontend/spree/orders/payment/_paypal_commerce_platform.html.erb +17 -0
  76. data/lib/views/frontend/spree/products/payment/_paypal_commerce_platform.html.erb +15 -0
  77. data/solidus_paypal_commerce_platform.gemspec +39 -0
  78. data/spec/features/backend/new_payment_method_spec.rb +40 -0
  79. data/spec/features/frontend/cart_spec.rb +38 -0
  80. data/spec/features/frontend/checkout_spec.rb +81 -0
  81. data/spec/features/frontend/product_spec.rb +79 -0
  82. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/CHECKOUT.ORDER.COMPLETED.v2.json +121 -0
  83. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/CHECKOUT.ORDER.PROCESSED.v2.json +121 -0
  84. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.COMPLETED.v1.json +50 -0
  85. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.COMPLETED.v2.json +72 -0
  86. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.DENIED.v1.json +50 -0
  87. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.DENIED.v2.json +68 -0
  88. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.REFUNDED.v1.json +51 -0
  89. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.REFUNDED.v2.json +63 -0
  90. data/spec/jobs/solidus_paypal_commerce_platform/webhook_job_spec.rb +44 -0
  91. data/spec/lib/solidus_paypal_commerce_platform/client_spec.rb +21 -0
  92. data/spec/lib/solidus_paypal_commerce_platform/configuration_spec.rb +67 -0
  93. data/spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb +102 -0
  94. data/spec/models/solidus_paypal_commerce_platform/payment_source_spec.rb +62 -0
  95. data/spec/models/solidus_paypal_commerce_platform/paypal_address_spec.rb +55 -0
  96. data/spec/models/solidus_paypal_commerce_platform/wizard_spec.rb +9 -0
  97. data/spec/requests/solidus_paypal_commerce_platform/orders_controller_spec.rb +36 -0
  98. data/spec/requests/solidus_paypal_commerce_platform/shipping_rates_controller_spec.rb +44 -0
  99. data/spec/requests/solidus_paypal_commerce_platform/wizard_controller_spec.rb +44 -0
  100. data/spec/spec_helper.rb +30 -0
  101. metadata +258 -0
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails gems
3
+ # installed from the root of your application.
4
+
5
+ ENGINE_ROOT = File.expand_path('..', __dir__)
6
+ ENGINE_PATH = File.expand_path('../lib/solidus_paypal_commerce_platform/engine', __dir__)
7
+
8
+ # Set up gems listed in the Gemfile.
9
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
10
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
11
+
12
+ require 'rails/all'
13
+ require 'rails/engine/commands'
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ app_root = 'sandbox'
6
+
7
+ unless File.exist? "#{app_root}/bin/rails"
8
+ warn 'Creating the sandbox app...'
9
+ Dir.chdir "#{__dir__}/.." do
10
+ system "#{__dir__}/sandbox" or begin # rubocop:disable Style/AndOr
11
+ warn 'Automatic creation of the sandbox app failed'
12
+ exit 1
13
+ end
14
+ end
15
+ end
16
+
17
+ Dir.chdir app_root
18
+ exec 'bin/rails', *ARGV
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "rubygems"
5
+ require "bundler/setup"
6
+
7
+ load Gem.bin_path("rake", "rake")
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ case "$DB" in
6
+ postgres|postgresql)
7
+ RAILSDB="postgresql"
8
+ ;;
9
+ mysql)
10
+ RAILSDB="mysql"
11
+ ;;
12
+ sqlite|'')
13
+ RAILSDB="sqlite3"
14
+ ;;
15
+ *)
16
+ echo "Invalid DB specified: $DB"
17
+ exit 1
18
+ ;;
19
+ esac
20
+
21
+ if [ ! -z $SOLIDUS_BRANCH ]
22
+ then
23
+ BRANCH=$SOLIDUS_BRANCH
24
+ else
25
+ BRANCH="master"
26
+ fi
27
+
28
+ extension_name="solidus_paypal_commerce_platform"
29
+
30
+ # Stay away from the bundler env of the containing extension.
31
+ function unbundled {
32
+ ruby -rbundler -e'b = proc {system *ARGV}; Bundler.respond_to?(:with_unbundled_env) ? Bundler.with_unbundled_env(&b) : Bundler.with_clean_env(&b)' -- $@
33
+ }
34
+
35
+ rm -rf ./sandbox
36
+ unbundled bundle exec rails new sandbox --database="$RAILSDB" \
37
+ --skip-bundle \
38
+ --skip-git \
39
+ --skip-keeps \
40
+ --skip-rc \
41
+ --skip-spring \
42
+ --skip-test \
43
+ --skip-javascript
44
+
45
+ if [ ! -d "sandbox" ]; then
46
+ echo 'sandbox rails application failed'
47
+ exit 1
48
+ fi
49
+
50
+ cd ./sandbox
51
+ cat <<RUBY >> Gemfile
52
+ gem 'solidus', github: 'solidusio/solidus', branch: '$BRANCH'
53
+ gem 'solidus_auth_devise', '>= 2.1.0'
54
+ gem 'rails-i18n'
55
+ gem 'solidus_i18n'
56
+
57
+ gem '$extension_name', path: '..'
58
+
59
+ group :test, :development do
60
+ platforms :mri do
61
+ gem 'pry-byebug'
62
+ end
63
+ end
64
+ RUBY
65
+
66
+ unbundled bundle install --gemfile Gemfile
67
+
68
+ unbundled bundle exec rake db:drop db:create
69
+
70
+ unbundled bundle exec rails generate spree:install \
71
+ --auto-accept \
72
+ --user_class=Spree::User \
73
+ --enforce_available_locales=true \
74
+ --with-authentication=false \
75
+ $@
76
+
77
+ unbundled bundle exec rails generate solidus:auth:install
78
+ unbundled bundle exec rails generate solidus_paypal_commerce_platform:install
79
+ unbundled bundle exec rails db:migrate
80
+
81
+ echo
82
+ echo "🚀 Sandbox app successfully created for $extension_name!"
83
+ echo "🚀 Using $RAILSDB and Solidus $BRANCH"
84
+ echo "🚀 Use 'export DB=[postgres|mysql|sqlite]' to control the DB adapter"
85
+ echo "🚀 Use 'export SOLIDUS_BRANCH=<BRANCH-NAME>' to control the Solidus version"
86
+ echo "🚀 This app is intended for test purposes."
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ gem install bundler --conservative
7
+ bundle update
8
+ bin/rake clobber
@@ -0,0 +1,14 @@
1
+ coverage:
2
+ round: up
3
+
4
+ status:
5
+ project:
6
+ default:
7
+ target: auto
8
+ threshold: 2%
9
+ base: auto
10
+ patch:
11
+ default:
12
+ target: 70%
13
+ threshold: 5%
14
+ base: auto
@@ -0,0 +1,37 @@
1
+ en:
2
+ spree:
3
+ hints:
4
+ solidus_paypal_commerce_platform/payment_method:
5
+ available_to_users: You must verify that your email is confirmed with PayPal before users will be able to check out with this payment method.
6
+ payment_source: source
7
+ activerecord:
8
+ attributes:
9
+ solidus_paypal_commerce_platform/payment_source:
10
+ paypal_order_id: "PayPal order"
11
+ payment_method_id: "payment method"
12
+ models:
13
+ solidus_paypal_commerce_platform/payment_source: PayPal Payment Source
14
+ solidus_paypal_commerce_platform/payment_method: PayPal Commerce Platform
15
+ hello: Hello world
16
+ sign_up_for_paypal: Setup PayPal Commerce Platform
17
+ start_paying_with_paypal: Start paying with Paypal Commerce Platform
18
+ paypal_capture_id: PayPal Capture ID
19
+ paypal_debug_id: PayPal Debug ID
20
+ paypal_email: PayPal Email
21
+ payment_email: "Payment Email: %{email}"
22
+ virtual_terminal_notice_html: Backend Credit Card payments can only be accepted through PayPal via the <a href="https://www.paypal.com/merchantapps/appcenter/acceptpayments/virtualterminal">PayPal Virtual Terminal</a>.
23
+ solidus_paypal_commerce_platform:
24
+ responses:
25
+ pay_pal_checkout_sdk/orders/orders_capture_request:
26
+ success: "Payment captured"
27
+ failure: "Your payment was declined - please try again using a different payment method."
28
+ pay_pal_checkout_sdk/orders/orders_authorize_request:
29
+ success: "Payment authorized"
30
+ failure: "Your payment was declined - please try again using a different payment method."
31
+ pay_pal_checkout_sdk/payments/authorizations_capture_request:
32
+ success: "Authorization captured"
33
+ pay_pal_checkout_sdk/payments/captures_refund_request:
34
+ success: "Payment refunded for %{amount}"
35
+ pay_pal_checkout_sdk/payments/authorizations_void_request:
36
+ success: "Payment voided"
37
+
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ SolidusPaypalCommercePlatform::Engine.routes.draw do
4
+ # Add your extension routes here
5
+ resources :wizard, only: [:create]
6
+ resources :paypal_orders, only: [:show], param: :order_id
7
+ resources :orders, only: [:create]
8
+ resources :payments, only: [:create]
9
+ get :shipping_rates, to: "shipping_rates#simulate_shipping_rates"
10
+ post :update_address, to: "orders#update_address"
11
+ get :verify_total, to: "orders#verify_total"
12
+ end
@@ -0,0 +1,13 @@
1
+ class AddPaypalCommercePlatformSources < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :paypal_commerce_platform_sources do |t|
4
+ t.integer :payment_method_id
5
+ t.string :authorization_id
6
+ t.string :capture_id
7
+ t.string :paypal_email
8
+ t.string :paypal_order_id
9
+ t.string :refund_id
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusPaypalCommercePlatform
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ class_option :auto_run_migrations, type: :boolean, default: false
7
+
8
+ def add_javascripts
9
+ append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/solidus_paypal_commerce_platform\n" # rubocop:disable Metrics/LineLength
10
+ append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/solidus_paypal_commerce_platform\n" # rubocop:disable Metrics/LineLength
11
+ end
12
+
13
+ def add_stylesheets
14
+ inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/solidus_paypal_commerce_platform\n", before: %r{\*/}, verbose: true # rubocop:disable Metrics/LineLength
15
+ inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/solidus_paypal_commerce_platform\n", before: %r{\*/}, verbose: true # rubocop:disable Metrics/LineLength
16
+ end
17
+
18
+ def add_migrations
19
+ run 'bin/rails railties:install:migrations FROM=solidus_paypal_commerce_platform'
20
+ end
21
+
22
+ def mount_engine
23
+ insert_into_file File.join('config', 'routes.rb'), after: "Rails.application.routes.draw do\n" do
24
+ "mount SolidusPaypalCommercePlatform::Engine, at: '/solidus_paypal_commerce_platform'\n"
25
+ end
26
+ end
27
+
28
+ def run_migrations
29
+ run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]')) # rubocop:disable Metrics/LineLength
30
+ if run_migrations
31
+ run 'bin/rails db:migrate'
32
+ else
33
+ puts 'Skipping bin/rails db:migrate, don\'t forget to run it!' # rubocop:disable Rails/Output
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solidus_core'
4
+ require 'solidus_support'
5
+
6
+ require 'solidus_paypal_commerce_platform/version'
7
+ require 'solidus_paypal_commerce_platform/configuration'
8
+ require 'solidus_paypal_commerce_platform/client'
9
+ require 'solidus_paypal_commerce_platform/engine'
10
+
11
+ module SolidusPaypalCommercePlatform
12
+ class << self
13
+ def config
14
+ @config ||= Configuration.new
15
+ end
16
+
17
+ def configure
18
+ yield config
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusPaypalCommercePlatform
4
+ class AccessTokenAuthorizationRequest
5
+ attr_accessor :verb, :path, :headers, :body
6
+
7
+ def initialize(environment:, auth_code:, nonce:)
8
+ @verb = "POST"
9
+ @path = "/v1/oauth2/token"
10
+ @headers = {
11
+ "Content-Type" => "application/x-www-form-urlencoded",
12
+ "Authorization" => environment.authorizationString,
13
+ }
14
+ @body = {
15
+ grant_type: "authorization_code",
16
+ code: auth_code,
17
+ code_verifier: nonce,
18
+ }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solidus_paypal_commerce_platform/access_token_authorization_request'
4
+ require 'solidus_paypal_commerce_platform/fetch_merchant_credentials_request'
5
+
6
+ require 'paypal-checkout-sdk'
7
+
8
+ module SolidusPaypalCommercePlatform
9
+ class Client
10
+ SUCCESS_STATUS_CODES = [201, 204].freeze
11
+
12
+ PARTNER_ATTRIBUTION_INJECTOR = ->(request) {
13
+ request.headers["PayPal-Partner-Attribution-Id"] = "Solidus_PCP_SP"
14
+ }.freeze
15
+
16
+ attr_reader :environment
17
+
18
+ def initialize(test_mode: nil, client_id:, client_secret: "")
19
+ test_mode = SolidusPaypalCommercePlatform.config.env.sandbox? if test_mode.nil?
20
+ env_class = test_mode ? PayPal::SandboxEnvironment : PayPal::LiveEnvironment
21
+
22
+ @environment = env_class.new(client_id, client_secret)
23
+ @paypal_client = PayPal::PayPalHttpClient.new(@environment)
24
+
25
+ @paypal_client.add_injector(&PARTNER_ATTRIBUTION_INJECTOR)
26
+ end
27
+
28
+ def execute(request)
29
+ @paypal_client.execute(request)
30
+ rescue PayPalHttp::HttpError
31
+ OpenStruct.new(status_code: nil)
32
+ end
33
+
34
+ def execute_with_response(request, success_message: nil, failure_message: nil)
35
+ i18n_scope = i18n_scope_for(request)
36
+ wrap_response(
37
+ execute(request),
38
+ success_message: success_message || I18n.t("#{i18n_scope}.success", default: nil),
39
+ failure_message: failure_message || I18n.t("#{i18n_scope}.failure", default: nil)
40
+ )
41
+ end
42
+
43
+ def i18n_scope_for(request)
44
+ "solidus_paypal_commerce_platform.responses.#{request.class.name.underscore}"
45
+ end
46
+
47
+ def self.fetch_api_credentials(auth_code:, client_id:, nonce:)
48
+ client = new(client_id: client_id)
49
+
50
+ access_token = client.execute(AccessTokenAuthorizationRequest.new(
51
+ environment: client.environment,
52
+ auth_code: auth_code,
53
+ nonce: nonce,
54
+ )).result.access_token
55
+
56
+ client.execute(FetchMerchantCredentialsRequest.new(
57
+ access_token: access_token,
58
+ partner_merchant_id: SolidusPaypalCommercePlatform.config.partner_id,
59
+ )).result
60
+ end
61
+
62
+ private
63
+
64
+ def wrap_response(response, success_message: nil, failure_message: nil)
65
+ if SUCCESS_STATUS_CODES.include? response.status_code
66
+ success_message ||= "Success."
67
+
68
+ ActiveMerchant::Billing::Response.new(
69
+ true,
70
+ success_message,
71
+ result: response.result,
72
+ paypal_debug_id: response.headers["paypal-debug-id"]
73
+ )
74
+ else
75
+ failure_message ||= "A problem has occurred with this payment, please try again."
76
+
77
+ ActiveMerchant::Billing::Response.new(
78
+ false,
79
+ failure_message
80
+ )
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'paypal-checkout-sdk'
4
+
5
+ module SolidusPaypalCommercePlatform
6
+ class Configuration
7
+ InvalidEnvironment = Class.new(StandardError)
8
+
9
+ DEFAULT_PARTNER_ID = {
10
+ sandbox: "5LQZV7RJDGKG2",
11
+ live: "NBKVLAA7K2V5S",
12
+ }.freeze
13
+
14
+ DEFAULT_PARTNER_CLIENT_ID = {
15
+ sandbox: "ATDpQjHzjCz_C_qbbJ76Ca0IjcmwlS4FztD6YfuRFZXDCmcWWw8-8QWcF3YIkbC85ixTUuuSEvrBMVSX",
16
+ live: "ASOxaUMkeX5bv7PbXnWUDnqb3SVYkzRSosApmLGFih-eAhB_OS_Wo6juijE5t8NCmWDgpN2ugHMmQFWA",
17
+ }.freeze
18
+
19
+ def env=(value)
20
+ unless %w[live sandbox].include? value
21
+ raise InvalidEnvironment, "#{value} is not a valid environment"
22
+ end
23
+
24
+ @env = ActiveSupport::StringInquirer.new(value)
25
+ end
26
+
27
+ def env
28
+ self.env = default_env unless @env
29
+
30
+ @env
31
+ end
32
+
33
+ def default_env
34
+ return ENV['PAYPAL_ENV'] if ENV['PAYPAL_ENV']
35
+
36
+ case Rails.env
37
+ when 'production'
38
+ return 'live'
39
+ when 'test', 'development'
40
+ return 'sandbox'
41
+ else
42
+ raise InvalidEnvironment, "Unable to guess the PayPal env, please set #{self}.env= explicitly."
43
+ end
44
+ end
45
+
46
+ def env_class
47
+ env.live? ? PayPal::LiveEnvironment : PayPal::SandboxEnvironment
48
+ end
49
+
50
+ def env_domain
51
+ env.live? ? "www.paypal.com" : "www.sandbox.paypal.com"
52
+ end
53
+
54
+ attr_writer :partner_id, :partner_client_id
55
+
56
+ def partner_id
57
+ @partner_id ||= ENV['PAYPAL_PARTNER_ID'] || DEFAULT_PARTNER_ID[env.to_sym]
58
+ end
59
+
60
+ def partner_client_id
61
+ @partner_client_id ||= ENV['PAYPAL_PARTNER_CLIENT_ID'] || DEFAULT_PARTNER_CLIENT_ID[env.to_sym]
62
+ end
63
+ end
64
+ end