<?php

namespace App\Repositories\Shop;

use App;
use Devpark\PayboxGateway\Requests\AuthorizationWithCapture;
use Devpark\PayboxGateway\Requests\Capture;
use Devpark\PayboxGateway\Responses\Verify;

class Paybox
{
    public static function makeAuthorizationRequest($amount, $customer_email = 'test@example.com')
    {
        $authorizationRequest = App::make(AuthorizationWithCapture::class);

        return $authorizationRequest->setAmount($amount)->setCustomerEmail($customer_email)
            ->setPaymentNumber(1)->send('paybox.send');
    }

    public static function verifyPayment($invoice_id)
    {
        $invoice = Invoices::get($invoice_id);
        $payboxVerify = App::make(Verify::class);
        try {
            $success = $payboxVerify->isSuccess($invoice->total_shipped);
            if ($success) {
                // process order here after making sure it was real payment
            }
            echo 'OK';
        } catch (InvalidSignature $e) {
            Log::alert('Invalid payment signature detected');
        }
    }

    public static function getPreviousAuthorizedRequest()
    {
        $payment = PaymentModel::find($idOfAuthorizedPayment);
        $captureRequest = App::make(Capture::class);
        $response = $captureRequest->setAmount($payment->amount)
            ->setPayboxCallNumber($payment->call_number)
            ->setPayboxTransactionNumber($payment->transaction_number)
            ->setDayRequestNumber(2)
            ->send();

        if ($response->isSuccess()) {
            // process order here
        }
    }
}
