#MikeD-Webhook

1 messages ยท Page 1 of 1 (latest)

night minnow
#

Hi @primal forum! Let's chat in this thead. Reading...

primal forum
#
\Stripe\Stripe::setApiKey('...');
        try {
            $event = \Stripe\Event::constructFrom(
                json_decode($request, true)
            );

            switch($event->object) {
                case 'subscription':
                    $t = new \App\Models\Tag(); $t->name = 'test'; $t->save();
                    break;
            }
        } catch(\UnexpectedValueException $e) {
            return response(201, 'bad parse');
        }
    }
night minnow
#

How are you creating $request?

primal forum
#

Sorry

#

I was afk

#

I am updating stuff through Stripe Client through tinker.

#

Receiving a webhook through the Stripe CLI

#

That is forwarding it to me.

#

$sc->subscriptions->update('sub_1KSpcgGbltLCtZUA3oF3ygnS', ['metadata' => ['test1' => 'test2']]);

night minnow
#

Can you provide the full code? How are you creating the $request variable that you are passing to json_decode?

primal forum
#

It's standard laravel API

#

Route::post('/stripe/webhooks', 'Stripe\WebHookController@index');

#
class WebHookController extends Controller
{
    //

    public function index (Request $request) {
        \Stripe\Stripe::setApiKey( env('STRIPE_KEY', 'Laravel'));
        try {
            $event = \Stripe\Event::constructFrom(
                json_decode($request, true)
            );

            switch($event->object) {
                case 'subscription':
                    $t = new \App\Models\Tag(); $t->name = 'test'; $t->save();
                    break;
            }
        } catch(\UnexpectedValueException $e) {
            return response(201, 'bad parse');
        }
    }
}

night minnow
#

Go ahead and edit that to remove your secret key please, but thanks for full code!

primal forum
#

yup sorry forgot about that.

#

it's a TEST key but none the less. Thank you

night minnow
#

Yep you still don't want anyone to have access to your test key

#

Okay so instead of case 'subscription' it looks like you are attempting to listen for a customer.subscription.updated event here.

primal forum
#

right but it doesn't even matter. It's failing on

$event = \Stripe\Event::constructFrom(
                json_decode($request, true)
            );
night minnow
#

Hrmm gotcha. And if you add an error_log($request) before the try what does it log?

primal forum
#

I don't get anything.

night minnow
#

You should see a server log of the raw $request object, but it is just undefined?

primal forum
#

I don't get anything which is interesting.

night minnow
#

Hrmm yeah that is strange.

primal forum
#

Also, what's interesting I don't get anything when I respond to the webhook in the dashboard.

night minnow
#

What do you mean "respond to the webhook in the dashboard"?

primal forum
#

so if I do like return response(200, 'something something') that would typically respond out something to the request.

#

with code 200

#

I would expect it to be in the "response body"

night minnow
#

Oh I see you mean there is no response shown in the Dashboard.

#

Correct

primal forum
#

Ok. I havne't used laravel log debugging in a while I found it and wrote debug. Let me read this for a sec

#

Alright I got something finally. UGH So It's been a while response needs to be like response('content', code)

#

so it's logging the request correctly now.

#

alright would like you like me to paste the whole whole request?

night minnow
#

Ah gotcha. I'm not super familiar with Laravel but I really should play with it...

#

So you are now responding correctly, but you can't read the event data, correct?

primal forum
night minnow
#

Okay so you are receiving the request but still hitting an error on json_decode?

primal forum
#

yup

#

doesn't seem like it's sending json

night minnow
#

Hrmm wait the above event already is decoded...

#

Oh wait no

primal forum
#

looks like it's already an assoc array

night minnow
#

So I think what is happening is Laravel is modifying your body so you can't json_decode it as normal

primal forum
#

maybe the API middleware is already doing it?

night minnow
#

right

primal forum
#

Maybe I can encode decode see what happens?

#

Nope samething

night minnow
#

One sec, let me just ask someone on my team more familiar with PHP

primal forum
#

looks like spatite actually has a service provider. Let me try implementing that real quick

#

well known group

#

What is the "stripe_webhook_secret" ?

night minnow
#

The secret is for handling your webhook securely

primal forum
#

is that the signing secret?

night minnow
#

Yes

primal forum
#

gotcha so as I dev i have to keep changing

#

but prod stays the same?

night minnow
#

It will stay the same in both dev and prod for your account endpoints. If you are using CLI then the CLI will provide you a secret when you login

#

However, if you plan on using the webhook secret, you are going to run into an issue, because in order to verify the secret, you must have the raw request body.

#

And middleware that messes with it will cause verification to fail.

primal forum
#

Right

#

That is why spatite is saying for that endpoint you need to add it to the exclusion list so it doesn't get processed

night minnow
#

Gotcha, well if you add it to the exclusion list then I think it would solve both problems because then you can json_decode the raw body

primal forum
#

Yeah, I am taking a look

primal forum
#

Alright looks I was able to get this package to work.

#

Still checking on it right now.

#

Great working and now no lol

night minnow
#

lol was working and now it isn't?

primal forum
#

I got it finally accidently commented out the api route lol

night minnow
#

Great!

primal forum
#

going to see if I can test one thing if so we can mark it solved.

primal forum
#

We are working ๐Ÿ™‚ Yay

#

I have a quick question unrelated. Using the frontend vuejs
https://vuestripe.com/stripe-elements/card

I assume this will generate some kind of token that I can send to my backend. The the backend can send this to stripe and tie to the customer. for which then I can process their subscription that is in trial etc.

#

Also do you have a link to the api that shows how I can get all sources for a customer?

#

I may want to have them be able to manage cards in my system by showing them the last 4 and the card type.

#

and I will do the swap on my end and attach and detatch source from subscription as needed based on their settings in the app

#

ahh they are literally called card objects now not source objects.

#

Beautiful ๐Ÿ™‚

finite scarab
#

Yes that's true. However, we recommend using PaymentMethods now

primal forum
#

awesome. however after reading this. i am almost inclined to use checkout

#

however does checkout support tiered sub?

#

now that I have a webhook service provider I can do anything lol

#

and just use the data given to you for me :p

#

looks like only metered or flat rate

#

No tiering.

finite scarab
#

You could have different products for each tier that the user would select on the website, then create the checkout session with the applicable price for that tier