solidus_braintree 1.2.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +78 -0
  3. data/.gem_release.yml +5 -0
  4. data/.github/stale.yml +1 -0
  5. data/.github_changelog_generator +2 -0
  6. data/.gitignore +21 -10
  7. data/.rspec +1 -1
  8. data/.rubocop.yml +86 -0
  9. data/CHANGELOG.md +186 -18
  10. data/Gemfile +35 -17
  11. data/LICENSE +26 -0
  12. data/README.md +389 -24
  13. data/Rakefile +6 -16
  14. data/app/assets/config/solidus_braintree_manifest.js +0 -0
  15. data/app/assets/javascripts/spree/backend/solidus_braintree/client.js +239 -0
  16. data/app/assets/javascripts/spree/backend/solidus_braintree/constants.js +89 -0
  17. data/app/assets/javascripts/spree/backend/solidus_braintree/hosted_form.js +46 -0
  18. data/app/assets/javascripts/spree/backend/solidus_braintree/promise.js +20 -0
  19. data/app/assets/javascripts/spree/backend/solidus_braintree.js +96 -0
  20. data/app/assets/stylesheets/spree/backend/solidus_braintree.scss +28 -0
  21. data/app/decorators/controllers/solidus_braintree/admin_payments_controller_decorator.rb +11 -0
  22. data/app/decorators/controllers/solidus_braintree/client_tokens_controller.rb +41 -0
  23. data/app/decorators/models/solidus_braintree/spree/store_decorator.rb +20 -0
  24. data/app/decorators/models/solidus_braintree/spree/user_decorator.rb +13 -0
  25. data/app/helpers/solidus_braintree/braintree_admin_helper.rb +23 -0
  26. data/app/models/application_record.rb +5 -0
  27. data/app/models/solidus_braintree/address.rb +64 -0
  28. data/app/models/solidus_braintree/avs_result.rb +69 -0
  29. data/app/models/solidus_braintree/configuration.rb +39 -0
  30. data/app/models/solidus_braintree/customer.rb +8 -0
  31. data/app/models/solidus_braintree/gateway.rb +437 -0
  32. data/app/models/solidus_braintree/response.rb +80 -0
  33. data/app/models/solidus_braintree/source.rb +140 -0
  34. data/app/models/solidus_braintree/transaction.rb +31 -0
  35. data/app/models/solidus_braintree/transaction_address.rb +88 -0
  36. data/app/models/solidus_braintree/transaction_import.rb +98 -0
  37. data/app/views/spree/api/payments/source_views/_braintree.json.jbuilder +3 -0
  38. data/bin/console +4 -1
  39. data/bin/dummy-app +37 -0
  40. data/bin/rails +5 -5
  41. data/bin/rails-dummy-app +17 -0
  42. data/bin/rails-engine +13 -0
  43. data/bin/rails-sandbox +16 -0
  44. data/bin/rake +7 -0
  45. data/bin/rspec +11 -0
  46. data/bin/sandbox +61 -0
  47. data/bin/setup +5 -4
  48. data/config/locales/en.yml +94 -2
  49. data/config/locales/it.yml +56 -0
  50. data/config/routes.rb +12 -3
  51. data/db/migrate/20160830061749_create_solidus_paypal_braintree_sources.rb +16 -0
  52. data/db/migrate/20160906201711_create_solidus_paypal_braintree_customers.rb +13 -0
  53. data/db/migrate/20161114231422_create_solidus_paypal_braintree_configurations.rb +11 -0
  54. data/db/migrate/20161125172005_add_braintree_configuration_to_stores.rb +7 -0
  55. data/db/migrate/20170203191030_add_credit_card_to_braintree_configuration.rb +6 -0
  56. data/db/migrate/20170505193712_add_null_constraint_to_sources.rb +38 -0
  57. data/db/migrate/20170508085402_add_not_null_constraint_to_sources_payment_type.rb +14 -0
  58. data/db/migrate/20190705115327_add_paypal_button_preferences_to_braintree_configurations.rb +5 -0
  59. data/db/migrate/20190911141712_add_3d_secure_to_braintree_configuration.rb +5 -0
  60. data/db/migrate/20211222170950_add_paypal_funding_source_to_solidus_paypal_braintree_sources.rb +5 -0
  61. data/db/migrate/20220104150301_add_venmo_to_braintree_configuration.rb +5 -0
  62. data/db/migrate/20230109080950_rename_solidus_paypal_braintree_source_type.rb +31 -0
  63. data/db/migrate/20230210104310_add_device_data_to_braintree_sources.rb +5 -0
  64. data/lib/controllers/backend/solidus_braintree/configurations_controller.rb +48 -0
  65. data/lib/generators/solidus_braintree/install/install_generator.rb +155 -19
  66. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_active_blue_button_280x48.svg +19 -0
  67. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_active_blue_button_320x48.svg +19 -0
  68. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_active_blue_button_375x48.svg +19 -0
  69. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_active_white_button_280x48.svg +19 -0
  70. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_active_white_button_320x48.svg +19 -0
  71. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_active_white_button_375x48.svg +19 -0
  72. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_blue_acceptance_mark.svg +15 -0
  73. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_blue_button_280x48.svg +19 -0
  74. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_blue_button_320x48.svg +19 -0
  75. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_blue_button_375x48.svg +19 -0
  76. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_blue_logo.svg +18 -0
  77. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_white_acceptance_mark.svg +20 -0
  78. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_white_button_280x48.svg +19 -0
  79. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_white_button_320x48.svg +19 -0
  80. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_white_button_375x48.svg +19 -0
  81. data/lib/generators/solidus_braintree/install/templates/app/assets/images/solidus_braintree/venmo/venmo_white_logo.svg +18 -0
  82. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/paypal_button.js +34 -0
  83. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/ajax.js +13 -0
  84. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/apple_pay_button.js +179 -0
  85. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/checkout.js +113 -0
  86. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/client.js +239 -0
  87. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/constants.js +89 -0
  88. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/frontend.js +15 -0
  89. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/hosted_form.js +48 -0
  90. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/paypal_button.js +178 -0
  91. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/paypal_messaging.js +22 -0
  92. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/promise.js +20 -0
  93. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree/venmo_button.js +86 -0
  94. data/lib/generators/solidus_braintree/install/templates/app/assets/javascripts/spree/frontend/solidus_braintree.js +1 -0
  95. data/lib/generators/solidus_braintree/install/templates/app/assets/stylesheets/spree/frontend/solidus_braintree.scss +62 -0
  96. data/lib/generators/solidus_braintree/install/templates/app/controllers/solidus_braintree/checkouts_controller.rb +31 -0
  97. data/lib/generators/solidus_braintree/install/templates/app/controllers/solidus_braintree/transactions_controller.rb +67 -0
  98. data/lib/generators/solidus_braintree/install/templates/app/helpers/solidus_braintree/braintree_checkout_helper.rb +60 -0
  99. data/lib/generators/solidus_braintree/install/templates/app/views/checkouts/existing_payment/_braintree.html.erb +2 -0
  100. data/lib/generators/solidus_braintree/install/templates/app/views/checkouts/payment/_braintree.html.erb +23 -0
  101. data/lib/generators/solidus_braintree/install/templates/app/views/payments/_braintree_payment_details.html.erb +9 -0
  102. data/lib/generators/solidus_braintree/install/templates/app/views/spree/shared/_apple_pay_button.html.erb +27 -0
  103. data/lib/generators/solidus_braintree/install/templates/app/views/spree/shared/_braintree_errors.html.erb +16 -0
  104. data/lib/generators/solidus_braintree/install/templates/app/views/spree/shared/_braintree_head_scripts.html.erb +26 -0
  105. data/lib/generators/solidus_braintree/install/templates/app/views/spree/shared/_braintree_hosted_fields.html.erb +40 -0
  106. data/lib/generators/solidus_braintree/install/templates/app/views/spree/shared/_paypal_cart_button.html.erb +38 -0
  107. data/lib/generators/solidus_braintree/install/templates/app/views/spree/shared/_paypal_checkout_button.html.erb +32 -0
  108. data/lib/generators/solidus_braintree/install/templates/app/views/spree/shared/_paypal_messaging.html.erb +13 -0
  109. data/lib/generators/solidus_braintree/install/templates/app/views/spree/shared/_venmo_button.html.erb +33 -0
  110. data/lib/generators/solidus_braintree/install/templates/config/initializers/solidus_braintree.rb +6 -0
  111. data/lib/solidus_braintree/country_mapper.rb +37 -0
  112. data/lib/solidus_braintree/engine.rb +61 -11
  113. data/lib/solidus_braintree/extension_configuration.rb +23 -0
  114. data/lib/solidus_braintree/request_protection.rb +21 -0
  115. data/lib/solidus_braintree/version.rb +3 -1
  116. data/lib/solidus_braintree.rb +14 -2
  117. data/lib/solidus_paypal_braintree.rb +6 -0
  118. data/lib/views/backend/solidus_braintree/configurations/list.html.erb +63 -0
  119. data/lib/views/backend/spree/admin/payments/source_forms/_braintree.html.erb +16 -0
  120. data/lib/views/backend/spree/admin/payments/source_views/_braintree.html.erb +39 -0
  121. data/lib/views/backend/spree/admin/shared/preference_fields/_preference_select.html.erb +13 -0
  122. data/lib/views/backend_v1.2/spree/admin/payments/source_forms/_braintree.html.erb +16 -0
  123. data/lib/views/backend_v2.4/spree/admin/shared/preference_fields/_hash.html.erb +12 -0
  124. data/solidus_braintree.gemspec +37 -38
  125. data/spec/controllers/solidus_braintree/checkouts_controller_spec.rb +99 -0
  126. data/spec/controllers/solidus_braintree/client_tokens_controller_spec.rb +55 -0
  127. data/spec/controllers/solidus_braintree/configurations_controller_spec.rb +73 -0
  128. data/spec/controllers/solidus_braintree/transactions_controller_spec.rb +183 -0
  129. data/spec/fixtures/cassettes/admin/invalid_credit_card.yml +63 -0
  130. data/spec/fixtures/cassettes/admin/resubmit_credit_card.yml +352 -0
  131. data/spec/fixtures/cassettes/admin/valid_credit_card.yml +412 -0
  132. data/spec/fixtures/cassettes/braintree/create_profile.yml +71 -0
  133. data/spec/fixtures/cassettes/braintree/generate_token.yml +63 -0
  134. data/spec/fixtures/cassettes/braintree/token.yml +63 -0
  135. data/spec/fixtures/cassettes/checkout/invalid_credit_card.yml +63 -0
  136. data/spec/fixtures/cassettes/checkout/resubmit_credit_card.yml +216 -0
  137. data/spec/fixtures/cassettes/checkout/update.yml +71 -0
  138. data/spec/fixtures/cassettes/checkout/valid_credit_card.yml +171 -0
  139. data/spec/fixtures/cassettes/checkout/valid_venmo_transaction.yml +599 -0
  140. data/spec/fixtures/cassettes/gateway/authorize/credit_card/address.yml +86 -0
  141. data/spec/fixtures/cassettes/gateway/authorize/merchant_account/EUR.yml +154 -0
  142. data/spec/fixtures/cassettes/gateway/authorize/paypal/EUR.yml +90 -0
  143. data/spec/fixtures/cassettes/gateway/authorize/paypal/address.yml +90 -0
  144. data/spec/fixtures/cassettes/gateway/authorize.yml +86 -0
  145. data/spec/fixtures/cassettes/gateway/authorized_transaction.yml +73 -0
  146. data/spec/fixtures/cassettes/gateway/cancel/missing.yml +63 -0
  147. data/spec/fixtures/cassettes/gateway/cancel/refunds.yml +272 -0
  148. data/spec/fixtures/cassettes/gateway/cancel/void.yml +201 -0
  149. data/spec/fixtures/cassettes/gateway/capture.yml +141 -0
  150. data/spec/fixtures/cassettes/gateway/complete.yml +157 -0
  151. data/spec/fixtures/cassettes/gateway/credit.yml +208 -0
  152. data/spec/fixtures/cassettes/gateway/customer.yml +79 -0
  153. data/spec/fixtures/cassettes/gateway/purchase.yml +87 -0
  154. data/spec/fixtures/cassettes/gateway/settled_transaction.yml +140 -0
  155. data/spec/fixtures/cassettes/gateway/void.yml +137 -0
  156. data/spec/fixtures/cassettes/source/bin.yml +295 -0
  157. data/spec/fixtures/cassettes/source/card_type.yml +267 -0
  158. data/spec/fixtures/cassettes/source/last4.yml +267 -0
  159. data/spec/fixtures/cassettes/transaction/import/valid/capture.yml +224 -0
  160. data/spec/fixtures/cassettes/transaction/import/valid.yml +71 -0
  161. data/spec/fixtures/views/carts/_cart_footer.html.erb +18 -0
  162. data/spec/helpers/solidus_braintree/braintree_admin_helper_spec.rb +17 -0
  163. data/spec/helpers/solidus_braintree/braintree_checkout_helper_spec.rb +70 -0
  164. data/spec/models/solidus_braintree/address_spec.rb +71 -0
  165. data/spec/models/solidus_braintree/avs_result_spec.rb +317 -0
  166. data/spec/models/solidus_braintree/gateway_spec.rb +774 -0
  167. data/spec/models/solidus_braintree/response_spec.rb +280 -0
  168. data/spec/models/solidus_braintree/source_spec.rb +555 -0
  169. data/spec/models/solidus_braintree/transaction_address_spec.rb +235 -0
  170. data/spec/models/solidus_braintree/transaction_import_spec.rb +302 -0
  171. data/spec/models/solidus_braintree/transaction_spec.rb +86 -0
  172. data/spec/models/spree/store_spec.rb +14 -0
  173. data/spec/requests/spree/api/orders_controller_spec.rb +36 -0
  174. data/spec/solidus_braintree_helper.rb +7 -0
  175. data/spec/support/solidus_braintree/capybara.rb +7 -0
  176. data/spec/support/solidus_braintree/factories.rb +55 -0
  177. data/spec/support/solidus_braintree/gateway_helpers.rb +29 -0
  178. data/spec/support/solidus_braintree/order_ready_for_payment.rb +44 -0
  179. data/spec/support/solidus_braintree/order_walkthrough.rb +87 -0
  180. data/spec/support/solidus_braintree/vcr.rb +42 -0
  181. data/spec/support/solidus_braintree/with_prepended_view_fixtures.rb +19 -0
  182. data/spec/system/backend/configuration_spec.rb +23 -0
  183. data/spec/system/backend/new_payment_spec.rb +136 -0
  184. data/spec/system/frontend/braintree_credit_card_checkout_spec.rb +199 -0
  185. data/spec/system/frontend/paypal_checkout_spec.rb +169 -0
  186. data/spec/system/frontend/venmo_checkout_spec.rb +193 -0
  187. metadata +289 -255
  188. data/.travis.yml +0 -41
  189. data/LICENSE.txt +0 -21
  190. data/app/controllers/spree/api/braintree_client_token_controller.rb +0 -13
  191. data/app/helpers/braintree_view_helpers.rb +0 -20
  192. data/app/models/concerns/solidus_braintree/add_name_validation_concern.rb +0 -8
  193. data/app/models/concerns/solidus_braintree/inject_device_data_concern.rb +0 -18
  194. data/app/models/concerns/solidus_braintree/payment_braintree_nonce_concern.rb +0 -8
  195. data/app/models/concerns/solidus_braintree/permitted_attributes_concern.rb +0 -11
  196. data/app/models/concerns/solidus_braintree/skip_require_card_numbers_concern.rb +0 -14
  197. data/app/models/concerns/solidus_braintree/use_data_field_concern.rb +0 -23
  198. data/app/models/credit_card_decorator.rb +0 -3
  199. data/app/models/payment_decorator.rb +0 -2
  200. data/app/models/permitted_attributes_decorator.rb +0 -1
  201. data/app/models/solidus/gateway/braintree_gateway.rb +0 -306
  202. data/app/overrides/spree/checkout/_confirm/braintree_security.html.erb.deface +0 -9
  203. data/app/views/spree/admin/payments/source_forms/_braintree.html.erb +0 -38
  204. data/app/views/spree/admin/payments/source_views/_braintree.html.erb +0 -30
  205. data/app/views/spree/checkout/payment/_braintree.html.erb +0 -55
  206. data/app/views/spree/checkout/payment/_braintree_initialization.html.erb +0 -12
  207. data/config/initializers/braintree.rb +0 -1
  208. data/db/migrate/20150910170527_add_data_to_credit_card.rb +0 -5
  209. data/db/migrate/20160426221931_add_braintree_device_data_to_order.rb +0 -5
  210. data/lib/assets/javascripts/spree/backend/braintree/solidus_braintree.js +0 -59
  211. data/lib/assets/javascripts/spree/frontend/braintree/solidus_braintree.js +0 -144
  212. data/lib/assets/javascripts/vendor/braintree.js +0 -8
  213. data/lib/assets/stylesheets/spree/frontend/solidus_braintree.scss +0 -26
@@ -0,0 +1,239 @@
1
+ /**
2
+ * Braintree client interface
3
+ * @external "braintree.Client"
4
+ * @see {@link https://braintree.github.io/braintree-web/current/Client.html|Braintree Client Docs}
5
+ **/
6
+
7
+ /**
8
+ * Braintree paypal interface
9
+ * @external "braintree.PayPal"
10
+ * @see {@link https://braintree.github.io/braintree-web/current/PayPal.html|Braintree Paypal Docs}
11
+ **/
12
+
13
+ /**
14
+ * Braintree paypal interface
15
+ * @external "braintree.ApplePay"
16
+ * @see {@link https://braintree.github.io/braintree-web/current/ApplePay.html|Braintree Apple Pay Docs}
17
+ **/
18
+
19
+ /**
20
+ * Braintree dataCollector interface
21
+ * @external "braintree.DataCollector"
22
+ * @see {@link https://braintree.github.io/braintree-web/current/DataCollector.html|Braintree DataCollector Docs}
23
+ **/
24
+
25
+ /**
26
+ * jQuery.Deferred interface
27
+ *
28
+ * We use this for our promises because ES6 promises are non standard, and because jquery 1/2
29
+ * promises do not play nicely with them.
30
+ * @external "jQuery.Deferred"
31
+ * @see {@link https://api.jquery.com/category/deferred-object/|jQuery Deferred Documentation}
32
+ **/
33
+
34
+ /**
35
+ * Represents a wrapper around the braintree js library.
36
+ *
37
+ * This class is responsible for fetching tokens from a solidus store and using them
38
+ * to manage a braintree client. It takes a number of options as capabilities for the client
39
+ * depending on if you want to use use the data collector or paypal.
40
+ *
41
+ * We use this class mostly to hide the token operations for users.
42
+ *
43
+ * After creating the class, a call should be made to initialize before using it.
44
+ * @see initialize
45
+ *
46
+ * @constructor
47
+ * @param {Object} config Initalization options for the client
48
+ * @param {Boolean} config.useDataCollector Use data collector capabilities for the braintree client
49
+ * @param {Boolean} config.usePaypal Use Paypal capabilities for the braintree client
50
+ * @param {requestCallback} config.readyCallback A callback to be invoked when the client is ready to go.
51
+ * @param {Number} config.paymentMethodId A number indicating a specific payment method to be preferrred.
52
+ *
53
+ **/
54
+ SolidusBraintree.Client = function(config) {
55
+ this.paymentMethodId = config.paymentMethodId;
56
+ this.readyCallback = config.readyCallback;
57
+ this.useDataCollector = config.useDataCollector;
58
+ this.usePaypal = config.usePaypal;
59
+ this.useApplepay = config.useApplepay;
60
+ this.useVenmo = config.useVenmo;
61
+ this.flow = config.flow;
62
+ this.venmoNewTabSupported = config.newBrowserTabSupported
63
+ this.useThreeDSecure = config.useThreeDSecure;
64
+
65
+ this._braintreeInstance = null;
66
+ this._dataCollectorInstance = null;
67
+ this._paypalInstance = null;
68
+ this._venmoInstance = null;
69
+ this._threeDSecureInstance = null;
70
+ };
71
+
72
+ /**
73
+ * Fetches a client token from the backend and initializes the braintree client.
74
+ * @returns {external:"jQuery.Deferred"} Promise to be invoked after initialization is complete
75
+ **/
76
+ SolidusBraintree.Client.prototype.initialize = function() {
77
+ var initializationPromise = this._fetchToken().
78
+ then(this._createBraintreeInstance.bind(this));
79
+
80
+ if (this.useDataCollector) {
81
+ initializationPromise = initializationPromise.then(this._createDataCollector.bind(this));
82
+ }
83
+
84
+ if (this.usePaypal) {
85
+ initializationPromise = initializationPromise.then(this._createPaypal.bind(this));
86
+ }
87
+
88
+ if (this.useApplepay) {
89
+ initializationPromise = initializationPromise.then(this._createApplepay.bind(this));
90
+ }
91
+
92
+ if (this.useVenmo) {
93
+ initializationPromise = initializationPromise.then(this._createVenmo.bind(this));
94
+ }
95
+
96
+ if (this.useThreeDSecure) {
97
+ initializationPromise = initializationPromise.then(this._createThreeDSecure.bind(this));
98
+ }
99
+
100
+ return initializationPromise.then(this._invokeReadyCallback.bind(this));
101
+ };
102
+
103
+ /**
104
+ * Returns the braintree client instance
105
+ * @returns {external:"braintree.Client"} The braintree client that was initialized by this class
106
+ **/
107
+ SolidusBraintree.Client.prototype.getBraintreeInstance = function() {
108
+ return this._braintreeInstance;
109
+ };
110
+
111
+ /**
112
+ * Returns the braintree paypal instance
113
+ * @returns {external:"braintree.PayPal"} The braintree paypal that was initialized by this class
114
+ **/
115
+ SolidusBraintree.Client.prototype.getPaypalInstance = function() {
116
+ return this._paypalInstance;
117
+ };
118
+
119
+ /**
120
+ * Returns the braintree Apple Pay instance
121
+ * @returns {external:"braintree.ApplePay"} The Braintree Apple Pay that was initialized by this class
122
+ **/
123
+ SolidusBraintree.Client.prototype.getApplepayInstance = function() {
124
+ return this._applepayInstance;
125
+ };
126
+
127
+ /**
128
+ * Returns the braintree Venmo instance
129
+ * @returns {external:"braintree.Venmo"} The Braintree Venmo that was initialized by this class
130
+ **/
131
+ SolidusBraintree.Client.prototype.getVenmoInstance = function() {
132
+ return this._venmoInstance;
133
+ };
134
+
135
+ /**
136
+ * Returns the braintree dataCollector instance
137
+ * @returns {external:"braintree.DataCollector"} The braintree dataCollector that was initialized by this class
138
+ **/
139
+ SolidusBraintree.Client.prototype.getDataCollectorInstance = function() {
140
+ return this._dataCollectorInstance;
141
+ };
142
+
143
+
144
+ SolidusBraintree.Client.prototype._fetchToken = function() {
145
+ var payload = {
146
+ dataType: 'json',
147
+ type: 'POST',
148
+ url: SolidusBraintree.config.paths.clientTokens,
149
+ error: function(xhr) {
150
+ console.error("Error fetching braintree token");
151
+ }
152
+ };
153
+
154
+ if (this.paymentMethodId) {
155
+ payload.data = {
156
+ payment_method_id: this.paymentMethodId
157
+ };
158
+ }
159
+
160
+ return Spree.ajax(payload);
161
+ };
162
+
163
+ SolidusBraintree.Client.prototype._createBraintreeInstance = function(tokenResponse) {
164
+ this.paymentMethodId = tokenResponse.payment_method_id;
165
+
166
+ return SolidusBraintree.PromiseShim.convertBraintreePromise(braintree.client.create, [{
167
+ authorization: tokenResponse.client_token
168
+ }]).then(function (clientInstance) {
169
+ this._braintreeInstance = clientInstance;
170
+ return clientInstance;
171
+ }.bind(this));
172
+ };
173
+
174
+ SolidusBraintree.Client.prototype._invokeReadyCallback = function() {
175
+ if(this.readyCallback) {
176
+ this.readyCallback(this._braintreeInstance);
177
+ }
178
+
179
+ return this;
180
+ };
181
+
182
+ SolidusBraintree.Client.prototype._createDataCollector = function() {
183
+ return SolidusBraintree.PromiseShim.convertBraintreePromise(braintree.dataCollector.create, [{
184
+ client: this._braintreeInstance,
185
+ paypal: !!this.usePaypal
186
+ }]).then(function (dataCollectorInstance) {
187
+ this._dataCollectorInstance = dataCollectorInstance;
188
+ return dataCollectorInstance;
189
+ }.bind(this));
190
+ };
191
+
192
+ SolidusBraintree.Client.prototype._createPaypal = function() {
193
+ return SolidusBraintree.PromiseShim.convertBraintreePromise(braintree.paypalCheckout.create, [{
194
+ client: this._braintreeInstance
195
+ }]).then(function (paypalInstance) {
196
+ this._paypalInstance = paypalInstance;
197
+ return paypalInstance;
198
+ }.bind(this), function(error) {
199
+ console.error(error.name + ':', error.message);
200
+ });
201
+ };
202
+
203
+ SolidusBraintree.Client.prototype._createApplepay = function() {
204
+ return SolidusBraintree.PromiseShim.convertBraintreePromise(braintree.applePay.create, [{
205
+ client: this._braintreeInstance
206
+ }]).then(function (applePayInstance) {
207
+ this._applepayInstance = applePayInstance;
208
+ return applePayInstance;
209
+ }.bind(this));
210
+ };
211
+
212
+ SolidusBraintree.Client.prototype._createVenmo = function() {
213
+ return SolidusBraintree.PromiseShim.convertBraintreePromise(braintree.venmo.create, [{
214
+ client: this._braintreeInstance,
215
+ allowDesktop: true,
216
+ paymentMethodUsage: this.flow === 'vault' ? 'multi_use' : 'single_use',
217
+ allowNewBrowserTab: this.venmoNewTabSupported
218
+ }]).then(function (venmoInstance) {
219
+ // Verify browser support before proceeding.
220
+ if (!venmoInstance.isBrowserSupported()) {
221
+ console.log('Browser does not support Venmo');
222
+ return;
223
+ }
224
+
225
+ this._venmoInstance = venmoInstance;
226
+ return venmoInstance;
227
+ }.bind(this));
228
+ };
229
+
230
+ SolidusBraintree.Client.prototype._createThreeDSecure = function() {
231
+ return SolidusBraintree.PromiseShim.convertBraintreePromise(braintree.threeDSecure.create, [{
232
+ client: this._braintreeInstance,
233
+ version: 2
234
+ }]).then(function (threeDSecureInstance) {
235
+ this._threeDSecureInstance = threeDSecureInstance;
236
+ }.bind(this), function(error) {
237
+ console.log(error);
238
+ });
239
+ };
@@ -0,0 +1,89 @@
1
+ SolidusBraintree = {
2
+ APPLE_PAY_API_VERSION: 1,
3
+
4
+ config: {
5
+ paths: {
6
+ clientTokens: Spree.pathFor('solidus_braintree/client_token'),
7
+ transactions: Spree.pathFor('solidus_braintree/transactions')
8
+ },
9
+
10
+ // Override to provide your own error messages.
11
+ braintreeErrorHandle: function(braintreeError) {
12
+ BraintreeError.getErrorFromSlug(braintreeError.code);
13
+ SolidusBraintree.showError(error);
14
+ },
15
+
16
+ classes: {
17
+ hostedForm: function() {
18
+ return SolidusBraintree.HostedForm;
19
+ },
20
+
21
+ client: function() {
22
+ return SolidusBraintree.Client;
23
+ },
24
+
25
+ paypalButton: function() {
26
+ return SolidusBraintree.PaypalButton;
27
+ },
28
+
29
+ paypalMessaging: function() {
30
+ return SolidusBraintree.PaypalMessaging;
31
+ },
32
+
33
+ applepayButton: function() {
34
+ return SolidusBraintree.ApplepayButton;
35
+ },
36
+
37
+ venmoButton: function() {
38
+ return SolidusBraintree.VenmoButton;
39
+ }
40
+ }
41
+ },
42
+
43
+ showError: function(error) {
44
+ var $contentContainer = $("#content");
45
+ var $flash = $("<div class='flash error'>" + error + "</div>");
46
+ $contentContainer.prepend($flash);
47
+ $flash.show().delay(5000).fadeOut(500);
48
+ },
49
+
50
+ createHostedForm: function() {
51
+ return SolidusBraintree._factory(SolidusBraintree.config.classes.hostedForm(), arguments);
52
+ },
53
+
54
+ createClient: function() {
55
+ return SolidusBraintree._factory(SolidusBraintree.config.classes.client(), arguments);
56
+ },
57
+
58
+ createPaypalButton: function() {
59
+ return SolidusBraintree._factory(SolidusBraintree.config.classes.paypalButton(), arguments);
60
+ },
61
+
62
+ createPaypalMessaging: function() {
63
+ return SolidusBraintree._factory(SolidusBraintree.config.classes.paypalMessaging(), arguments);
64
+ },
65
+
66
+ createApplePayButton: function() {
67
+ return SolidusBraintree._factory(SolidusBraintree.config.classes.applepayButton(), arguments);
68
+ },
69
+
70
+ createVenmoButton: function() {
71
+ return SolidusBraintree._factory(SolidusBraintree.config.classes.venmoButton(), arguments);
72
+ },
73
+
74
+ _factory: function(klass, args) {
75
+ var normalizedArgs = Array.prototype.slice.call(args);
76
+ return new (Function.prototype.bind.apply(klass, [null].concat(normalizedArgs)));
77
+ }
78
+ };
79
+
80
+ BraintreeError = {
81
+ DEFAULT: "Something bad happened!",
82
+
83
+ getErrorFromSlug: function(slug) {
84
+ error = BraintreeError.DEFAULT
85
+ if (slug in BraintreeError)
86
+ error = BraintreeError[slug]
87
+ return error
88
+ }
89
+ }
@@ -0,0 +1,46 @@
1
+ SolidusBraintree.HostedForm = function(paymentMethodId) {
2
+ this.paymentMethodId = paymentMethodId;
3
+ this.client = null;
4
+ };
5
+
6
+ SolidusBraintree.HostedForm.prototype.initialize = function() {
7
+ this.client = SolidusBraintree.createClient({
8
+ paymentMethodId: this.paymentMethodId,
9
+ useThreeDSecure: (typeof(window.threeDSecureOptions) !== 'undefined'),
10
+ });
11
+
12
+ return this.client.initialize().
13
+ then(this._createHostedFields.bind(this));
14
+ };
15
+
16
+ SolidusBraintree.HostedForm.prototype._createHostedFields = function () {
17
+ if (!this.client) {
18
+ throw new Error("Client not initialized, please call initialize first!");
19
+ }
20
+
21
+ var opts = {
22
+ _solidusClient: this.client,
23
+ client: this.client.getBraintreeInstance(),
24
+
25
+ fields: {
26
+ number: {
27
+ selector: "#card_number" + this.paymentMethodId,
28
+ placeholder: placeholder_text["number"]
29
+ },
30
+
31
+ cvv: {
32
+ selector: "#card_code" + this.paymentMethodId,
33
+ placeholder: placeholder_text["cvv"]
34
+ },
35
+
36
+ expirationDate: {
37
+ selector: "#card_expiry" + this.paymentMethodId,
38
+ placeholder: placeholder_text["expirationDate"]
39
+ }
40
+ },
41
+
42
+ styles: credit_card_fields_style
43
+ };
44
+
45
+ return SolidusBraintree.PromiseShim.convertBraintreePromise(braintree.hostedFields.create, [opts]);
46
+ };
@@ -0,0 +1,20 @@
1
+ SolidusBraintree.PromiseShim = {
2
+ convertBraintreePromise: function(fn, args, context) {
3
+ var jqPromise = $.Deferred();
4
+
5
+ args = args || [];
6
+ context = context || this;
7
+
8
+ args = args.concat(function(error, data) {
9
+ if (error) {
10
+ jqPromise.reject(error);
11
+ } else {
12
+ jqPromise.resolve(data);
13
+ }
14
+ });
15
+
16
+ fn.apply(context, args);
17
+
18
+ return jqPromise.promise();
19
+ }
20
+ }
@@ -0,0 +1,96 @@
1
+ //= require spree/backend/solidus_braintree/constants
2
+ //= require spree/backend/solidus_braintree/client
3
+ //= require spree/backend/solidus_braintree/promise
4
+ //= require spree/backend/solidus_braintree/hosted_form
5
+
6
+ $(function() {
7
+ var $paymentForm = $("#new_payment"),
8
+ $hostedFields = $("[data-braintree-hosted-fields]"),
9
+ hostedFieldsInstance = null;
10
+
11
+ function onError (err) {
12
+ var msg = err.name + ": " + err.message;
13
+ show_flash("error", msg);
14
+ }
15
+
16
+ function showForm(id) {
17
+ $("#card_form" + id).show();
18
+ }
19
+
20
+ function hideForm(id) {
21
+ $("#card_form" + id).hide();
22
+ }
23
+
24
+ function addFormHook(braintreeForm, errorCallback) {
25
+ var shouldSubmit = false;
26
+
27
+ function submit(payload) {
28
+ shouldSubmit = true;
29
+
30
+ $("#payment_method_nonce", braintreeForm.hostedFields).val(payload.nonce);
31
+ $paymentForm.submit();
32
+ }
33
+
34
+ return function(hostedFields) {
35
+ $paymentForm.on("submit", function(e) {
36
+ if ($hostedFields.is(":visible") && !shouldSubmit) {
37
+ e.preventDefault();
38
+
39
+ hostedFields.tokenize(function(err, payload) {
40
+ if (err) {
41
+ errorCallback(err);
42
+ } else {
43
+ submit(payload);
44
+ }
45
+ });
46
+ }
47
+ });
48
+ };
49
+ }
50
+
51
+ function initFields($container, id) {
52
+ function setHostedFieldsInstance(instance) {
53
+ hostedFieldsInstance = instance;
54
+ return instance;
55
+ }
56
+
57
+ if (hostedFieldsInstance === null) {
58
+ braintreeForm = new SolidusBraintree.createHostedForm(id);
59
+ braintreeForm.initialize().
60
+ then(setHostedFieldsInstance).
61
+ then(addFormHook(braintreeForm, onError)).
62
+ fail(onError);
63
+ }
64
+ }
65
+
66
+ // exit early if we're not looking at the New Payment form, or if no
67
+ // SolidusBraintree payment methods have been configured.
68
+ if (!$paymentForm.length || !$hostedFields.length) { return; }
69
+
70
+ $.when(
71
+ $.getScript("https://js.braintreegateway.com/web/3.84.0/js/client.min.js"),
72
+ $.getScript("https://js.braintreegateway.com/web/3.84.0/js/hosted-fields.min.js")
73
+ ).done(function() {
74
+ $hostedFields.each(function() {
75
+ var $this = $(this),
76
+ $radios = $("[name=card]", $this),
77
+ id = $this.data("payment-method-id");
78
+
79
+ // If we have previous cards, init fields on change of radio button
80
+ if ($radios.length) {
81
+ $radios.on("change", function() {
82
+ if ($(this).val() == 'new') {
83
+ showForm(id);
84
+ initFields($this, id);
85
+ } else {
86
+ hideForm(id);
87
+ }
88
+ });
89
+ } else {
90
+ // If we don't have previous cards, init fields immediately
91
+ initFields($this, id);
92
+ showForm(id);
93
+ }
94
+ });
95
+ });
96
+ });
@@ -0,0 +1,28 @@
1
+ /*
2
+ Placeholder manifest file.
3
+ the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/backend/all.css'
4
+ */
5
+
6
+ // Set Bootstrap variables if they're not already defined
7
+ // e.g if we're on Solidus < v1.4
8
+ $input-border-color: #cee1f4 !default;
9
+ $border-radius: 3px !default;
10
+ $input-color: #5498DA !default;
11
+ $brand-danger: #C60F13 !default;
12
+ $brand-success: #9FC820 !default;
13
+
14
+ .braintree-hosted-fields .input {
15
+ border: 1px solid $input-border-color;
16
+ border-radius: $border-radius;
17
+ color: $input-color;
18
+ height: 30px;
19
+ padding: 7px 10px;
20
+
21
+ &.braintree-hosted-fields-invalid {
22
+ border-color: $brand-danger;
23
+ }
24
+
25
+ &.braintree-hosted-fields-valid {
26
+ border-color: $brand-success;
27
+ }
28
+ }
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusBraintree
4
+ module AdminPaymentsControllerDecorator
5
+ def self.prepended(base)
6
+ base.helper ::SolidusBraintree::BraintreeAdminHelper
7
+ end
8
+
9
+ ::Spree::Admin::PaymentsController.prepend(self)
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusBraintree
4
+ class ClientTokensController < ::Spree::Api::BaseController
5
+ skip_before_action :authenticate_user
6
+
7
+ before_action :load_gateway
8
+
9
+ def create
10
+ token = @gateway.generate_token
11
+ if token
12
+ render json: { client_token: token, payment_method_id: @gateway.id }
13
+ else
14
+ render json: { error: Gateway::TOKEN_GENERATION_DISABLED_MESSAGE }, status: :unprocessable_entity
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def load_gateway
21
+ if params[:payment_method_id]
22
+ @gateway = ::SolidusBraintree::Gateway.find(params[:payment_method_id])
23
+ else
24
+ store_payment_methods_scope =
25
+ if current_store.payment_methods.empty?
26
+ ::SolidusBraintree::Gateway.all
27
+ else
28
+ ::SolidusBraintree::Gateway.where(id: current_store.payment_method_ids)
29
+ end
30
+ @gateway = ::SolidusBraintree::Gateway.where(active: true).merge(store_payment_methods_scope).first!
31
+ end
32
+ end
33
+
34
+ def generate_token
35
+ @gateway.generate_token
36
+ rescue ::SolidusBraintree::Gateway::TokenGenerationDisabledError => e
37
+ Rails.logger.error e
38
+ nil
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusBraintree
4
+ module Spree
5
+ module StoreDecorator
6
+ def self.prepended(base)
7
+ base.has_one :braintree_configuration, class_name: "SolidusBraintree::Configuration", dependent: :destroy
8
+ base.before_create :build_default_configuration
9
+ end
10
+
11
+ private
12
+
13
+ def build_default_configuration
14
+ build_braintree_configuration unless braintree_configuration
15
+ end
16
+
17
+ ::Spree::Store.prepend self
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusBraintree
4
+ module Spree
5
+ module UserDecorator
6
+ def self.prepended(base)
7
+ base.has_one :braintree_customer, class_name: 'SolidusBraintree::Customer', inverse_of: :user
8
+ end
9
+
10
+ ::Spree.user_class.prepend self
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusBraintree
4
+ module BraintreeAdminHelper
5
+ # Returns a link to the Braintree web UI for the given Braintree payment
6
+ def braintree_transaction_link(payment)
7
+ environment = payment.payment_method.preferred_environment == 'sandbox' ? 'sandbox' : 'www'
8
+ merchant_id = payment.payment_method.preferred_merchant_id
9
+ response_code = payment.response_code
10
+
11
+ return if response_code.blank?
12
+ return response_code if merchant_id.blank?
13
+
14
+ link_to(
15
+ response_code,
16
+ "https://#{environment}.braintreegateway.com/merchants/#{merchant_id}/transactions/#{response_code}",
17
+ title: 'Show payment on Braintree',
18
+ target: '_blank',
19
+ rel: 'noopener'
20
+ )
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
5
+ end