solidus_paypal_commerce_platform 0.7.0 → 1.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +21 -9
  3. data/.gem_release.yml +1 -1
  4. data/.gitignore +1 -0
  5. data/.rspec +0 -1
  6. data/.rubocop.yml +3 -2
  7. data/CHANGELOG.md +12 -17
  8. data/Gemfile +0 -41
  9. data/README.md +10 -14
  10. data/Rakefile +6 -1
  11. data/app/models/solidus_paypal_commerce_platform/payment_method.rb +1 -18
  12. data/bin/dummy-app +37 -0
  13. data/bin/rails-dummy-app +17 -0
  14. data/bin/rspec +11 -0
  15. data/bin/sandbox +3 -8
  16. data/config/locales/en.yml +0 -1
  17. data/lib/generators/solidus_paypal_commerce_platform/install/install_generator.rb +35 -21
  18. data/lib/generators/solidus_paypal_commerce_platform/install/templates/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/button_actions.js +272 -0
  19. data/{app → lib/generators/solidus_paypal_commerce_platform/install/templates/app}/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform.js +7 -0
  20. data/{app → lib/generators/solidus_paypal_commerce_platform/install/templates/app}/controllers/solidus_paypal_commerce_platform/paypal_orders_controller.rb +1 -1
  21. data/lib/{views/frontend/spree/checkout → generators/solidus_paypal_commerce_platform/install/templates/app/views/checkouts}/payment/_paypal_commerce_platform.html.erb +5 -4
  22. data/lib/{views/frontend/spree → generators/solidus_paypal_commerce_platform/install/templates/app/views}/orders/payment/_paypal_commerce_platform.html.erb +4 -3
  23. data/lib/{views/frontend/spree → generators/solidus_paypal_commerce_platform/install/templates/app/views}/products/payment/_paypal_commerce_platform.html.erb +1 -1
  24. data/lib/{views/frontend → generators/solidus_paypal_commerce_platform/install/templates/app/views}/solidus_paypal_commerce_platform/shared/_javascript_sdk_tag.html.erb +4 -2
  25. data/lib/solidus_paypal_commerce_platform/version.rb +1 -1
  26. data/solidus_paypal_commerce_platform.gemspec +1 -2
  27. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/CHECKOUT.ORDER.COMPLETED.v2.json +121 -0
  28. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/CHECKOUT.ORDER.PROCESSED.v2.json +121 -0
  29. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.COMPLETED.v1.json +50 -0
  30. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.COMPLETED.v2.json +72 -0
  31. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.DENIED.v1.json +50 -0
  32. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.DENIED.v2.json +68 -0
  33. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.REFUNDED.v1.json +51 -0
  34. data/spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.REFUNDED.v2.json +63 -0
  35. data/spec/jobs/solidus_paypal_commerce_platform/webhook_job_spec.rb +44 -0
  36. data/spec/lib/solidus_paypal_commerce_platform/client_spec.rb +21 -0
  37. data/spec/lib/solidus_paypal_commerce_platform/configuration_spec.rb +91 -0
  38. data/spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb +200 -0
  39. data/spec/models/solidus_paypal_commerce_platform/payment_source_spec.rb +119 -0
  40. data/spec/models/solidus_paypal_commerce_platform/paypal_address_spec.rb +67 -0
  41. data/spec/models/solidus_paypal_commerce_platform/paypal_order_spec.rb +80 -0
  42. data/spec/models/solidus_paypal_commerce_platform/state_guesser_spec.rb +38 -0
  43. data/spec/models/solidus_paypal_commerce_platform/wizard_spec.rb +9 -0
  44. data/spec/requests/solidus_paypal_commerce_platform/orders_controller_spec.rb +36 -0
  45. data/spec/requests/solidus_paypal_commerce_platform/shipping_rates_controller_spec.rb +44 -0
  46. data/spec/requests/solidus_paypal_commerce_platform/wizard_controller_spec.rb +59 -0
  47. data/spec/solidus_paypal_commerce_platform_spec_helper.rb +5 -0
  48. data/spec/support/solidus_paypal_commerce_platform/factories.rb +5 -0
  49. data/spec/support/solidus_paypal_commerce_platform/paypal_sdk_script_tag_helper.rb +13 -0
  50. data/spec/system/backend/new_payment_method_spec.rb +40 -0
  51. data/spec/system/frontend/cart_spec.rb +53 -0
  52. data/spec/system/frontend/checkout_spec.rb +104 -0
  53. data/spec/system/frontend/product_spec.rb +98 -0
  54. metadata +53 -37
  55. data/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/button_actions.js +0 -206
  56. /data/{app → lib/generators/solidus_paypal_commerce_platform/install/templates/app}/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/buttons.js +0 -0
  57. /data/{app → lib/generators/solidus_paypal_commerce_platform/install/templates/app}/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/namespace.js +0 -0
  58. /data/{app → lib/generators/solidus_paypal_commerce_platform/install/templates/app}/assets/stylesheets/spree/frontend/solidus_paypal_commerce_platform.css +0 -0
  59. /data/{app → lib/generators/solidus_paypal_commerce_platform/install/templates/app}/controllers/solidus_paypal_commerce_platform/orders_controller.rb +0 -0
  60. /data/{app → lib/generators/solidus_paypal_commerce_platform/install/templates/app}/controllers/solidus_paypal_commerce_platform/payments_controller.rb +0 -0
  61. /data/{app → lib/generators/solidus_paypal_commerce_platform/install/templates/app}/controllers/solidus_paypal_commerce_platform/shipping_rates_controller.rb +0 -0
  62. /data/{app → lib/generators/solidus_paypal_commerce_platform/install/templates/app}/controllers/solidus_paypal_commerce_platform/wizard_controller.rb +0 -0
  63. /data/lib/{views/frontend → generators/solidus_paypal_commerce_platform/install/templates/app/views}/solidus_paypal_commerce_platform/cart/_cart_buttons.html.erb +0 -0
  64. /data/lib/{views/frontend → generators/solidus_paypal_commerce_platform/install/templates/app/views}/solidus_paypal_commerce_platform/payments/_payment.html.erb +0 -0
  65. /data/lib/{views/frontend → generators/solidus_paypal_commerce_platform/install/templates/app/views}/solidus_paypal_commerce_platform/product/_product_buttons.html.erb +0 -0
  66. /data/lib/generators/solidus_paypal_commerce_platform/install/templates/{initializer.rb → config/initializers/solidus_paypal_commerce_platform.rb} +0 -0
@@ -0,0 +1,98 @@
1
+ require 'solidus_paypal_commerce_platform_spec_helper'
2
+
3
+ RSpec.describe "Product page", js: true do
4
+ describe "paypal button", :skip do
5
+ before { create(:store, default: true) }
6
+
7
+ let!(:paypal_payment_method) { create(:paypal_payment_method) }
8
+ let!(:variants) {
9
+ option_type = create(:option_type)
10
+ product = create(:product, option_types: [option_type], slug: 'foo-bar')
11
+ option_s = create(:option_value, option_type: option_type, presentation: 'S')
12
+ option_xs = create(:option_value, option_type: option_type, presentation: 'XS')
13
+
14
+ [
15
+ create(:variant, product: product, option_values: [option_s]),
16
+ create(:variant, product: product, option_values: [option_xs]),
17
+ ]
18
+ }
19
+
20
+ context "when generating a script tag" do
21
+ it "generates a url with the correct credentials attached" do
22
+ visit "/products/foo-bar"
23
+
24
+ expect(js_sdk_script_query).to include("client-id=#{paypal_payment_method.preferences[:client_id]}")
25
+ end
26
+
27
+ it "generates a partner_id attribute with the correct partner code attached" do
28
+ visit "/products/foo-bar"
29
+
30
+ expect(js_sdk_script_partner_id).to eq("Solidus_PCP_SP")
31
+ end
32
+
33
+ it "generates a URL with the correct currency" do
34
+ allow_any_instance_of(SolidusPaypalCommercePlatform::PricingOptions).to receive(:currency).and_return "EUR"
35
+
36
+ visit "/products/foo-bar"
37
+
38
+ expect(js_sdk_script_query).to include("currency=EUR")
39
+ end
40
+
41
+ context "when auto-capture is set to true" do
42
+ it "generates a url with intent capture" do
43
+ paypal_payment_method.update(auto_capture: true)
44
+
45
+ visit "/products/foo-bar"
46
+
47
+ expect(js_sdk_script_query).to include("client-id=#{paypal_payment_method.preferences[:client_id]}")
48
+ expect(js_sdk_script_query).to include("intent=capture")
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "order creation" do
54
+ it "creates an order successfully" do
55
+ visit "/products/foo-bar"
56
+ page.evaluate_script("SolidusPaypalCommercePlatform.createOrder()")
57
+
58
+ expect(Spree::Order.last).to be_an_instance_of(Spree::Order)
59
+ end
60
+
61
+ it "sets the Spree number and token variables" do
62
+ visit "/products/foo-bar"
63
+ page.evaluate_script("SolidusPaypalCommercePlatform.createOrder()")
64
+
65
+ order = Spree::Order.last
66
+ expect(page.evaluate_script("SolidusPaypalCommercePlatform.current_order_id")).to eq order.number
67
+ expect(page.evaluate_script("SolidusPaypalCommercePlatform.current_order_token")).to eq order.guest_token
68
+ end
69
+
70
+ it "uses the specified quantity" do
71
+ visit "/products/foo-bar"
72
+ fill_in('quantity', with: 12)
73
+ page.evaluate_script("SolidusPaypalCommercePlatform.createOrder()")
74
+
75
+ expect(Spree::Order.last.line_items.first.quantity).to eq(12)
76
+ end
77
+
78
+ it "uses the selected variant" do
79
+ visit "/products/foo-bar"
80
+ find('label', text: 'XS').click
81
+ page.evaluate_script("SolidusPaypalCommercePlatform.createOrder()")
82
+
83
+ expect(Spree::Order.last.line_items.first.variant).to eq(variants.last)
84
+ end
85
+
86
+ it "assigns the order to the api token holder" do
87
+ user = create(:user)
88
+ user.generate_spree_api_key!
89
+
90
+ visit "/products/foo-bar"
91
+ page.evaluate_script("SolidusPaypalCommercePlatform.api_key = #{user.spree_api_key.to_json}")
92
+ page.evaluate_script("SolidusPaypalCommercePlatform.createOrder()")
93
+
94
+ expect(Spree::Order.last.user).to eq(user)
95
+ end
96
+ end
97
+ end
98
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_paypal_commerce_platform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 1.0.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Denny
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-01-18 00:00:00.000000000 Z
12
+ date: 2022-12-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: deface
@@ -95,20 +95,6 @@ dependencies:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
- - !ruby/object:Gem::Dependency
99
- name: cuprite
100
- requirement: !ruby/object:Gem::Requirement
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- version: '0'
105
- type: :development
106
- prerelease: false
107
- version_requirements: !ruby/object:Gem::Requirement
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- version: '0'
112
98
  - !ruby/object:Gem::Dependency
113
99
  name: solidus_dev_support
114
100
  requirement: !ruby/object:Gem::Requirement
@@ -144,17 +130,7 @@ files:
144
130
  - app/assets/javascripts/spree/backend/solidus_paypal_commerce_platform.js
145
131
  - app/assets/javascripts/spree/backend/solidus_paypal_commerce_platform/namespace.js
146
132
  - app/assets/javascripts/spree/backend/solidus_paypal_commerce_platform/wizard.js
147
- - app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform.js
148
- - app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/button_actions.js
149
- - app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/buttons.js
150
- - app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/namespace.js
151
133
  - app/assets/stylesheets/spree/backend/solidus_paypal_commerce_platform.css
152
- - app/assets/stylesheets/spree/frontend/solidus_paypal_commerce_platform.css
153
- - app/controllers/solidus_paypal_commerce_platform/orders_controller.rb
154
- - app/controllers/solidus_paypal_commerce_platform/payments_controller.rb
155
- - app/controllers/solidus_paypal_commerce_platform/paypal_orders_controller.rb
156
- - app/controllers/solidus_paypal_commerce_platform/shipping_rates_controller.rb
157
- - app/controllers/solidus_paypal_commerce_platform/wizard_controller.rb
158
134
  - app/decorators/models/solidus_paypal_commerce_platform/spree/address_decorator.rb
159
135
  - app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb
160
136
  - app/jobs/solidus_paypal_commerce_platform/application_job.rb
@@ -178,10 +154,13 @@ files:
178
154
  - app/views/solidus_paypal_commerce_platform/admin/payment_methods/_setup_wizards.html.erb
179
155
  - app/views/solidus_paypal_commerce_platform/admin/payments/_log_entries.html.erb
180
156
  - bin/console
157
+ - bin/dummy-app
181
158
  - bin/rails
159
+ - bin/rails-dummy-app
182
160
  - bin/rails-engine
183
161
  - bin/rails-sandbox
184
162
  - bin/rake
163
+ - bin/rspec
185
164
  - bin/sandbox
186
165
  - bin/setup
187
166
  - codecov.yml
@@ -190,7 +169,24 @@ files:
190
169
  - db/migrate/20200521190038_add_paypal_commerce_platform_sources.rb
191
170
  - db/migrate/20211220133406_add_paypal_funding_source_to_paypal_commerce_platform_sources.rb
192
171
  - lib/generators/solidus_paypal_commerce_platform/install/install_generator.rb
193
- - lib/generators/solidus_paypal_commerce_platform/install/templates/initializer.rb
172
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform.js
173
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/button_actions.js
174
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/buttons.js
175
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/namespace.js
176
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/assets/stylesheets/spree/frontend/solidus_paypal_commerce_platform.css
177
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/controllers/solidus_paypal_commerce_platform/orders_controller.rb
178
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/controllers/solidus_paypal_commerce_platform/payments_controller.rb
179
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/controllers/solidus_paypal_commerce_platform/paypal_orders_controller.rb
180
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/controllers/solidus_paypal_commerce_platform/shipping_rates_controller.rb
181
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/controllers/solidus_paypal_commerce_platform/wizard_controller.rb
182
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/views/checkouts/payment/_paypal_commerce_platform.html.erb
183
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/views/orders/payment/_paypal_commerce_platform.html.erb
184
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/views/products/payment/_paypal_commerce_platform.html.erb
185
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/views/solidus_paypal_commerce_platform/cart/_cart_buttons.html.erb
186
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/views/solidus_paypal_commerce_platform/payments/_payment.html.erb
187
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/views/solidus_paypal_commerce_platform/product/_product_buttons.html.erb
188
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/app/views/solidus_paypal_commerce_platform/shared/_javascript_sdk_tag.html.erb
189
+ - lib/generators/solidus_paypal_commerce_platform/install/templates/config/initializers/solidus_paypal_commerce_platform.rb
194
190
  - lib/paypal/access_token.rb
195
191
  - lib/paypal/lib.rb
196
192
  - lib/paypal/paypal_checkout_sdk/orders/orders_authorize_request.rb
@@ -227,14 +223,34 @@ files:
227
223
  - lib/views/backend/spree/admin/payments/source_forms/_paypal_commerce_platform.html.erb
228
224
  - lib/views/backend/spree/admin/payments/source_views/_paypal_commerce_platform.html.erb
229
225
  - lib/views/backend/spree/admin/shared/preference_fields/_paypal_select.html.erb
230
- - lib/views/frontend/solidus_paypal_commerce_platform/cart/_cart_buttons.html.erb
231
- - lib/views/frontend/solidus_paypal_commerce_platform/payments/_payment.html.erb
232
- - lib/views/frontend/solidus_paypal_commerce_platform/product/_product_buttons.html.erb
233
- - lib/views/frontend/solidus_paypal_commerce_platform/shared/_javascript_sdk_tag.html.erb
234
- - lib/views/frontend/spree/checkout/payment/_paypal_commerce_platform.html.erb
235
- - lib/views/frontend/spree/orders/payment/_paypal_commerce_platform.html.erb
236
- - lib/views/frontend/spree/products/payment/_paypal_commerce_platform.html.erb
237
226
  - solidus_paypal_commerce_platform.gemspec
227
+ - spec/jobs/solidus_paypal_commerce_platform/fixtures/CHECKOUT.ORDER.COMPLETED.v2.json
228
+ - spec/jobs/solidus_paypal_commerce_platform/fixtures/CHECKOUT.ORDER.PROCESSED.v2.json
229
+ - spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.COMPLETED.v1.json
230
+ - spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.COMPLETED.v2.json
231
+ - spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.DENIED.v1.json
232
+ - spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.DENIED.v2.json
233
+ - spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.REFUNDED.v1.json
234
+ - spec/jobs/solidus_paypal_commerce_platform/fixtures/PAYMENT.CAPTURE.REFUNDED.v2.json
235
+ - spec/jobs/solidus_paypal_commerce_platform/webhook_job_spec.rb
236
+ - spec/lib/solidus_paypal_commerce_platform/client_spec.rb
237
+ - spec/lib/solidus_paypal_commerce_platform/configuration_spec.rb
238
+ - spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb
239
+ - spec/models/solidus_paypal_commerce_platform/payment_source_spec.rb
240
+ - spec/models/solidus_paypal_commerce_platform/paypal_address_spec.rb
241
+ - spec/models/solidus_paypal_commerce_platform/paypal_order_spec.rb
242
+ - spec/models/solidus_paypal_commerce_platform/state_guesser_spec.rb
243
+ - spec/models/solidus_paypal_commerce_platform/wizard_spec.rb
244
+ - spec/requests/solidus_paypal_commerce_platform/orders_controller_spec.rb
245
+ - spec/requests/solidus_paypal_commerce_platform/shipping_rates_controller_spec.rb
246
+ - spec/requests/solidus_paypal_commerce_platform/wizard_controller_spec.rb
247
+ - spec/solidus_paypal_commerce_platform_spec_helper.rb
248
+ - spec/support/solidus_paypal_commerce_platform/factories.rb
249
+ - spec/support/solidus_paypal_commerce_platform/paypal_sdk_script_tag_helper.rb
250
+ - spec/system/backend/new_payment_method_spec.rb
251
+ - spec/system/frontend/cart_spec.rb
252
+ - spec/system/frontend/checkout_spec.rb
253
+ - spec/system/frontend/product_spec.rb
238
254
  homepage: https://github.com/solidusio/solidus_paypal_commerce_platform
239
255
  licenses:
240
256
  - BSD-3-Clause
@@ -254,11 +270,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
254
270
  version: '2.7'
255
271
  required_rubygems_version: !ruby/object:Gem::Requirement
256
272
  requirements:
257
- - - ">="
273
+ - - ">"
258
274
  - !ruby/object:Gem::Version
259
- version: '0'
275
+ version: 1.3.1
260
276
  requirements: []
261
- rubygems_version: 3.3.7
277
+ rubygems_version: 3.3.23
262
278
  signing_key:
263
279
  specification_version: 4
264
280
  summary: Integrate Solidus with Paypal Commerce Platform
@@ -1,206 +0,0 @@
1
- SolidusPaypalCommercePlatform.showOverlay = function() {
2
- document.getElementById("paypal_commerce_platform_overlay").style.display = "block";
3
- }
4
-
5
- SolidusPaypalCommercePlatform.hideOverlay = function() {
6
- document.getElementById("paypal_commerce_platform_overlay").style.display = "none";
7
- }
8
-
9
- SolidusPaypalCommercePlatform.handleError = function(error) {
10
- console.error(error);
11
- alert("There was a problem connecting with PayPal.")
12
- throw error
13
- }
14
-
15
- SolidusPaypalCommercePlatform.sendOrder = function(payment_method_id) {
16
- return Spree.ajax({
17
- url: '/solidus_paypal_commerce_platform/paypal_orders/' + Spree.current_order_id,
18
- method: 'GET',
19
- data: {
20
- payment_method_id: payment_method_id,
21
- order_token: Spree.current_order_token
22
- }
23
- }).then(function(success_response) {
24
- return success_response.table.result.table.id
25
- }, function(failure_response) {
26
- return failure_response.responseJSON.table.error.table
27
- })
28
- }
29
-
30
- SolidusPaypalCommercePlatform.createAndSendOrder = function(payment_method_id) {
31
- return SolidusPaypalCommercePlatform.createOrder().then(function(){
32
- return SolidusPaypalCommercePlatform.sendOrder(payment_method_id)
33
- })
34
- }
35
-
36
- SolidusPaypalCommercePlatform.createOrder = function() {
37
- var data = {
38
- order: {
39
- line_items_attributes: [{
40
- variant_id: SolidusPaypalCommercePlatform.getVariantId(),
41
- quantity: SolidusPaypalCommercePlatform.getQuantity()
42
- }]
43
- }
44
- }
45
-
46
- return Spree.ajax({
47
- url: "/solidus_paypal_commerce_platform/orders",
48
- method: 'POST',
49
- data: data,
50
- error: function(response) {
51
- message = response.responseJSON
52
- alert('A problem has occurred while creating your order - ' + message);
53
- }
54
- }).then(function(response) {
55
- Spree.current_order_id = response.number
56
- Spree.current_order_token = response.guest_token
57
- });
58
- }
59
-
60
- SolidusPaypalCommercePlatform.getVariantId = function() {
61
- var variants = document.getElementsByName("variant_id")
62
- var variant_id;
63
- if(variants.length == 1){
64
- variant_id = variants[0].value
65
- }else{
66
- var i;
67
- for (i = 0; i < variants.length; i++) {
68
- if (variants[i].checked) {
69
- variant_id = variants[i].value
70
- }
71
- }
72
- }
73
- return variant_id
74
- }
75
-
76
- SolidusPaypalCommercePlatform.getQuantity = function() {
77
- return document.getElementById("quantity").value
78
- }
79
-
80
- SolidusPaypalCommercePlatform.approveOrder = function(data, actions) {
81
- SolidusPaypalCommercePlatform.showOverlay()
82
- actions.order.get().then(function(response){
83
- SolidusPaypalCommercePlatform.updateAddress(response).then(function() {
84
- SolidusPaypalCommercePlatform.verifyTotal(response.purchase_units[0].amount.value).then(function(){
85
- $("#payments_source_paypal_order_id").val(data.orderID)
86
- $("#payments_source_paypal_email").val(response.payer.email_address)
87
- $("#payments_source_paypal_funding_source").val(SolidusPaypalCommercePlatform.fundingSource)
88
- $("#checkout_form_payment").submit()
89
- })
90
- })
91
- })
92
- }
93
-
94
- SolidusPaypalCommercePlatform.shippingChange = function(data, actions) {
95
- Spree.ajax({
96
- url: '/solidus_paypal_commerce_platform/shipping_rates',
97
- method: 'GET',
98
- data: {
99
- order_id: Spree.current_order_id,
100
- order_token: Spree.current_order_token,
101
- address: data.shipping_address
102
- },
103
- error: function(response) {
104
- message = response.responseJSON;
105
- console.log('There were some problems with your payment address - ' + message);
106
- actions.reject()
107
- }
108
- }).then(function(response) {
109
- actions.order.patch([response]).catch(function() {
110
- actions.reject()
111
- })
112
- })
113
- }
114
-
115
- SolidusPaypalCommercePlatform.verifyTotal = function(paypal_total) {
116
- return Spree.ajax({
117
- url: '/solidus_paypal_commerce_platform/verify_total',
118
- method: 'GET',
119
- data: {
120
- order_id: Spree.current_order_id,
121
- order_token: Spree.current_order_token,
122
- paypal_total: paypal_total
123
- },
124
- error: function(response) {
125
- SolidusPaypalCommercePlatform.hideOverlay()
126
- alert('There were some problems with your payment - ' + response.responseJSON.errors.expected_total);
127
- }
128
- })
129
- }
130
-
131
- SolidusPaypalCommercePlatform.finalizeOrder = function(payment_method_id, data, actions) {
132
- SolidusPaypalCommercePlatform.showOverlay()
133
- actions.order.get().then(function(response){
134
- SolidusPaypalCommercePlatform.updateAddress(response).then(function() {
135
- var paypal_amount = response.purchase_units[0].amount.value
136
- SolidusPaypalCommercePlatform.advanceOrder().then(function() {
137
- SolidusPaypalCommercePlatform.verifyTotal(paypal_amount).then(function(){
138
- SolidusPaypalCommercePlatform.addPayment(paypal_amount, payment_method_id, data, response.payer.email_address).then(function() {
139
- window.location.href = SolidusPaypalCommercePlatform.checkout_url
140
- })
141
- })
142
- })
143
- })
144
- })
145
- }
146
-
147
- SolidusPaypalCommercePlatform.advanceOrder = function() {
148
- return Spree.ajax({
149
- url: '/api/checkouts/' + Spree.current_order_id + '/advance',
150
- method: 'PUT',
151
- data: {
152
- order_token: Spree.current_order_token
153
- },
154
- error: function(response) {
155
- SolidusPaypalCommercePlatform.hideOverlay()
156
- alert('There were some problems with your order');
157
- }
158
- })
159
- }
160
-
161
- SolidusPaypalCommercePlatform.addPayment = function(paypal_amount, payment_method_id, data, email) {
162
- return Spree.ajax({
163
- url: '/api/checkouts/' + Spree.current_order_id + '/payments',
164
- method: 'POST',
165
- data: {
166
- order_token: Spree.current_order_token,
167
- payment: {
168
- amount: paypal_amount,
169
- payment_method_id: payment_method_id,
170
- source_attributes: {
171
- paypal_order_id: data.orderID,
172
- paypal_email: email,
173
- paypal_funding_source: SolidusPaypalCommercePlatform.fundingSource
174
- }
175
- }
176
- },
177
- error: function(response) {
178
- SolidusPaypalCommercePlatform.hideOverlay()
179
- alert('There were some problems with your payment');
180
- }
181
- })
182
- }
183
-
184
- SolidusPaypalCommercePlatform.updateAddress = function(response) {
185
- var shipping = response.purchase_units[0].shipping;
186
- if (!shipping) return Promise.resolve({});
187
-
188
- var updated_address = shipping.address;
189
- return Spree.ajax({
190
- url: '/solidus_paypal_commerce_platform/update_address',
191
- method: 'POST',
192
- data: {
193
- address: {
194
- updated_address: updated_address,
195
- recipient: response.payer
196
- },
197
- order_id: Spree.current_order_id,
198
- order_token: Spree.current_order_token
199
- },
200
- error: function(response) {
201
- SolidusPaypalCommercePlatform.hideOverlay()
202
- message = response.responseJSON;
203
- alert('There were some problems with your payment address - ' + message);
204
- }
205
- })
206
- }