#u2ojr2-pi-fields

1 messages ยท Page 1 of 1 (latest)

solemn nebula
#

Hey there, there's a few options really (think we spoke yesterday)

primal thorn
#

Yeah we did, I tried a few of them and none worked unfortunately

#

I've gone back to the drawing board

#

trying to keep it simple as possible

solemn nebula
#

What exactly are you building and why do you need those fields?

primal thorn
#

So this is a checkout page for a product that will eventually be shipped to the customer

#

for that reason we need a shipping address/email/name ect

#

but I am just focusing on getting 1 field such as email address for now

#

once I have that I can do the rest on my own

#

Does that make sense

solemn nebula
#

Sure. Are you likely to re-use this customer data? Like, will they have follow-up purchases where they may want to re-use this data?

primal thorn
#

Potentially in an upsell page that would be straight after this checkout

#

However, I need to use "PaymentIntent" as we have webhooks already built that work around this event

#

I'd like to send this data inside a PaymentIntent -> Metadata[]

#
    $paymentIntent = \Stripe\PaymentIntent::create(
        [
            'amount' => calculateOrderAmount($jsonObj->items),
            'currency' => 'eur',
            'automatic_payment_methods' => [
                'enabled' => true,
            ],
            'description' => 'test@test.com | Product: test x1',
            'metadata' => [
                'products' => 'test x1',
                'email' => 'test@test.com',
                'name' => $name,
                'first_name' => 'Jack',
                'last_name' => 'Test',
                'testing' => $jsonObj->items[0]->id,
                'testing2' => $jsonObj->items[1],
                'testing3' => $jsonStr,
                'testing4' => $jsonObj->items[0]->name,
            ],
        ]
    );```
#

Like this for example?

solemn nebula
#

Yep, you need to use Payment Intents regardless. That's the right API for charging users

primal thorn
#

See my problem isn't creating the events.

I am having difficulty creating additional inputs on the form and sending that data to stripe

solemn nebula
primal thorn
#

Would you know how I create additional inputs such as an email field

#

that will then send that back to stripe

solemn nebula
#

You'd need to build those yourself in your UI. Have you considered using Checkout which will handle this for you?

primal thorn
#

I have buiilt them myself

#

Just I can't get them to post to the file for some reason?

solemn nebula
#

Can you share the full PHP file? Specifically how you receive the JSON from your front-end

primal thorn
#

Sure 1 moment

#

require $_SERVER['DOCUMENT_ROOT'] . '/Stripe/init.php';

// This is your test secret API key.
\Stripe\Stripe::setApiKey('sk_test');

function calculateOrderAmount(array $items): int {
    // Replace this constant with a calculation of the order's amount
    // Calculate the order total on the server to prevent
    // people from directly manipulating the amount on the client
    return 1400;
}

$firstName = $_POST['first_name']

header('Content-Type: application/json');

try {
    // retrieve JSON from POST body
    $jsonStr = file_get_contents('php://input');
    $jsonObj = json_decode($jsonStr);

    // Create a PaymentIntent with amount and currency
    $paymentIntent = \Stripe\PaymentIntent::create([
        'amount' => calculateOrderAmount($jsonObj->items),
        'currency' => 'eur',
        'automatic_payment_methods' => [
            'enabled' => true,
        ],
        'metadata' => [
            'products' => 'test x1',
            'email' => 'test@test.com',
            'first_name' => $firstName
        ],
    ]);

    $output = [
        'clientSecret' => $paymentIntent->client_secret,
    ];

    echo json_encode($output);
} catch (Error $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}```
#

This is my php

#

This is my form

<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Accept a payment</title>
    <meta name="description" content="A demo of a payment on Stripe" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="checkout.css" />
    <script src="https://js.stripe.com/v3/"></script>
    <script src="checkout.js" defer></script>
</head>

<body>
    <!-- Display a payment form -->
    <form id="payment-form" method="post" action="https://mydomain.com/Testing/order_5/create.php">
        <label for="first_name">First Name</label>
        <input type="text" name="first_name" required>
        <div id="payment-element">
            <!--Stripe.js injects the Payment Element-->
        </div>
        <button id="submit">
            <div class="spinner hidden" id="spinner"></div>
            <span id="button-text">Pay now</span>
        </button>
        <div id="payment-message" class="hidden"></div>
    </form>
</body>

</html>```
#

As you can see I am posting the first_name to my create.php where the payment intent is created

solemn nebula
#

What is the contents of $jsonObj in your PHP file?

primal thorn
#

That gets passed from the checkout.js file

#

It's on stripes website I haven't edited that file

solemn nebula
#

Yes, but my expectation is that your first_name field will be inside that $jsonObj variable

#

Can you log it out?

#
$firstName = $_POST['first_name']
#

This is likely not working as you'd expect

primal thorn
#

Yeah that field is empty

#

But makes no sense as to why, this is how you do a traditional php post

#

I have tried doing a post as an example without stripe and it works fine

#

This is what is inside the $jsonObj

#

const items = [{ id: "xl-tshirt" }];

#

this is just set by default from the quickstart code

solemn nebula
#

But that's a JS output of the variable

#

Can you log it in the PHP function?

primal thorn
#

sure 1 moment will send a screenshot

#

I'm struggling to find out the contents without sending it to stripe

#

Every debug method I try on the create.php causes the checkout to not load

lyric mica
#

Can you look at the request in the Network section of your browser's dev tools?

#

Also, hi again ๐Ÿ‘‹

primal thorn
#

Heya

lyric mica
#

It sounds like we know the values are there on the JS side of things, but we're not sure if they're making it to your PHP code, and how to get to them if they are. Does that sound about right for current state?

primal thorn
#

Yeah sort of

#

Hmm well no actually

#

the values aren't being passed anywhere that are input to the form

#

I'm quite surprised there are no forms that need to collect a customers first name and store it in Stripe

#

do you know of any example forms I can look at, perhaps I can reverse engineer them

lyric mica
#

Hm, not off the top of my head. I think the payment element for some payment methods that require more information.