<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\CardDataStripe;
use App\Models\Plan;
use Stripe\Stripe;
use Stripe\StripeClient;
use Stripe\Customer;

class StripePaymentController extends Controller
{
    public function create_customers(Request $request){

        try {
            Stripe::setApiKey(env('STRIPE_SECRET_KEY'));

            $get_data = $request->data;
            $customer = Customer::create([
                'name' => 'Faizan Khan',
                'email' => 'm.faizankhan4510@gmail.com',
                'source' => $get_data['id'], //token_id
            ]);

            $get_plan = Plan::where('id', 4)->first();

            CardDataStripe::updateOrcreate(
                [
                    'user_id' => 3,
                    'card_no' => $get_data['card']['last4'],
                ],
                [
                    'user_id' => 3,
                    'customer_id' => $customer->id,
                    'card_id' => $get_data['card']['id'],
                    'name' => $get_data['card']['name'],
                    'card_no' => $get_data['card']['last4'],
                    'brand' => $get_data['card']['brand'],
                    'month' => $get_data['card']['exp_month'],
                    'year' => $get_data['card']['exp_year'],
                ]
            );


            $stripe = new StripeClient(env('STRIPE_SECRET_KEY'));
            $charge_data = $stripe->charges->create([
              'amount' => 1*100,
              'currency' => 'usd',
              'customer' => $customer->id,
              'description' => 'One dollar charge',
            ]);

            dd($charge_data);

        } catch(\Exception $e) {
            return response()->json(['success' => false, 'msg' => $e->getMessage()]);
        }
    }

    protected function subscription_page(){
        return view('subscriptionpage');
    }
}
