Skip to content

Commit

Permalink
fetch stripe token if not present in DOM; use already loaded token if…
Browse files Browse the repository at this point in the history
… present in DOM
  • Loading branch information
mcfarlan committed Feb 6, 2018
1 parent 1c6596d commit d2e1432
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions class-rcp-payment-gateway-stripe-elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,26 @@ public function fields() {
function stripeTokenHandler( token ) {
var form, hiddenInput;

// Insert the token ID into the form so it gets submitted to the server
form = document.getElementById( form_id );
hiddenInput = document.createElement( 'input' );

// Assign attributes to hidden field
hiddenInput.setAttribute( 'type', 'hidden' );
hiddenInput.setAttribute( 'name', 'stripeToken' );
hiddenInput.setAttribute( 'value', token.id );
hiddenInput.setAttribute( 'id', 'stripeToken' );

form.appendChild( hiddenInput );
form.submit();
// Use token if already present, work around for better Chrome support during redirects
if ( document.getElementById( 'stripeToken' ) ) {
hiddenInput = document.getElementById( 'stripeToken' );

// If token isn't present in DOM, create new one and submit form
} else {
hiddenInput = document.createElement( 'input' );

// Append the token form field into the form with applicable attributes
form = document.getElementById( form_id );

// Assign attributes to hidden field
hiddenInput.setAttribute( 'type', 'hidden' );
hiddenInput.setAttribute( 'name', 'stripeToken' );
hiddenInput.setAttribute( 'value', token.id );
hiddenInput.setAttribute( 'id', 'stripeToken' );

form.appendChild( hiddenInput );
form.submit();
}
}

// Attempts the creation of a Stripe Token
Expand Down

0 comments on commit d2e1432

Please sign in to comment.