1

I am exploring the possibility of implementing a Stripe Customer Portal (https://stripe.com/docs/billing/subscriptions/customer-portal) using the Stripe PHP API (https://stripe.com/docs/billing/subscriptions/integrating-customer-portal).

The Portal call would be made available on a button once users are authenticated so that customer IDs can be retrieved from external DB using the session's log-in credentials.

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

Action would run the following command nested in the PHP file. This snippet sends a POST request to Stripe servers and triggers a response from Stripe servers which contains 7 attributes:

POST Request

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

Response Body

{
  "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}"
}

The next step is to fetch the "URL" attribute contained in the response body and use it to redirect the user to his/her customized Customer Portal. I am having trouble with this step.

Any help would be greatly appreciated! Thanks a lot.

iAmOren
  • 2,760
  • 2
  • 11
  • 23
tlt
  • 13
  • 3
  • Welcome to Stack Overflow, please take a look for code formatting to better shape your questions: https://stackoverflow.com/editing-help – endo64 Sep 27 '20 at 18:12

1 Answers1

1

Once you have created the Customer Portal session:

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

It would be a case of redirecting your user to the URL returned in that session. For example:

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

How you implement that last step is up to you and how your web server is set up.

Paul Asjes
  • 5,361
  • 1
  • 18
  • 20