i have this handleWebhook() function to receive coinbase webhook events
public function handleWebhook(Request $request)
{
try {
// Retrieve the webhook payload
$payload = file_get_contents("php://input");
$event = json_decode($payload, true);
if (!$event) {
return response()->json(['error' => 'Invalid JSON payload'], 400);
}
// Retrieve the transaction metadata
$metadata = $event['data']['metadata'];
// Retrieve the transaction charge ID from the metadata
$trx = $metadata['trx'];
$data = Deposit::where('trx', $trx)->orderBy('id', 'DESC')->first();
$coinbaseAccwebhook = $data->gateway->val2;
// Verify the signature
$headerSignature = $_SERVER['HTTP_X_CC_WEBHOOK_SIGNATURE'];
$isValidSignature = hash_hmac('sha256', $payload,$coinbaseAccwebhook ) === $headerSignature;
if (!$isValidSignature) {
return response()->json(['error' => 'Invalid signature'], 400);
}else{
// Retrieve the transaction hash ID, amount, and status from the webhook event
$transactionId = $event['data']['payments'][0]['transaction_id'];
$amount = $event['data']['pricing']['local']['amount'];
if ($event['type'] === 'charge:created') {
$payment = new Payment;
$payment['user_id'] = Auth::id();
$payment['gateway_id'] = $data->gateway->id;
$payment['trx'] = $transactionId;
$payment['amount'] =$data->amount;
$payment['dpamount'] =$amount;
$payment['trx_charge'] =$data->trx_charge;
$payment['status'] = "pending";
$payment->save();
}
if ($event['type'] === 'charge:confirmed') {
if ($data->status == 0) {
$data->status = 1;
$data->save();
$user = User::find($data->user_id);
$user->balance += $amount-($data->trx_charge);
$user->save();
}
Payment::where('trx', $transactionId)
->where('user_id', Auth::id())
->update(['status' => "confirmed"]);
}
return response()->json(['success' => ' signature'], 200);
}
} catch (Exception $e) {
return response()->json(['error' => 'An error occurred while processing the webhook'], 500);
}
}
here is route
Route::post('/webhook/coinbase',[App\Http\Controllers\HomeController::class,'handleWebhook'])->name('charge.webhook');
here is exception csrf token for this route in verifyCsrfToken.php
protected $except = [
'/webhook/coinbase',
];
after that , i make send test in coinbasecommerce notification webhook endpoint URL .
it show error
Remote server at mydomain returned an HTTP 302
is it coinbase server side error or not. if not what happening in my laravel code .
I test this code in live nginx AWS EC2 server with https domain .
I want to receive coinbase commerce webhook event correctly .
Can someone help me.