|
1
|
|
|
jQuery( function( $ ) { |
|
2
|
|
|
'use strict'; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Object to handle Stripe payment forms. |
|
6
|
|
|
*/ |
|
7
|
|
|
var wc_stripe_form = { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Initialize e handlers and UI state. |
|
11
|
|
|
*/ |
|
12
|
|
|
init: function( form ) { |
|
13
|
|
|
this.form = form; |
|
14
|
|
|
this.stripe_submit = false; |
|
15
|
|
|
|
|
16
|
|
|
$( this.form ) |
|
17
|
|
|
// We need to bind directly to the click (and not checkout_place_order_stripe) to avoid popup blockers |
|
18
|
|
|
// especially on mobile devices (like on Chrome for iOS) from blocking StripeCheckout.open from opening a tab |
|
19
|
|
|
.on( 'click', '#place_order', this.onSubmit ) |
|
20
|
|
|
|
|
21
|
|
|
// WooCommerce lets us return a false on checkout_place_order_{gateway} to keep the form from submitting |
|
22
|
|
|
.on( 'submit checkout_place_order_stripe' ); |
|
23
|
|
|
|
|
24
|
|
|
$( document.body ).on( 'checkout_error', this.resetModal ); |
|
25
|
|
|
}, |
|
26
|
|
|
|
|
27
|
|
|
isStripeChosen: function() { |
|
28
|
|
|
return $( '#payment_method_stripe' ).is( ':checked' ) && ( ! $( 'input[name="wc-stripe-payment-token"]:checked' ).length || 'new' === $( 'input[name="wc-stripe-payment-token"]:checked' ).val() ); |
|
29
|
|
|
}, |
|
30
|
|
|
|
|
31
|
|
|
isStripeModalNeeded: function( e ) { |
|
|
|
|
|
|
32
|
|
|
var token = wc_stripe_form.form.find( 'input.stripe_token' ), |
|
33
|
|
|
$required_inputs; |
|
34
|
|
|
|
|
35
|
|
|
// If this is a stripe submission (after modal) and token exists, allow submit. |
|
36
|
|
|
if ( wc_stripe_form.stripe_submit && token ) { |
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// Don't affect submission if modal is not needed. |
|
41
|
|
|
if ( ! wc_stripe_form.isStripeChosen() ) { |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// Don't open modal if required fields are not complete |
|
46
|
|
|
if ( $( 'input#terms' ).length === 1 && $( 'input#terms:checked' ).length === 0 ) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ( $( '#createaccount' ).is( ':checked' ) && $( '#account_password' ).length && $( '#account_password' ).val() === '' ) { |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// check to see if we need to validate shipping address |
|
55
|
|
|
if ( $( '#ship-to-different-address-checkbox' ).is( ':checked' ) ) { |
|
56
|
|
|
$required_inputs = $( '.woocommerce-billing-fields .validate-required, .woocommerce-shipping-fields .validate-required' ); |
|
57
|
|
|
} else { |
|
58
|
|
|
$required_inputs = $( '.woocommerce-billing-fields .validate-required' ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ( $required_inputs.length ) { |
|
62
|
|
|
var required_error = false; |
|
63
|
|
|
|
|
64
|
|
|
$required_inputs.each( function() { |
|
65
|
|
|
if ( $( this ).find( 'input.input-text, select' ).not( $( '#account_password, #account_username' ) ).val() === '' ) { |
|
66
|
|
|
required_error = true; |
|
67
|
|
|
} |
|
68
|
|
|
}); |
|
69
|
|
|
|
|
70
|
|
|
if ( required_error ) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return true; |
|
76
|
|
|
}, |
|
77
|
|
|
|
|
78
|
|
|
block: function() { |
|
79
|
|
|
wc_stripe_form.form.block({ |
|
80
|
|
|
message: null, |
|
81
|
|
|
overlayCSS: { |
|
82
|
|
|
background: '#fff', |
|
83
|
|
|
opacity: 0.6 |
|
84
|
|
|
} |
|
85
|
|
|
}); |
|
86
|
|
|
}, |
|
87
|
|
|
|
|
88
|
|
|
unblock: function() { |
|
89
|
|
|
wc_stripe_form.form.unblock(); |
|
90
|
|
|
}, |
|
91
|
|
|
|
|
92
|
|
|
onClose: function() { |
|
93
|
|
|
wc_stripe_form.unblock(); |
|
94
|
|
|
}, |
|
95
|
|
|
|
|
96
|
|
|
onSubmit: function( e ) { |
|
97
|
|
|
if ( wc_stripe_form.isStripeModalNeeded() ) { |
|
98
|
|
|
e.preventDefault(); |
|
99
|
|
|
|
|
100
|
|
|
// Capture submittal and open stripecheckout |
|
101
|
|
|
var $form = wc_stripe_form.form, |
|
102
|
|
|
$data = $( '#stripe-payment-data' ), |
|
103
|
|
|
token = $form.find( 'input.stripe_token' ); |
|
104
|
|
|
|
|
105
|
|
|
token.val( '' ); |
|
106
|
|
|
|
|
107
|
|
|
var token_action = function( res ) { |
|
108
|
|
|
$form.find( 'input.stripe_token' ).remove(); |
|
109
|
|
|
$form.append( '<input type="hidden" class="stripe_token" name="stripe_token" value="' + res.id + '"/>' ); |
|
110
|
|
|
wc_stripe_form.stripe_submit = true; |
|
111
|
|
|
$form.submit(); |
|
112
|
|
|
}; |
|
113
|
|
|
|
|
114
|
|
|
StripeCheckout.open({ |
|
|
|
|
|
|
115
|
|
|
key : wc_stripe_params.key, |
|
|
|
|
|
|
116
|
|
|
billingAddress : 'yes' === wc_stripe_params.stripe_checkout_require_billing_address, |
|
117
|
|
|
amount : $data.data( 'amount' ), |
|
118
|
|
|
name : $data.data( 'name' ), |
|
119
|
|
|
description : $data.data( 'description' ), |
|
120
|
|
|
currency : $data.data( 'currency' ), |
|
121
|
|
|
image : $data.data( 'image' ), |
|
122
|
|
|
bitcoin : $data.data( 'bitcoin' ), |
|
123
|
|
|
locale : $data.data( 'locale' ), |
|
124
|
|
|
email : $( '#billing_email' ).val() || $data.data( 'email' ), |
|
125
|
|
|
panelLabel : $data.data( 'panel-label' ), |
|
126
|
|
|
allowRememberMe : $data.data( 'allow-remember-me' ), |
|
127
|
|
|
token : token_action, |
|
128
|
|
|
closed : wc_stripe_form.onClose() |
|
129
|
|
|
}); |
|
130
|
|
|
|
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return true; |
|
135
|
|
|
}, |
|
136
|
|
|
|
|
137
|
|
|
resetModal: function() { |
|
138
|
|
|
wc_stripe_form.form.find( 'input.stripe_token' ).remove(); |
|
139
|
|
|
wc_stripe_form.stripe_submit = false; |
|
140
|
|
|
} |
|
141
|
|
|
}; |
|
142
|
|
|
|
|
143
|
|
|
wc_stripe_form.init( $( "form.checkout, form#order_review, form#add_payment_method" ) ); |
|
144
|
|
|
} ); |
|
145
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.