solidus_paypal_braintree 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +49 -36
- data/app/assets/javascripts/solidus_paypal_braintree/apple_pay_button.js +179 -0
- data/app/assets/javascripts/{spree/checkout/braintree.js → solidus_paypal_braintree/checkout.js} +35 -16
- data/app/assets/javascripts/solidus_paypal_braintree/client.js +186 -0
- data/app/assets/javascripts/solidus_paypal_braintree/constants.js +58 -0
- data/app/assets/javascripts/solidus_paypal_braintree/frontend.js +12 -0
- data/app/assets/javascripts/solidus_paypal_braintree/hosted_form.js +36 -0
- data/app/assets/javascripts/solidus_paypal_braintree/paypal_button.js +114 -0
- data/app/assets/javascripts/solidus_paypal_braintree/promise.js +20 -0
- data/app/assets/javascripts/spree/backend/solidus_paypal_braintree.js +34 -4
- data/app/assets/javascripts/spree/frontend/paypal_button.js +9 -159
- data/app/assets/javascripts/spree/frontend/solidus_paypal_braintree.js +1 -188
- data/app/assets/stylesheets/spree/frontend/solidus_paypal_braintree.css +23 -0
- data/lib/generators/solidus_paypal_braintree/install/install_generator.rb +1 -1
- data/lib/solidus_paypal_braintree/engine.rb +3 -2
- data/lib/solidus_paypal_braintree/version.rb +1 -1
- data/lib/views/frontend/spree/checkout/payment/_paypal_braintree.html.erb +52 -14
- metadata +11 -5
- data/app/assets/javascripts/spree/braintree_hosted_form.js +0 -98
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b312aa57cfc45563e9144f43afd85c8f948de70
|
4
|
+
data.tar.gz: 21c6f9335713d620391a3b88dfd0a547bcc3c972
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ece71d9f4adef4898506c4394ff4325a6718f26f0cb3bb9df8cd06922797a024f0fb522e31855586d6adcffcd4168fd94b7618dc4dd533a79dde7d4507fac1b
|
7
|
+
data.tar.gz: 4dadbe474742f3250c38e4bd1be205601c3334d532a67a4b38011dd2be2c8d855e8b77840e83d4e694884874d9c8eb07dbd8e17cb1f83aeb4014467bf3269891
|
data/README.md
CHANGED
@@ -83,45 +83,58 @@ Spree::Store.all.each do |store|
|
|
83
83
|
end
|
84
84
|
```
|
85
85
|
|
86
|
-
3. If your site uses an unmodified `solidus_frontend`, it should now be ready to take
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
###
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
86
|
+
3. If your site uses an unmodified `solidus_frontend`, it should now be ready to take payments. See below for more information on configuring Paypal and ApplePay.
|
87
|
+
|
88
|
+
4. Typical Solidus sites will have customized frontend code, and may require some additional work. Use `lib/views/frontend/spree/checkout/payment/_paypal_braintree.html.erb` and `app/assets/javascripts/solidus_paypal_braintree/checkout.js` as models.
|
89
|
+
|
90
|
+
## Apple Pay
|
91
|
+
### Developing with Apple Pay
|
92
|
+
You'll need the following:
|
93
|
+
- A device running iOS 10+.
|
94
|
+
- An Apple Pay sandbox account. You can check out Apple's [documentation](https://developer.apple.com/support/apple-pay-sandbox/) for additional help in performing this step.
|
95
|
+
- A site served via HTTPS. To set this up for development we recommend setting up a reverse proxy server. There are [lots of guides](https://www.google.ca/search?q=nginx+reverse+proxy+ssl+localhost) on how this can be achieved.
|
96
|
+
- A Braintree sandbox account with Apple Pay enabled (`Settings>Processing`) and configured (`Settings>Processing>Options`) with your Apple Merchant ID and the HTTPS domain for your site.
|
97
|
+
- A sandbox user logged in to your device, with a [test card](https://developer.apple.com/support/apple-pay-sandbox/) in its Wallet
|
98
|
+
|
99
|
+
### Enabling Apple Pay for custom frontends
|
100
|
+
The following is a relatively bare-bones implementation to enable Apple Pay on the frontend:
|
101
|
+
|
102
|
+
```html
|
103
|
+
<% if current_store.braintree_configuration.apple_pay? %>
|
104
|
+
<script src="https://js.braintreegateway.com/web/3.14.0/js/apple-pay.min.js"></script>
|
105
|
+
|
106
|
+
<button id="apple-pay-button" class="apple-pay-button"></button>
|
107
|
+
|
108
|
+
<script>
|
109
|
+
var applePayButtonElement = document.getElementById('apple-pay-button');
|
110
|
+
var applePayOptions = {
|
111
|
+
paymentMethodId: <%= id %>,
|
112
|
+
storeName: "<%= current_store.name %>",
|
113
|
+
orderEmail: "<%= current_order.email %>",
|
114
|
+
amount: "<%= current_order.total %>",
|
115
|
+
shippingContact: {
|
116
|
+
emailAddress: '<%= current_order.email %>',
|
117
|
+
familyName: '<%= address.firstname %>',
|
118
|
+
givenName: '<%= address.lastname %>',
|
119
|
+
phoneNumber: '<%= address.phone %>',
|
120
|
+
addressLines: ['<%= address.address1 %>','<%= address.address2 %>'],
|
121
|
+
locality: '<%= address.city %>',
|
122
|
+
administrativeArea: '<%= address.state.name %>',
|
123
|
+
postalCode: '<%= address.zipcode %>',
|
124
|
+
country: '<%= address.country.name %>',
|
125
|
+
countryCode: '<%= address.country.iso %>'
|
126
|
+
}
|
127
|
+
};
|
128
|
+
var button = new SolidusPaypalBraintree.createApplePayButton(applePayButtonElement, applePayOptions);
|
129
|
+
button.initialize();
|
130
|
+
</script>
|
131
|
+
<% end %>
|
115
132
|
```
|
116
133
|
|
117
|
-
|
118
|
-
|
119
|
-
### Development
|
120
|
-
Developing with Apple Pay has a few gotchas. First and foremost, you'll need to ensure you have access to a device running iOS 10+. (I've heard there's also been progress on adding support to the Simulator.)
|
121
|
-
|
122
|
-
Next, you'll need an Apple Pay sandbox account. You can check out Apple's [documentation](https://developer.apple.com/support/apple-pay-sandbox/) for additional help in performing this step.
|
134
|
+
### Further information
|
135
|
+
Braintree has some [excellent documentation](https://developers.braintreepayments.com/guides/apple-pay/configuration/javascript/v3) on what you'll need to do to get Apple Pay up and running.
|
123
136
|
|
124
|
-
|
137
|
+
For additional information check out [Apple's documentation](https://developer.apple.com/reference/applepayjs/) and [Braintree's documentation](https://developers.braintreepayments.com/guides/apple-pay/client-side/javascript/v3).
|
125
138
|
|
126
139
|
PayPal
|
127
140
|
------
|
@@ -0,0 +1,179 @@
|
|
1
|
+
//= require solidus_paypal_braintree/constants
|
2
|
+
/**
|
3
|
+
* Constructor for Apple Pay button object
|
4
|
+
* @constructor
|
5
|
+
* @param {object} element - The DOM element of your Apple Pay button
|
6
|
+
*/
|
7
|
+
SolidusPaypalBraintree.ApplepayButton = function(element, applepayOptions) {
|
8
|
+
this._element = element;
|
9
|
+
this._applepayOptions = applepayOptions || {};
|
10
|
+
this._client = null;
|
11
|
+
|
12
|
+
if(!this._element) {
|
13
|
+
throw new Error("Element for the Apple Pay button must be present on the page");
|
14
|
+
}
|
15
|
+
};
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Creates the Apple Pay session using the provided options and enables the button
|
19
|
+
*
|
20
|
+
* @param {object} options - The options passed to tokenize when constructing
|
21
|
+
* the Apple Pay instance
|
22
|
+
*
|
23
|
+
* See {@link https://braintree.github.io/braintree-web/3.9.0/Apple Pay.html#tokenize}
|
24
|
+
*/
|
25
|
+
SolidusPaypalBraintree.ApplepayButton.prototype.initialize = function() {
|
26
|
+
if (window.ApplePaySession && ApplePaySession.canMakePayments()) {
|
27
|
+
this._client = new SolidusPaypalBraintree.createClient(
|
28
|
+
{
|
29
|
+
useDataCollector: false,
|
30
|
+
useApplepay: true,
|
31
|
+
paymentMethodId: this._applepayOptions.paymentMethodId
|
32
|
+
}
|
33
|
+
);
|
34
|
+
return this._client.initialize().then(this.initializeCallback.bind(this));
|
35
|
+
}
|
36
|
+
};
|
37
|
+
|
38
|
+
SolidusPaypalBraintree.ApplepayButton.prototype.initializeCallback = function() {
|
39
|
+
this._paymentMethodId = this._client.paymentMethodId;
|
40
|
+
this._applePayInstance = this._client.getApplepayInstance();
|
41
|
+
|
42
|
+
this._element.removeAttribute('disabled');
|
43
|
+
this._element.classList.add("visible");
|
44
|
+
this._element.addEventListener('click', function(event) {
|
45
|
+
this.initializeApplePaySession();
|
46
|
+
event.preventDefault();
|
47
|
+
}.bind(this), false);
|
48
|
+
};
|
49
|
+
|
50
|
+
/**
|
51
|
+
* Initialize and begin the ApplePay session
|
52
|
+
**/
|
53
|
+
SolidusPaypalBraintree.ApplepayButton.prototype.initializeApplePaySession = function() {
|
54
|
+
var paymentRequest = this._applePayInstance.createPaymentRequest(this._paymentRequestHash());
|
55
|
+
var session = new ApplePaySession(SolidusPaypalBraintree.APPLE_PAY_API_VERSION, paymentRequest);
|
56
|
+
var applePayButton = this;
|
57
|
+
|
58
|
+
session.onvalidatemerchant = function (event) {
|
59
|
+
applePayButton.validateMerchant(session, paymentRequest);
|
60
|
+
};
|
61
|
+
|
62
|
+
session.onpaymentauthorized = function (event) {
|
63
|
+
applePayButton.tokenize(session, event.payment);
|
64
|
+
};
|
65
|
+
|
66
|
+
session.begin();
|
67
|
+
};
|
68
|
+
|
69
|
+
SolidusPaypalBraintree.ApplepayButton.prototype.validateMerchant = function(session, paymentRequest) {
|
70
|
+
this._applePayInstance.performValidation({
|
71
|
+
validationURL: event.validationURL,
|
72
|
+
displayName: paymentRequest.total.label,
|
73
|
+
}, function (validationErr, merchantSession) {
|
74
|
+
if (validationErr) {
|
75
|
+
console.error('Error validating Apple Pay:', validationErr);
|
76
|
+
session.abort();
|
77
|
+
return;
|
78
|
+
}
|
79
|
+
session.completeMerchantValidation(merchantSession);
|
80
|
+
});
|
81
|
+
};
|
82
|
+
|
83
|
+
SolidusPaypalBraintree.ApplepayButton.prototype.tokenize = function (session, payment) {
|
84
|
+
this._applePayInstance.tokenize(
|
85
|
+
{token: payment.token},
|
86
|
+
function (tokenizeErr, payload) {
|
87
|
+
if (tokenizeErr) {
|
88
|
+
console.error('Error tokenizing Apple Pay:', tokenizeErr);
|
89
|
+
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
90
|
+
}
|
91
|
+
this._createTransaction(session, payment, payload);
|
92
|
+
}.bind(this)
|
93
|
+
);
|
94
|
+
};
|
95
|
+
|
96
|
+
SolidusPaypalBraintree.ApplepayButton.prototype._createTransaction = function (session, payment, payload) {
|
97
|
+
Spree.ajax({
|
98
|
+
data: this._transactionParams(payload, payment.shippingContact),
|
99
|
+
dataType: 'json',
|
100
|
+
type: 'POST',
|
101
|
+
url: SolidusPaypalBraintree.config.paths.transactions,
|
102
|
+
success: function(response) {
|
103
|
+
session.completePayment(ApplePaySession.STATUS_SUCCESS);
|
104
|
+
window.location.replace(response.redirectUrl);
|
105
|
+
},
|
106
|
+
error: function(xhr) {
|
107
|
+
if (xhr.status === 422) {
|
108
|
+
var errors = xhr.responseJSON.errors;
|
109
|
+
|
110
|
+
if (errors && errors.Address) {
|
111
|
+
session.completePayment(ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS);
|
112
|
+
} else {
|
113
|
+
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
114
|
+
}
|
115
|
+
}
|
116
|
+
}
|
117
|
+
});
|
118
|
+
};
|
119
|
+
|
120
|
+
// countryCode
|
121
|
+
// currencyCode
|
122
|
+
// merchantCapabilities
|
123
|
+
// supportedNetworks
|
124
|
+
// ... are added by the Braintree gateway, but can be overridden
|
125
|
+
// See https://developer.apple.com/documentation/applepayjs/applepaypaymentrequest
|
126
|
+
SolidusPaypalBraintree.ApplepayButton.prototype._paymentRequestHash = function() {
|
127
|
+
return {
|
128
|
+
total: {
|
129
|
+
label: this._applepayOptions.storeName,
|
130
|
+
amount: this._applepayOptions.amount
|
131
|
+
},
|
132
|
+
shippingContact: this._applepayOptions.shippingContact,
|
133
|
+
requiredShippingContactFields: ['postalAddress', 'phone', 'email']
|
134
|
+
};
|
135
|
+
};
|
136
|
+
|
137
|
+
/**
|
138
|
+
* Builds the transaction parameters to submit to Solidus for the given
|
139
|
+
* payload returned by Braintree
|
140
|
+
*
|
141
|
+
* @param {object} payload - The payload returned by Braintree after tokenization
|
142
|
+
*/
|
143
|
+
SolidusPaypalBraintree.ApplepayButton.prototype._transactionParams = function(payload, shippingContact) {
|
144
|
+
return {
|
145
|
+
payment_method_id: this._applepayOptions.paymentMethodId,
|
146
|
+
transaction: {
|
147
|
+
email: shippingContact.emailAddress,
|
148
|
+
nonce: payload.nonce,
|
149
|
+
payment_type: payload.type,
|
150
|
+
phone: shippingContact.phoneNumber,
|
151
|
+
address_attributes: this._addressParams(shippingContact)
|
152
|
+
}
|
153
|
+
};
|
154
|
+
};
|
155
|
+
|
156
|
+
/**
|
157
|
+
* Builds the address parameters to submit to Solidus using the payload
|
158
|
+
* returned by Braintree
|
159
|
+
*
|
160
|
+
* @param {object} payload - The payload returned by Braintree after tokenization
|
161
|
+
*/
|
162
|
+
SolidusPaypalBraintree.ApplepayButton.prototype._addressParams = function(shippingContact) {
|
163
|
+
var addressHash = {
|
164
|
+
country_name: shippingContact.country,
|
165
|
+
country_code: shippingContact.countryCode,
|
166
|
+
first_name: shippingContact.givenName,
|
167
|
+
last_name: shippingContact.familyName,
|
168
|
+
state_code: shippingContact.administrativeArea,
|
169
|
+
city: shippingContact.locality,
|
170
|
+
zip: shippingContact.postalCode,
|
171
|
+
address_line_1: shippingContact.addressLines[0]
|
172
|
+
};
|
173
|
+
|
174
|
+
if(shippingContact.addressLines.length > 1) {
|
175
|
+
addressHash.address_line_2 = shippingContact.addressLines[1];
|
176
|
+
}
|
177
|
+
|
178
|
+
return addressHash;
|
179
|
+
};
|
data/app/assets/javascripts/{spree/checkout/braintree.js → solidus_paypal_braintree/checkout.js}
RENAMED
@@ -1,11 +1,11 @@
|
|
1
|
-
//= require
|
1
|
+
//= require solidus_paypal_braintree/frontend
|
2
2
|
|
3
3
|
$(function() {
|
4
4
|
/* This provides a default error handler for Braintree. Since we prevent
|
5
5
|
* submission if tokenization fails, we need to manually re-enable the
|
6
6
|
* submit button. */
|
7
7
|
function braintreeError (err) {
|
8
|
-
SolidusPaypalBraintree.braintreeErrorHandle(err);
|
8
|
+
SolidusPaypalBraintree.config.braintreeErrorHandle(err);
|
9
9
|
enableSubmit();
|
10
10
|
}
|
11
11
|
|
@@ -29,32 +29,51 @@ $(function() {
|
|
29
29
|
$submitButton.attr("disabled", true).removeClass("primary").addClass("disabled");
|
30
30
|
}
|
31
31
|
|
32
|
+
function addFormHook(braintreeForm, hostedField) {
|
33
|
+
$paymentForm.on("submit",function(event) {
|
34
|
+
var $field = $(hostedField);
|
35
|
+
|
36
|
+
if ($field.is(":visible") && !$field.data("submitting")) {
|
37
|
+
var $nonce = $("#payment_method_nonce", $field);
|
38
|
+
|
39
|
+
if ($nonce.length > 0 && $nonce.val() === "") {
|
40
|
+
event.preventDefault();
|
41
|
+
disableSubmit();
|
42
|
+
|
43
|
+
braintreeForm.tokenize(function(error, payload) {
|
44
|
+
if (error) {
|
45
|
+
braintreeError(error);
|
46
|
+
} else {
|
47
|
+
$nonce.val(payload.nonce);
|
48
|
+
$paymentForm.submit();
|
49
|
+
}
|
50
|
+
});
|
51
|
+
}
|
52
|
+
}
|
53
|
+
});
|
54
|
+
}
|
55
|
+
|
32
56
|
var $paymentForm = $("#checkout_form_payment");
|
33
57
|
var $hostedFields = $("[data-braintree-hosted-fields]");
|
34
58
|
var $submitButton = $("input[type='submit']", $paymentForm);
|
35
|
-
var $checkoutForm = $("#checkout_form_payment");
|
36
59
|
|
37
60
|
// If we're not using hosted fields, the form doesn't need to wait.
|
38
61
|
if ($hostedFields.length > 0) {
|
39
62
|
disableSubmit();
|
40
|
-
}
|
41
|
-
|
42
|
-
$checkoutForm.submit(disableSubmit);
|
43
63
|
|
44
|
-
|
45
|
-
$.getScript("https://js.braintreegateway.com/web/3.9.0/js/client.min.js"),
|
46
|
-
$.getScript("https://js.braintreegateway.com/web/3.9.0/js/hosted-fields.min.js")
|
47
|
-
).done(function() {
|
48
|
-
var fieldPromises = $hostedFields.map(function() {
|
64
|
+
var fieldPromises = $hostedFields.map(function(index, field) {
|
49
65
|
var $this = $(this);
|
50
66
|
var id = $this.data("id");
|
51
67
|
|
52
|
-
var braintreeForm = new
|
53
|
-
|
54
|
-
|
55
|
-
|
68
|
+
var braintreeForm = new SolidusPaypalBraintree.createHostedForm(id);
|
69
|
+
|
70
|
+
var formInitializationSuccess = function(formObject) {
|
71
|
+
addFormHook(formObject, field);
|
72
|
+
}
|
73
|
+
|
74
|
+
return braintreeForm.initialize().then(formInitializationSuccess, braintreeError);
|
56
75
|
});
|
57
76
|
|
58
77
|
$.when.apply($, fieldPromises).done(enableSubmit);
|
59
|
-
}
|
78
|
+
}
|
60
79
|
});
|
@@ -0,0 +1,186 @@
|
|
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
|
+
SolidusPaypalBraintree.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
|
+
|
61
|
+
this._braintreeInstance = null;
|
62
|
+
this._dataCollectorInstance = null;
|
63
|
+
this._paypalInstance = null;
|
64
|
+
};
|
65
|
+
|
66
|
+
/**
|
67
|
+
* Fetches a client token from the backend and initializes the braintree client.
|
68
|
+
* @returns {external:"jQuery.Deferred"} Promise to be invoked after initialization is complete
|
69
|
+
**/
|
70
|
+
SolidusPaypalBraintree.Client.prototype.initialize = function() {
|
71
|
+
var initializationPromise = this._fetchToken().
|
72
|
+
then(this._createBraintreeInstance.bind(this));
|
73
|
+
|
74
|
+
if(this.useDataCollector) {
|
75
|
+
initializationPromise = initializationPromise.then(this._createDataCollector.bind(this));
|
76
|
+
}
|
77
|
+
|
78
|
+
if(this.usePaypal) {
|
79
|
+
initializationPromise = initializationPromise.then(this._createPaypal.bind(this));
|
80
|
+
}
|
81
|
+
|
82
|
+
if(this.useApplepay) {
|
83
|
+
initializationPromise = initializationPromise.then(this._createApplepay.bind(this));
|
84
|
+
}
|
85
|
+
|
86
|
+
return initializationPromise.then(this._invokeReadyCallback.bind(this));
|
87
|
+
};
|
88
|
+
|
89
|
+
/**
|
90
|
+
* Returns the braintree client instance
|
91
|
+
* @returns {external:"braintree.Client"} The braintree client that was initialized by this class
|
92
|
+
**/
|
93
|
+
SolidusPaypalBraintree.Client.prototype.getBraintreeInstance = function() {
|
94
|
+
return this._braintreeInstance;
|
95
|
+
};
|
96
|
+
|
97
|
+
/**
|
98
|
+
* Returns the braintree paypal instance
|
99
|
+
* @returns {external:"braintree.PayPal"} The braintree paypal that was initialized by this class
|
100
|
+
**/
|
101
|
+
SolidusPaypalBraintree.Client.prototype.getPaypalInstance = function() {
|
102
|
+
return this._paypalInstance;
|
103
|
+
};
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Returns the braintree Apple Pay instance
|
107
|
+
* @returns {external:"braintree.ApplePay"} The Braintree Apple Pay that was initialized by this class
|
108
|
+
**/
|
109
|
+
SolidusPaypalBraintree.Client.prototype.getApplepayInstance = function() {
|
110
|
+
return this._applepayInstance;
|
111
|
+
};
|
112
|
+
|
113
|
+
/**
|
114
|
+
* Returns the braintree dataCollector instance
|
115
|
+
* @returns {external:"braintree.DataCollector"} The braintree dataCollector that was initialized by this class
|
116
|
+
**/
|
117
|
+
SolidusPaypalBraintree.Client.prototype.getDataCollectorInstance = function() {
|
118
|
+
return this._dataCollectorInstance;
|
119
|
+
};
|
120
|
+
|
121
|
+
|
122
|
+
SolidusPaypalBraintree.Client.prototype._fetchToken = function() {
|
123
|
+
var payload = {
|
124
|
+
dataType: 'json',
|
125
|
+
type: 'POST',
|
126
|
+
url: SolidusPaypalBraintree.config.paths.clientTokens,
|
127
|
+
error: function(xhr) {
|
128
|
+
console.error("Error fetching braintree token");
|
129
|
+
}
|
130
|
+
};
|
131
|
+
|
132
|
+
if (this.paymentMethodId) {
|
133
|
+
payload.data = {
|
134
|
+
payment_method_id: this.paymentMethodId
|
135
|
+
};
|
136
|
+
}
|
137
|
+
|
138
|
+
return Spree.ajax(payload);
|
139
|
+
};
|
140
|
+
|
141
|
+
SolidusPaypalBraintree.Client.prototype._createBraintreeInstance = function(tokenResponse) {
|
142
|
+
this.paymentMethodId = tokenResponse.payment_method_id;
|
143
|
+
|
144
|
+
return SolidusPaypalBraintree.PromiseShim.convertBraintreePromise(braintree.client.create, [{
|
145
|
+
authorization: tokenResponse.client_token
|
146
|
+
}]).then(function (clientInstance) {
|
147
|
+
this._braintreeInstance = clientInstance;
|
148
|
+
return clientInstance;
|
149
|
+
}.bind(this));
|
150
|
+
};
|
151
|
+
|
152
|
+
SolidusPaypalBraintree.Client.prototype._invokeReadyCallback = function() {
|
153
|
+
if(this.readyCallback) {
|
154
|
+
this.readyCallback(this._braintreeInstance);
|
155
|
+
}
|
156
|
+
|
157
|
+
return this;
|
158
|
+
};
|
159
|
+
|
160
|
+
SolidusPaypalBraintree.Client.prototype._createDataCollector = function() {
|
161
|
+
return SolidusPaypalBraintree.PromiseShim.convertBraintreePromise(braintree.dataCollector.create, [{
|
162
|
+
client: this._braintreeInstance,
|
163
|
+
paypal: !!this.usePaypal
|
164
|
+
}]).then(function (dataCollectorInstance) {
|
165
|
+
this._dataCollectorInstance = dataCollectorInstance;
|
166
|
+
return dataCollectorInstance;
|
167
|
+
}.bind(this));
|
168
|
+
};
|
169
|
+
|
170
|
+
SolidusPaypalBraintree.Client.prototype._createPaypal = function() {
|
171
|
+
return SolidusPaypalBraintree.PromiseShim.convertBraintreePromise(braintree.paypal.create, [{
|
172
|
+
client: this._braintreeInstance
|
173
|
+
}]).then(function (paypalInstance) {
|
174
|
+
this._paypalInstance = paypalInstance;
|
175
|
+
return paypalInstance;
|
176
|
+
}.bind(this));
|
177
|
+
};
|
178
|
+
|
179
|
+
SolidusPaypalBraintree.Client.prototype._createApplepay = function() {
|
180
|
+
return SolidusPaypalBraintree.PromiseShim.convertBraintreePromise(braintree.applePay.create, [{
|
181
|
+
client: this._braintreeInstance
|
182
|
+
}]).then(function (applePayInstance) {
|
183
|
+
this._applepayInstance = applePayInstance;
|
184
|
+
return applePayInstance;
|
185
|
+
}.bind(this));
|
186
|
+
};
|