You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Stripe实现支付后跳转PHP页面并获取用户邮箱生成序列号

Hey there! Let's break down how to tackle your Stripe integration needs step by step, including answers to your specific questions:

1. How to Redirect to Your PHP Success Page After Payment

Stripe Checkout handles most of the heavy lifting here—you don’t need custom navigation code for basic redirects. Here’s how to set it up properly:

Use Redirect Checkout (Best for Your Use Case)

When creating a Stripe Checkout Session on your backend (PHP), define the success_url parameter to point directly to your PHP success page. Stripe will automatically redirect users here once payment is confirmed.

Example backend code to create the session:

<?php
require 'vendor/autoload.php';

\Stripe\Stripe::setApiKey('your_stripe_secret_key');

// If you collected the user's email in a pre-payment form, pass it here
$user_email = $_POST['user_email'] ?? null;

$session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [[
    'price_data' => [
      'currency' => 'usd',
      'product_data' => ['name' => 'Your Software'],
      'unit_amount' => 2999, // $29.99 in cents
    ],
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  // Stripe replaces {CHECKOUT_SESSION_ID} with the actual session ID
  'success_url' => 'https://yourdomain.com/success.php?session_id={CHECKOUT_SESSION_ID}',
  'cancel_url' => 'https://yourdomain.com/cancel.php',
  // Pre-fill email if you collected it, or let Stripe collect it during checkout
  'customer_email' => $user_email,
]);

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

Frontend Trigger for Checkout

On your frontend, call Stripe’s redirect function to send users to the Stripe payment page:

const stripe = Stripe('your_stripe_public_key');

document.getElementById('pay-button').addEventListener('click', async () => {
  // Fetch the session ID from your backend
  const response = await fetch('/create-checkout-session.php', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
      user_email: document.getElementById('email-input').value
    })
  });
  const { session_id } = await response.json();

  // Redirect to Stripe Checkout
  const result = await stripe.redirectToCheckout({ sessionId: session_id });
  
  if (result.error) {
    alert(result.error.message); // Handle errors like invalid session
  }
});

Once payment succeeds, Stripe will automatically send users to your success.php page—no extra redirect code needed.

2. How to Retrieve the User’s Email

You have two reliable ways to get the user’s email, depending on your flow:

Option 1: Fetch Email from Stripe Session (Most Secure)

On your success.php page, use the session ID from the URL to retrieve the full Checkout Session details via Stripe’s API. This gives you access to the customer_email field (either pre-filled by you or collected by Stripe during checkout):

<?php
require 'vendor/autoload.php';

\Stripe\Stripe::setApiKey('your_stripe_secret_key');

// Get the session ID from the URL query parameter
$session_id = $_GET['session_id'];
$session = \Stripe\Checkout\Session::retrieve($session_id);

// Extract the user's email
$user_email = $session->customer_email;

// Generate your serial number here
$serial_number = generateSerial($user_email);

// Example serial number generator
function generateSerial($email) {
  return strtoupper(substr(md5($email . uniqid()), 0, 8)) . '-' . substr(str_shuffle('0123456789ABCDEF'), 0, 4);
}

// Display or save the serial number
echo "Thanks for your purchase! Your serial number: <strong>$serial_number</strong>";
?>

Option 2: Store Email in Your Own Session (If Pre-Collected)

If you collected the email in a form before sending users to Stripe, you can store it in a PHP session before redirecting:

// On your pre-payment form page
session_start();
$_SESSION['user_email'] = $_POST['user_email'];

// Then on success.php
session_start();
$user_email = $_SESSION['user_email'];

Note: This is less secure than using Stripe’s session data, as users could manipulate local session data. Using the Stripe API is always safer.

Should You Use the Popstate Listener Code?

Short answer: Only if you’re using Embedded Checkout (the version where Stripe Checkout appears as a modal on your page, not a full redirect). That code closes the modal if the user clicks the browser’s back button.

Since you’re using Redirect Checkout (sending users to Stripe’s dedicated payment page), this code is irrelevant—you don’t need it. Stripe handles the redirect flow entirely, so there’s no modal to close when navigating back.

内容的提问来源于stack exchange,提问作者stighy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:34:14