|
1
|
|
|
/* global wc_stripe_params */ |
|
2
|
|
|
Stripe.setPublishableKey( wc_stripe_params.key ); |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
jQuery( function( $ ) { |
|
5
|
|
|
'use strict'; |
|
6
|
|
|
|
|
7
|
|
|
/* Open and close for legacy class */ |
|
8
|
|
|
$( 'form.checkout, form#order_review' ).on( 'change', 'input[name="wc-stripe-payment-token"]', function() { |
|
9
|
|
|
if ( 'new' === $( '.stripe-legacy-payment-fields input[name="wc-stripe-payment-token"]:checked' ).val() ) { |
|
10
|
|
|
$( '.stripe-legacy-payment-fields #stripe-payment-data' ).slideDown( 200 ); |
|
11
|
|
|
} else { |
|
12
|
|
|
$( '.stripe-legacy-payment-fields #stripe-payment-data' ).slideUp( 200 ); |
|
13
|
|
|
} |
|
14
|
|
|
} ); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Object to handle Stripe payment forms. |
|
18
|
|
|
*/ |
|
19
|
|
|
var wc_stripe_form = { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Initialize event handlers and UI state. |
|
23
|
|
|
*/ |
|
24
|
|
|
init: function() { |
|
25
|
|
|
// checkout page |
|
26
|
|
|
if ( $( 'form.woocommerce-checkout' ).length ) { |
|
27
|
|
|
this.form = $( 'form.woocommerce-checkout' ); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$( 'form.woocommerce-checkout' ) |
|
31
|
|
|
.on( |
|
32
|
|
|
'checkout_place_order_stripe', |
|
33
|
|
|
this.onSubmit |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
// pay order page |
|
37
|
|
|
if ( $( 'form#order_review' ).length ) { |
|
38
|
|
|
this.form = $( 'form#order_review' ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$( 'form#order_review' ) |
|
42
|
|
|
.on( |
|
43
|
|
|
'submit', |
|
44
|
|
|
this.onSubmit |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
// add payment method page |
|
48
|
|
|
if ( $( 'form#add_payment_method' ).length ) { |
|
49
|
|
|
this.form = $( 'form#add_payment_method' ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$( 'form#add_payment_method' ) |
|
53
|
|
|
.on( |
|
54
|
|
|
'submit', |
|
55
|
|
|
this.onSubmit |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$( document ) |
|
59
|
|
|
.on( |
|
60
|
|
|
'change', |
|
61
|
|
|
'#wc-stripe-cc-form :input', |
|
62
|
|
|
this.onCCFormChange |
|
63
|
|
|
) |
|
64
|
|
|
.on( |
|
65
|
|
|
'stripeError', |
|
66
|
|
|
this.onError |
|
67
|
|
|
) |
|
68
|
|
|
.on( |
|
69
|
|
|
'checkout_error', |
|
70
|
|
|
this.clearToken |
|
71
|
|
|
); |
|
72
|
|
|
}, |
|
73
|
|
|
|
|
74
|
|
|
isStripeChosen: function() { |
|
75
|
|
|
return $( '#payment_method_stripe' ).is( ':checked' ) && ( ! $( 'input[name="wc-stripe-payment-token"]:checked' ).length || 'new' === $( 'input[name="wc-stripe-payment-token"]:checked' ).val() ); |
|
76
|
|
|
}, |
|
77
|
|
|
|
|
78
|
|
|
hasToken: function() { |
|
79
|
|
|
return 0 < $( 'input.stripe_token' ).length; |
|
80
|
|
|
}, |
|
81
|
|
|
|
|
82
|
|
|
block: function() { |
|
83
|
|
|
wc_stripe_form.form.block({ |
|
84
|
|
|
message: null, |
|
85
|
|
|
overlayCSS: { |
|
86
|
|
|
background: '#fff', |
|
87
|
|
|
opacity: 0.6 |
|
88
|
|
|
} |
|
89
|
|
|
}); |
|
90
|
|
|
}, |
|
91
|
|
|
|
|
92
|
|
|
unblock: function() { |
|
93
|
|
|
wc_stripe_form.form.unblock(); |
|
94
|
|
|
}, |
|
95
|
|
|
|
|
96
|
|
|
onError: function( e, responseObject ) { |
|
97
|
|
|
var message = responseObject.response.error.message; |
|
98
|
|
|
|
|
99
|
|
|
// Customers do not need to know the specifics of the below type of errors |
|
100
|
|
|
// therefore return a generic localizable error message. |
|
101
|
|
|
if ( |
|
102
|
|
|
'invalid_request_error' === responseObject.response.error.type || |
|
103
|
|
|
'api_connection_error' === responseObject.response.error.type || |
|
104
|
|
|
'api_error' === responseObject.response.error.type || |
|
105
|
|
|
'authentication_error' === responseObject.response.error.type || |
|
106
|
|
|
'rate_limit_error' === responseObject.response.error.type |
|
107
|
|
|
) { |
|
108
|
|
|
message = wc_stripe_params.invalid_request_error; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if ( 'card_error' === responseObject.response.error.type && wc_stripe_params.hasOwnProperty( responseObject.response.error.code ) ) { |
|
112
|
|
|
message = wc_stripe_params[ responseObject.response.error.code ]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$( '.wc-stripe-error, .stripe_token' ).remove(); |
|
116
|
|
|
$( '#stripe-card-number' ).closest( 'p' ).before( '<ul class="woocommerce_error woocommerce-error wc-stripe-error"><li>' + message + '</li></ul>' ); |
|
117
|
|
|
wc_stripe_form.unblock(); |
|
118
|
|
|
}, |
|
119
|
|
|
|
|
120
|
|
|
onSubmit: function( e ) { |
|
121
|
|
|
if ( wc_stripe_form.isStripeChosen() && ! wc_stripe_form.hasToken() ) { |
|
|
|
|
|
|
122
|
|
|
e.preventDefault(); |
|
123
|
|
|
wc_stripe_form.block(); |
|
124
|
|
|
|
|
125
|
|
|
var card = $( '#stripe-card-number' ).val(), |
|
126
|
|
|
cvc = $( '#stripe-card-cvc' ).val(), |
|
127
|
|
|
expires = $( '#stripe-card-expiry' ).payment( 'cardExpiryVal' ), |
|
128
|
|
|
first_name = $( '#billing_first_name' ).length ? $( '#billing_first_name' ).val() : wc_stripe_params.billing_first_name, |
|
129
|
|
|
last_name = $( '#billing_last_name' ).length ? $( '#billing_last_name' ).val() : wc_stripe_params.billing_last_name, |
|
130
|
|
|
data = { |
|
131
|
|
|
number : card, |
|
132
|
|
|
cvc : cvc, |
|
133
|
|
|
exp_month: parseInt( expires.month, 10 ) || 0, |
|
134
|
|
|
exp_year : parseInt( expires.year, 10 ) || 0 |
|
135
|
|
|
}; |
|
136
|
|
|
|
|
137
|
|
|
if ( first_name && last_name ) { |
|
138
|
|
|
data.name = first_name + ' ' + last_name; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ( $( '#billing_address_1' ).length > 0 ) { |
|
142
|
|
|
data.address_line1 = $( '#billing_address_1' ).val(); |
|
143
|
|
|
data.address_line2 = $( '#billing_address_2' ).val(); |
|
144
|
|
|
data.address_state = $( '#billing_state' ).val(); |
|
145
|
|
|
data.address_city = $( '#billing_city' ).val(); |
|
146
|
|
|
data.address_zip = $( '#billing_postcode' ).val(); |
|
147
|
|
|
data.address_country = $( '#billing_country' ).val(); |
|
148
|
|
|
} else if ( wc_stripe_params.billing_address_1 ) { |
|
149
|
|
|
data.address_line1 = wc_stripe_params.billing_address_1; |
|
150
|
|
|
data.address_line2 = wc_stripe_params.billing_address_2; |
|
151
|
|
|
data.address_state = wc_stripe_params.billing_state; |
|
152
|
|
|
data.address_city = wc_stripe_params.billing_city; |
|
153
|
|
|
data.address_zip = wc_stripe_params.billing_postcode; |
|
154
|
|
|
data.address_country = wc_stripe_params.billing_country; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
Stripe.createToken( data, wc_stripe_form.onStripeResponse ); |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
// Prevent form submitting |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
}, |
|
163
|
|
|
|
|
164
|
|
|
onCCFormChange: function() { |
|
165
|
|
|
$( '.wc-stripe-error, .stripe_token' ).remove(); |
|
166
|
|
|
}, |
|
167
|
|
|
|
|
168
|
|
|
onStripeResponse: function( status, response ) { |
|
169
|
|
|
if ( response.error ) { |
|
170
|
|
|
$( document ).trigger( 'stripeError', { response: response } ); |
|
|
|
|
|
|
171
|
|
|
} else { |
|
172
|
|
|
// check if we allow prepaid cards |
|
173
|
|
|
if ( 'no' === wc_stripe_params.allow_prepaid_card && 'prepaid' === response.card.funding ) { |
|
174
|
|
|
response.error = { message: wc_stripe_params.no_prepaid_card_msg }; |
|
175
|
|
|
|
|
176
|
|
|
$( document ).trigger( 'stripeError', { response: response } ); |
|
177
|
|
|
|
|
178
|
|
|
return false; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
// token contains id, last4, and card type |
|
182
|
|
|
var token = response.id; |
|
183
|
|
|
|
|
184
|
|
|
// insert the token into the form so it gets submitted to the server |
|
185
|
|
|
wc_stripe_form.form.append( "<input type='hidden' class='stripe_token' name='stripe_token' value='" + token + "'/>" ); |
|
186
|
|
|
wc_stripe_form.form.submit(); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
}, |
|
189
|
|
|
|
|
190
|
|
|
clearToken: function() { |
|
191
|
|
|
$( '.stripe_token' ).remove(); |
|
192
|
|
|
} |
|
193
|
|
|
}; |
|
194
|
|
|
|
|
195
|
|
|
wc_stripe_form.init(); |
|
196
|
|
|
} ); |
|
197
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.