#feminaagravat_39821

1 messages · Page 1 of 1 (latest)

minor ginkgoBOT
#

Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

astral vigil
#

Hi
For security reasons, sessions are short-lived and will expire if the customer does not visit the URL. Create sessions on-demand when customers intend to manage their subscriptions and billing details.

silent hearth
#

Right now I am using the API

#

public function getCustomerPortal($stripeCustomerID){
// Authenticate your user.
$session = \Stripe\BillingPortal\Session::create([
'customer' => $stripeCustomerID,
'return_url' => $this->baseURL.'owner/renewal',
'flow_data' =>[
'type'=>'payment_method_update',
'after_completion' => [
'type' => 'redirect',
'redirect' => ['return_url' => $this->baseURL.'owner/renewal?paymentUpdated=true'],
],
]
]);
return $session->url;

}

#

And I am redirecting User to that URL

#

How can I start the fresh session for each user ?

#

Are you there ? Are we connected ?

astral vigil
#

Yes sorry for the late reply..

#

How can I start the fresh session for each user ?
First how are you sharing/opening the customer portal with/for the user ?

silent hearth
#

I have shared code above

#

using API

minor ginkgoBOT
astral vigil
#

No I mean how you share the customer portal link with the customer

silent hearth
#

I do create it using API, the api gives me session->url .

I redirect them through code,

#

THey have a button , when they click on that button i do call this API to creteCustomer portal session

primal herald
#

can you show the full context of how getCustomerPortal is called?

primal herald
#

I mean the code.

silent hearth
#

Ok

#

This is the controller Code Route

/**
* @Route("/owner/create-customer-portal-session",name="create-customer-portal-session")
*/
public function createCustomerPortalSession(Request $request)
{
$requestData = $request->request->all();
//dump($requestData);exit;
$stripeService = $this->get('frontend.client.stripe');
$entityManager = $this->getDoctrine()->getManager();
$userData = $this->getUser();
$customerId = $requestData["vStripeCustomerId"] ?? $userData->getVStripeCustomerId();

     //FIRST CHECK IF CUSTOMER ID IS NOT CREATED THEN CREATE CUSTOMER ID IN BACKGROUND AND USE IT FOR STRIPE CUSTOMER PORTAL
     if(empty($customerId)){
        try {
            $data= [
                "email"=>$userData->getVEmail(),
                "name"=>$userData->getVFirstName()." ". $userData->getVLastName()
            ];
            $customer = $stripeService->createCustomer($data);
            $customerId = $customer->id;

            $userObj = $entityManager->getRepository(User::class)->find($userData->getId());
            $userObj->setVStripeCustomerId($customerId);
            $userObj->setDtUpdated();
            $entityManager->persist($userObj);
            $entityManager->flush();
        } catch (\Throwable $th) {
            dump($th);exit;
            //throw $th;
        }
    }

    $url = $stripeService->getCustomerPortal($customerId);
    $returnArr["url"] = $url;
    if(empty($requestData["vStripeCustomerId"]) && !empty($requestData['ajaxCustPortal'])){
        return new JsonResponse($returnArr);
    }else{
                // Redirect to the customer portal.
    header("Location: " . $url);
    exit();

    }

}
#

public function getCustomerPortal($stripeCustomerID){
// Authenticate your user.
$session = \Stripe\BillingPortal\Session::create([
'customer' => $stripeCustomerID,
'return_url' => $this->baseURL.'owner/renewal',
]);
return $session->url;

}

primal herald
#

ok, and the frontend code that invokes this create-customer-portal-session URL and handles the result, can you share that too?

silent hearth
#

yes

#

<p> Stripe.com securely stores all payment information which will be used for future billings.<br>
<a href="{{ url('create-customer-portal-session') }}"><i class="fa fa-solid fa-arrow-right"></i> Manage Auto Renewal payment details on Stripe.com.</a>
</p>

primal herald
#

hmm your backend code doesn't seem to match what that expects

#

though maybe I'm wrong

#

I don't know what this url(..) function is or how that works, what is that?

#

also, in the backend code you have this

 if(empty($requestData["vStripeCustomerId"]) && !empty($requestData['ajaxCustPortal'])){
            return new JsonResponse($returnArr);
        }else{
                    // Redirect to the customer portal.
        header("Location: " . $url);
        exit();

which of this if-branches do you expect to be happening?

minor ginkgoBOT
silent hearth
#

header("Location: " . $url);
exit();

this line redirect users to stripe customer portal.
I am getting this $url from stripe API response, \Stripe\BillingPortal\Session::create() this function return the short-lived url , so i have used this as return value and assinged it to $url.

silent hearth
primal herald
#

if you're doing a redirect then it seems confusing to me to have this <a> tag