I'm trying to create multiple tokens clientside to send to the server to make multiple Destination Charges on Stripe Connect.
I can't use the Customer object because for Destination Charges, the Customer has to be a connected account on the platform, so guest checkouts won't work.
So i need to call Stripe.createToken 'x' times, and save the tokens in 'x' number of hidden inputs using the stripeResponseHandler function, which are then submitted to serverside for processing the charges.
This is how I managed to do it for 2 tokens very very manually:
Stripe.createToken( {
number: card,
cvc: cvc,
exp_month: month,
exp_year: year,
name: name,
address_line1: address_line1,
address_line2: address_line2,
address_state: address_state,
address_city: address_city,
address_zip: address_zip,
address_country: address_country
}, stripeResponseHandler1 );
Stripe.createToken( {
number: card,
cvc: cvc,
exp_month: month,
exp_year: year,
name: name,
address_line1: address_line1,
address_line2: address_line2,
address_state: address_state,
address_city: address_city,
address_zip: address_zip,
address_country: address_country
}, stripeResponseHandler2 );
function stripeResponseHandler1( status, response ) {
var $form = jQuery("form.checkout, form#order_review");
if ( response.error ) {
jQuery('.woocommerce_error, .woocommerce-error, .woocommerce-message, .woocommerce_message, .stripe_token').remove();
jQuery('#dokan-stripe-connect-card-number').closest('p').before( '<ul class="woocommerce_error woocommerce-error"><li>' + response.error.message + '</li></ul>' );
$form.unblock();
} else {
var token = response['id'];
wc_stripe_connect_params.token_done = true;
jQuery( '.stripe_token').remove();
$form.append("<input type='hidden' class='stripe_token' name='stripe_token1' value='" + token + "'/>");
}
}
function stripeResponseHandler2( status, response ) {
var $form = jQuery("form.checkout, form#order_review");
if ( response.error ) {
jQuery('.woocommerce_error, .woocommerce-error, .woocommerce-message, .woocommerce_message, .stripe_token').remove();
jQuery('#dokan-stripe-connect-card-number').closest('p').before( '<ul class="woocommerce_error woocommerce-error"><li>' + response.error.message + '</li></ul>' );
$form.unblock();
} else {
var token = response['id'];
wc_stripe_connect_params.token_done = true;
$form.append("<input type='hidden' class='stripe_token2' name='stripe_token2' value='" + token + "'/>");
$form.submit();
}
}
Obviously, this is not how anything should be coded, so I'm thinking I need it do something along the lines of:
var vendor_count = jQuery(".vendor_count").first().data("count");
for(i = 1; i <= vendor_count; i++ ) {
Stripe.createToken( {
number: card,
cvc: cvc,
exp_month: month,
exp_year: year,
name: name,
address_line1: address_line1,
address_line2: address_line2,
address_state: address_state,
address_city: address_city,
address_zip: address_zip,
address_country: address_country
}, stripeResponseHandler );
}
But I can't figure out how to pass the vendor_count index to the stripeResponseHandler to create the extra hidden input fields for sending.
Am I totally going about this the wrong way? I feel like based on my reasons at the beginning of this post, I have reason to require multiple token creation in this way.