samedi 11 août 2018

Laravel how to extract this controller code

In my laravel controller function I need to do a stripe payment and call a bunch for jobs. The only problem is that there are differenc scenarios so I dont want to retype the exact same code in my controller function.

How can I extract the code bellow and put it in another file:

try {
                $charge = Charge::create([
                  'amount' => $amountToPay * 100,
                  'currency' => 'usd',
                  'description' => $iteminfo->desc,
                  'source' => $srcId,
                  'metadata' => $meta,
]);

            } catch (\Exception $e) {
                Log::error('err log: '.$e);

                //Set to active
                Item::withoutSyncingToSearch(function () use ($dId) {
                  Item::where('id', $dId)->update(['is_active' => 1]);
                });

                return response()->json(['errors' => [trans('api.stripePayFail')]], 422);
            }

            try {
                dispatch(new InsertS(1, $amountToPay, $item));
            } catch (\Exception $e) {
                Log::error('err log: '.$e);
            }

            try {
                dispatch(new DeleteItemOcg($Id));
            } catch (\Exception $e) {
                Log::error('err log: '.$e);
            }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire