简体   繁体   English

如何使用 Stripe 和 PHP 创建用于未来付款的表单?

[英]How can I creat a form for future payments using Stripe and PHP?

Hello I'm trying to create a form that will collect clients info (including payment info) in order to charge them for a service at a later point in time.您好,我正在尝试创建一个表单,该表单将收集客户信息(包括付款信息),以便在以后向他们收取服务费用。 I'm having trouble getting the payment method id and when i check my stripe account at all the test customers it doesn't seem like the payment method is being updated at all.我在获取付款方式 ID 时遇到问题,当我在所有测试客户处检查我的条带帐户时,付款方式似乎根本没有更新。 I'm following the instructions on this page .我正在按照此页面上的说明进行操作。

Here is my code so far:到目前为止,这是我的代码:

<?php
require_once('../vendor/stripe/stripe-php/init.php');
require_once('../vendor/autoload.php');

// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey('SECRET_KEY');

$customer = \Stripe\Customer::create([
    'description' => 'My First Test Customer (created for API docs)',
]);

$intent = \Stripe\SetupIntent::create([
  'customer' => $customer->id
]);

$buildForm = true;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $buildForm = false;
    $stripe = new \Stripe\StripeClient('SECRET_KEY');
    $cardholder_name = $_POST['cardholder_name'];
    $customer_id = $_POST['customer_id'];

    $stripe->customers->update(
        $customer_id,
        ['name' => $cardholder_name,]
      );

    $payment_method = \Stripe\PaymentMethod::all([
        'customer' => $customer_id,
        'type' => 'card',
    ]);

    // THIS PRINTS AN OBJECT BUT THE DATA ARRAY IS EMPTY.
    print "<pre>"; print_r($payment_method); print "</pre>"; exit;
}

?>

<html>

<head>

</head>

<body>
    <!-- placeholder for Elements -->

    <?php if($buildForm): ?>
        <form id="setup-form" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]) ?>" method="post">
            <input id="cardholder-name" type="text" required>
            <div id="card-element"></div>
            <input type="hidden" name="customer_id" value="<?php echo $customer->id ?>">
            <button id="card-button" data-secret="<?php echo $intent->client_secret ?>">
                Save Card
            </button>
        </form>
    <?php endif; ?>

    <script src="https://js.stripe.com/v3/"></script>

    <script type="text/javascript">
        var stripe = Stripe('PRIVATE_KEY');
        var elements = stripe.elements();
        var cardElement = elements.create('card');
        cardElement.mount('#card-element');
    </script>
    <script type="text/javascript">
        var cardholderName = document.getElementById('cardholder-name');
        var cardButton = document.getElementById('card-button');
        var clientSecret = cardButton.dataset.secret;

        cardButton.addEventListener('click', function (ev) {
            stripe.confirmCardSetup(
                clientSecret, {
                    payment_method: {
                        card: cardElement,
                        billing_details: {
                            name: cardholderName.value,
                        },
                    },
                }
            ).then(function (result) {
                if (result.error) {
                    // Display error.message in your UI.
                } else {
                    console.log(result);
                }
            });
        });
    </script>
</body>

</html>

Are you getting any console errors?您是否收到任何控制台错误? Any errors in your Dashboard logs?您的仪表板日志中有任何错误吗?

If that doesn't shed any light, you may want to write into Support so they can look into the details for you: https://support.stripe.com/contact/email如果这没有任何意义,您可能需要写信给支持,以便他们可以为您查看详细信息: https : //support.stripe.com/contact/email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM