Stripe now offers payouts, which work by creating a bank account token on the front end. From there, the token is send to the server where you create a recipient object with that bank account token. From there you use this recipient object in transfers.
While I am familiar you can create custom forms for payments with stripe.js, when it comes to creating a token for recipients, I can't seem to find any documentation other than this.
Here is my problem, first my form:
<form method="POST" id="inst-form">
<div class="form-row">
<label>
<span>Bank Location</span>
<select data-stripe="country">
<option value="US">United States</option>
</select>
</label>
</div>
<div class="form-row">
<label>
<span>Routing Number</span>
<input type="text" size="9" data-stripe="routingNumber"/>
</label>
</div>
<div class="form-row">
<label>
<span>Account Number</span>
<input type="text" size="17" data-stripe="accountNumber"/>
</label>
</div>
<button type="submit">Make Recipient!</button>
</form>
So far, the three things I need from this form are country, routingNumber, and accountNumber. Lets look at the Javascript so I can use those fields with Stripe.js:
// Create a handler to manage what Stripe returns.
var stripeResponseHandler = function(status, response) {
var $form = $('#inst-form');
if (response.error)
{
alert("Error");
// Not sure how to get these errors.
$form.find('button').prop('disabled', false);
}
else
{
var token = response.id;
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
$form.get(0).submit();
}
};
// Now the handler is done, lets use it when the form is submitted.
// On form submission execute:
jQuery(function($) {
$('#inst-form').submit(function(event) {
// Get the form object.
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);
// Create a token with Stripe
Stripe.bankAccount.createToken({
country: $('.country').val(),
routingNumber: $('.routingNumber').val(),
accountNumber: $('.accountNumber').val(),
}, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
});
My problem is that the alert() is being triggered, which means there are errors. I looked through the API and I am not sure on how to display those errors specifically for creating a recipient. For payments it gives the following example:
$(".payment-errors").text(response.error.message);
Note - I excluded the import of jQuery/Stripe.js - that is surely not the problem. Thanks for the help!
My experience has been that response.error.message is indeed the correct way to proceed.
Here is a sample of my own code for this:
Stripe.bankAccount.createToken({
country: 'US',
routingNumber: bank_routing_number,
accountNumber: bank_account_number,
}, function(status, response) {
if (response.error)
{
console.log(response);
alert(response.error.message);
}
else
{
var token = response.id;
/* send the token to the server along with the withdraw request */
}
});
If you still aren't seeing the correct errors, you should try to console.log(response) and view the javascript console for clues.
Here is an example of an easy error to reproduce:
Routing number must have 9 digits
You should, of course, be validating your account and routing numbers with the routines that Stripe.js provides. Here example usage taken from their documentation:
// This will return true, indicating a potentially valid bank
// routing number.
Stripe.bankAccount.validateRoutingNumber('111000025')
Stripe.bankAccount.validateRoutingNumber('110000000')
// These invalid bank routing numbers will all return false.
Stripe.bankAccount.validateRoutingNumber('990000000')
// (Doesn't pass the checksum check.)
Stripe.bankAccount.validateRoutingNumber('12345')
Stripe.bankAccount.validateRoutingNumber('mistake')
The docs for this are here: https://stripe.com/docs/stripe.js#collecting-bank-account-details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With