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 SolidusBraintree.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: Solidus.pathFor('solidus_braintree/client_token'),
7
+ transactions: Solidus.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 = $("main");
45
+ var $flash = $("<div class='flash error'>" + error + "</div>");
46
+ $contentContainer.before($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,15 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require spree/frontend/solidus_braintree/constants
8
+ //= require spree/frontend/solidus_braintree/ajax
9
+ //= require spree/frontend/solidus_braintree/promise
10
+ //= require spree/frontend/solidus_braintree/client
11
+ //= require spree/frontend/solidus_braintree/hosted_form
12
+ //= require spree/frontend/solidus_braintree/paypal_button
13
+ //= require spree/frontend/solidus_braintree/paypal_messaging
14
+ //= require spree/frontend/solidus_braintree/apple_pay_button
15
+ //= require spree/frontend/solidus_braintree/venmo_button
@@ -0,0 +1,48 @@
1
+ SolidusBraintree.HostedForm = function(paymentMethodId, useDataCollector) {
2
+ this.paymentMethodId = paymentMethodId;
3
+ this.useDataCollector = useDataCollector;
4
+ this.client = null;
5
+ };
6
+
7
+ SolidusBraintree.HostedForm.prototype.initialize = function() {
8
+ this.client = SolidusBraintree.createClient({
9
+ paymentMethodId: this.paymentMethodId,
10
+ useThreeDSecure: (typeof(window.threeDSecureOptions) !== 'undefined'),
11
+ useDataCollector: this.useDataCollector,
12
+ });
13
+
14
+ return this.client.initialize().
15
+ then(this._createHostedFields.bind(this));
16
+ };
17
+
18
+ SolidusBraintree.HostedForm.prototype._createHostedFields = function () {
19
+ if (!this.client) {
20
+ throw new Error("Client not initialized, please call initialize first!");
21
+ }
22
+
23
+ var opts = {
24
+ _solidusClient: this.client,
25
+ client: this.client.getBraintreeInstance(),
26
+
27
+ fields: {
28
+ number: {
29
+ selector: "#card_number" + this.paymentMethodId,
30
+ placeholder: placeholder_text["number"]
31
+ },
32
+
33
+ cvv: {
34
+ selector: "#card_code" + this.paymentMethodId,
35
+ placeholder: placeholder_text["cvv"]
36
+ },
37
+
38
+ expirationDate: {
39
+ selector: "#card_expiry" + this.paymentMethodId,
40
+ placeholder: placeholder_text["expirationDate"]
41
+ }
42
+ },
43
+
44
+ styles: credit_card_fields_style
45
+ };
46
+
47
+ return SolidusBraintree.PromiseShim.convertBraintreePromise(braintree.hostedFields.create, [opts]);
48
+ };
@@ -0,0 +1,178 @@
1
+ //= require spree/frontend/solidus_braintree/constants
2
+ /**
3
+ * Constructor for PayPal button object
4
+ * @constructor
5
+ * @param {object} element - The DOM element of your PayPal button
6
+ */
7
+ SolidusBraintree.PaypalButton = function(element, paypalOptions, options) {
8
+ this._element = element;
9
+ this._paypalOptions = paypalOptions || {};
10
+
11
+ this.locale = paypalOptions['locale'] || "en_US";
12
+ this.style = paypalOptions['style'] || {};
13
+ delete paypalOptions['locale'];
14
+ delete paypalOptions['style'];
15
+
16
+ this._options = options || {};
17
+ this._client = null;
18
+ this._environment = this._paypalOptions.environment || 'sandbox';
19
+ delete this._paypalOptions.environment;
20
+
21
+ this._buyerCountry = this._paypalOptions.buyerCountry;
22
+ delete paypalOptions['buyerCountry'];
23
+
24
+ this._enabledFunding = [];
25
+
26
+ if (paypalOptions['venmoFunding']) this._enabledFunding.push('venmo');
27
+ delete paypalOptions['venmoFunding'];
28
+
29
+ if(!this._element) {
30
+ throw new Error("Element for the paypal button must be present on the page");
31
+ }
32
+ };
33
+
34
+ /**
35
+ * Creates the PayPal session using the provided options and enables the button
36
+ *
37
+ * @param {object} options - The options passed to tokenize when constructing
38
+ * the PayPal instance
39
+ *
40
+ * See {@link https://braintree.github.io/braintree-web/3.9.0/PayPal.html#tokenize}
41
+ */
42
+ SolidusBraintree.PaypalButton.prototype.initialize = function() {
43
+ this._client = new SolidusBraintree.createClient({
44
+ useDataCollector: this._paypalOptions.useDataCollector,
45
+ usePaypal: true
46
+ });
47
+
48
+ return this._client.initialize().then(this.initializeCallback.bind(this));
49
+ };
50
+
51
+ SolidusBraintree.PaypalButton.prototype.initializeCallback = function() {
52
+ this._paymentMethodId = this._client.paymentMethodId;
53
+
54
+ var args = {
55
+ "client-id": this._environment === "sandbox" ? "sb" : null,
56
+ currency: this._paypalOptions.currency,
57
+ commit: true,
58
+ vault: this._paypalOptions.flow == "vault",
59
+ components: this.style['messaging'] == "true" && this._paypalOptions.flow != "vault" ? "buttons,messages" : "buttons",
60
+ intent: this._paypalOptions.flow == "vault" ? "tokenize" : "authorize"
61
+ };
62
+
63
+ if (this._environment === "sandbox" && this._buyerCountry) {
64
+ args["buyer-country"] = this._buyerCountry
65
+ }
66
+ if (this._enabledFunding.length !== 0) {
67
+ args["enable-funding"] = this._enabledFunding.join(',');
68
+ }
69
+
70
+ this._client.getPaypalInstance().loadPayPalSDK(args).then(() => {
71
+ var create_method = this._paypalOptions.flow == "vault" ? "createBillingAgreement" : "createOrder"
72
+
73
+ var render_config = {
74
+ style: this.style,
75
+ onClick: (data) => { SolidusBraintree.fundingSource = data.fundingSource },
76
+ [create_method]: function () {
77
+ return this._client.getPaypalInstance().createPayment(this._paypalOptions);
78
+ }.bind(this),
79
+ onApprove: function (data, actions) {
80
+ return this._client.getPaypalInstance().tokenizePayment(data, this._tokenizeCallback.bind(this));
81
+ }.bind(this)
82
+ };
83
+
84
+ paypal.Buttons(render_config).render(this._element);
85
+ })
86
+ };
87
+
88
+ /**
89
+ * Default callback function for when tokenization completes
90
+ *
91
+ * @param {object|null} tokenizeErr - The error returned by Braintree on failure
92
+ * @param {object} payload - The payload returned by Braintree on success
93
+ */
94
+ SolidusBraintree.PaypalButton.prototype._tokenizeCallback = function(tokenizeErr, payload) {
95
+ if (tokenizeErr) {
96
+ SolidusBraintree.config.braintreeErrorHandle(tokenizeErr);
97
+ return;
98
+ }
99
+
100
+ var params = this._transactionParams(payload);
101
+
102
+ return SolidusBraintree.ajax({
103
+ url: SolidusBraintree.config.paths.transactions,
104
+ type: 'POST',
105
+ dataType: 'json',
106
+ data: params,
107
+ success: function(response) {
108
+ window.location.href = response.redirectUrl;
109
+ },
110
+ error: function(xhr) {
111
+ var errorText = BraintreeError.DEFAULT;
112
+
113
+ if (xhr.responseJSON && xhr.responseJSON.errors) {
114
+ var errors = [];
115
+ $.each(xhr.responseJSON.errors, function(key, values) {
116
+ $.each(values, function(index, value) {
117
+ errors.push(key + " " + value)
118
+ });
119
+ });
120
+
121
+ if (errors.length > 0)
122
+ errorText = errors.join(", ");
123
+ }
124
+
125
+ console.error("Error submitting transaction: " + errorText);
126
+ SolidusBraintree.showError(errorText);
127
+ },
128
+ });
129
+ };
130
+
131
+ /**
132
+ * Builds the transaction parameters to submit to Solidus for the given
133
+ * payload returned by Braintree
134
+ *
135
+ * @param {object} payload - The payload returned by Braintree after tokenization
136
+ */
137
+ SolidusBraintree.PaypalButton.prototype._transactionParams = function(payload) {
138
+ return {
139
+ "payment_method_id" : this._paymentMethodId,
140
+ "options": this._options,
141
+ "transaction" : {
142
+ "email" : payload.details.email,
143
+ "phone" : payload.details.phone,
144
+ "nonce" : payload.nonce,
145
+ "payment_type" : payload.type,
146
+ "paypal_funding_source": SolidusBraintree.fundingSource,
147
+ "address_attributes" : this._addressParams(payload)
148
+ }
149
+ };
150
+ };
151
+
152
+ /**
153
+ * Builds the address parameters to submit to Solidus using the payload
154
+ * returned by Braintree
155
+ *
156
+ * @param {object} payload - The payload returned by Braintree after tokenization
157
+ */
158
+ SolidusBraintree.PaypalButton.prototype._addressParams = function(payload) {
159
+ var name;
160
+ var payload_address = payload.details.shippingAddress || payload.details.billingAddress;
161
+ if (!payload_address) return {};
162
+
163
+ if (payload_address.recipientName) {
164
+ name = payload_address.recipientName
165
+ } else {
166
+ name = payload.details.firstName + " " + payload.details.lastName;
167
+ }
168
+
169
+ return {
170
+ "name" : name,
171
+ "address_line_1" : payload_address.line1,
172
+ "address_line_2" : payload_address.line2,
173
+ "city" : payload_address.city,
174
+ "state_code" : payload_address.state,
175
+ "zip" : payload_address.postalCode,
176
+ "country_code" : payload_address.countryCode
177
+ };
178
+ };
@@ -0,0 +1,22 @@
1
+ //= require spree/frontend/solidus_braintree/constants
2
+
3
+ SolidusBraintree.PaypalMessaging = function(paypalOptions) {
4
+ this._paypalOptions = paypalOptions || {};
5
+
6
+ this._client = null;
7
+ };
8
+
9
+ SolidusBraintree.PaypalMessaging.prototype.initialize = function() {
10
+ this._client = new SolidusBraintree.createClient({usePaypal: true});
11
+
12
+ return this._client.initialize().then(this.initializeCallback.bind(this));
13
+ };
14
+
15
+ SolidusBraintree.PaypalMessaging.prototype.initializeCallback = function() {
16
+ this._paymentMethodId = this._client.paymentMethodId;
17
+
18
+ this._client.getPaypalInstance().loadPayPalSDK({
19
+ currency: this._paypalOptions.currency,
20
+ components: "messages"
21
+ })
22
+ };
@@ -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
+ }