I know a global variable should be initialized by contractor. what if need to init a global variable inside one function and access it in another function.
this is my first function
public $customerId;
public function store(CheckoutRequest $request)
{
try {
$customer = Stripe::customers()->create([
'source' => $request->stripeToken,
'email' => $request->email,
'description' => $request->name
]);
**$this->customerId** = $customer->id;
$this->chargeCustomer($request, $customer->id);
return redirect()->route('shipper.payment')->with('success_message', 'Thank you! your payment was successfull.');
} catch (Exception $e) {
return back()->withErrors('Error! ' . $e->getMessage());
}
}
this is my second function
public function paymentDetails()
{
return $this->customerId;
}
I'm creating customer in Stripe and assign created customer id to $customerId global variable, I need $customerId insid paymentDetails function which gets calling by another route, but paymentDetails return null. could anyone help please, how to get customer id in other functions?
If you, as you describe, are calling
paymentDetails()
later via another route, then the public variable will not be set anymore. The controller is resolved per request, so it does not store anything for the next request.Your best bet is probably to store some the customer on your own end and use that to retrieve the customerId on the next request, or maybe use https://laravel.com/docs/7.x/session