stripe_checkout.js
4.24 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
jQuery( function( $ ) {
'use strict';
/**
* Object to handle Stripe payment forms.
*/
var wc_stripe_form = {
/**
* Initialize e handlers and UI state.
*/
init: function( form ) {
this.form = form;
this.stripe_submit = false;
$( this.form )
// We need to bind directly to the click (and not checkout_place_order_stripe) to avoid popup blockers
// especially on mobile devices (like on Chrome for iOS) from blocking StripeCheckout.open from opening a tab
.on( 'click', '#place_order', this.onSubmit )
// WooCommerce lets us return a false on checkout_place_order_{gateway} to keep the form from submitting
.on( 'submit checkout_place_order_stripe' );
$( document.body ).on( 'checkout_error', this.resetModal );
},
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() );
},
isStripeModalNeeded: function( e ) {
var token = wc_stripe_form.form.find( 'input.stripe_token' ),
$required_inputs;
// If this is a stripe submission (after modal) and token exists, allow submit.
if ( wc_stripe_form.stripe_submit && token ) {
return false;
}
// Don't affect submission if modal is not needed.
if ( ! wc_stripe_form.isStripeChosen() ) {
return false;
}
// Don't open modal if required fields are not complete
if ( $( 'input#terms' ).length === 1 && $( 'input#terms:checked' ).length === 0 ) {
return false;
}
if ( $( '#createaccount' ).is( ':checked' ) && $( '#account_password' ).length && $( '#account_password' ).val() === '' ) {
return false;
}
// check to see if we need to validate shipping address
if ( $( '#ship-to-different-address-checkbox' ).is( ':checked' ) ) {
$required_inputs = $( '.woocommerce-billing-fields .validate-required, .woocommerce-shipping-fields .validate-required' );
} else {
$required_inputs = $( '.woocommerce-billing-fields .validate-required' );
}
if ( $required_inputs.length ) {
var required_error = false;
$required_inputs.each( function() {
if ( $( this ).find( 'input.input-text, select' ).not( $( '#account_password, #account_username' ) ).val() === '' ) {
required_error = true;
}
});
if ( required_error ) {
return false;
}
}
return true;
},
block: function() {
wc_stripe_form.form.block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
},
unblock: function() {
wc_stripe_form.form.unblock();
},
onClose: function() {
wc_stripe_form.unblock();
},
onSubmit: function( e ) {
if ( wc_stripe_form.isStripeModalNeeded() ) {
e.preventDefault();
// Capture submittal and open stripecheckout
var $form = wc_stripe_form.form,
$data = $( '#stripe-payment-data' ),
token = $form.find( 'input.stripe_token' );
token.val( '' );
var token_action = function( res ) {
$form.find( 'input.stripe_token' ).remove();
$form.append( '<input type="hidden" class="stripe_token" name="stripe_token" value="' + res.id + '"/>' );
wc_stripe_form.stripe_submit = true;
$form.submit();
};
StripeCheckout.open({
key : wc_stripe_params.key,
billingAddress : 'yes' === wc_stripe_params.stripe_checkout_require_billing_address ? true : false,
amount : $data.data( 'amount' ),
name : $data.data( 'name' ),
description : $data.data( 'description' ),
currency : $data.data( 'currency' ),
image : $data.data( 'image' ),
bitcoin : $data.data( 'bitcoin' ),
locale : $data.data( 'locale' ),
email : $( '#billing_email' ).val() || $data.data( 'email' ),
panelLabel : $data.data( 'panel-label' ),
token : token_action,
closed : wc_stripe_form.onClose()
});
return false;
}
return true;
},
resetModal: function() {
wc_stripe_form.form.find( 'input.stripe_token' ).remove();
wc_stripe_form.stripe_submit = false;
}
};
wc_stripe_form.init( $( "form.checkout, form#order_review, form#add_payment_method" ) );
} );