Bob Pritchard
Bob Pritchard

Reputation: 41

How do I retrieve Stripe Session ID for a connected account?

I'm using Stripe checkout. I create the session and process the charge. On success, I have the session id. I want to retrieve the session object for the connected account. (This works fine for a charge to my standard account, but fails when for retrieving the session for the connected account).

For reference, here's the PHP code for creating the session before the charge:

     \Stripe\Stripe::setApiKey($Skey);
     $session = \Stripe\Checkout\Session::create([
       'customer_email' => $Email,
       'payment_method_types' => ['card'],
       'line_items' => $itms,
       'payment_intent_data' => [
          'description' => $Web_Short_Name . '*' .$Transaction_ID,
          'application_fee_amount' => $Fee,
          'metadata' => ['Transaction_ID' => $Transaction_ID],
       ],

       'success_url' => 'https://[myweb]/success.php?session_id={CHECKOUT_SESSION_ID}',
       'cancel_url' => 'https://[myweb]/cart.php',
     ],
     ['stripe_account' => $Stripe_Account] );
     }

FIRST ATTEMPT:

$s = $_GET['session_id'];
$stripe = new \Stripe\StripeClient(
  ['api_key' => '[my secret key'],
  ['stripe_account' => '[connected account]']
);
$s2=$stripe->checkout->sessions->retrieve($s,[]); 

SECOND ATTEMPT:

$s = $_GET['session_id'];
\Stripe\Stripe::setApiKey('[my secret key]');
$stripe = new \Stripe\StripeClient(
 '[my secret key]'
);
$s2=$stripe->checkout->sessions->retrieve($s,[]);

Thanks in advance! Bob (I've used StackOverflow as a resource for years...but this is my first post).

Upvotes: 3

Views: 5010

Answers (3)

Ahmed Memon
Ahmed Memon

Reputation: 170

  \Stripe\Stripe::setApiKey('<your API key>');
 $session = \Stripe\Checkout\Session::retrieve('<the session id>',[], [
     'stripe_account' => '<the id of the connected Stripe account>'
 ]);

The second param array should be empty if no other parameters to pass and stripe_account is to be passed in the third array which is for the options array.

Upvotes: 1

Peter
Peter

Reputation: 21

For connected accounts, you can fill the second parameter of the retrieve function, just like you did when creating the session:

     \Stripe\Stripe::setApiKey('<your API key>');
     $session = \Stripe\Checkout\Session::retrieve('<the session id>', [
         'stripe_account' => '<the id of the connected Stripe account>'
     ]);

Upvotes: 2

Bob Pritchard
Bob Pritchard

Reputation: 41

Got what I needed to work. Essentially I was looking for the PaymentIntent object when I got the call to my webhook from Stripe.

Snippet from my checkout webhook:

<?
require __DIR__ . '/vendor/autoload.php';
$payload = @file_get_contents('php://input');
$event = null;
try {
    $event = \Stripe\Event::constructFrom(
        json_decode($payload, true)
    );
} catch(\UnexpectedValueException $e) {
    // Invalid payload
    http_response_code(400);
    exit();
}


// Handle the event
\Stripe\Stripe::setApiKey({YOUR_SECRET_KEY});

$session_id = $event->data->object->id;

switch ($event->type) {
    case 'checkout.session.completed':
        $checkout = $event->data->object; // contains a \Stripe\PaymentIntent
        // Then define and call a method to handle the successful payment intent.
         //handleCheckoutSucceeded($checkout);
         handleCheckout($db,$Transaction_ID,$session_id,'SUCCEEDED');
?>

Upvotes: 1

Related Questions