<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\PostbackRejectedException;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class NotikController extends Controller
{
    // api/notik?user_id=...&offer_id=...&offer_name=...&amount=...&conversion_ip=...&trx=...&secret=...
    public function callback(Request $request)
    {
        try {
            $trx = $this->verifyProviderPostback($request, 'Notik');

            $data = $request->all();
            $data['company'] = "Notik";
            $data['campaign_id'] = $data['offer_id'] ?? null;
            $data['campaign_name'] = $data['offer_name'] ?? null;
            $data['ip'] = $data['conversion_ip'] ?? null;
            $data['trx'] = $trx;

            $this->creditLead($data);
        } catch (PostbackRejectedException $e) {
            return response($e->getMessage(), $e->httpStatus);
        } catch (\Exception $e) {
            return response('NOT OK', 400);
        }

        return response('OK', 200);
    }
}
