I am working on an implementation of REST API for a mobile Application into which I need to implement feature to make payment to customers directly into their bank accounts. I have integrated Stripe payment gateway to accomplish payment process which offers feature to initiate a payout to either a bank account or debit card of a connected Stripe account.
<?php
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("sk_test_tD8Pxvmv2sOhucPuhqvPDvSP");
$error = '';
$success = '';
if ($_POST) {
try {
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
$payment_result=\Stripe\Charge::create(array("amount" => 2000,
"currency" => "usd",
"card" => $_POST['stripeToken']));
$success = 'Your payment was successful.';
}
catch (Exception $e) {
$error = $e->getMessage();
}
}
try{
$Account=\Stripe\Account::create(array(
"type" => "custom",
"country" => "US",
"email" => "demo3455675666gfg5@info.com"
));
if($Account!=NULL){
$TokenInfo=\Stripe\Token::create(array(
"bank_account" => array(
"country" => "US",
"currency" => "usd",
"account_holder_name" => "Mason Wilson",
"account_holder_type" => "individual",
"routing_number" => "110000000",
"account_number" => "000123456789"
)
));
$external_account_info=$Account->external_accounts->create(array("external_account" => $TokenInfo->id));
$payout_response=\Stripe\Payout::create(array(
"amount" => 400,
"currency" => "usd",
"destination"=>$external_account_info->id
));
}
}
catch(Exception $e)
{
echo $e->getMessage();exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Stripe Getting Started Form</title>
<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<!-- jQuery is used only for this example; it isn't required to use Stripe -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
// this identifies your website in the createToken call below
Stripe.setPublishableKey('pk_test_W2y6W7HcwlV02a8MuCQUHnDn');
function stripeResponseHandler(status, response) {
if (response.error) {
// re-enable the submit button
$('.submit-button').removeAttr("disabled");
// show the errors on the form
$(".payment-errors").html(response.error.message);
} else {
var form$ = $("#payment-form");
// token contains id, last4, and card type
var token = response['id'];
// insert the token into the form so it gets submitted to the server
form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
// and submit
form$.get(0).submit();
}
}
$(document).ready(function() {
$("#payment-form").submit(function(event) {
// disable the submit button to prevent repeated clicks
$('.submit-button').attr("disabled", "disabled");
// createToken returns immediately - the supplied callback submits the form if there are no errors
Stripe.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
return false; // submit from callback
});
});
</script>
</head>
<body>
<h1>Charge $10 with Stripe</h1>
<!-- to display errors returned by createToken -->
<span class="payment-errors"><?= $error ?></span>
<span class="payment-success"><?= $success ?></span>
<form action="" method="POST" id="payment-form">
<div class="form-row">
<label>Card Number</label>
<input type="text" size="20" autocomplete="off" class="card-number" />
</div>
<div class="form-row">
<label>CVC</label>
<input type="text" size="4" autocomplete="off" class="card-cvc" />
</div>
<div class="form-row">
<label>Expiration (MM/YYYY)</label>
<input type="text" size="2" class="card-expiry-month"/>
<span> / </span>
<input type="text" size="4" class="card-expiry-year"/>
</div>
<button type="submit" class="submit-button">Submit Payment</button>
</form>
</body>
</html>
Upon execution of above code snippet It generates an error which states that
No such external account: ba_1CHYY2DVYgPG6LmYtQdcu53B
Please suggest me an appropriate solution to pay to bank account using Stripe payment gateway.
Your call to create a Payout is failing because you are executing it on your Stripe account, rather than the Connected Stripe Account.
You need to add a Stripe-Account header on your Payout call because the bank account is attached to the Connected Account rather than your own Stripe account.
$payout_response=\Stripe\Payout::create(array(
"amount" => 400,
"currency" => "usd",
"destination"=>$external_account_info->id
),
array("stripe_account"=>$Account->id)
);
See here for more about this https://stripe.com/docs/connect/authentication#stripe-account-header
Taking a step back, the funds to payout to a bank account come from the Connect Account's balance. To get funds into the balance of a Connected Account you need to use one of the methods described here, most likely you'll use either a Destination Charge or Separate Charges and Transfers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.