<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Log;
use App\Models\Pembayaran;
use App\Models\Pembelian;
use App\Models\Layanan;
use App\Models\Kategori;


class DigiflazzCallbackController extends Controller
{

    public function handle(Request $request)
    {
        $secret = 'GENZ';
        $post_data = file_get_contents('php://input');
        $signature = hash_hmac('sha1', $post_data, $secret);

        if ($request->header('X-Hub-Signature') == 'sha1=' . $signature) {
            $data = json_decode($request->getContent(), true);
            $refId = $data['data']['ref_id'];
            $updateStatus = $data['data']['status'];
            $ser_n = $data['data']['sn'];

            if ($request->header('X-Digiflazz-Event') == 'update') {
                $invoice = Pembelian::where('provider_order_id', $refId)->where('status', 'Proses')->first();

                if ($invoice) {
                    $updateData = [
                        'status' => $updateStatus,
                        'log' => json_encode($data)
                    ];

                    if ($invoice->tipe_transaksi == 'voucher') {
                        $updateData['voucher'] = $ser_n;
                    }

                    $invoice->update($updateData);
                }

                $updatePesanan = Pembayaran::where('order_id', $invoice->order_id)->where('metode', 'SALDO')->first();

                if ($updatePesanan) {
                    $pesanSukses = "*Pembelian Sukses*\n\n" .
                        "No Invoice: *$invoice->order_id*\n" .
                        "Layanan: *$invoice->layanan*\n" .
                        "ID : *$invoice->user_id*\n" .
                        "Server : *$invoice->zone*\n" .
                        "Nickname : *$invoice->nickname*\n" .
                        "Harga: *Rp. " . number_format($invoice->harga, 0, '.', ',') . "*\n" .
                        "Status Pembelian: *Sukses*\n" .
                        "Metode Pembayaran: *$updatePesanan->metode*\n\n" .
                        "*Invoice* : " . env("APP_URL") . "/pembelian/invoice/$invoice->order_id\n\n" .
                        "INI ADALAH PESAN OTOMATIS";

                    $this->msg($updatePesanan->no_pembeli, $pesanSukses);
                }
            }
        }
    }

    public function msg($nomor, $msg)
    {
        $api = \DB::table('setting_webs')->where('id', 1)->first();
        $token = $api->wa_key;
        $data = [
            'target' => $nomor,
            'message' => $msg,
        ];

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => "https://api.fonnte.com/send",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => http_build_query($data),
            CURLOPT_HTTPHEADER => [
                "Authorization: $token",
            ],
        ]);

        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

}
