Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.1 kB
1
Indexable
Never
<?php
namespace App\Services\Payments\Webhooks;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use App\Models\Invoice;

class RaiffaisenWebhook
{
    public function dispatch(Request $request)
    {
        // Extract all data from the POST request
        $MerchantID = $request['MerchantID'];
        $TerminalID = $request['TerminalID'];
        $OrderID = $request['OrderID'];
        $Delay = $request['Delay'];
        $PurchaseTime = $request['PurchaseTime'];
        $TotalAmount = $request['TotalAmount'];
        $AltTotalAmount = $request["AltTotalAmount"];
        $CurrencyID = $request['Currency'];
        $AltCurrencyID = $request['AltCurrency'];
        $XID = $request['XID'];
        $SD = $request['SD'];
        $TranCode = $request['TranCode'];
        $ApprovalCode = $request['ApprovalCode'];
        $signature = $request["Signature"];


        // Reconstruct the data string as per UPC's format
        $dataString = "$MerchantID;$TerminalID;$PurchaseTime;$OrderID;$CurrencyID;$TotalAmount;$SD;";
        $dataString = $MerchantID . ";" . $TerminalID . ";" . $PurchaseTime . ";" . $OrderID . ";" . $XID . ";" . $CurrencyID . ";" . $TotalAmount . ";" . $SD . ";" . $TranCode . ";" . $ApprovalCode . ";";


        if ($this->verifySignature($dataString, $signature)) {
            $this->updatePaymentStatus($OrderID);
        } else {
            Log::error('UPC Webhook: Signature Verification Failed');
        }
    }

    protected function verifySignature($data, $signature)
    {
        $decodedSignature = base64_decode($signature);

        Log::info($data);

        $publicKey = openssl_pkey_get_public(file_get_contents(Storage::path('1758006.pub')));

        return openssl_verify($data, $decodedSignature, $publicKey, OPENSSL_ALGO_SHA256) == 1;
    }

    protected function updatePaymentStatus($data)
    {
        $invoice = Invoice::where('gateway_data->order_id', $data)->first();
        if ($invoice) {
            $invoice->assignStatus('PAID');
        }
    }

}
Leave a Comment