I'm new to PHP development
I'm trying to learn by implementing some real projects for fun. So I tried to build a bitcoin app where customers can pay in cryptocurrency.
So I start with Coinbase commerce API
I successfully implement the charge page and everything is working well until I reached the point where I have to deal with WEBHOOKS 😔
I'm following this documentation
https://github.com/coinbase/coinbase-commerce-php/blob/master/README.md
And that's the WEBHOOKs code
`<?php
require_once __DIR__ . "/vendor/autoload.php";
use CoinbaseCommerce\Webhook;
/**
* To run this example please read README.md file
* Past your Webhook Secret Key from Settings/Webhook section
* Make sure you don't store your Secret Key in your source code!
*/
$secret = 'SECRET_KEY';
$headerName = 'X-Cc-Webhook-Signature';
$headers = getallheaders();
$signraturHeader = isset($headers[$headerName]) ? $headers[$headerName] : null;
$payload = trim(file_get_contents('php://input'));
try {
$event = Webhook::buildEvent($payload, $signraturHeader, $secret);
http_response_code(200);
echo sprintf('Successully verified event with id %s and type %s.', $event->id, $event->type);
} catch (\Exception $exception) {
http_response_code(400);
echo 'Error occured. ' . $exception->getMessage();
}
`
When I access to the we hooks URL I got this error
Error occured. Invalid payload provided. No JSON object could be decoded
Please 🙏 I want someone to explain to me this error
Thanks in advance.
Seems like you are making a GET (No payload data) request to a url that is expecting a POST (Has payload data) request from the web-hook.
To test API's with POST, PUT, GET requests, you can use tools like PostMan .
You can build JSON payloads manually and test your endpoints.
Try this
$headerName = 'x-cc-webhook-signature';
$signraturHeader = isset($headers[$headerName]) ? $headers[$headerName] : null;
instead of
$headerName = 'X-Cc-Webhook-Signature';
$signraturHeader = isset($headers[$headerName]) ? $headers[$headerName] : null;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.