#chri5tian101_checkout-more-payment-methods-than-just-card

1 messages ยท Page 1 of 1 (latest)

weary zephyrBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1343990436393517228

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

shadow spruce
#

Hiii, this is my Checkout.php code:

`<?php
session_start();
require 'vendor/autoload.php';
include 'include/connection.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);

header('Content-Type: application/json');
\Stripe\Stripe::setApiKey(KEY HERE');

// Ensure user is logged in
if (!isset($_SESSION['userId']) || !isset($_SESSION['email'])) {
die(json_encode(["error" => "User session missing. Please log in."]));
}

$userId = $_SESSION['userId'];
$email = $_SESSION['email'];

// Validate input parameters
if (!isset($_POST['item_type']) || !isset($_POST['item_key'])) {
die(json_encode(["error" => "Invalid request parameters."]));
}

$itemType = $_POST['item_type'];
$itemKey = $_POST['item_key'];

// Define product prices
$plans = [
"sapphire_monthly" => "price_key",
"sapphire_yearly" => "price_key",
"diamond_monthly" => "price_key",
"diamond_yearly" => "price_key",
"ruby_monthly" => "price_key",
"ruby_yearly" => "price_key",
];

$extraServices = [
"Custom SEO Audit Report" => "price_key",
"In-depth Keyword Research" => "price_key",
"SEO web design starting price" => "price_key",
"SEO content creation" => "price_key",
"Complete SEO Action" => "price_key"
];

// Determine pricing and mode
if ($itemType === 'subscription') {
$priceId = $plans[$itemKey] ?? null;
$mode = 'subscription';
} elseif ($itemType === 'extra_service') {
$priceId = $extraServices[$itemKey] ?? null;
$mode = 'payment';
} else {
die(json_encode(["error" => "Invalid item type."]));
}

if (!$priceId) {
die(json_encode(["error" => "Invalid item selected."]));
}

try {
// Check if customer exists in Stripe
$customers = \Stripe\Customer::all(["email" => $email, "limit" => 1]);
$customerId = count($customers->data) > 0 ? $customers->data[0]->id : null;

// Create a new customer if not found
if (!$customerId) {
    $customer = \Stripe\Customer::create([
        'email' => $email,
        'metadata' => ['userId' => $userId]
    ]);
    $customerId = $customer->id;
}

// Create Stripe Checkout Session
$session = \Stripe\Checkout\Session::create([
    'payment_method_types' => ['card'],
    'mode' => $mode,
    'customer' => $customerId,
    'billing_address_collection' => 'required',
    'allow_promotion_codes' => true,
    'line_items' => [[
        'price' => $priceId,
        'quantity' => 1,
    ]],
    'metadata' => [
        'userId' => $userId,
        'itemType' => $itemType,
        'itemKey' => $itemKey
    ],
    'success_url' => 'https://advraja.makemypancard.com/success.php?session_id={CHECKOUT_SESSION_ID}',
    'cancel_url' => 'https://advraja.makemypancard.com/cancel.php',
]);

echo json_encode(['id' => $session->id]);

} catch (\Stripe\Exception\ApiErrorException $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => 'An unexpected error occurred.']);
}
?>
`

heady tusk
shadow spruce
#

I also forgot to mention: I've enabled klarna paypal etc in my Stripe Dashboard settings.

#

if i do a test link it shows all payment options in checkout. If i implement the changes using the code, it shows just credit card as the option to pay

heady tusk
#

Right, beause you're telling it to only allow 'card'.

#

You could try removing 'payment_method_types' altogether.

shadow spruce
heady tusk
#

Should, yes.

shadow spruce
#

Thank you so much it worked!!

heady tusk
#

Awesome! You're welcome! ๐Ÿ™‚