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