braintree_api/app/Http/Controllers/WebhookController.php
2025-05-30 14:14:53 +02:00

103 lines
3.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\BraintreePayment;
use App\Models\BraintreePaymentRefund;
use App\Models\BraintreePaymentStatus;
use App\Models\OrdersStatusHistory;
use Braintree\Exception\InvalidSignature;
use Braintree\Gateway;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class WebhookController extends Controller
{
protected Gateway $gateway;
public function __construct(Gateway $gateway)
{
$this->gateway = $gateway;
}
public function testWebhook(Request $request): \Illuminate\Http\JsonResponse
{
try {
$sampleNotification = $this->gateway->webhookTesting()->sampleNotification(
$request->input('webhook_kind'),
$request->input('transaction_id'),
);
// Simulate receiving the webhook directly
$webhookRequest = new Request([
'bt_signature' => $sampleNotification['bt_signature'],
'bt_payload' => $sampleNotification['bt_payload'],
]);
// Call your actual webhook handler
$this->handle($webhookRequest);
return response()->json(['success' => true]);
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
}
/**
* @throws InvalidSignature
*/
public function handle(Request $request): Response|ResponseFactory
{
$webhookNotification = $this->gateway->webhookNotification()->parse(
$request->input('bt_signature'),
$request->input('bt_payload')
);
Log::channel('braintree')->info('Webhook ', ['kind' => $webhookNotification->kind]);
Log::channel('braintree')->info('Webhook ', ['timestamp' => $webhookNotification->timestamp]);
Log::channel('braintree')->info('Webhook ', ['transaction' => $webhookNotification->transaction ?? null]);
Log::channel('braintree')->info('Webhook ', ['subscription' => $webhookNotification->subscription ?? null]);
Log::channel('braintree')->info('', []);
$webhookTrans = $webhookNotification->subject['transaction'];
$status = '';
if($refund = BraintreePaymentRefund::where('refund_id', $webhookTrans['id'])->first()){
$origTrans = BraintreePayment::where('transaction_id', $refund->transaction_id)->first();
$status = 'refund_';
} else {
$origTrans = BraintreePayment::where('transaction_id', $webhookTrans['id'])->first();
}
switch ($webhookNotification->kind) {
case 'transaction_settled': {
$status = $status . 'settled';
}break;
case 'transaction_disbursed': {
$status = $status . 'disbursed';
} break;
case 'transaction_settlement_declined': {
$status = $status . 'settlement_declined';
}
}
$this->updateStatusDb($origTrans, $status);
return response('', 200);
}
public function updateStatusDb($transaction, $status) {
BraintreePaymentStatus::create([
'transaction_id'=>$transaction->transaction_id,
'status_name'=>$status,
]);
OrdersStatusHistory::create([
'orders_id'=>$transaction->orders_id,
'orders_status_id'=> 44,
'customer_notified'=>1,
'comments'=>$status,
'hidden'=>0,
'highlight'=>0,
'message_msg'=>'not yet analyzed',
'message_provider'=>'vat'
]);
}
}