<?php

namespace App\Http\Controllers;

use App\Models\Concepto;
use App\Models\Pago;
use Stripe\Stripe;
use Stripe\Checkout\Session;

class StripeController extends Controller
{
    public function pagar(Pago $pago)
    {
        $concepto = Concepto::where('id', $pago->concepto)->first();

        Stripe::setApiKey(config('services.stripe.secret'));

        $session = Session::create([
            'mode' => 'payment',
            'line_items' => [[
                'price_data' => [
                    'currency' => 'mxn',
                    'product_data' => [
                        'name' => $concepto->name,
                    ],
                    'unit_amount' => $pago->monto * 100,
                ],
                'quantity' => 1,
            ]],
            'success_url' => route('pagar.show',1),
            'cancel_url' => route('pagar.show',0),
            'metadata' => [
                'pago_id' => $pago->id,
            ],
        ]);

        return redirect()->away($session->url);
    }

    public function show($status)
    {
    if ($status == 1) {
        session()->flash('swal', [
            'icon'  => 'success',
            'title' => 'Pago en proceso',
            'text'  => 'Estamos validando tu pago'
        ]);
    } else {
        session()->flash('swal', [
            'icon'  => 'error',
            'title' => 'Pago cancelado',
            'text'  => 'No se realizó ningún cargo'
        ]);
    }

    return redirect()->route('pago.index');
    }

}
