1

I would like to provide a permanent link to the stripe site for subscribers' subscriptions via stripe for payment methods, upgrades/downgrades, cancellations etc.

I have already tried with the session id but get the error message:

<br />
<b>Fatal error</b>:  Uncaught Stripe\Exception\UnexpectedValueException: Could not determine which URL to request: Stripe\Checkout\Session instance has invalid ID:  in /var/www/vhosts/syedyjyq.host242.checkdomain.de/xnexus.at/payment/vendor/stripe/stripe-php/lib/ApiResource.php:104
Stack trace:
#0 /var/www/vhosts/syedyjyq.host242.checkdomain.de/xnexus.at/payment/vendor/stripe/stripe-php/lib/ApiResource.php(118): Stripe\ApiResource::resourceUrl(NULL)
#1 /var/www/vhosts/syedyjyq.host242.checkdomain.de/xnexus.at/payment/vendor/stripe/stripe-php/lib/ApiResource.php(56): Stripe\ApiResource-&gt;instanceUrl()
#2 /var/www/vhosts/syedyjyq.host242.checkdomain.de/xnexus.at/payment/vendor/stripe/stripe-php/lib/ApiOperations/Retrieve.php(26): Stripe\ApiResource-&gt;refresh()
#3 /var/www/vhosts/syedyjyq.host242.checkdomain.de/xnexus.at/payment/account.php(23): Stripe\Checkout\Session::retrieve(NULL)
#4 {main}
  thrown in <b>/var/www/vhosts/syedyjyq.host242.checkdomain.de/xnexus.at/payment/vendor/stripe/stripe-php/lib/ApiResource.php</b> on line <b>104</b><br />

My php file looks this:

<?php

    require 'vendor/autoload.php';
// This is your real test secret API key.
\Stripe\Stripe::setApiKey('sk_test_123');

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


    include('../includes/connection.php');
    session_start();
    $id = $_SESSION["uid"];
    
    $sql = "select * from mollby_users where id='".$id."'";
    $antwort = $conn->query($sql) or die($conn->error);
    while($datensatz = $antwort->fetch_object()){
        $session_id = $datensatz->checkout_session_id;
    }
    
    
    
    try {
      $checkout_session = \Stripe\Checkout\Session::retrieve($_POST[$session_id]);
      // Authenticate your user.
      $session = \Stripe\BillingPortal\Session::create([
        'customer' => $checkout_session->customer,
        'return_url' => $return_url,
      ]);
    } catch (Error $e) {
      http_response_code(500);
      echo json_encode(['error' => $e->getMessage()]);
    }
    
?>
 <form action="/create-portal-session.php" method="POST">
            <input type="hidden" id="session-id" name="session_id" value="" />
            <button id="checkout-and-portal-button" type="submit">Manage your billing information</button>
        </form>

EDIT: I am using the Pre-built subscription page with Stripe Checkout!

koopajah
  • 23,792
  • 9
  • 78
  • 104
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Oct 21 '21 at 20:57
  • Is the error you're getting from when you retrieve the Checkout Session, or from when you create the Customer Billing Portal Session? I'm curious which line specifically is failing – karbi Oct 22 '21 at 23:15

0 Answers0