Pathsofdesign
Pathsofdesign

Reputation: 5038

Laravel Cashier saving to stripe but not DB

Laravel Cashier is not saving stripe data to my DB after a user is created.

When I create a new user and then try to assign a subscription() to that user (or any other user for that matter) the data is being sent to Stripe approriately, as I can see the customer id, plan chosen, etc... But my database user stripe columns do not get updated in my database.

When I remove the code to create() a user and just subscribe a user, the data persists to the database like it should. It only doesn't work after I've created a new user THEN try to subscribe any user.

I'm using Jeffrey Way's Model Validation Package.

$user = $this->userModel->create($input);

if ($user->hasErrors()) {
    throw new \Exception($user->getErrors());
}

$this->createStripeSubscription($user->id, $input['creditCardToken']);

private function createStripeSubscription($user_id, $token) {
    //$this->userModel->find(1)->subscription('monthly')->create($token); // Doesn't work either
    $this->userModel->find($user_id)->subscription('monthly')->create($token);
}

Upvotes: 1

Views: 1319

Answers (3)

13garth
13garth

Reputation: 783

I had the same issue of Laravel Cashier saving fine to stripe but not to my database.
However, I was experiencing this on my local development environment.
Running stripe listen command fixed the issue.

Here are the details :
Understanding Why the Records Save Only When Running stripe listen.

The reason your subscription records are saved to your database only when you run:

stripe listen --forward-to http://localhost-url/stripe/webhook

is because your local development environment (http://localhost-url) is not accessible from the internet. Therefore, Stripe cannot send webhook events directly to your local server. The stripe listen command provided by the Stripe CLI acts as a proxy between Stripe's servers and your local machine, forwarding the webhook events to your local webhook endpoint.

In summary:

Without stripe listen: Stripe's servers cannot reach your local webhook endpoint (http://localhost-url/stripe/webhook) because it's not publicly accessible. With stripe listen: The Stripe CLI listens for events on Stripe's servers and forwards them to your local endpoint, allowing your application to process the webhooks.


Also, I had added this route to my web.php file.

Cashier route I added for stripe webhooks

You don't need to add this route. Cashier adds it on it's own for you.

Upvotes: 0

zub0r
zub0r

Reputation: 1379

I had similar problem with Ardent validation in Confide.

Overriding saving method of BillableTrait in user model solved the issue.

class User extends ConfideUser implements BillableInterface {

    use BillableTrait;

    public function saveBillableInstance()
    {                            
        $this->forceSave();        
    }
}

Upvotes: 1

Pathsofdesign
Pathsofdesign

Reputation: 5038

The problem was a conflict with Jeffrey Way's Validation Model package. Or at least the way I was trying to implement it while using Cashier. I decided to abstract out my Validation in it's own class and remove the Validation Model package. All is good.

Upvotes: 0

Related Questions