简体   繁体   中英

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. 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

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.

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