首页 > 解决方案 > Stripe 客户门户 PHP 实施 - 将客户重定向到 URL

问题描述

我正在探索使用 Stripe PHP API(https://stripe.com/docs/billing/subscriptions/)实现 Stripe 客户门户(https://stripe.com/docs/billing/subscriptions/customer-portal)可能性集成客户门户)。

一旦用户通过身份验证,门户调用将在按钮上可用,以便可以使用会话的登录凭据从外部数据库检索客户 ID。

form method="POST" action="/customer_portal.php"

操作将运行嵌套在 PHP 文件中的以下命令。此代码段向 Stripe 服务器发送 POST 请求并触发来自 Stripe 服务器的响应,其中包含 7 个属性:

POST 请求

\Stripe\Stripe::setApiKey('secret_key');
\Stripe\BillingPortal\Session::create([
  'customer' => 'customer_id'
]);

响应体

{
  "id": "portal_session_X",
  "object": "billing_portal.session",
  "created": 2569878511,
  "customer": "customer_id",
  "livemode": false,
  "return_url": "https://example.com/account",
  "url": "https://billing.stripe.com/session/{SESSION_SECRET}"
}

下一步是获取响应正文中包含的“URL”属性,并使用它将用户重定向到他/她的自定义客户门户。我在这一步遇到问题。

任何帮助将不胜感激!非常感谢。

标签: phpstripe-payments

解决方案


创建客户门户会话后:

$stripe = new \Stripe\StripeClient(
  'sk_test_123'
);
$customer_portal = $stripe->billingPortal->sessions->create([
  'customer' => 'cus_123',
  'return_url' => 'https://example.com/account',
]);

这将是一种将您的用户重定向到该会话中返回的 URL 的情况。例如:

header('Location: ' . $customer_portal->url);

如何实现最后一步取决于您以及如何设置 Web 服务器。


推荐阅读