<?php

namespace App\Http\Controllers;

use App\Models\Pay_ref;
use Illuminate\Http\Request;

class PayRefController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function show($merchantRequestID)
    {
        $mpesaref= Pay_ref::where('merchantRequestID', $merchantRequestID)->first();
        return response()->json([
            'success' => true,
            'data' => $mpesaref
        ], 200);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $merchant = $request['Body']['stkCallback']['MerchantRequestID'];
        $checkout = $request['Body']['stkCallback']['CheckoutRequestID'];
        $amount= $request['Body']['stkCallback']['CallbackMetadata']['Item'][0]['Value'];
        $mpesaref = $request['Body']['stkCallback']['CallbackMetadata']['Item'][1]['Value'];

        $pay = Pay_ref::create([
            'merchantRequestID' => $merchant,
            'checkoutRequestID' => $checkout,
            'mpesaref' => $mpesaref,
            'amount'=> $amount
        ]);

        return response()->json([
            'success' => true,
            'message' => 'Payment created successfully',
        ], 201);
    }

}
