1

I'm setting my webhook STRIPE and i want to send message on Telegram when happen event in STRIPE (checkout.session.completed so after payment).

To set webhook is very easy so i'm sure the problem is really easy (just not for me).

\Stripe\Stripe::setApiKey('sk_test_xxx');

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

$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
  http_response_code(400);
  exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
  // Invalid signature
  http_response_code(400);
  exit();
}

// Handle the checkout.session.completed event
if ($event->type == 'checkout.session.completed') {
  $session = $event->data->object;

  // Fulfill the purchase...
  handle_checkout_session($session);
}

http_response_code(200);

If payment is successfull (so if $event->type == 'checkout.session.completed') i want to write a message on telegram. To write message is very easy:

    $url= 'https://api.telegram.org/botxxx/sendMessage?chat_id=xxx&parse_mode=HTML&text=ok-webhook';    
    file_get_contents($url);  

Why doesn't work ? I tried to put it (code to write message) inside last if but nothing. I know payment works because i can read the request to my endpoint and status code 200 (OK).

I hope you can help me...

Borja
  • 3,359
  • 7
  • 33
  • 66
  • Well the most simple step to start "debugging" would be to check what that telegram API URL you got there returns when you call it directly in a private browser window or tab ... I guess that will probably just be an error of some sorts, because I highly doubt telegram would let you send messages by just providing a chat ID, and no additional form of authentication beyond that whatsoever. – 04FS Nov 20 '19 at 16:31
  • @04FS the strange thing is that telegram API URL is correct...if i paste that $url in browser (obviously inserting the real data of the bot and the chat_id) it writes message on my telegram group. So the problem is not about telegram method – Borja Nov 20 '19 at 16:34
  • @04FS no it is really so: D if you have a bot and you know the id of a chat (where the bot is admin) you can write the message without further authentication. I have a lot of bots and I know it's true. The problem in the case of this question is not about "how to write the message" – Borja Nov 20 '19 at 16:36
  • Well then you need to check what response you actually get when you make that API call from within your webhook setting - start by logging the return value of file_get_contents somewhere you can check it. (That might be just false - in that case, go read up on how to make the function return the actual response body in case of an error, it will not do that by default.) – 04FS Nov 20 '19 at 16:36
  • @04FS can you help me ? how can i save the response ? (i'm not really god programmer with php). – Borja Nov 20 '19 at 16:38
  • No. My "go read up on" was deliberately phrased this way. – 04FS Nov 20 '19 at 16:41
  • @04FS can you suggest me some question about my similiar problem ? – Borja Nov 20 '19 at 16:42
  • 1
    https://stackoverflow.com/questions/6040978/need-response-body-of-http-500-with-file-get-contents-php – 04FS Nov 20 '19 at 16:51

0 Answers0