如何在PHP中使用PLAID实现Stripe ACH

时间:2019-09-25 10:19:50

标签: php stripe-payments plaid

第1步:首先使用Stripe与PLAID连接并使用公钥接收public_token和account_id

<script src="https://cdn.plaid.com/link/v2/stable/link-initialize.js"></script>

 <script>
 var linkHandler = Plaid.create({
    clientName: 'Scholastic Expeditions',
    countryCodes: ['US'],
    env: 'sandbox',
    // Replace with your public_key from the Dashboard
    key: '071e075d1d6a109d6g2adb12c426c08',
    product: ['auth'],
    // Optional, use webhooks to get transaction and error updates
    webhook: 'http://test.code.org',
    // Optional, specify a language to localize Link
    language: 'en',
    onLoad: function() {
      // Optional, called when Link loads
    },
    onSuccess: function(public_token, metadata) {
        //account_id = metadata.account_id
        account_id = metadata.accounts[0].id
        sendBackendServer(public_token,account_id);
    },
    onExit: function(err, metadata) {
        // The user exited the Link flow.
        if (err != null) {
            // The user encountered a Plaid API error prior to exiting.
        }
    }
});
document.getElementById('linkButton').onclick = function() {
   linkHandler.open();
};
</script>

步骤2:在服务器端,使用curl接收access_token和stripe_token

$plaid_client_id = '5d82ff239085b9001306139c';
$public_token = $_REQUEST['public_token'];
$plaid_secret = '565228fd7511c2d1ea2d1bc46eab15';
$env = 'sandbox';
$account_id = $_REQUEST['account_id'];


$headers[] = 'Content-Type: application/json';

$params = array(
    "client_id" => $plaid_client_id,
    "secret" => $plaid_secret,
    "public_token" => $public_token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://" . $env . ".plaid.com/item/public_token/exchange");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 80);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

if(!$result = curl_exec($ch)) {
    trigger_error(curl_error($ch));
}
curl_close($ch);

$jsonParsed = json_decode($result);

$btok_params = array(
    'client_id'    => $plaid_client_id,
    'secret'       => $plaid_secret,
    'access_token' => $jsonParsed->access_token,
    'account_id'   => $account_id
 );

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://" . $env . ".plaid.com/processor/stripe/bank_account_token/create");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($btok_params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 80);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

if(!$result = curl_exec($ch)) {
    trigger_error(curl_error($ch));
}
curl_close($ch);

$btoken = json_decode($result);

$stripe_bank_account_token = $btoken->stripe_bank_account_token;

第3步:使用stripe_bank_account_token收费金额

$charge = Stripe_Charge::create(array(       
    "amount" => $totalPayAmount,
    "currency" => 'USD',
    "source" => $tokenID,
    "description" => 'test_account')              
);

我的问题是: 1.生产中可能需要银行验证,并且要花一些时间,所以我们最终如何处理。 2.如何自动从用户帐户自动充值

0 个答案:

没有答案