solidus_paypal_braintree 0.2.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +40 -0
  3. data/.gem_release.yml +5 -0
  4. data/.github/stale.yml +17 -0
  5. data/.gitignore +18 -0
  6. data/.rspec +2 -0
  7. data/.rubocop.yml +76 -0
  8. data/CHANGELOG.md +258 -0
  9. data/Gemfile +41 -0
  10. data/LICENSE +2 -2
  11. data/README.md +208 -48
  12. data/Rakefile +4 -28
  13. data/app/assets/config/solidus_paypal_braintree_manifest.js +1 -0
  14. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_blue_button_280x48.svg +19 -0
  15. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_blue_button_320x48.svg +19 -0
  16. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_blue_button_375x48.svg +19 -0
  17. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_white_button_280x48.svg +19 -0
  18. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_white_button_320x48.svg +19 -0
  19. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_white_button_375x48.svg +19 -0
  20. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_blue_acceptance_mark.svg +15 -0
  21. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_blue_button_280x48.svg +19 -0
  22. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_blue_button_320x48.svg +19 -0
  23. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_blue_button_375x48.svg +19 -0
  24. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_blue_logo.svg +18 -0
  25. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_white_acceptance_mark.svg +20 -0
  26. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_white_button_280x48.svg +19 -0
  27. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_white_button_320x48.svg +19 -0
  28. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_white_button_375x48.svg +19 -0
  29. data/app/assets/images/solidus_paypal_braintree/venmo/venmo_white_logo.svg +18 -0
  30. data/app/assets/javascripts/solidus_paypal_braintree/checkout.js +32 -3
  31. data/app/assets/javascripts/solidus_paypal_braintree/client.js +58 -5
  32. data/app/assets/javascripts/solidus_paypal_braintree/constants.js +36 -5
  33. data/app/assets/javascripts/solidus_paypal_braintree/frontend.js +2 -0
  34. data/app/assets/javascripts/solidus_paypal_braintree/hosted_form.js +15 -5
  35. data/app/assets/javascripts/solidus_paypal_braintree/paypal_button.js +90 -26
  36. data/app/assets/javascripts/solidus_paypal_braintree/paypal_messaging.js +22 -0
  37. data/app/assets/javascripts/solidus_paypal_braintree/venmo_button.js +86 -0
  38. data/app/assets/javascripts/spree/backend/solidus_paypal_braintree.js +2 -2
  39. data/app/assets/javascripts/spree/frontend/paypal_button.js +15 -13
  40. data/app/assets/stylesheets/spree/frontend/solidus_paypal_braintree.css +12 -0
  41. data/app/decorators/controllers/solidus_paypal_braintree/admin_payments_controller_decorator.rb +11 -0
  42. data/app/decorators/controllers/solidus_paypal_braintree/checkout_controller_decorator.rb +11 -0
  43. data/app/decorators/controllers/solidus_paypal_braintree/client_tokens_controller.rb +41 -0
  44. data/app/decorators/controllers/solidus_paypal_braintree/orders_controller_decorator.rb +11 -0
  45. data/app/decorators/models/solidus_paypal_braintree/spree/store_decorator.rb +20 -0
  46. data/app/decorators/models/solidus_paypal_braintree/spree/user_decorator.rb +13 -0
  47. data/app/helpers/solidus_paypal_braintree/braintree_admin_helper.rb +23 -0
  48. data/app/helpers/solidus_paypal_braintree/braintree_checkout_helper.rb +60 -0
  49. data/app/models/application_record.rb +2 -0
  50. data/app/models/solidus_paypal_braintree/address.rb +64 -0
  51. data/app/models/solidus_paypal_braintree/avs_result.rb +69 -0
  52. data/app/models/solidus_paypal_braintree/configuration.rb +39 -3
  53. data/app/models/solidus_paypal_braintree/customer.rb +7 -3
  54. data/app/models/solidus_paypal_braintree/gateway.rb +150 -43
  55. data/app/models/solidus_paypal_braintree/response.rb +49 -21
  56. data/app/models/solidus_paypal_braintree/source.rb +70 -10
  57. data/app/models/solidus_paypal_braintree/transaction.rb +3 -2
  58. data/app/models/solidus_paypal_braintree/transaction_address.rb +36 -15
  59. data/app/models/solidus_paypal_braintree/transaction_import.rb +18 -12
  60. data/app/overrides/spree/payments/payment/add_paypal_funding_source_to_payment.rb +9 -0
  61. data/app/views/spree/api/payments/source_views/_paypal_braintree.json.jbuilder +3 -0
  62. data/app/views/spree/checkout/existing_payment/_paypal_braintree.html.erb +10 -0
  63. data/app/views/spree/shared/_apple_pay_button.html.erb +27 -0
  64. data/app/views/spree/shared/_braintree_errors.html.erb +16 -0
  65. data/app/views/spree/shared/_braintree_hosted_fields.html.erb +25 -8
  66. data/app/views/spree/shared/_paypal_braintree_head_scripts.html.erb +26 -0
  67. data/app/views/spree/shared/_paypal_cart_button.html.erb +38 -0
  68. data/app/views/spree/shared/_paypal_messaging.html.erb +13 -0
  69. data/app/views/spree/shared/_venmo_button.html.erb +33 -0
  70. data/bin/console +17 -0
  71. data/bin/rails +15 -0
  72. data/bin/setup +8 -0
  73. data/config/locales/en.yml +66 -0
  74. data/config/locales/it.yml +56 -0
  75. data/config/routes.rb +2 -0
  76. data/db/migrate/20160906201711_create_solidus_paypal_braintree_customers.rb +3 -1
  77. data/db/migrate/20161125172005_add_braintree_configuration_to_stores.rb +5 -7
  78. data/db/migrate/20170505193712_add_null_constraint_to_sources.rb +3 -1
  79. data/db/migrate/20190705115327_add_paypal_button_preferences_to_braintree_configurations.rb +5 -0
  80. data/db/migrate/20190911141712_add_3d_secure_to_braintree_configuration.rb +5 -0
  81. data/db/migrate/20211222170950_add_paypal_funding_source_to_solidus_paypal_braintree_sources.rb +5 -0
  82. data/db/migrate/20220104150301_add_venmo_to_braintree_configuration.rb +5 -0
  83. data/lib/controllers/backend/solidus_paypal_braintree/configurations_controller.rb +23 -5
  84. data/lib/controllers/frontend/solidus_paypal_braintree/checkouts_controller.rb +25 -21
  85. data/lib/controllers/frontend/solidus_paypal_braintree/transactions_controller.rb +56 -50
  86. data/lib/generators/solidus_paypal_braintree/install/install_generator.rb +7 -6
  87. data/lib/solidus_paypal_braintree/country_mapper.rb +4 -2
  88. data/lib/solidus_paypal_braintree/engine.rb +34 -22
  89. data/lib/solidus_paypal_braintree/factories.rb +41 -6
  90. data/lib/solidus_paypal_braintree/request_protection.rb +21 -0
  91. data/lib/solidus_paypal_braintree/version.rb +3 -1
  92. data/lib/solidus_paypal_braintree.rb +5 -1
  93. data/lib/views/backend/solidus_paypal_braintree/configurations/list.html.erb +38 -5
  94. data/lib/views/backend/spree/admin/payments/source_forms/_paypal_braintree.html.erb +2 -2
  95. data/lib/views/backend/spree/admin/payments/source_views/_paypal_braintree.html.erb +7 -2
  96. data/lib/views/backend/spree/admin/shared/preference_fields/_preference_select.html.erb +13 -0
  97. data/lib/views/backend_v1.2/spree/admin/payments/source_forms/_paypal_braintree.html.erb +2 -2
  98. data/lib/views/backend_v2.4/spree/admin/shared/preference_fields/_hash.html.erb +12 -0
  99. data/lib/views/frontend/solidus_paypal_braintree/payments/_payment.html.erb +12 -0
  100. data/lib/views/frontend/spree/checkout/payment/_paypal_braintree.html.erb +10 -77
  101. data/lib/views/frontend/spree/shared/_paypal_checkout_button.html.erb +32 -0
  102. data/solidus_paypal_braintree.gemspec +43 -0
  103. data/spec/controllers/solidus_paypal_braintree/checkouts_controller_spec.rb +99 -0
  104. data/spec/controllers/solidus_paypal_braintree/client_tokens_controller_spec.rb +55 -0
  105. data/spec/controllers/solidus_paypal_braintree/configurations_controller_spec.rb +73 -0
  106. data/spec/controllers/solidus_paypal_braintree/transactions_controller_spec.rb +183 -0
  107. data/spec/features/backend/configuration_spec.rb +23 -0
  108. data/spec/features/backend/new_payment_spec.rb +137 -0
  109. data/spec/features/frontend/braintree_credit_card_checkout_spec.rb +191 -0
  110. data/spec/features/frontend/paypal_checkout_spec.rb +166 -0
  111. data/spec/features/frontend/venmo_checkout_spec.rb +189 -0
  112. data/spec/fixtures/cassettes/admin/invalid_credit_card.yml +63 -0
  113. data/spec/fixtures/cassettes/admin/resubmit_credit_card.yml +352 -0
  114. data/spec/fixtures/cassettes/admin/valid_credit_card.yml +412 -0
  115. data/spec/fixtures/cassettes/braintree/create_profile.yml +71 -0
  116. data/spec/fixtures/cassettes/braintree/generate_token.yml +63 -0
  117. data/spec/fixtures/cassettes/braintree/token.yml +63 -0
  118. data/spec/fixtures/cassettes/checkout/invalid_credit_card.yml +63 -0
  119. data/spec/fixtures/cassettes/checkout/resubmit_credit_card.yml +216 -0
  120. data/spec/fixtures/cassettes/checkout/update.yml +71 -0
  121. data/spec/fixtures/cassettes/checkout/valid_credit_card.yml +156 -0
  122. data/spec/fixtures/cassettes/checkout/valid_venmo_transaction.yml +599 -0
  123. data/spec/fixtures/cassettes/gateway/authorize/credit_card/address.yml +86 -0
  124. data/spec/fixtures/cassettes/gateway/authorize/merchant_account/EUR.yml +154 -0
  125. data/spec/fixtures/cassettes/gateway/authorize/paypal/EUR.yml +90 -0
  126. data/spec/fixtures/cassettes/gateway/authorize/paypal/address.yml +90 -0
  127. data/spec/fixtures/cassettes/gateway/authorize.yml +86 -0
  128. data/spec/fixtures/cassettes/gateway/authorized_transaction.yml +73 -0
  129. data/spec/fixtures/cassettes/gateway/cancel/missing.yml +63 -0
  130. data/spec/fixtures/cassettes/gateway/cancel/refunds.yml +272 -0
  131. data/spec/fixtures/cassettes/gateway/cancel/void.yml +201 -0
  132. data/spec/fixtures/cassettes/gateway/capture.yml +141 -0
  133. data/spec/fixtures/cassettes/gateway/complete.yml +157 -0
  134. data/spec/fixtures/cassettes/gateway/credit.yml +208 -0
  135. data/spec/fixtures/cassettes/gateway/purchase.yml +87 -0
  136. data/spec/fixtures/cassettes/gateway/settled_transaction.yml +140 -0
  137. data/spec/fixtures/cassettes/gateway/void.yml +137 -0
  138. data/spec/fixtures/cassettes/source/card_type.yml +267 -0
  139. data/spec/fixtures/cassettes/source/last4.yml +267 -0
  140. data/spec/fixtures/cassettes/transaction/import/valid/capture.yml +224 -0
  141. data/spec/fixtures/cassettes/transaction/import/valid.yml +71 -0
  142. data/spec/fixtures/views/spree/orders/edit.html.erb +50 -0
  143. data/spec/helpers/solidus_paypal_braintree/braintree_admin_helper_spec.rb +17 -0
  144. data/spec/helpers/solidus_paypal_braintree/braintree_checkout_helper_spec.rb +70 -0
  145. data/spec/models/solidus_paypal_braintree/address_spec.rb +71 -0
  146. data/spec/models/solidus_paypal_braintree/avs_result_spec.rb +317 -0
  147. data/spec/models/solidus_paypal_braintree/gateway_spec.rb +692 -0
  148. data/spec/models/solidus_paypal_braintree/response_spec.rb +280 -0
  149. data/spec/models/solidus_paypal_braintree/source_spec.rb +499 -0
  150. data/spec/models/solidus_paypal_braintree/transaction_address_spec.rb +235 -0
  151. data/spec/models/solidus_paypal_braintree/transaction_import_spec.rb +300 -0
  152. data/spec/models/solidus_paypal_braintree/transaction_spec.rb +85 -0
  153. data/spec/models/spree/store_spec.rb +14 -0
  154. data/spec/requests/spree/api/orders_controller_spec.rb +36 -0
  155. data/spec/spec_helper.rb +29 -0
  156. data/spec/support/capybara.rb +7 -0
  157. data/spec/support/factories.rb +2 -0
  158. data/spec/support/gateway_helpers.rb +29 -0
  159. data/spec/support/order_ready_for_payment.rb +37 -0
  160. data/spec/support/vcr.rb +42 -0
  161. data/spec/support/views.rb +1 -0
  162. metadata +226 -166
  163. data/app/controllers/solidus_paypal_braintree/client_tokens_controller.rb +0 -21
  164. data/app/helpers/braintree_admin_helper.rb +0 -18
  165. data/app/models/spree/store_decorator.rb +0 -11
  166. data/app/overrides/admin_navigation_menu.rb +0 -6
  167. data/config/initializers/braintree.rb +0 -1
  168. data/lib/views/backend/solidus_paypal_braintree/configurations/_admin_tab.html.erb +0 -3
@@ -0,0 +1,12 @@
1
+ <% label = local_assigns[:label].presence %>
2
+ <% html_options = {class: 'input_hash fullwidth'}.update(local_assigns[:html_options] || {}) %>
3
+
4
+ <div class="field">
5
+ <% if local_assigns[:form] %>
6
+ <%= form.label attribute, label %>
7
+ <%= form.text_area attribute, html_options %>
8
+ <% else %>
9
+ <%= label_tag name, label %>
10
+ <%= text_area_tag name, value, html_options %>
11
+ <% end %>
12
+ </div>
@@ -0,0 +1,12 @@
1
+ <br />
2
+ <%= payment.source.display_payment_type %>
3
+
4
+ <% if payment.source.paypal? %>
5
+ <% if payment.source.respond_to?(:paypal_funding_source) && payment.source.paypal_funding_source.present? %>
6
+ <br />
7
+ <%= t('spree.paypal_funding', funding: payment.source.display_paypal_funding_source) %>
8
+ <% end %>
9
+ <% elsif payment.source.venmo? %>
10
+ <br />
11
+ <%= payment.source.source_description %>
12
+ <% end %>
@@ -1,90 +1,23 @@
1
- <% address = current_order.ship_address %>
2
1
  <% id = payment_method.id %>
3
2
 
4
- <% content_for :head do %>
5
- <script src="https://js.braintreegateway.com/web/3.14.0/js/client.min.js"></script>
6
- <script src="https://js.braintreegateway.com/web/3.14.0/js/data-collector.min.js"></script>
7
-
8
- <% if current_store.braintree_configuration.paypal? %>
9
- <script src="https://js.braintreegateway.com/web/3.14.0/js/paypal.min.js"></script>
10
- <% end %>
11
-
12
- <% if current_store.braintree_configuration.credit_card? %>
13
- <script src="https://js.braintreegateway.com/web/3.14.0/js/hosted-fields.min.js"></script>
14
- <% end %>
15
-
16
- <% if current_store.braintree_configuration.apple_pay? %>
17
- <script src="https://js.braintreegateway.com/web/3.14.0/js/apple-pay.min.js"></script>
18
- <% end %>
19
-
20
- <%= javascript_include_tag "solidus_paypal_braintree/checkout" %>
21
- <% end %>
3
+ <%= render "spree/shared/paypal_braintree_head_scripts" %>
22
4
 
23
5
  <% if current_store.braintree_configuration.paypal? %>
24
- <script src="https://www.paypalobjects.com/api/button.js?"
25
- data-merchant="braintree"
26
- data-id="paypal-button"
27
- data-button="checkout"
28
- data-color="blue"
29
- data-size="medium"
30
- data-shape="pill"
31
- data-button_type="button"
32
- data-button_disabled="true"
33
- ></script>
34
-
35
- <script>
36
- var address = {
37
- line1: '<%= address.address1 %>',
38
- line2: '<%= address.address2 %>',
39
- city: '<%= address.city %>',
40
- state: '<%= address.state.name %>',
41
- postalCode: '<%= address.zipcode %>',
42
- countryCode: '<%= address.country.iso %>',
43
- phone: '<%= address.phone %>',
44
- recipientName: '<%= "#{address.firstname} #{address.lastname}" %>'
45
- }
46
-
47
- var paypalOptions = {
48
- flow: 'vault',
49
- enableShippingAddress: true,
50
- shippingAddressOverride: address,
51
- shippingAddressEditable: false
52
- }
53
-
54
- var button = new SolidusPaypalBraintree.createPaypalButton(document.querySelector("#paypal-button"), paypalOptions);
55
- button.initialize();
56
- </script>
6
+ <%= render "spree/shared/paypal_checkout_button" %>
57
7
  <% end %>
58
8
 
59
9
  <% if current_store.braintree_configuration.credit_card? %>
60
10
  <fieldset class="braintree-hosted-fields" data-braintree-hosted-fields data-id="<%= id %>">
61
- <%= render "spree/shared/braintree_hosted_fields", id: id %>
11
+ <%= render "spree/shared/braintree_hosted_fields", payment_method: payment_method %>
62
12
  </fieldset>
63
13
  <% end %>
64
14
 
65
15
  <% if current_store.braintree_configuration.apple_pay? %>
66
- <button id="apple-pay-button" class="apple-pay-button"></button>
67
- <script>
68
- var applePayButtonElement = document.getElementById('apple-pay-button');
69
- var applePayOptions = {
70
- paymentMethodId: <%= id %>,
71
- storeName: "<%= current_store.name %>",
72
- orderEmail: "<%= current_order.email %>",
73
- amount: "<%= current_order.total %>",
74
- shippingContact: {
75
- emailAddress: '<%= current_order.email %>',
76
- familyName: '<%= address.firstname %>',
77
- givenName: '<%= address.lastname %>',
78
- phoneNumber: '<%= address.phone %>',
79
- addressLines: ['<%= address.address1 %>','<%= address.address2 %>'],
80
- locality: '<%= address.city %>',
81
- administrativeArea: '<%= address.state.name %>',
82
- postalCode: '<%= address.zipcode %>',
83
- country: '<%= address.country.name %>',
84
- countryCode: '<%= address.country.iso %>'
85
- }
86
- };
87
- var button = new SolidusPaypalBraintree.createApplePayButton(applePayButtonElement, applePayOptions);
88
- button.initialize();
89
- </script>
16
+ <%= render "spree/shared/apple_pay_button", id: id %>
90
17
  <% end %>
18
+
19
+ <% if current_store.braintree_configuration.venmo? %>
20
+ <%= render "spree/shared/venmo_button", payment_method: payment_method %>
21
+ <% end %>
22
+
23
+ <%= render "spree/shared/braintree_errors" %>
@@ -0,0 +1,32 @@
1
+ <div id="paypal-button"></div>
2
+ <div data-pp-message data-pp-placement="payment" data-pp-amount="<%= @order.total %>"></div>
3
+
4
+ <script>
5
+ var address = <%= sanitize SolidusPaypalBraintree::Address.new(current_order.ship_address).to_json %>
6
+
7
+ var paypalOptions = {
8
+ flow: '<%= SolidusPaypalBraintree::Gateway.first.preferred_paypal_flow %>',
9
+ amount: '<%= current_order.total %>',
10
+ currency: '<%= current_order.currency %>',
11
+ enableShippingAddress: true,
12
+ venmoFunding: <%= SolidusPaypalBraintree::Gateway.first.preferred_enable_venmo_funding %>,
13
+ buyerCountry: '<%= SolidusPaypalBraintree::Gateway.first.preferred_force_buyer_country %>',
14
+ shippingAddressOverride: address,
15
+ shippingAddressEditable: false,
16
+ environment: '<%= Rails.env.production? ? "production" : "sandbox" %>',
17
+ locale: '<%= paypal_button_preference(:paypal_button_locale, store: current_store) %>',
18
+ style: {
19
+ color: '<%= paypal_button_preference(:paypal_button_color, store: current_store) %>',
20
+ shape: '<%= paypal_button_preference(:paypal_button_shape, store: current_store) %>',
21
+ label: '<%= paypal_button_preference(:paypal_button_label, store: current_store) %>',
22
+ layout: '<%= paypal_button_preference(:paypal_button_layout, store: current_store) %>',
23
+ <% if paypal_button_preference(:paypal_button_layout, store: current_store) == "horizontal" %>
24
+ tagline: '<%= paypal_button_preference(:paypal_button_tagline, store: current_store) %>',
25
+ <% end %>
26
+ messaging: '<%= paypal_button_preference(:paypal_button_messaging, store: current_store) %>'
27
+ }
28
+ }
29
+
30
+ var button = new SolidusPaypalBraintree.createPaypalButton(document.querySelector("#paypal-button"), paypalOptions);
31
+ button.initialize();
32
+ </script>
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ $:.push File.expand_path('lib', __dir__)
4
+ require 'solidus_paypal_braintree/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'solidus_paypal_braintree'
8
+ s.version = SolidusPaypalBraintree::VERSION
9
+ s.summary = 'Officially supported Paypal/Braintree extension'
10
+ s.description = 'Uses the javascript API for seamless braintree payments'
11
+ s.license = 'BSD-3-Clause'
12
+
13
+ s.author = 'Stembolt'
14
+ s.email = 'braintree+gemfile@stembolt.com'
15
+ s.homepage = 'https://github.com/solidusio/solidus_paypal_braintree'
16
+
17
+ s.required_ruby_version = '>= 2.5'
18
+
19
+ s.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ s.test_files = Dir['spec/**/*']
23
+ s.bindir = "exe"
24
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+
27
+ if s.respond_to?(:metadata)
28
+ s.metadata["homepage_uri"] = s.homepage if s.homepage
29
+ s.metadata["source_code_uri"] = s.homepage if s.homepage
30
+ s.metadata["rubygems_mfa_required"] = 'true'
31
+ end
32
+
33
+ s.add_dependency 'activemerchant', '~> 1.48'
34
+ s.add_dependency 'braintree', '~> 3.4'
35
+ s.add_dependency 'solidus_api', ['>= 2.0.0', '< 4']
36
+ s.add_dependency 'solidus_core', ['>= 2.0.0', '< 4']
37
+ s.add_dependency 'solidus_support', ['>= 0.8.1', '< 1']
38
+
39
+ s.add_development_dependency 'selenium-webdriver'
40
+ s.add_development_dependency 'solidus_dev_support'
41
+ s.add_development_dependency 'vcr'
42
+ s.add_development_dependency 'webmock'
43
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+ require 'support/order_ready_for_payment'
3
+
4
+ RSpec.describe SolidusPaypalBraintree::CheckoutsController, type: :controller do
5
+ routes { SolidusPaypalBraintree::Engine.routes }
6
+
7
+ include_context 'when order is ready for payment'
8
+
9
+ describe 'PATCH update' do
10
+ subject(:patch_update) { patch :update, params: params }
11
+
12
+ let(:params) do
13
+ {
14
+ "state" => "payment",
15
+ "order" => {
16
+ "payments_attributes" => [
17
+ {
18
+ "payment_method_id" => payment_method.id,
19
+ "source_attributes" => {
20
+ "nonce" => "fake-paypal-billing-agreement-nonce",
21
+ "payment_type" => SolidusPaypalBraintree::Source::PAYPAL
22
+ }
23
+ }
24
+ ],
25
+ "use_billing" => "1",
26
+ "use_postmates_shipping" => "0"
27
+ },
28
+ "reuse_credit" => "1",
29
+ "order_bill_address" => "",
30
+ "reuse_bill_address" => "1"
31
+ }
32
+ end
33
+
34
+ let!(:payment_method) do
35
+ create_gateway
36
+ end
37
+
38
+ before do
39
+ allow(controller).to receive(:try_spree_current_user) { user }
40
+ allow(controller).to receive(:spree_current_user) { user }
41
+ allow(controller).to receive(:current_order) { order }
42
+ end
43
+
44
+ context "when a payment is created successfully", vcr: {
45
+ cassette_name: 'checkout/update',
46
+ match_requests_on: [:braintree_uri]
47
+ } do
48
+ it 'creates a payment' do
49
+ expect { patch_update }.
50
+ to change { order.payments.count }.
51
+ from(0).
52
+ to(1)
53
+ end
54
+
55
+ it 'creates a payment source' do
56
+ expect { patch_update }.
57
+ to change { SolidusPaypalBraintree::Source.count }.
58
+ from(0).
59
+ to(1)
60
+ end
61
+
62
+ it 'assigns @order' do
63
+ patch_update
64
+ expect(assigns(:order)).to eq order
65
+ end
66
+
67
+ it "is successful" do
68
+ expect(patch_update).to be_successful
69
+ end
70
+
71
+ it "renders 'ok'" do
72
+ expect(patch_update.body).to eql("ok")
73
+ end
74
+ end
75
+
76
+ context "when a payment is not created successfully" do
77
+ before do
78
+ allow_any_instance_of(::Spree::Payment).to receive(:save).and_return(false)
79
+ end
80
+
81
+ # No idea why this is the case, but I'm just adding the tests here
82
+ it "is successful" do
83
+ expect(patch_update).to be_successful
84
+ end
85
+
86
+ it "renders 'not-ok'" do
87
+ expect(patch_update.body).to eq('not-ok')
88
+ end
89
+
90
+ it "does not change the number of payments in the system" do
91
+ expect{ patch_update }.not_to(change{ ::Spree::Payment.count })
92
+ end
93
+
94
+ it "does not change the number of sources in the system" do
95
+ expect{ patch_update }.not_to(change{ SolidusPaypalBraintree::Source.count })
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe SolidusPaypalBraintree::ClientTokensController do
4
+ routes { SolidusPaypalBraintree::Engine.routes }
5
+
6
+ cassette_options = { cassette_name: "braintree/token" }
7
+ describe "POST create", vcr: cassette_options do
8
+ let!(:gateway) { create_gateway }
9
+ let(:user) { create(:user) }
10
+ let(:json) { JSON.parse(response.body) }
11
+
12
+ before { user.generate_spree_api_key! }
13
+
14
+ context 'without a payment method id' do
15
+ subject(:response) do
16
+ post :create, params: { token: user.spree_api_key }
17
+ end
18
+
19
+ it "returns a client token", aggregate_failures: true do
20
+ expect(response).to have_http_status(:success)
21
+ expect(response.content_type).to include 'application/json'
22
+ expect(json["client_token"]).to be_present
23
+ expect(json["client_token"]).to be_a String
24
+ expect(json["payment_method_id"]).to eq gateway.id
25
+ end
26
+
27
+ context "when there's two gateway's for different stores" do
28
+ let!(:store1) { create(:store, code: 'store_1') }
29
+ let!(:store2) { create(:store, code: 'store_2') }
30
+ let!(:gateway_for_store1) { create_gateway.tap{ |gw| store1.payment_methods << gw } }
31
+ let!(:gateway_for_store2) { create_gateway.tap{ |gw| store2.payment_methods << gw } }
32
+
33
+ it "returns the correct gateway for store1" do
34
+ allow_any_instance_of(described_class).to receive(:current_store).and_return store1
35
+ expect(json["payment_method_id"]).to eq gateway_for_store1.id
36
+ end
37
+
38
+ it "returns the correct gateway for store2" do
39
+ allow_any_instance_of(described_class).to receive(:current_store).and_return store2
40
+ expect(json["payment_method_id"]).to eq gateway_for_store2.id
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'with a payment method id' do
46
+ subject(:response) do
47
+ post :create, params: { token: user.spree_api_key, payment_method_id: gateway.id }
48
+ end
49
+
50
+ it 'uses the selected gateway' do
51
+ expect(json["payment_method_id"]).to eq gateway.id
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe SolidusPaypalBraintree::ConfigurationsController, type: :controller do
4
+ routes { SolidusPaypalBraintree::Engine.routes }
5
+
6
+ let!(:store_1) { create :store }
7
+ let!(:store_2) { create :store }
8
+
9
+ stub_authorization!
10
+
11
+ describe "GET #list" do
12
+ subject { get :list }
13
+
14
+ it "assigns all store's configurations as @configurations" do
15
+ subject
16
+ expect(assigns(:configurations)).
17
+ to eq [store_1.braintree_configuration, store_2.braintree_configuration]
18
+ end
19
+
20
+ it "renders the correct view" do
21
+ expect(subject).to render_template :list
22
+ end
23
+ end
24
+
25
+ describe "POST #update" do
26
+ subject { post :update, params: configurations_params }
27
+
28
+ let(:paypal_button_color) { 'blue' }
29
+ let(:configurations_params) do
30
+ {
31
+ configurations: {
32
+ configuration_fields: {
33
+ store_1.braintree_configuration.id.to_s => { paypal: true, apple_pay: true },
34
+ store_2.braintree_configuration.id.to_s => {
35
+ paypal: true,
36
+ apple_pay: false,
37
+ preferred_paypal_button_color: paypal_button_color
38
+ }
39
+ }
40
+ }
41
+ }
42
+ end
43
+
44
+ context "with valid parameters" do
45
+ it "updates the configuration" do
46
+ expect { subject }.to change { store_1.braintree_configuration.reload.paypal }.
47
+ from(false).to(true)
48
+ end
49
+
50
+ it "displays a success message to the user" do
51
+ subject
52
+ expect(flash[:success]).to eq "Successfully updated Braintree configurations."
53
+ end
54
+
55
+ it "displays all configurations" do
56
+ expect(subject).to redirect_to action: :list
57
+ end
58
+ end
59
+
60
+ context "with invalid parameters" do
61
+ let(:paypal_button_color) { 'invalid-color' }
62
+
63
+ it "displays an error message to the user" do
64
+ subject
65
+ expect(flash[:error]).to eq "An error occurred while updating Braintree configurations."
66
+ end
67
+
68
+ it "returns the user to the edit page" do
69
+ expect(subject).to redirect_to action: :list
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,183 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe SolidusPaypalBraintree::TransactionsController, type: :controller do
4
+ routes { SolidusPaypalBraintree::Engine.routes }
5
+
6
+ let!(:country) { create :country }
7
+ let(:line_item) { create :line_item, price: 50 }
8
+ let(:order) do
9
+ Spree::Order.create!(
10
+ line_items: [line_item],
11
+ email: 'test@example.com',
12
+ bill_address: create(:address, country: country),
13
+ ship_address: create(:address, country: country),
14
+ user: create(:user)
15
+ )
16
+ end
17
+
18
+ let(:payment_method) { create_gateway }
19
+
20
+ before do
21
+ allow(controller).to receive(:spree_current_user) { order.user }
22
+ allow(controller).to receive(:current_order) { order }
23
+ create :shipping_method, cost: 5
24
+ create :state, abbr: "WA", country: country
25
+ end
26
+
27
+ cassette_options = {
28
+ cassette_name: "transactions_controller/create",
29
+ match_requests_on: [:braintree_uri]
30
+ }
31
+ describe "POST create", vcr: cassette_options do
32
+ subject(:post_create) { post :create, params: params }
33
+
34
+ let!(:country) { create :country, iso: 'US' }
35
+
36
+ let(:params) do
37
+ {
38
+ transaction: {
39
+ nonce: "fake-valid-nonce",
40
+ payment_type: SolidusPaypalBraintree::Source::PAYPAL,
41
+ phone: "1112223333",
42
+ email: "batman@example.com",
43
+ address_attributes: {
44
+ name: "Wade Wilson",
45
+ address_line_1: "123 Fake Street",
46
+ city: "Seattle",
47
+ zip: "98101",
48
+ state_code: "WA",
49
+ country_code: "US"
50
+ }
51
+ },
52
+ payment_method_id: payment_method.id
53
+ }
54
+ end
55
+
56
+ before do
57
+ create :state, abbr: "WA", country: country
58
+ end
59
+
60
+ context "when import has invalid address" do
61
+ before { params[:transaction][:address_attributes][:city] = nil }
62
+
63
+ it "raises a validation error" do
64
+ expect { post_create }.to raise_error(
65
+ SolidusPaypalBraintree::TransactionsController::InvalidImportError,
66
+ "Import invalid: " \
67
+ "Address is invalid, " \
68
+ "Address city can't be blank"
69
+ )
70
+ end
71
+ end
72
+
73
+ context "when the transaction is valid", vcr: {
74
+ cassette_name: 'transaction/import/valid',
75
+ match_requests_on: [:braintree_uri]
76
+ } do
77
+ it "imports the payment" do
78
+ expect { post_create }.to change { order.payments.count }.by(1)
79
+ expect(order.payments.first.amount).to eq 55
80
+ end
81
+
82
+ context "when no end state is provided" do
83
+ it "advances the order to confirm" do
84
+ post_create
85
+ expect(order).to be_confirm
86
+ end
87
+ end
88
+
89
+ context "when end state provided is delivery" do
90
+ let(:params) { super().merge(state: 'delivery') }
91
+
92
+ it "advances the order to delivery" do
93
+ post_create
94
+ expect(order).to be_delivery
95
+ end
96
+ end
97
+
98
+ context "with provided address" do
99
+ it "creates a new address" do
100
+ # Creating the order also creates 3 addresses, we want to make sure
101
+ # the transaction import only creates 1 new one
102
+ order
103
+ expect { post_create }.to change { Spree::Address.count }.by(1)
104
+ expect(Spree::Address.last.address1).to eq "123 Fake Street"
105
+ end
106
+ end
107
+
108
+ context "without country ISO" do
109
+ before do
110
+ params[:transaction][:address_attributes][:country_code] = ""
111
+ params[:transaction][:address_attributes][:country_name] = "United States"
112
+ end
113
+
114
+ it "creates a new address, looking up the ISO by country name" do
115
+ order
116
+ expect { post_create }.to change { Spree::Address.count }.by(1)
117
+ expect(Spree::Address.last.country.iso).to eq "US"
118
+ end
119
+ end
120
+
121
+ context "without transaction address" do
122
+ before { params[:transaction].delete(:address_attributes) }
123
+
124
+ it "does not create a new address" do
125
+ order
126
+ expect { post_create }.not_to(change { Spree::Address.count })
127
+ end
128
+ end
129
+
130
+ context "when format is HTML" do
131
+ context "when import! leaves the order in confirm" do
132
+ it "redirects the user to the confirm page" do
133
+ expect(post_create).to redirect_to spree.checkout_state_path("confirm")
134
+ end
135
+ end
136
+
137
+ context "when import! completes the order" do
138
+ before { allow(order).to receive(:complete?).and_return(true) }
139
+
140
+ it "displays the order to the user" do
141
+ expect(post_create).to redirect_to spree.order_path(order)
142
+ end
143
+ end
144
+ end
145
+
146
+ context "when format is JSON" do
147
+ before { params[:format] = :json }
148
+
149
+ it "has a successful response" do
150
+ post_create
151
+ expect(response).to be_successful
152
+ end
153
+ end
154
+ end
155
+
156
+ context "when the transaction is invalid" do
157
+ before { params[:transaction][:email] = nil }
158
+
159
+ context "when format is HTML" do
160
+ it "raises an error including the validation messages" do
161
+ expect { post_create }.to raise_error(
162
+ SolidusPaypalBraintree::TransactionsController::InvalidImportError,
163
+ "Import invalid: Email can't be blank"
164
+ )
165
+ end
166
+ end
167
+
168
+ context "when format is JSON" do
169
+ before { params[:format] = :json }
170
+
171
+ it "has a failed status" do
172
+ post_create
173
+ expect(response.status).to eq 422
174
+ end
175
+
176
+ it "returns the errors as JSON" do
177
+ post_create
178
+ expect(JSON.parse(response.body)["errors"]["email"]).to eq ["can't be blank"]
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe "viewing the configuration interface" do
4
+ stub_authorization!
5
+
6
+ # Regression to ensure this page still renders on old versions of solidus
7
+ it "doesn't raise any errors due to unavailable route helpers" do
8
+ visit "/solidus_paypal_braintree/configurations/list"
9
+ expect(page).to have_content("Braintree Configurations")
10
+ end
11
+
12
+ # Regression to ensure this page renders on Solidus 2.4
13
+ it "doesn't raise any errors due to unavailable preference field partial" do
14
+ Rails.application.config.spree.payment_methods << SolidusPaypalBraintree::Gateway
15
+ Spree::PaymentMethod.create!(
16
+ type: 'SolidusPaypalBraintree::Gateway',
17
+ name: 'Braintree Payments'
18
+ )
19
+ visit '/admin/payment_methods'
20
+ page.find('a[title="Edit"]').click
21
+ expect(page).to have_field 'Name', with: 'Braintree Payments'
22
+ end
23
+ end