<?php

namespace App\Http\Controllers;

use App\Models\Order;
use App\Models\Transaction;
use Illuminate\Http\Request;

class PaypalPaymentController extends Controller
{

    /**
     * @throws \Throwable
     */
    public function handlePayment()
    {
        return view('payment.make_payment');
    }
    public function paymentCancel()
    {
        return view('payment.payment_cancel');
    }

    public function paymentSuccess(Request $request)
    {
        return view('payment.payment_success');
    }

    public function payment_confirmation(Request $request)
    {

//amount: "5.00"
//date: "2021-09-24T11:07:35Z"
//id: "27C02156312801322"
//order_id: "3"
//status: "COMPLETED"

        $order = Order::where('id', $request->input('order_id'))->first();
        $data['payment_status'] = $request->input('status');
        $order->update($data);

        $amount = $order->amount;

        $payment['order_id'] = $order->id;
        $payment['transaction_id'] = $request->id;
        $payment['amount'] = $amount;
        $payment['status'] = $request->status;
        $payment['paid_amount'] = $request->amount;
        $payment['paypal_order_id'] = $request->order_id;
        $payment['create_time'] = $request->date;

        Transaction::create($payment);
        return response()->json(['msg'=>'success']);
    }
}
