0

I have this public function below but I will have to write a similar code in about 60 other places, I don't want to repeat myself rather, I want to be able to write a single function such that all I need change is 'Dailysaving::', and '00:00:00' each time I use the function. And please note that I will be creating several other schedule commands which this function should work for. How do I go about this please and where am I supposed to place the function I write And how do I access different models from the function. Thanks in advance for anyone that will help me out. public function handle() {

    $users= Dailysaving::where('debit_time', '00:00:00')->where('status', 'Active')->get();
    //die($users);
    foreach ($users as $user) {
        $email = $user->email;
        $amount = $user->amount_daily * 100;
        //Let's know where the payment is on the db
        $user_id = $user->user_id;
        $savings_id = $user->id;
        $auth_code= $user->authorization_code;
        //

        $metastring = '{"custom_fields":[{"user_id":'. $user_id. '}, {"action": "activatedaily"},{"savings_id": '.$savings_id.'},{"savingstype": "dailysavings"}]}';
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://api.paystack.co/transaction/charge_authorization",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode([
                'amount'=>$amount,
                'email'=>$email,
                'authorization_code' =>$auth_code,
                'metadata' => $metastring,
            ]),
            CURLOPT_HTTPHEADER => [
                "authorization:Bearer sk_test_656456h454545",
                "content-type: application/json",
                "cache-control: no-cache"
            ],
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);


        if($err){

            $failedtranx = new Failedtransaction;

            $failedtranx->error = $err;
            $failedtranx->save();
        }
        if($response) {

            $tranx = json_decode($response);

            if (!$tranx->status) {

                // there was an error contacting the Paystack API
                //register in failed transaction table
                $failedtranx = new Failedtransaction;

                $failedtranx->error = $err;
                $failedtranx->save();
            }


            if ('success' == $tranx->data->status) {


                $auth_code = $tranx->data->authorization->authorization_code;
                $amount = ($tranx->data->amount) / 100;
                $last_transaction = $tranx->data->transaction_date;

                $payment_ref = $tranx->data->reference;


                $record = new Transactionrecord;

                $record->payment_ref = $payment_ref;
                $record->save();
                //saving complete
                //die('saved');

                $item = Dailysaving::find($savings_id);
                $total_deposit = $item->total_deposit + $amount;
                $item->total_deposit = $total_deposit;
                $item->last_transaction_date = date('Y-m-d H:i:s');
                $target = $item->day_target;
                if ($target == $total_deposit) {
                    $item->status = 'Completed';
                }

                $item->save();


            }

            echo 'done';
        }
        else{
            echo 'failed';
        }
    }

}
mykoman
  • 1,715
  • 1
  • 19
  • 33

1 Answers1

0

As I understand you are trying to make custom helper functions.

So you need create helpers.php with your functions and follow instructions in the below answer: https://stackoverflow.com/a/28290359/5407558

Makashov Nurbol
  • 574
  • 3
  • 13