如何使用条带创建用户银行帐户?

时间:2016-10-22 15:06:02

标签: javascript php stripe-payments

使用以下php代码,我可以轻松地将我的用户的银行帐户添加到我的条带帐户中。

<?php
try {
require_once('vendor/autoload.php');
 Stripe\Stripe::setApiKey("sk_test_code"); 

 $account = \Stripe\Account::create(
    array(
        "country" => "US", 
        "managed" => true,
        "email" => "email@gmail.com",
        "legal_entity" => array(
            'address' => array(
                'city' => 'Carlifonia', // 
                'country' => 'US',
                "line1" => 'Address line 1',  
                "line2" => 'Address line 2', 
                "postal_code" => '90046', 
                "state" => 'Denver' // 
            ),
            'business_name' => '',
            'business_tax_id' => '',
            'dob' => array(
                'day' => '01', 
                'month' => '01', 
                'year' => '1990', 
            ),
            'first_name' => 'Test Lynn First Name',
            'last_name' => 'Test Lynn Last Name',
            //'personal_id_number' => '000000000',
            'ssn_last_4' => '4444', 
            'type' => 'individual' 
        ),
        'tos_acceptance' => array(
            'date' => time(),
            'ip' => $_SERVER['REMOTE_ADDR']
        ),
        'transfer_schedule' => array(
            "interval" => 'weekly', 
            "weekly_anchor" => 'sunday'
        ),
        'external_account' => array(
            "object" => "bank_account",
            "country" => "US",
            "account_holder_type" => 'individual', r
            "routing_number" => "111000025", 
            "account_number" => "000123456789" 
        )
    )
);

    //send the file, this line will be reached if no error was thrown above
    echo "<h1>Account ID = ".$account['id']."</h1>";
    echo "<h1>Secret Key = ".$account['keys']['secret']."</h1>";
    echo "<h1>Publish Key = ".$account['keys']['publishable']."</h1>";

}
//catch the errors in any way you like

catch(Stripe_CardError $e) {
    print_r($e);
}

catch (Stripe_InvalidRequestError $e) {
// Invalid parameters were supplied to Stripe's API
    print_r($e);

} catch (Stripe_AuthenticationError $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
    print_r($e);

} catch (Stripe_ApiConnectionError $e) {
// Network communication with Stripe failed
    print_r($e);
} catch (Stripe_Error $e) {
    print_r($e);
// Display a very generic error to the user, and maybe send
// yourself an email
} catch (Exception $e) {
    echo '<pre>';
    $e_json = $e->getJsonBody();
    echo $error = $e_json['error']['message'];
    echo '</pre>';
// Something else happened, completely unrelated to Stripe
}
?>

html表单如下所示:

<form role="form" id="bank-payment-form" method="POST" action="<?php echo esc_url(SITE_URL.'test-bank-create') ?>">
    <div class="row">
        <div class="col-md-12"><h3>Identity Verification<hr/></h3></div>
        <div class="bank-errors"></div>
        <div class="form-group col-xs-12 col-sm-4 col-md-4">
            <label class="control-label" for="date-time">Date Of Birth</label>
            <select class="form-control day">
                <option value="">--Select Day--</option>
                <?php
                for ($d = 1; $d <=31; $d++) {
                    echo "<option value='$d'>$d</option>";
                }
                ?>
            </select>
        </div>
        <div class="form-group col-xs-12 col-sm-4 col-md-4">
            <label class="control-label" for="date-time">&nbsp;</label>
            <select class="form-control month">
                <option value="">--Select Month--</option>
                <?php
                for ($m = 1; $m <=12; $m++) {
                    echo "<option value='$m'>$m</option>";
                }
                ?>
            </select>
        </div>
        <div class="form-group col-xs-12 col-sm-4 col-md-4">
            <label class="control-label" for="date-time">&nbsp;</label>
            <select class="form-control year">>
                <option value="">--Select Year--</option>
                <?php
                for ($i =2010; $i > 1900  ; $i--) {
                    echo "<option value='$i'>$i</option>";
                }
                ?>
            </select>
        </div>
        <div class="form-group col-xs-12 col-sm-12 col-md-12">
            <label class="control-label" for="date-time">Address</label>
            <input type="text" placeholder="Address 1" class="form-control address1" value="">
        </div>
        <div class="form-group col-xs-12 col-sm-12 col-md-12">
            <input type="text" placeholder="Address 2 (optional)" class="form-control address2" value="">
        </div>
        <div class="form-group col-xs-12 col-sm-6 col-md-6">
            <label class="control-label" for="date-time">Country</label>
            <input type="text" value="US" disabled class="form-control country">
            <input type="hidden" value="USD" data-stripe="country" disabled class="form-control" data-stripe="country">
        </div>
        <div class="form-group col-xs-12 col-sm-6 col-md-6">
            <label class="control-label" for="date-time">State</label>  
            <select class="form-control state" id="area" required>
            <?php
            $get_location = mysqli_query($conn, "SELECT * FROM product_area");
            if(mysqli_num_rows($get_location) == 0 ) {
                $choose = 'No state found';
            } else {
                $choose = 'Choose State';
            }
            ?>
            <option value=""><?php echo $choose; ?></option>
            <?php                                         
            while($get_location_result = mysqli_fetch_array($get_location) ) {
                $location_id = (int) $get_location_result['parea_id'];
                $location_name = htmlspecialchars($get_location_result['parea_name']);
                echo "<option value='$location_id'>$location_name</option>";
            }
            ?>                    
            </select>
        </div>
        <div class="form-group col-xs-12 col-sm-6 col-md-6">
            <label class="control-label" for="date-time">City</label>  
            <select id="sub_area" class="form-control city">
                    <option value="">--Select City--</option>
            </select>
        </div>
        <div class="form-group col-xs-12 col-sm-6 col-md-6">
            <label class="control-label" for="date-time">Zip Code</label>
            <input type="text" value="<?php echo $zip_db; ?>" placeholder="Zip Code" class="form-control zip">
        </div>

        <div class="form-group col-xs-12 col-sm-12 col-md-12">
            <label class="control-label" for="date-time">SSN Number</label>
            <input type="text" placeholder="SSN Number" class="form-control ssn" value="">
            <small>Your SSN is used to verify your identity.</small>
        </div>
        <div class="col-md-12"><h4><b>Funds Recipient</b></h4></div>
        <div class="form-group col-xs-12 col-sm-12 col-md-12">
            <select class="form-control account_holder_type">
                <option value="">--Select Account Holder Type--</option>
                <option value="individual">Individual</option>
                <option value="company">Company</option>
            </select>
        </div>
        <div class="col-md-12"><h4><b>Bank Account</b></h4></div>
        <div class="form-group col-xs-12 col-sm-6 col-md-6">
            <label class="control-label" for="date-time">Routing Number</label>
            <input type="text" class="form-control routing_number" value="" placeholder="123456789">
            <small>Your routing number will be 9 digits</small>
        </div>                    
        <div class="form-group col-xs-12 col-sm-6 col-md-6">
            <label class="control-label" for="date-time">Account Number</label>
            <input type="text" class="form-control account_number" value="" placeholder="">
            <small>Your routing number will be 9 digits</small>
        </div>                    
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input type="submit" value="Verify Identity" class="submit btn btn-booking">
        </div>                    
        <?php } ?>    
    </div>                     
</div>    
</div>
</form> 

我看到了stripe.js。他们使用以下js代码添加银行帐户:

Stripe.bankAccount.createToken({
  country: $('.country').val(),
  currency: $('.currency').val(),
  routing_number: $('.routing-number').val(),
  account_number: $('.account-number').val(),
  account_holder_name: $('.name').val(),
  account_holder_type: $('.account_holder_type').val()
}, stripeResponseHandler);

但是你看到了,在我的html表单中,我有额外的字段,例如出生日期。

我的问题是如何在我的所有html表单字段中使用此stripe.js添加用户的银行帐户?

1 个答案:

答案 0 :(得分:2)

托管帐户的legal entity属性所需的字段不是您可以直接通过Stripe.js传递的字段。

可以在此处执行的操作是创建银行帐户令牌,将其附加到包含其他信息的表单,然后将该表单提交给后端进行处理。

以下是它的工作原理:设置处理程序以收听表单提交或点击提交按钮,使用此处理程序触发Stripe.bankAccount.createToken来电。

在您的createToken调用的stripeResponseHandler回调中,将名为stripeToken的隐藏输入附加到目标表单,然后提交表单。

表单将通过银行帐户令牌以及您在<form></form>代码和已命名之间创建的任何其他表单字段传递到您的后端,然后您可以使用php检索这些字段(例如{ {1}}表示您已添加的银行帐户令牌,或$_POST['stripeToken']如果您的字段具有属性$_POST['dob-year'])。

您可以在此处查看基本构思: https://jsfiddle.net/pay62y2f/