如何在laravel中编写单个函数,该函数可用于不同的控制器或调度命令并访问不同的模型外观

时间:2017-12-05 19:14:14

标签: function cron scheduled-tasks laravel-5.5

下面我有这个公共功能,但是我必须在大约60个其他地方编写类似的代码,我不想重复自己,我希望能够编写一个单独的函数,这样我只需要改变我每次使用这个功能都是'Dailysaving ::'和'00:00:00'。请注意,我将创建此函数应该用于的其他几个计划命令。我该如何解决这个问题以及我应该在哪里放置我编写的函数以及如何从函数中访问不同的模型。提前感谢任何能帮助我的人。 公共函数句柄()     {

    $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';
        }
    }

}

1 个答案:

答案 0 :(得分:0)

据我了解,您正在尝试制作自定义辅助函数。

因此,您需要使用自己的功能创建helpers.php并按照以下答案中的说明进行操作:https://stackoverflow.com/a/28290359/5407558