<?php namespace App\Http\Controllers; use App\Store; use App\StoreAmountGeneration; use Illuminate\Http\Request; use Ramsey\Uuid\Uuid; use DB; class StoreAmountGenerationController extends Controller { protected $request; protected $store; protected $response; public function generateTransaction(Request $request) { $this->validate($request, [ 'amount' => 'required', 'store_id' => 'required' ], [ 'store_id.required' => 'O Campo store_id é Obrigatório', 'amount.required' => 'O Campo amount é Obrigatório', ]); if ($request->isJson()) { $checkStore = Store::query() ->join('user_clients', 'user_clients.id', '=', 'stores.user_client_id') ->join('merchant_accounts', 'merchant_accounts.id', '=', 'stores.merchant_account_id') ->where('stores.public_id', $request->store_id) ->where('stores.user_client_id', $request->user()->id) ->select('stores.*', 'merchant_accounts.institution') ->first(); if ($checkStore) { if (is_numeric($request->amount)) { $res = StoreAmountGeneration::create([ 'transaction' => Uuid::uuid4(), 'amount' => $request->amount, 'store_id' => $checkStore->id, 'user_client_id' => $request->user()->id ]); return response()->json(['message' => 'Transacção Gerada com Sucesso', 'transaction' => $res->transaction, 'account_number' => $checkStore->account_number, 'address_store' => $checkStore->address, 'institution' => $checkStore->institution], 201); } else { return response()->json(['message' => 'Introduza um montante válido'], 400); } } else return response()->json(['message' => 'Loja Inválida'], 400); } else { return response()->json(['message' => 'Erro nos dados'], 400); } } public function generateTransactionStore(Request $request) { $this->validate($request, [ 'amount' => 'required', 'store_id' => 'required' ], [ 'store_id.required' => 'O Campo store_id é Obrigatório', 'amount.required' => 'O Campo amount é Obrigatório', ]); $checkStore = Store::query() ->join('merchant_accounts', 'merchant_accounts.id', '=', 'stores.merchant_account_id') ->where('stores.id', '=', $request->user()->id) ->select('stores.*', 'merchant_accounts.institution') ->first(); if ($checkStore) { if (is_numeric($request->amount)) { $this->request = $request; $this->store = $checkStore; DB::transaction(function () { $transaction = Uuid::uuid4(); DB::table('payments')->insert([ 'transaction_id' => $transaction, 'amount' => $this->request->amount, 'store_id' => $this->store->id, 'firebase_token' => $this->request->firebase_token, 'status' => 'pending', 'estado' => 'pending', 'created_at' => now(), 'updated_at' => now() ]); $res = DB::table('payments')->where('transaction_id', $transaction)->first(); $this->response = $res; }, 2); return response()->json(['message' => trans('transaction_generated'), 'transaction' => $this->response->transaction_id, 'account_number' => $checkStore->account_number, 'address_store' => $checkStore->address, 'institution' => $checkStore->institution, 'firebase_token' => $this->response->firebase_token], 201); } else { return response()->json(['message' => trans('insert_valid_amount')], 400); } } else return response()->json(['message' => trans('invalid_store')], 400); } }