I am trying to prefill the shipping address section but I can't, I have passed multiple different parameters specifying the shipping address but each one throws an error saying unknown parameter, help!
Now I am adding these line because I am getting error while posting this question saying that it looks like your post is mostly code add some lines.
This is the related code
<?php
if (isset($_SESSION['billing_details'])) {
$billing_details = $_SESSION['billing_details'];
$name = $billing_details['name'];
$email = $billing_details['email'];
$number = $billing_details['number'];
$city = $billing_details['city'];
$country = $billing_details['country'];
$addressl1 = $billing_details['addressl1'];
$addressl2 = $billing_details['addressl2'];
$postalcode = $billing_details['postalcode'];
$state = $billing_details['state'];
}
require __DIR__ . "/vendor/autoload.php";
$user_email = $_SESSION['email'];
$stripe_secret_key = "";
\Stripe\Stripe::setApiKey($stripe_secret_key);
// Fetch cart items from the database
$query = $query = "SELECT cart.qty, products.product_price, products.product_title
FROM cart
INNER JOIN products ON cart.p_id = products.product_id
WHERE cart.user_email = '$user_email'";
$result = mysqli_query($conn, $query);
$line_items = [];
while ($row = mysqli_fetch_assoc($result)) {
$line_item = [
"quantity" => $row['qty'],
"price_data" => [
"currency" => "inr", // Assuming your prices are in USD, change if necessary
"unit_amount" => $row['product_price'] * 100, // Convert to cents
"product_data" => [
"name" => $row['product_title']
]
]
];
$line_items[] = $line_item;
}
$checkout_session = \Stripe\Checkout\Session::create([
"mode" => "payment",
"locale" => "auto",
"customer_email" => $email,
"line_items" => $line_items,
"billing_address_collection" => "required",
"shipping_address_collection" => [
"allowed_countries" => ["IN"],
],
"metadata" => [
"name" => $name,
"email" => $user_email,
"phone" => $number,
]
]);
http_response_code(303);
header("Location: " . $checkout_session->url);
You can't do it if
billing_address_collectionorshipping_address_collectionare enabled, as your Checkout Session will add fields for the customer to add their address, and you can't prefill them.If you already know your Customer's address, then create a Customer with their
addressandshippinginformation first, then create your Checkout Session withcustomer => $customer->id.Your Session will still show the Billing country field, as that's required for the payment method (card), but it won't prompt the customer to enter their address.