#santiago-webhook-php

1 messages · Page 1 of 1 (latest)

bronze pewterBOT
#

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.

shadow lion
#

Is your integration changing the header in any way? Can you do a console log to verify the signatures manually?

bronze pewterBOT
tropic cedar
#

It is not changing, anything, it is on the webhook itself, using php?. not the success page. so it technically comes from stripe directly, this is the code,straight from the demo.

fading aspen
#

Hello! I'm taking over and catching up...

tropic cedar
#

<?php
require '../vendor/autoload.php';
// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
\Stripe\Stripe::setApiKey('sk_test_***');

// You can find your endpoint's secret in your webhook settings
$endpoint_secret = "whsec_***";

//error log for checkign output
$log_file = "errors.log";

$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;

try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
error_log("\n\nInvalid Payload!\n\n",3,$log_file);
http_response_code(400);
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
error_log("\n\nInvalid Signature!\n\n",3,$log_file);
http_response_code(400);
exit();
}

error_log("\n\nPassed signature verification!\n\n",3,$log_file);
http_response_code

#

thiss is the code that 'redirects' to the payment page. if it is of any help

#

<?php
require '../vendor/autoload.php';
$stripe = new \Stripe\StripeClient('sk_test_****');

$data = json_decode(file_get_contents("php://input"));
if(!isset($data)){
echo "no data";
//header('Location: /local_upload/');
}
else {
session_start();
//add check if valid json data and all json keys are non null
//after check do this
$_SESSION['json'] = $data;
$checkout_session = $stripe->checkout->sessions->create([
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'product_data' => [
'name' => 'Hosting Ads Promo',
],
'unit_amount' => $data->price,
],
'quantity' => $data->quantity,
]],
'mode' => 'payment',
'success_url' => 'success.php?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => '/',
]);

echo $checkout_session->url;
}

fading aspen
#

Are you certain your whsec_ is correct? You're using the one from the Dashboard?

tropic cedar
#

the redirect is done on serverside

#

yes, i' m using the one from dashboard

#

copy/pste

fading aspen
#

What's the exact error you're getting?

#

Oh, wait.

#

This won't work: $data = json_decode(file_get_contents("php://input"));

#

Where did you get this code?

#

Hang on... I'm confused about your code. It came in two chunks, but I'm not sure how they relate?

tropic cedar
#

oh, the second one is just the call to the payment form url, I just sent it because you asked me if it modifies the headers in anyway.

#

the logfile now does not show 'invalid signature' but neither shows the 'passing' message that is put there

fading aspen
#

Okay, wait, let's back up. What Event type are you trying to listen for?

#

checkout.session.completed?

tropic cedar
#

i'm sorry it still shows invalid signature..

#

yes i'm listening to checkout.session.completed.

fading aspen
#

Okay, what's the full error message you're getting?

tropic cedar
#

I was just following the documentation for making sure it comes from stripe

#

to the webhook

#

just says 'invalid signature'

#

so it is showing this part

#

catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
error_log("\n\nInvalid Signature!\n\n",3,$log_file);
http_response_code(400);
exit();

fading aspen
#

Can you give me the ID of one of the checkout.session.completed Events which is triggering the error?

tropic cedar
#

anythng it ry does that, let me try to get it from cli

#

last test i did

#

i believe is this one

fading aspen
#

So this is all local? You're not trying to send this to an actual Webhook Endpoint yet?

#

Meaning you're sending it to your locally running code with stripe listen?

tropic cedar
#

oh, ok that might make sense, it is actually online the webhook, not local

#

this is how i did it, thoughti t'll work

fading aspen
#

What URL are you trying to send it to?

tropic cedar
#

i think

#

i mean it is like that, not sure if i made at typo but is that one

fading aspen
#

Ah, okay, so when you have your server online you should create an actual Webhook Endpoint in the Dashboard, not use stripe listen.

tropic cedar
#

here you go

#

yes, i created a webhook on the dashboard

#

oh but i cannot use the cli to listen ?

fading aspen
#

No, that's not how it works.

tropic cedar
#

let me see where it was

#

oh was actual webhook aparently

#

there

#

do i have to create it on the /test/webhook for signature to be validated?

#

when tsting?

fading aspen
#

Yes.

#

Test and live mode are separate, your live mode Webhook Endpoint won't receive Events from test mode.

#

stripe listen is only designed to relay Events from Stripe to your locally running web server when it's not online. If you have a server online you should set up a Webhook Endpoint in the Dashboard to send Events to it, not use stripe listen.

tropic cedar
#

oh ok, i thought i could just forward it to my webhook online because i just wanted to check the 'checkout.session.completed event.

#

quick question, once session completed on the webhook call , can i redirect directly from webhook to success page ?. or if not, how can i send some data to the webhook page? that i collected from the user?. since the user never visits the webhook url

fading aspen
#

No, the communication between Stripe and your Webhook Endpoint is entirely separate from what happens with the customer. If you want to customize the success page you can add {CHECKOUT_SESSION_ID} to your success_url. It will be replaced by the Checkout Session ID, which you can fetch when your success page is loaded.

tropic cedar
#

ok, so basically customer sends a picture and i want it to be 'saved' afterpayment is successful but i also do not want customer to access /success.php directly afterthey try it, so i thought maybe just saving it after webhook event has received checkout.session.completed. but looks like i cannot really do that, i think however i can do something different.. thanks for the help , yes i read that was trying to figure out, the session id just has customer information right ? nothing that shows 'session_completed" /

fading aspen
#

To clarify, you want people to upload a photo after they successfully pay?

tropic cedar
#

is actually before, just wanted a way to send the session.completed so it checks for it before uploading photo.

#

i've tried with the test endpoint and still have invalid signature

fading aspen
#

You're using the whsec_ value from the test mode Webhook Endpoint you set up in the Dashboard? Not the live mode one?

tropic cedar
#

yes i just figure that out

#

forgot to save it

fading aspen
#

I'm still not understanding about the photo. They upload the photo first, then you send them to Checkout?

tropic cedar
#

correct

#

i see on the webhook the payload says payment_status = paid'

fading aspen
#

Yep.

tropic cedar
#

yes, however i still cant see 'passes signature verification'

fading aspen
#

You're still seeing the invalid signature error?

bronze pewterBOT
tropic cedar
#

no, i think maybe is the way i'm logging it, i was able to see passed signature verification now, but i added a error_log($payload); right inside try after creating the event

#

and doesn't show it . but i'll check that, is probably on my wend

#

altough any help is appreciated. here is that part of the code.

  $event = \Stripe\Webhook::constructEvent(
    $payload, $sig_header, $endpoint_secret
    
  );
  error_log($payload);
} catch(\UnexpectedValueException $e) {
  // Invalid payload
  error_log("\n\nInvalid Payload!\n\n",3,$log_file);
  http_response_code(400);
  exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
  // Invalid signature
  error_log("\n\nInvalid Signature!\n\n",3,$log_file);
  http_response_code(400);
  exit();
}

error_log("\n\nPassed signature verification!\n\n",3,$log_file);
http_response_code(200);````
#

why i only get the passed signature verification' and not the 'payload' inside the try ?

grim pebble
#

I don't understand your question

#

santiago-webhook-php

tropic cedar
#

with that code, that i have error_log($payload); inside the try {} bracket

#

shouldn't it be saved to the logfile? i only get 'passed signature verification' at the end

grim pebble
#

You have a try/catch. If something fails an exception is raised. Here an exception is raised by constructEvent() which is called before the error_log so that's all expected.
Move it above

tropic cedar
#

how can i see the execption because it is supposed to be for signature or payload and supposedly they both passed.. ,

grim pebble
#

you have a catch, and there you can log all the information.