solidus_paypal_commerce_platform 1.0.3 → 1.0.4

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.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +17 -0
  3. data/.rubocop.yml +2 -1
  4. data/README.md +9 -1
  5. data/app/models/solidus_paypal_commerce_platform/gateway.rb +96 -9
  6. data/app/models/solidus_paypal_commerce_platform/payment_method.rb +1 -0
  7. data/app/models/solidus_paypal_commerce_platform/payment_source.rb +2 -2
  8. data/app/models/solidus_paypal_commerce_platform/paypal_address.rb +26 -10
  9. data/app/models/solidus_paypal_commerce_platform/paypal_order.rb +1 -1
  10. data/app/models/solidus_paypal_commerce_platform/wizard.rb +1 -1
  11. data/app/{decorators/models/solidus_paypal_commerce_platform/spree/address_decorator.rb → patches/models/solidus_paypal_commerce_platform/spree/address_patch.rb} +1 -1
  12. data/app/{decorators/models/solidus_paypal_commerce_platform/spree/variant/pricing_options_decorator.rb → patches/models/solidus_paypal_commerce_platform/spree/variant/pricing_options_patch.rb} +1 -1
  13. data/bin/dummy-app +21 -2
  14. data/bin/rspec +1 -1
  15. data/bin/sandbox +72 -34
  16. data/config/locales/en.yml +6 -0
  17. data/lib/generators/solidus_paypal_commerce_platform/install/install_generator.rb +11 -5
  18. data/lib/generators/solidus_paypal_commerce_platform/install/templates/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/button_actions.js +1 -1
  19. data/lib/generators/solidus_paypal_commerce_platform/install/templates/app/controllers/solidus_paypal_commerce_platform/orders_controller.rb +5 -1
  20. data/lib/paypal/paypal_http_client.rb +1 -2
  21. data/lib/solidus_paypal_commerce_platform/engine.rb +3 -3
  22. data/lib/solidus_paypal_commerce_platform/version.rb +1 -1
  23. data/solidus_paypal_commerce_platform.gemspec +3 -3
  24. data/spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb +168 -6
  25. data/spec/models/solidus_paypal_commerce_platform/paypal_address_spec.rb +100 -0
  26. data/spec/models/solidus_paypal_commerce_platform/paypal_order_spec.rb +18 -0
  27. data/spec/requests/solidus_paypal_commerce_platform/orders_controller_spec.rb +72 -0
  28. data/spec/solidus_paypal_commerce_platform_spec_helper.rb +5 -1
  29. data/spec/system/backend/new_payment_method_spec.rb +1 -1
  30. metadata +11 -15
  31. data/.circleci/config.yml +0 -80
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dfb7ee341074d3d81cfc59a715ef14aeb98cae039965ee08fcdd398bc09a223b
4
- data.tar.gz: '0491f84853194b9b601a19b22242604bb5c92c2b692e5f85ee61b704c4623fad'
3
+ metadata.gz: 5a09746abd568f135d0775fd0c7bb40817270afd574aa37ff345b7adfb4cf99a
4
+ data.tar.gz: 2b145c8d649383daf8c6567fd3a1c7052a1490224b326110cbd988f1acd60d32
5
5
  SHA512:
6
- metadata.gz: 945f8991754ec669587007c1cd0349d012fa27e0c7ac30e87d3a239555c1f70b6a701df67b099d182f9a5fd291c8520df2fe61bc4d987f99d023d0f70ab7a092
7
- data.tar.gz: cbff4cf00b1dfa87c052cb6e6e30b77af87e27cab7866fccb515ca37e33f7034bba609182516c83f50eb28645b779a6609aa0b3e411229401f8a706d30875316
6
+ metadata.gz: 1f4a6865f0941911abc8070732c13bab790e8ecb3c0af13f0e4ec5bcd1d01374fe6bdf0d78c64415733bc024589d9c30aef6420cc9585b57585673e7e15de9f5
7
+ data.tar.gz: ec867e5c0b10d3dade249bda6607a99305cd00f4606aa32d814caca7d112f485aba488b0f909d5ad01ad950530a1091d92b5ec9c7f556d16f776eceaa1d650db
@@ -0,0 +1,17 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+ schedule:
9
+ - cron: "0 0 * * 4" # every Thursday
10
+ workflow_call:
11
+
12
+ permissions:
13
+ contents: read
14
+
15
+ jobs:
16
+ rspec:
17
+ uses: solidusio/test-solidus-extension/.github/workflows/test.yml@main
data/.rubocop.yml CHANGED
@@ -3,7 +3,7 @@ require:
3
3
 
4
4
  AllCops:
5
5
  NewCops: disable
6
- TargetRubyVersion: '3.0'
6
+ TargetRubyVersion: "3.1"
7
7
  Exclude:
8
8
  - sandbox/**/*
9
9
  - dummy-app/**/*
@@ -64,6 +64,7 @@ RSpec/MultipleMemoizedHelpers:
64
64
  - spec/jobs/solidus_paypal_commerce_platform/webhook_job_spec.rb
65
65
  - spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb
66
66
  - spec/models/solidus_paypal_commerce_platform/payment_source_spec.rb
67
+ - spec/models/solidus_paypal_commerce_platform/paypal_address_spec.rb
67
68
 
68
69
  Rails/Inquiry:
69
70
  Enabled: true
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Solidus Paypal Commerce Platform
2
2
 
3
- [![CircleCI](https://circleci.com/gh/solidusio/solidus_paypal_commerce_platform.svg?style=shield)](https://circleci.com/gh/solidusio/solidus_paypal_commerce_platform)
3
+ [![Workflow Name](https://github.com/solidusio/solidus_paypal_commerce_platform/actions/workflows/test.yml/badge.svg)](https://github.com/solidusio/solidus_paypal_commerce_platform/actions/workflows/<WORKFLOW_FILE>)
4
4
  [![codecov](https://codecov.io/gh/solidusio-contrib/solidus_paypal_commerce_platform/branch/main/graph/badge.svg?token=J8HCLE0CZW)](https://codecov.io/gh/solidusio-contrib/solidus_paypal_commerce_platform)
5
5
 
6
6
  The official PayPal integration of Solidus.
@@ -179,6 +179,14 @@ The easiest way to change the `Payment Method`'s preferences is through admin: `
179
179
 
180
180
  See more about preferences [here](https://guides.solidus.io/developers/preferences/add-model-preferences.html#access-your-preferences)/
181
181
 
182
+ ### Verifying the payment recipient
183
+
184
+ Set the payment method's **Merchant ID** preference to your PayPal Merchant ID (also shown as
185
+ Account ID in your PayPal account settings). When it is set, the gateway confirms that each
186
+ approved PayPal order pays your own account before capturing or authorizing it, and refuses
187
+ payments that would be paid to a different account. The check is only active once the Merchant ID
188
+ is configured; leaving it blank keeps the previous behaviour.
189
+
182
190
  ## Development
183
191
 
184
192
  ### Testing the extension
@@ -16,25 +16,43 @@ module SolidusPaypalCommercePlatform
16
16
  end
17
17
 
18
18
  def purchase(money, source, options)
19
+ refusal = refuse_insufficient_approval(money, source, options)
20
+ return refusal if refusal
21
+
22
+ refusal = refuse_wrong_payee(source)
23
+ return refusal if refusal
24
+
19
25
  request = OrdersCaptureRequest.new(source.paypal_order_id)
20
26
  request.request_body(amount: { currency_code: options[:currency], value: Money.new(money).dollars })
21
27
 
22
28
  response = @client.execute_with_response(request)
23
- if response.success?
24
- capture_id = response.params["result"].purchase_units[0].payments.captures[0].id
25
- source.update(capture_id: capture_id)
26
- end
29
+ return response unless response.success?
30
+
31
+ capture = response.params["result"].purchase_units[0].payments.captures&.first
32
+ unsettled = refuse_unless_settled(capture, money, options, allowed_statuses: ["COMPLETED"])
33
+ return unsettled if unsettled
34
+
35
+ source.update(capture_id: capture.id)
27
36
  response
28
37
  end
29
38
 
30
- def authorize(_money, source, _options)
39
+ def authorize(money, source, options)
40
+ refusal = refuse_insufficient_approval(money, source, options)
41
+ return refusal if refusal
42
+
43
+ refusal = refuse_wrong_payee(source)
44
+ return refusal if refusal
45
+
31
46
  request = OrdersAuthorizeRequest.new(source.paypal_order_id)
32
47
 
33
48
  response = @client.execute_with_response(request)
34
- if response.success?
35
- authorization_id = response.params["result"].purchase_units[0].payments.authorizations[0].id
36
- source.update(authorization_id: authorization_id)
37
- end
49
+ return response unless response.success?
50
+
51
+ authorization = response.params["result"].purchase_units[0].payments.authorizations&.first
52
+ unsettled = refuse_unless_settled(authorization, money, options, allowed_statuses: ["CREATED"])
53
+ return unsettled if unsettled
54
+
55
+ source.update(authorization_id: authorization.id)
38
56
  response
39
57
  end
40
58
 
@@ -84,6 +102,75 @@ module SolidusPaypalCommercePlatform
84
102
 
85
103
  private
86
104
 
105
+ # PayPal authorizes and captures the amount the customer approved, whatever
106
+ # amount it is sent, so it must be checked against the payment's amount.
107
+ def refuse_insufficient_approval(money, source, options)
108
+ request = OrdersGetRequest.new(source.paypal_order_id)
109
+ response = @client.execute(request)
110
+ unless response.status_code.between?(200, 299)
111
+ return ActiveMerchant::Billing::Response.new(
112
+ false,
113
+ I18n.t(
114
+ "#{@client.i18n_scope_for(request)}.failure",
115
+ default: "Your payment was declined - please try again using a different payment method."
116
+ )
117
+ )
118
+ end
119
+
120
+ approved = response.result.purchase_units[0].amount
121
+ approved_cents = Money.from_amount(BigDecimal(approved.value), approved.currency_code).cents
122
+ return unless approved.currency_code != options[:currency] || approved_cents < money
123
+
124
+ ActiveMerchant::Billing::Response.new(
125
+ false,
126
+ I18n.t(
127
+ 'solidus_paypal_commerce_platform.responses.insufficient_approval',
128
+ default: "The amount approved with PayPal doesn't cover this payment - please pay with PayPal again."
129
+ )
130
+ )
131
+ end
132
+
133
+ # PayPal can return a 2xx for a capture or authorization that is pending,
134
+ # denied or for less than the payment. Only treat it as paid when PayPal
135
+ # confirms it settled for at least the payment's amount in its currency.
136
+ def refuse_unless_settled(settlement, money, options, allowed_statuses:)
137
+ if settlement && allowed_statuses.include?(settlement.status) &&
138
+ settlement.amount.currency_code == options[:currency] &&
139
+ Money.from_amount(BigDecimal(settlement.amount.value), settlement.amount.currency_code).cents >= money
140
+ return
141
+ end
142
+
143
+ ActiveMerchant::Billing::Response.new(
144
+ false,
145
+ I18n.t(
146
+ 'solidus_paypal_commerce_platform.responses.unsettled_payment',
147
+ default: "PayPal didn't confirm this payment - please try again using a different payment method."
148
+ )
149
+ )
150
+ end
151
+
152
+ # Refuse to capture or authorize a PayPal order unless it pays the store's
153
+ # own merchant. A buyer can create an order against the store's public
154
+ # client ID while naming a different payee, so the approved order's payee
155
+ # must match the configured merchant before any money moves.
156
+ def refuse_wrong_payee(source)
157
+ expected_merchant_id = @options[:merchant_id].to_s.strip
158
+ # Only stores that record their PayPal merchant ID can be protected. When
159
+ # it is set, every purchase unit must pay that merchant; a missing,
160
+ # mismatched, or unreadable order is refused.
161
+ return if expected_merchant_id.empty?
162
+
163
+ response = @client.execute(OrdersGetRequest.new(source.paypal_order_id))
164
+ purchase_units = response.result.purchase_units if response.respond_to?(:result)
165
+ return if purchase_units.present? &&
166
+ purchase_units.all? { |unit| unit.payee&.merchant_id == expected_merchant_id }
167
+
168
+ ActiveMerchant::Billing::Response.new(
169
+ false,
170
+ I18n.t("solidus_paypal_commerce_platform.responses.wrong_payee")
171
+ )
172
+ end
173
+
87
174
  def capture_with_response(request, success_message: nil, failure_message: nil)
88
175
  response = @client.execute(request)
89
176
  if response.result&.status == "PENDING"
@@ -5,6 +5,7 @@ module SolidusPaypalCommercePlatform
5
5
  include SolidusPaypalCommercePlatform::ButtonOptionsHelper
6
6
  preference :client_id, :string
7
7
  preference :client_secret, :string
8
+ preference :merchant_id, :string
8
9
  preference :paypal_button_color, :paypal_select, default: "gold"
9
10
  preference :paypal_button_size, :paypal_select, default: "responsive"
10
11
  preference :paypal_button_shape, :paypal_select, default: "rect"
@@ -3,11 +3,11 @@
3
3
  module SolidusPaypalCommercePlatform
4
4
  class PaymentSource < ::Spree::PaymentSource
5
5
  self.table_name = "paypal_commerce_platform_sources"
6
- enum paypal_funding_source: {
6
+ enum :paypal_funding_source, {
7
7
  applepay: 0, bancontact: 1, blik: 2, boleto: 3, card: 4, credit: 5, eps: 6, giropay: 7, ideal: 8,
8
8
  itau: 9, maxima: 10, mercadopago: 11, mybank: 12, oxxo: 13, p24: 14, paylater: 15, paypal: 16, payu: 17,
9
9
  sepa: 18, sofort: 19, trustly: 20, venmo: 21, verkkopankki: 22, wechatpay: 23, zimpler: 24
10
- }, _suffix: :funding
10
+ }, suffix: :funding
11
11
  validates :paypal_order_id, :payment_method_id, presence: true
12
12
 
13
13
  def display_paypal_funding_source
@@ -42,12 +42,26 @@ module SolidusPaypalCommercePlatform
42
42
  @order.update(email: recipient[:email_address])
43
43
  end
44
44
 
45
+ def find_country(state_name, country_code)
46
+ country = ::Spree::Country.find_by(iso: country_code)
47
+ return country if country.present?
48
+
49
+ state = ::Spree::State.find_by("lower(name) LIKE '%#{state_name.downcase}%'")
50
+ state.country
51
+ end
52
+
45
53
  def find_state(state_name, country)
46
- if state = country.states.find_by(abbr: state_name) || country.states.find_by(name: state_name)
47
- state
48
- else
49
- SolidusPaypalCommercePlatform.config.state_guesser_class.new(state_name, country).guess
50
- end
54
+ return nil if country.blank?
55
+
56
+ state = country.states.find_by(abbr: state_name) ||
57
+ country.states.find_by(name: state_name)
58
+ return state if state.present?
59
+
60
+ state = SolidusPaypalCommercePlatform.config.state_guesser_class.new(
61
+ state_name,
62
+ country
63
+ )
64
+ state.guess
51
65
  end
52
66
 
53
67
  def format_simulated_address(paypal_address)
@@ -60,11 +74,13 @@ module SolidusPaypalCommercePlatform
60
74
  end
61
75
 
62
76
  def address_attributes(address, recipient)
63
- country = ::Spree::Country.find_by(iso: address[:country_code])
64
- state = find_state(
65
- address[:admin_area_1] || address[:admin_area_2] || address[:state],
66
- country,
67
- )
77
+ state_name = address[:admin_area_1] ||
78
+ address[:admin_area_2] ||
79
+ address[:state]
80
+ country_code = address[:country_code]
81
+
82
+ country = find_country(state_name, country_code)
83
+ state = find_state(state_name, country)
68
84
 
69
85
  attributes = {
70
86
  address1: address[:address_line_1],
@@ -92,7 +92,7 @@ module SolidusPaypalCommercePlatform
92
92
  item_total: price(@order.item_total),
93
93
  shipping: price(@order.shipment_total),
94
94
  tax_total: price(@order.additional_tax_total),
95
- discount: price(@order.all_adjustments.promotion.sum(&:amount).abs)
95
+ discount: price(@order.promo_total.abs)
96
96
  }
97
97
  end
98
98
 
@@ -34,7 +34,7 @@ module SolidusPaypalCommercePlatform
34
34
  private
35
35
 
36
36
  def logo
37
- ActionController::Base.helpers.image_path(::Spree::Config[:admin_interface_logo])
37
+ ActionController::Base.helpers.image_path(::Spree::Config.admin_interface_logo)
38
38
  end
39
39
  end
40
40
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module SolidusPaypalCommercePlatform
4
4
  module Spree
5
- module AddressDecorator
5
+ module AddressPatch
6
6
  # PayPal doesn't use the phone number, so in cases where the user checks out via
7
7
  # PayPal, this field will be unpopulated. If you want to require phone numbers,
8
8
  # you'll need to turn off cart & product page displays on the payment method edit
@@ -3,7 +3,7 @@
3
3
  module SolidusPaypalCommercePlatform
4
4
  module Spree
5
5
  module Variant
6
- module PricingOptionsDecorator
6
+ module PricingOptionsPatch
7
7
  def cache_key
8
8
  SolidusPaypalCommercePlatform::PaymentMethod.
9
9
  active.
data/bin/dummy-app CHANGED
@@ -14,7 +14,8 @@ function unbundled {
14
14
  test "$DB" = "sqlite" && export DB="sqlite3"
15
15
 
16
16
  rm -rf ./dummy-app
17
- rails new dummy-app \
17
+ rails_version=`bundle exec ruby -e'require "rails"; puts Rails.version'`
18
+ bundle exec rails _${rails_version}_ new dummy-app \
18
19
  --database=${DB:-sqlite3} \
19
20
  --skip-git \
20
21
  --skip-keeps \
@@ -28,9 +29,27 @@ if [ ! -d "dummy-app" ]; then
28
29
  fi
29
30
 
30
31
  cd ./dummy-app
32
+
33
+ if [ -n "$DB_HOST" ]; then
34
+ echo "~~~> Configuring database.yml for DB_HOST=$DB_HOST"
35
+ ruby -i -pe '
36
+ if $_ =~ /^ (pool|max_connections):/
37
+ $_ += " host: <%= ENV.fetch(\"DB_HOST\", \"localhost\") %>\n"
38
+ $_ += " username: <%= ENV[\"DB_USERNAME\"] %>\n"
39
+ end
40
+ ' config/database.yml
41
+ fi
42
+
43
+ echo "Generating manifest file"
44
+ mkdir -p app/assets/config
45
+ cat <<MANIFESTJS > app/assets/config/manifest.js
46
+ //= link_tree ../images
47
+ //= link_directory ../javascripts .js
48
+ //= link_directory ../stylesheets .css
49
+ MANIFESTJS
50
+
31
51
  unbundled bundle add solidus --github solidusio/solidus --branch "${BRANCH:-main}" --version '> 0.a'
32
52
  unbundled bundle exec rake db:drop db:create
33
53
  unbundled bundle exec rails generate solidus:install --auto-accept --payment-method=none --no-seed --no-sample "$@"
34
54
  unbundled bundle add $extension_name --path ..
35
55
  unbundled bundle exec rails generate $extension_name:install --migrate --specs=all
36
-
data/bin/rspec CHANGED
@@ -5,7 +5,7 @@ set -e
5
5
  bin/rails-dummy-app generate solidus_paypal_commerce_platform:install --force --migrate --specs=all
6
6
 
7
7
  cd dummy-app/
8
- rspec "$@"
8
+ bundle exec rspec "$@"
9
9
  exit_status=$?
10
10
  cd -
11
11
  exit $exit_status
data/bin/sandbox CHANGED
@@ -1,61 +1,99 @@
1
1
  #!/usr/bin/env bash
2
2
 
3
3
  set -e
4
+ test -z "${DEBUG+empty_string}" || set -x
4
5
 
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
6
+ test "$DB" = "sqlite" && export DB="sqlite3"
20
7
 
21
- if [ ! -z $SOLIDUS_BRANCH ]
8
+ if [ -z "$SOLIDUS_BRANCH" ]
22
9
  then
23
- BRANCH=$SOLIDUS_BRANCH
24
- else
25
- BRANCH="main"
10
+ echo "~~> Use 'export SOLIDUS_BRANCH=[main|v4.0|...]' to control the Solidus branch"
11
+ SOLIDUS_BRANCH="main"
26
12
  fi
27
-
28
- extension_name="solidus_paypal_commerce_platform"
13
+ echo "~~> Using branch $SOLIDUS_BRANCH of solidus"
29
14
 
30
15
  # Stay away from the bundler env of the containing extension.
31
16
  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)' -- "$@"
17
+ ruby -rbundler -e'
18
+ Bundler.with_unbundled_env {system *ARGV}' -- \
19
+ env BUNDLE_SUPPRESS_INSTALL_USING_MESSAGES=true $@
33
20
  }
34
21
 
22
+ echo "~~~> Removing the old sandbox"
35
23
  rm -rf ./sandbox
36
- rails new sandbox \
37
- --database="$RAILSDB" \
24
+
25
+ echo "~~~> Creating a pristine Rails app"
26
+ rails_version=`bundle exec ruby -e'require "rails"; puts Rails.version'`
27
+
28
+ # --skip-solid was added in Rails 8.0 to skip Solid Cache, Queue, and Cable
29
+ # setup which is unnecessary for the sandbox and fails during generation.
30
+ rails_major=$(echo "$rails_version" | cut -d. -f1)
31
+ skip_solid_flag=""
32
+ if [ "$rails_major" -ge 8 ] 2>/dev/null; then
33
+ skip_solid_flag="--skip-solid"
34
+ fi
35
+
36
+ bundle exec rails _${rails_version}_ new sandbox \
37
+ --database="${DB:-sqlite3}" \
38
38
  --skip-git \
39
39
  --skip-keeps \
40
40
  --skip-rc \
41
41
  --skip-bootsnap \
42
- --skip-test
42
+ --skip-test \
43
+ $skip_solid_flag
43
44
 
44
45
  if [ ! -d "sandbox" ]; then
45
46
  echo 'sandbox rails application failed'
46
47
  exit 1
47
48
  fi
48
49
 
50
+ echo "~~~> Adding solidus (with i18n) to the Gemfile"
49
51
  cd ./sandbox
50
- unbundled bundle add solidus --github solidusio/solidus --branch "${BRANCH:-main}" --version '> 0.a'
52
+
53
+ if [ -n "$DB_HOST" ]; then
54
+ echo "~~~> Configuring database.yml for DB_HOST=$DB_HOST"
55
+ ruby -i -pe '
56
+ if $_ =~ /^ pool:/
57
+ $_ += " host: <%= ENV.fetch(\"DB_HOST\", \"localhost\") %>\n"
58
+ $_ += " username: <%= ENV[\"DB_USERNAME\"] %>\n"
59
+ end
60
+ ' config/database.yml
61
+ fi
62
+
63
+ cat <<RUBY >> Gemfile
64
+ gem 'solidus', github: 'solidusio/solidus', branch: '$SOLIDUS_BRANCH'
65
+ gem 'rails-i18n'
66
+ gem 'solidus_i18n'
67
+ gem 'solidus_auth_devise', github: "solidusio/solidus_auth_devise"
68
+
69
+ # Ruby 4.0 removed benchmark from default gems, but ActiveSupport requires it.
70
+ gem 'benchmark' if RUBY_VERSION >= '4.0'
71
+
72
+ group :test, :development do
73
+ platforms :mri do
74
+ gem 'pry-byebug'
75
+ end
76
+ end
77
+ RUBY
78
+
79
+ echo "Generating manifest file"
80
+ mkdir -p app/assets/config
81
+ cat <<MANIFESTJS > app/assets/config/manifest.js
82
+ //= link_tree ../images
83
+ //= link_directory ../javascripts .js
84
+ //= link_directory ../stylesheets .css
85
+ MANIFESTJS
86
+
87
+ unbundled bundle install --gemfile Gemfile
88
+
51
89
  unbundled bundle exec rake db:drop db:create
52
- unbundled bundle exec rails generate solidus:install --payment-method=none --auto-accept "$@"
53
- unbundled bundle add ${extension_name} --path '../'
54
- unbundled bundle exec rails generate ${extension_name}:install --frontend=starter --migrate=true
90
+
91
+ export FRONTEND="$PWD/../template.rb"
92
+ unbundled bundle exec rails generate solidus:install \
93
+ --auto-accept \
94
+ $@
55
95
 
56
96
  echo
57
- echo "🚀 Sandbox app successfully created for ${extension_name}!"
58
- echo "🚀 Using $RAILSDB and Solidus $BRANCH"
59
- echo "🚀 Use 'export DB=[postgres|mysql|sqlite]' to control the DB adapter"
60
- echo "🚀 Use 'export SOLIDUS_BRANCH=<BRANCH-NAME>' to control the Solidus version"
61
- echo "🚀 This app is intended for test purposes."
97
+ echo "🚀 Sandbox app successfully created for the storefront!"
98
+ echo "🚀 Use 'export DB=[postgres|mysql|sqlite3]' to control the DB adapter"
99
+ echo "🧪 This app is intended for test purposes."
@@ -3,6 +3,7 @@ en:
3
3
  hints:
4
4
  solidus_paypal_commerce_platform/payment_method:
5
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
+ merchant_id: Your PayPal Merchant ID (Account ID), found in your PayPal account settings. Set this so the gateway can confirm that payments are made to your account.
6
7
  payment_source: source
7
8
  activerecord:
8
9
  attributes:
@@ -28,9 +29,14 @@ en:
28
29
  invalid_paypal_order_id: "Invalid order confirmation data passed in"
29
30
  order_complete: "Order is already in complete state"
30
31
  responses:
32
+ insufficient_approval: "The amount approved with PayPal doesn't cover this payment - please pay with PayPal again."
33
+ unsettled_payment: "PayPal didn't confirm this payment - please try again using a different payment method."
34
+ wrong_payee: "This payment couldn't be verified - please try again using a different payment method."
31
35
  pay_pal_checkout_sdk/orders/orders_capture_request:
32
36
  success: "Payment captured"
33
37
  failure: "Your payment was declined - please try again using a different payment method."
38
+ pay_pal_checkout_sdk/orders/orders_get_request:
39
+ failure: "Your payment was declined - please try again using a different payment method."
34
40
  pay_pal_checkout_sdk/orders/orders_authorize_request:
35
41
  success: "Payment authorized"
36
42
  failure: "Your payment was declined - please try again using a different payment method."
@@ -16,7 +16,7 @@ module SolidusPaypalCommercePlatform
16
16
  def normalize_components_options
17
17
  @components = {
18
18
  backend: options[:backend],
19
- starter_frontend: options[:frontend] == 'starter',
19
+ storefront: options[:frontend] == 'starter' || options[:frontend] == 'storefront',
20
20
  classic_frontend: options[:frontend] == 'classic',
21
21
  }
22
22
  end
@@ -43,15 +43,21 @@ module SolidusPaypalCommercePlatform
43
43
  end
44
44
  end
45
45
 
46
- def install_solidus_starter_frontend_support
47
- support_code_for(:starter_frontend) do
46
+ def install_solidus_storefront_support
47
+ support_code_for(:storefront) do
48
48
  directory 'app', 'app'
49
+ js_manifest = File.exist?('app/assets/javascripts/solidus_storefront.js') ?
50
+ 'app/assets/javascripts/solidus_storefront.js' :
51
+ 'app/assets/javascripts/solidus_starter_frontend.js'
52
+ css_manifest = File.exist?('app/assets/stylesheets/solidus_storefront.css') ?
53
+ 'app/assets/stylesheets/solidus_storefront.css' :
54
+ 'app/assets/stylesheets/solidus_starter_frontend.css'
49
55
  append_file(
50
- 'app/assets/javascripts/solidus_starter_frontend.js',
56
+ js_manifest,
51
57
  "//= require spree/frontend/solidus_paypal_commerce_platform\n"
52
58
  )
53
59
  inject_into_file(
54
- 'app/assets/stylesheets/solidus_starter_frontend.css',
60
+ css_manifest,
55
61
  " *= require spree/frontend/solidus_paypal_commerce_platform\n",
56
62
  before: %r{\*/},
57
63
  verbose: true,
@@ -33,7 +33,7 @@ const authHeader = (options = {}) => {
33
33
 
34
34
  return {
35
35
  'Authorization': `Bearer ${apiKey}`,
36
- [document.querySelector('meta[name="csrf-param"]')?.content]: document.querySelector('meta[name="csrf-token"]')?.content,
36
+ 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]')?.content,
37
37
  }
38
38
  }
39
39
  SolidusPaypalCommercePlatform.showOverlay = function() {
@@ -3,6 +3,8 @@
3
3
  module SolidusPaypalCommercePlatform
4
4
  class OrdersController < ::Spree::Api::BaseController
5
5
  skip_before_action :authenticate_user
6
+ protect_from_forgery with: :exception
7
+
6
8
  include ::Spree::Core::ControllerHelpers::Auth
7
9
 
8
10
  def create
@@ -77,7 +79,9 @@ module SolidusPaypalCommercePlatform
77
79
  end
78
80
 
79
81
  def order_params
80
- params.require(:order).permit(permitted_order_attributes)
82
+ params.require(:order).permit([
83
+ line_items_attributes: permitted_line_item_attributes
84
+ ])
81
85
  end
82
86
 
83
87
  def load_order
@@ -45,8 +45,7 @@ module PayPal
45
45
  end
46
46
 
47
47
  def _is_auth_request(request)
48
- request.path == '/v1/oauth2/token' ||
49
- request.path == '/v1/identity/openidconnect/tokenservice'
48
+ ['/v1/oauth2/token', '/v1/identity/openidconnect/tokenservice'].include?(request.path)
50
49
  end
51
50
 
52
51
  def _has_auth_header(request)
@@ -8,7 +8,7 @@ require 'solidus_support'
8
8
 
9
9
  module SolidusPaypalCommercePlatform
10
10
  class Engine < Rails::Engine
11
- include SolidusSupport::EngineExtensions
11
+ include ::SolidusSupport::EngineExtensions
12
12
 
13
13
  isolate_namespace SolidusPaypalCommercePlatform
14
14
 
@@ -34,12 +34,12 @@ module SolidusPaypalCommercePlatform
34
34
 
35
35
  initializer "solidus_paypal_commerce_platform.add_wizard", after: "spree.register.payment_methods" do |app|
36
36
  # Adding the class set below - if this is ported to core, we can remove this line.
37
- Spree::Core::Environment.add_class_set("payment_setup_wizards")
37
+ ::Spree::Core::Environment.add_class_set("payment_setup_wizards")
38
38
  app.config.spree.payment_setup_wizards << "SolidusPaypalCommercePlatform::Wizard"
39
39
  end
40
40
 
41
41
  initializer "solidus_paypal_commerce_platform.webhooks" do
42
- SolidusWebhooks.config.register_webhook_handler :solidus_paypal_commerce_platform, ->(payload) {
42
+ ::SolidusWebhooks.config.register_webhook_handler :solidus_paypal_commerce_platform, ->(payload) {
43
43
  SolidusPaypalCommercePlatform::WebhooksJob.perform_now(payload)
44
44
  }
45
45
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusPaypalCommercePlatform
4
- VERSION = '1.0.3'
4
+ VERSION = '1.0.4'
5
5
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.metadata['changelog_uri'] = 'https://github.com/solidusio/solidus_paypal_commerce_platform/releases'
18
18
  spec.metadata['rubygems_mfa_required'] = 'true'
19
19
 
20
- spec.required_ruby_version = Gem::Requirement.new('>= 3.0')
20
+ spec.required_ruby_version = Gem::Requirement.new('>= 3.1')
21
21
 
22
22
  # Specify which files should be added to the gem when it is released.
23
23
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -30,8 +30,8 @@ Gem::Specification.new do |spec|
30
30
 
31
31
  spec.add_dependency 'deface', '~> 1.5'
32
32
  spec.add_dependency 'solidus_api'
33
- spec.add_dependency 'solidus_core', '>= 3.0', '< 5.0'
34
- spec.add_dependency 'solidus_support', '>= 0.8.0'
33
+ spec.add_dependency 'solidus_core', '>= 4.1', '< 5.0'
34
+ spec.add_dependency 'solidus_support', '>= 0.14.0'
35
35
  spec.add_dependency 'solidus_webhooks', '~> 0.2'
36
36
 
37
37
  spec.add_dependency 'paypalhttp'
@@ -36,27 +36,189 @@ RSpec.describe SolidusPaypalCommercePlatform::PaymentMethod, type: :model do
36
36
  end
37
37
  end
38
38
 
39
+ shared_examples "refusing insufficient PayPal approvals" do |request|
40
+ let(:source) { paypal_payment_method.payment_source_class.create(paypal_order_id: SecureRandom.hex(8)) }
41
+ let(:request_class) { SolidusPaypalCommercePlatform::Gateway.const_get(request) }
42
+
43
+ def refuse_to(action, money: 1000)
44
+ response = paypal_payment_method.public_send(action, money, source, currency: "USD")
45
+
46
+ expect(response).not_to be_success
47
+ expect(request_class).not_to have_received(:new)
48
+ response.message
49
+ end
50
+
51
+ before { allow(request_class).to receive(:new).and_call_original }
52
+
53
+ it "refuses when PayPal approved less than the amount" do
54
+ expect(refuse_to(action, money: 1001))
55
+ .to eq(I18n.t("solidus_paypal_commerce_platform.responses.insufficient_approval"))
56
+ end
57
+
58
+ context "when PayPal approved another currency" do
59
+ let(:approved_currency) { "MXN" }
60
+
61
+ it "refuses" do
62
+ expect(refuse_to(action))
63
+ .to eq(I18n.t("solidus_paypal_commerce_platform.responses.insufficient_approval"))
64
+ end
65
+ end
66
+
67
+ context "when the PayPal order can't be fetched" do
68
+ let(:response) { SolidusPaypalCommercePlatform::Client::Response.new(status_code: 422, error: Struct(name: "RESOURCE_NOT_FOUND")) }
69
+
70
+ it "refuses" do
71
+ expect(refuse_to(action)).to start_with("Your payment was declined")
72
+ end
73
+ end
74
+ end
75
+
76
+ shared_examples "refusing unconfirmed PayPal settlements" do
77
+ let(:unsettled) { I18n.t("solidus_paypal_commerce_platform.responses.unsettled_payment") }
78
+
79
+ context "when PayPal reports the settlement is not complete" do
80
+ let(:settlement_status) { "PENDING" }
81
+
82
+ it "refuses and does not record the settlement" do
83
+ response = paypal_payment_method.public_send(action, 1000, source, currency: "USD")
84
+ expect(response).not_to be_success
85
+ expect(response.message).to eq(unsettled)
86
+ expect(source).to have_attributes(capture_id: nil, authorization_id: nil)
87
+ end
88
+ end
89
+
90
+ context "when PayPal settled less than the payment" do
91
+ let(:settled_value) { "5.00" }
92
+
93
+ it "refuses" do
94
+ response = paypal_payment_method.public_send(action, 1000, source, currency: "USD")
95
+ expect(response).not_to be_success
96
+ expect(response.message).to eq(unsettled)
97
+ end
98
+ end
99
+
100
+ context "when PayPal settled in another currency" do
101
+ let(:settled_currency) { "MXN" }
102
+
103
+ it "refuses" do
104
+ response = paypal_payment_method.public_send(action, 1000, source, currency: "USD")
105
+ expect(response).not_to be_success
106
+ expect(response.message).to eq(unsettled)
107
+ end
108
+ end
109
+ end
110
+
39
111
  describe "#purchase" do
40
- let(:result) { Struct(purchase_units: [Struct(payments: payments)]) }
41
- let(:payments) { Struct(captures: [Struct(id: SecureRandom.hex(4))]) }
112
+ let(:merchant_id) { SecureRandom.hex(6) }
113
+ let(:payee_merchant_id) { merchant_id }
114
+ let(:result) { Struct(purchase_units: [Struct(payee: Struct(merchant_id: payee_merchant_id), payments: payments, amount: approved_amount)]) }
115
+ let(:payments) { Struct(captures: [Struct(id: SecureRandom.hex(4), status: settlement_status, amount: settled_amount)]) }
116
+ let(:approved_amount) { Struct(value: "10.00", currency_code: approved_currency) }
117
+ let(:approved_currency) { "USD" }
118
+ let(:settlement_status) { "COMPLETED" }
119
+ let(:settled_amount) { Struct(value: settled_value, currency_code: settled_currency) }
120
+ let(:settled_value) { "10.00" }
121
+ let(:settled_currency) { "USD" }
122
+ let(:source) { paypal_payment_method.payment_source_class.create(paypal_order_id: SecureRandom.hex(8)) }
123
+ let(:action) { :purchase }
124
+
125
+ before { paypal_payment_method.update!(preferred_merchant_id: merchant_id) }
42
126
 
43
127
  it "sends a purchase request to paypal" do
44
128
  paypal_order_id = SecureRandom.hex(8)
45
129
  source = paypal_payment_method.payment_source_class.create(paypal_order_id: paypal_order_id)
46
130
  expect_request(:OrdersCaptureRequest).to receive(:new).with(paypal_order_id).and_call_original
47
- paypal_payment_method.purchase(1000, source, {})
131
+ paypal_payment_method.purchase(1000, source, currency: "USD")
132
+ end
133
+
134
+ it "records the capture when PayPal confirms it" do
135
+ expect(paypal_payment_method.purchase(1000, source, currency: "USD")).to be_success
136
+ expect(source.capture_id).to be_present
137
+ end
138
+
139
+ it "allows approvals for more than the amount" do
140
+ expect(paypal_payment_method.purchase(900, source, currency: "USD")).to be_success
141
+ end
142
+
143
+ it_behaves_like "refusing insufficient PayPal approvals", :OrdersCaptureRequest
144
+ it_behaves_like "refusing unconfirmed PayPal settlements"
145
+
146
+ context "when the approved order pays a different merchant" do
147
+ let(:payee_merchant_id) { "a-different-merchant" }
148
+
149
+ it "refuses the payment without capturing" do
150
+ source = paypal_payment_method.payment_source_class.create(paypal_order_id: SecureRandom.hex(8))
151
+ expect_request(:OrdersCaptureRequest).not_to receive(:new)
152
+ expect(paypal_payment_method.purchase(1000, source, currency: "USD").success?).to be(false)
153
+ end
154
+ end
155
+
156
+ context "when no merchant is configured" do
157
+ before { paypal_payment_method.update!(preferred_merchant_id: nil) }
158
+
159
+ it "does not verify the payee and captures as before" do
160
+ paypal_order_id = SecureRandom.hex(8)
161
+ source = paypal_payment_method.payment_source_class.create(paypal_order_id: paypal_order_id)
162
+ expect_request(:OrdersCaptureRequest).to receive(:new).with(paypal_order_id).and_call_original
163
+ expect(paypal_payment_method.purchase(1000, source, currency: "USD").success?).to be(true)
164
+ end
48
165
  end
49
166
  end
50
167
 
51
168
  describe "#authorize" do
52
- let(:result) { Struct(purchase_units: [Struct(payments: payments)]) }
53
- let(:payments) { Struct(authorizations: [Struct(id: SecureRandom.hex(4))]) }
169
+ let(:merchant_id) { SecureRandom.hex(6) }
170
+ let(:payee_merchant_id) { merchant_id }
171
+ let(:result) { Struct(purchase_units: [Struct(payee: Struct(merchant_id: payee_merchant_id), payments: payments, amount: approved_amount)]) }
172
+ let(:payments) { Struct(authorizations: [Struct(id: SecureRandom.hex(4), status: settlement_status, amount: settled_amount)]) }
173
+ let(:approved_amount) { Struct(value: "10.00", currency_code: approved_currency) }
174
+ let(:approved_currency) { "USD" }
175
+ let(:settlement_status) { "CREATED" }
176
+ let(:settled_amount) { Struct(value: settled_value, currency_code: settled_currency) }
177
+ let(:settled_value) { "10.00" }
178
+ let(:settled_currency) { "USD" }
179
+ let(:source) { paypal_payment_method.payment_source_class.create(paypal_order_id: SecureRandom.hex(8)) }
180
+ let(:action) { :authorize }
181
+
182
+ before { paypal_payment_method.update!(preferred_merchant_id: merchant_id) }
54
183
 
55
184
  it "sends an authorize request to paypal" do
56
185
  paypal_order_id = SecureRandom.hex(8)
57
186
  source = paypal_payment_method.payment_source_class.create(paypal_order_id: paypal_order_id)
58
187
  expect_request(:OrdersAuthorizeRequest).to receive(:new).with(paypal_order_id)
59
- paypal_payment_method.authorize(1000, source, {})
188
+ paypal_payment_method.authorize(1000, source, currency: "USD")
189
+ end
190
+
191
+ it "records the authorization when PayPal confirms it" do
192
+ expect(paypal_payment_method.authorize(1000, source, currency: "USD")).to be_success
193
+ expect(source.authorization_id).to be_present
194
+ end
195
+
196
+ it "allows approvals for more than the amount" do
197
+ expect(paypal_payment_method.authorize(900, source, currency: "USD")).to be_success
198
+ end
199
+
200
+ it_behaves_like "refusing insufficient PayPal approvals", :OrdersAuthorizeRequest
201
+ it_behaves_like "refusing unconfirmed PayPal settlements"
202
+
203
+ context "when the approved order pays a different merchant" do
204
+ let(:payee_merchant_id) { "a-different-merchant" }
205
+
206
+ it "refuses the payment without authorizing" do
207
+ source = paypal_payment_method.payment_source_class.create(paypal_order_id: SecureRandom.hex(8))
208
+ expect_request(:OrdersAuthorizeRequest).not_to receive(:new)
209
+ expect(paypal_payment_method.authorize(1000, source, currency: "USD").success?).to be(false)
210
+ end
211
+ end
212
+
213
+ context "when no merchant is configured" do
214
+ before { paypal_payment_method.update!(preferred_merchant_id: nil) }
215
+
216
+ it "does not verify the payee and authorizes as before" do
217
+ paypal_order_id = SecureRandom.hex(8)
218
+ source = paypal_payment_method.payment_source_class.create(paypal_order_id: paypal_order_id)
219
+ expect_request(:OrdersAuthorizeRequest).to receive(:new).with(paypal_order_id).and_call_original
220
+ expect(paypal_payment_method.authorize(1000, source, currency: "USD").success?).to be(true)
221
+ end
60
222
  end
61
223
  end
62
224
 
@@ -55,6 +55,106 @@ RSpec.describe SolidusPaypalCommercePlatform::PaypalAddress, type: :model do
55
55
  expect(subject).to be_present
56
56
  end
57
57
  end
58
+
59
+ context "with international addresses" do
60
+ context "with China address" do
61
+ let!(:country) do
62
+ create(
63
+ :country,
64
+ name: "China",
65
+ iso: "CN",
66
+ states_required: false
67
+ )
68
+ end
69
+ let!(:state) do
70
+ create(
71
+ :state,
72
+ name: "Beijing Shi",
73
+ abbr: "BJ",
74
+ country: country
75
+ )
76
+ end
77
+ let(:address) do
78
+ create(
79
+ :address,
80
+ name_attributes.merge({
81
+ address1: "123 Fuqian 2nd St",
82
+ address2: "",
83
+ city: "Beijing City",
84
+ state: state,
85
+ country: country,
86
+ zipcode: "100001"
87
+ })
88
+ )
89
+ end
90
+ let(:params) {
91
+ {
92
+ updated_address: {
93
+ admin_area_1: address.state.abbr,
94
+ admin_area_2: address.city,
95
+ address_line_1: address.address1,
96
+ address_line_2: address.address2,
97
+ postal_code: address.zipcode,
98
+ country_code: address.country.iso
99
+ },
100
+ recipient: {
101
+ name: {
102
+ given_name: "Alexander",
103
+ surname: "Hamilton"
104
+ }
105
+ }
106
+ }
107
+ }
108
+
109
+ context "with CN as country code" do
110
+ it "formats PayPal addresses correctly" do
111
+ order.ship_address = original_address
112
+
113
+ described_class.new(order).update(params)
114
+
115
+ expect(subject.state).to eq address.state
116
+ expect(subject.city).to eq address.city
117
+ expect(subject.address1).to eq address.address1
118
+ expect(subject.address2).to eq address.address2
119
+ expect(subject.zipcode).to eq address.zipcode
120
+ expect(subject.country).to eq address.country
121
+ if SolidusSupport.combined_first_and_last_name_in_address?
122
+ expect(subject.name).to eq address.name
123
+ else
124
+ expect(subject.firstname).to eq address.firstname
125
+ expect(subject.lastname).to eq address.lastname
126
+ end
127
+ expect(subject.phone).to eq original_address.phone
128
+ end
129
+ end
130
+
131
+ context "with C2 as country code" do
132
+ before do
133
+ country.update(iso: "C2")
134
+ end
135
+
136
+ it "formats PayPal addresses correctly" do
137
+ order.ship_address = original_address
138
+
139
+ described_class.new(order).update(params)
140
+
141
+ expect(subject.state).to eq address.state
142
+ expect(subject.city).to eq address.city
143
+ expect(subject.address1).to eq address.address1
144
+ expect(subject.address2).to eq address.address2
145
+ expect(subject.zipcode).to eq address.zipcode
146
+ expect(subject.country).to eq address.country
147
+ if SolidusSupport.combined_first_and_last_name_in_address?
148
+ expect(subject.name).to eq address.name
149
+ else
150
+ expect(subject.firstname).to eq address.firstname
151
+ expect(subject.lastname).to eq address.lastname
152
+ end
153
+ expect(subject.phone).to eq original_address.phone
154
+ end
155
+ end
156
+ end
157
+ end
58
158
  end
59
159
 
60
160
  def name_attributes
@@ -70,6 +70,24 @@ RSpec.describe SolidusPaypalCommercePlatform::PaypalOrder, type: :model do
70
70
  )
71
71
  end
72
72
  end
73
+
74
+ context 'when an order has an inapplicable promo adjustment' do
75
+ let(:order) { create :order_with_line_items }
76
+ let(:best_promotion) { create(:promotion, :with_line_item_adjustment, adjustment_rate: 2.5) }
77
+ let(:worst_promotion) { create(:promotion, :with_line_item_adjustment, adjustment_rate: 1) }
78
+
79
+ before do
80
+ best_promotion.activate(order: order)
81
+ worst_promotion.activate(order: order)
82
+ order.recalculate
83
+ end
84
+
85
+ it 'does not include it in the paypal order breakdown' do
86
+ expect(to_json.dig(:purchase_units, 0).dig(:amount, :breakdown)).to match hash_including(
87
+ discount: { currency_code: "USD", value: "2.50" }
88
+ )
89
+ end
90
+ end
73
91
  end
74
92
 
75
93
  private
@@ -33,4 +33,76 @@ RSpec.describe SolidusPaypalCommercePlatform::OrdersController, type: :request d
33
33
  end
34
34
  end
35
35
  end
36
+
37
+ describe "CSRF protection" do
38
+ # The test environment disables forgery protection, so enable it here.
39
+ around do |example|
40
+ original = ActionController::Base.allow_forgery_protection
41
+ ActionController::Base.allow_forgery_protection = true
42
+ example.run
43
+ ensure
44
+ ActionController::Base.allow_forgery_protection = original
45
+ end
46
+
47
+ let!(:store) { create(:store) }
48
+ let(:variant) { create(:variant) }
49
+ let(:json_body) { { order: { line_items_attributes: [{ variant_id: variant.id, quantity: 1 }] } }.to_json }
50
+
51
+ def csrf_token
52
+ get '/'
53
+ response.body[/<meta name="csrf-token" content="([^"]+)"/, 1]
54
+ end
55
+
56
+ # Depending on the Rails version, the test environment either raises or
57
+ # renders the error.
58
+ def rejected?
59
+ yield
60
+ response.status == 422
61
+ rescue ActionController::InvalidAuthenticityToken
62
+ true
63
+ end
64
+
65
+ ['/solidus_paypal_commerce_platform/orders.json', '/solidus_paypal_commerce_platform/orders?format=json'].each do |path|
66
+ it "rejects a cross-site form POST to #{path}" do
67
+ form_body = "order[line_items_attributes][0][variant_id]=#{variant.id}&" \
68
+ "order[line_items_attributes][0][quantity]=1"
69
+
70
+ expect {
71
+ expect(
72
+ rejected? {
73
+ post path, params: form_body, headers: {
74
+ 'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
75
+ 'Origin' => 'http://attacker.example'
76
+ }
77
+ }
78
+ ).to be(true)
79
+ }.not_to change(Spree::Order, :count)
80
+ end
81
+ end
82
+
83
+ it "accepts a JSON POST with the token in the X-CSRF-Token header" do
84
+ token = csrf_token
85
+
86
+ expect {
87
+ post '/solidus_paypal_commerce_platform/orders', params: json_body, headers: {
88
+ 'CONTENT_TYPE' => 'application/json',
89
+ 'X-CSRF-Token' => token
90
+ }
91
+ }.to change(Spree::Order, :count).by(1)
92
+
93
+ expect(response).to have_http_status(:ok)
94
+ end
95
+
96
+ it "rejects a JSON POST without a token" do
97
+ expect {
98
+ expect(
99
+ rejected? {
100
+ post '/solidus_paypal_commerce_platform/orders', params: json_body, headers: {
101
+ 'CONTENT_TYPE' => 'application/json'
102
+ }
103
+ }
104
+ ).to be(true)
105
+ }.not_to change(Spree::Order, :count)
106
+ end
107
+ end
36
108
  end
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'solidus_starter_frontend_spec_helper'
3
+ begin
4
+ require 'solidus_storefront_spec_helper'
5
+ rescue LoadError
6
+ require 'solidus_starter_frontend_spec_helper'
7
+ end
4
8
 
5
9
  Dir["#{__dir__}/support/solidus_paypal_commerce_platform/**/*.rb"].sort.each { |f| require f }
@@ -24,7 +24,7 @@ RSpec.describe "creating a new payment" do
24
24
  end
25
25
 
26
26
  it "displays the onboarding button", :js do
27
- visit "/admin/payment_methods"
27
+ visit "/admin/payment_methods?solidus_admin=false"
28
28
 
29
29
  # main_window = current_window
30
30
 
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_paypal_commerce_platform
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Denny
8
8
  - Elia Schito
9
- autorequire:
10
9
  bindir: exe
11
10
  cert_chain: []
12
- date: 2023-12-22 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: deface
@@ -45,7 +44,7 @@ dependencies:
45
44
  requirements:
46
45
  - - ">="
47
46
  - !ruby/object:Gem::Version
48
- version: '3.0'
47
+ version: '4.1'
49
48
  - - "<"
50
49
  - !ruby/object:Gem::Version
51
50
  version: '5.0'
@@ -55,7 +54,7 @@ dependencies:
55
54
  requirements:
56
55
  - - ">="
57
56
  - !ruby/object:Gem::Version
58
- version: '3.0'
57
+ version: '4.1'
59
58
  - - "<"
60
59
  - !ruby/object:Gem::Version
61
60
  version: '5.0'
@@ -65,14 +64,14 @@ dependencies:
65
64
  requirements:
66
65
  - - ">="
67
66
  - !ruby/object:Gem::Version
68
- version: 0.8.0
67
+ version: 0.14.0
69
68
  type: :runtime
70
69
  prerelease: false
71
70
  version_requirements: !ruby/object:Gem::Requirement
72
71
  requirements:
73
72
  - - ">="
74
73
  - !ruby/object:Gem::Version
75
- version: 0.8.0
74
+ version: 0.14.0
76
75
  - !ruby/object:Gem::Dependency
77
76
  name: solidus_webhooks
78
77
  requirement: !ruby/object:Gem::Requirement
@@ -115,15 +114,14 @@ dependencies:
115
114
  - - "~>"
116
115
  - !ruby/object:Gem::Version
117
116
  version: '2.5'
118
- description:
119
117
  email: contact@solidus.io
120
118
  executables: []
121
119
  extensions: []
122
120
  extra_rdoc_files: []
123
121
  files:
124
- - ".circleci/config.yml"
125
122
  - ".gem_release.yml"
126
123
  - ".github/stale.yml"
124
+ - ".github/workflows/test.yml"
127
125
  - ".github_changelog_generator"
128
126
  - ".gitignore"
129
127
  - ".rspec"
@@ -137,8 +135,6 @@ files:
137
135
  - app/assets/javascripts/spree/backend/solidus_paypal_commerce_platform/namespace.js
138
136
  - app/assets/javascripts/spree/backend/solidus_paypal_commerce_platform/wizard.js
139
137
  - app/assets/stylesheets/spree/backend/solidus_paypal_commerce_platform.css
140
- - app/decorators/models/solidus_paypal_commerce_platform/spree/address_decorator.rb
141
- - app/decorators/models/solidus_paypal_commerce_platform/spree/variant/pricing_options_decorator.rb
142
138
  - app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb
143
139
  - app/jobs/solidus_paypal_commerce_platform/application_job.rb
144
140
  - app/jobs/solidus_paypal_commerce_platform/webhook_job.rb
@@ -155,6 +151,8 @@ files:
155
151
  - app/overrides/spree/orders/edit/insert_cart_buttons.rb
156
152
  - app/overrides/spree/payments/payment/add_paypal_email_to_payment.rb
157
153
  - app/overrides/spree/products/cart_form/insert_product_buttons.rb
154
+ - app/patches/models/solidus_paypal_commerce_platform/spree/address_patch.rb
155
+ - app/patches/models/solidus_paypal_commerce_platform/spree/variant/pricing_options_patch.rb
158
156
  - app/views/solidus_paypal_commerce_platform/admin/payment_methods/_available_to_users.html.erb
159
157
  - app/views/solidus_paypal_commerce_platform/admin/payment_methods/_paypal_wizard.html.erb
160
158
  - app/views/solidus_paypal_commerce_platform/admin/payment_methods/_setup_wizards.html.erb
@@ -265,7 +263,6 @@ metadata:
265
263
  source_code_uri: https://github.com/solidusio/solidus_paypal_commerce_platform
266
264
  changelog_uri: https://github.com/solidusio/solidus_paypal_commerce_platform/releases
267
265
  rubygems_mfa_required: 'true'
268
- post_install_message:
269
266
  rdoc_options: []
270
267
  require_paths:
271
268
  - lib
@@ -273,15 +270,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
273
270
  requirements:
274
271
  - - ">="
275
272
  - !ruby/object:Gem::Version
276
- version: '3.0'
273
+ version: '3.1'
277
274
  required_rubygems_version: !ruby/object:Gem::Requirement
278
275
  requirements:
279
276
  - - ">="
280
277
  - !ruby/object:Gem::Version
281
278
  version: '0'
282
279
  requirements: []
283
- rubygems_version: 3.4.10
284
- signing_key:
280
+ rubygems_version: 4.0.20
285
281
  specification_version: 4
286
282
  summary: Integrate Solidus with Paypal Commerce Platform
287
283
  test_files: []
data/.circleci/config.yml DELETED
@@ -1,80 +0,0 @@
1
- version: 2.1
2
-
3
- orbs:
4
- browser-tools: circleci/browser-tools@1.1
5
-
6
- # Always take the latest version of the orb, this allows us to
7
- # run specs against Solidus supported versions only without the need
8
- # to change this configuration every time a Solidus version is released
9
- # or goes EOL.
10
- solidusio_extensions: solidusio/extensions@volatile
11
-
12
- commands:
13
- test-with-starter-frontend:
14
- steps:
15
- - checkout
16
- - browser-tools/install-chrome
17
- - run:
18
- name: Install libvips
19
- command: |
20
- sudo apt-get update
21
- sudo apt-get install -yq libvips-dev
22
- - solidusio_extensions/test-branch:
23
- branch: main
24
- command: |
25
- export FRONTEND=starter
26
- sudo gem update --system
27
- gem install bundler rails
28
- bin/dummy-app
29
- bin/rspec
30
- - solidusio_extensions/store-test-results
31
-
32
- jobs:
33
- run-specs-with-postgres:
34
- executor:
35
- name: solidusio_extensions/postgres
36
- ruby_version: '3.2'
37
- steps:
38
- - test-with-starter-frontend
39
-
40
- run-specs-with-mysql:
41
- executor:
42
- name: solidusio_extensions/mysql
43
- ruby_version: '3.1'
44
- steps:
45
- - test-with-starter-frontend
46
-
47
- run-specs-with-sqlite:
48
- executor:
49
- name: solidusio_extensions/sqlite
50
- ruby_version: '3.0'
51
- steps:
52
- - test-with-starter-frontend
53
-
54
- lint-code:
55
- executor:
56
- name: solidusio_extensions/sqlite-memory
57
- ruby_version: '3.0'
58
- steps:
59
- - solidusio_extensions/lint-code
60
-
61
- workflows:
62
- "Run specs on supported Solidus versions":
63
- jobs:
64
- - run-specs-with-postgres
65
- - run-specs-with-mysql
66
- - run-specs-with-sqlite
67
- - lint-code
68
-
69
- "Weekly run specs against main":
70
- triggers:
71
- - schedule:
72
- cron: "0 0 * * 4" # every Thursday
73
- filters:
74
- branches:
75
- only:
76
- - main
77
- jobs:
78
- - run-specs-with-postgres
79
- - run-specs-with-mysql
80
- - run-specs-with-sqlite