$reqParams = []; $reqParams['_token'] = $request->_token; $reqParams['phone'] = $request->phone; $reqParams['wallet_id_name'] = $request->wallet_id_name; $reqParams['user_id'] = $request->user_id; $reqParams['wallet_id'] = $request->wallet_id; $reqParams['currency_id_name'] = $request->currency_id_name; $reqParams['currency_id'] = $request->currency_id; $reqParams['service_charge'] = $request->service_charge; $reqParams['product_id'] = $product->id; $reqParams['sender_amount'] = $request->sender_amount; $reqParams['receiver_amount'] = $request->receiver_amount; $priceParams = []; $priceParams['wallet_initial_balance'] = $wallet_payer->wallet_balance(); $priceParams['wallet_after_balance'] = $wallet_payer->wallet_balance() - Auth::user()->fromBasicCurrency($request->sender_amount, $wallet_payer->country_currency); $priceParams['wallet_currency_exr'] = $wallet_payer->country_currency->exchange_rate; $priceParams['wallet_currency'] = $wallet_payer->country_currency->code; $priceParams['product_currency'] = Auth::user()->myBasicCurrency()->code; $priceParams['price'] = $request->sender_amount; $priceParams['selling_rate'] = Auth::user()->priceToComissionedPrice($product->forcedPrice($user_payer->id), $request->user_id); $priceParams['selling_price'] = $request->sender_amount; $priceParams['selling_price_pyrwlt'] = Auth::user()->fromBasicCurrency($request->sender_amount, $wallet_payer->country_currency); $priceParams['agent_price_pyrwlt'] = Auth::user()->fromBasicCurrency($the_cost, $wallet_payer->country_currency); $priceParams['agent_price'] = $the_cost; $priceParams['base_cost'] = $product->cost; $priceParams['force_price'] = $product->forcedPrice($user_payer->id); $priceParams['force_price_min'] = $product->forcedPriceMin($user_payer->id); $priceParams['force_price_max'] = $product->forcedPriceMax($user_payer->id); $priceParams['base_price'] = $product->price; $priceParams['base_price_min'] = $product->price_min; $priceParams['base_price_max'] = $product->price_max; $priceParams['base_rcv_min'] = $product->ranking; $priceParams['base_rcv_max'] = $product->priority; $priceParams['cost'] = $base_cost; $priceParams['admin_price'] = $admin_price; $priceParams['forced_price'] = $forced_price; $priceParams['commission_amount'] = $product->commission_amount; $priceParams['discount_amount'] = $product->discount_amount; $priceParams['service_charge'] = $request->service_charge; $priceParams['sender_amount'] = $request->sender_amount; $priceParams['receiver_amount'] = $request->receiver_amount; $purchase_trx = PurchaseTransaction::create([ 'type' => 'recharge', 'category' => 'transaction', 'api_provider' => $product->category, 'status' => 'initiated', 'start_point' => $product->id, 'end_point' => '', 'start_snap' => json_encode($product), 'end_snap' => '', 'parameters' => json_encode($priceParams), 'priority' => $request->wallet_id, 'note' => $random_ext_id, 'request' => json_encode($reqParams), 'response' => '', 'created_by' => Auth::user()->id, 'updated_by' => Auth::user()->id, ]); New Transaction $fillable public $fillable = [ 'purchase_id', 'purchase_trx', 'method', 'category', 'type', 'status', 'api_provider_id', 'api_provider', 'note', 'details', 'trx_time', 'transaction_id', 'trx_number', 'trx_id', 'trx_group', 'trx_sequence', 'trx_priority', 'phone_number', 'customer_id', 'customer_name', 'customer_email', 'customer_address', 'customer_city', 'customer_state', 'customer_zip', 'customer_country', 'customer_ip', 'customer_note', 'merchant_id', 'merchant_name', 'merchant_reference', 'merchant_details', 'merchant_ip', 'merchant_note', 'agent_id', 'agent_name', 'agent_reference', 'agent_details', 'agent_ip', 'agent_note', 'cm_name', 'cm_ip', 'cm_note', 'api_provider_name', 'api_provider_ip', 'api_provider_address', 'api_provider_note', 'purchase_transaction_id', 'purchase_transaction_number', 'purchase_transaction_status', 'purchase_transaction_date', 'purchase_transaction_amount', 'purchase_transaction_currency', 'purchase_transaction_type', 'purchase_transaction_method', 'purchase_transaction_note', 'wallet_id', 'wallet_name', 'wallet_reference', 'wallet_country_currency', 'wallet_note', 'previous_balance', 'wallet_new_balance', 'wallet_trx_type', 'wallet_account_number', 'product_id', 'product_name', 'product_sku', 'product_code', 'product_description', 'product_category', 'product_price', 'product_country_currency', 'product_type', 'product_status', 'product_image', 'product_dimensions', 'product_qty', 'product_stock', 'product_tags', 'base_currency_id', 'base_currency_name', 'base_currency_multiplier', 'admin_currency_id', 'admin_currency_name', 'admin_currency_multiplier', 'agent_currency_id', 'agent_currency_name', 'agent_currency_multiplier', 'merchant_currency_id', 'merchant_currency_name', 'merchant_currency_multiplier', 'admin_product_cost', 'admin_commission', 'admin_product_price', 'agent_commission', 'agent_commission_paid', 'agent_price', 'merchant_commission', 'merchant_commission_paid', 'merchant_price', 'service_charge', 'from_amount', 'to_amount', 'wallet_initial_balance', 'wallet_after_balance', 'wallet_currency_exr', 'wallet_currency', 'product_currency', 'price', 'selling_rate', 'selling_price', 'selling_price_pyrwlt', 'agent_price_pyrwlt', 'base_cost', 'force_price', 'force_price_min', 'force_price_max', 'base_price', 'base_price_min', 'base_price_max', 'base_rcv_min', 'base_rcv_max', 'cost', 'admin_cost', 'admin_price', 'forced_price', 'commission_amount', 'discount_amount', 'sender_amount', 'receiver_amount', 'final_price', 'user_id', 'user_name', 'user_email', 'user_phone', 'user_country', 'user_address', 'user_note', 'user_ip', 'user_agent', 'user_browser', 'user_os', 'user_device' ]; this is the code how a PurchaseTransaction is created. Now write me a code to create a New Transaction from a PurchaseTransaction with json_decode when necessary as well as call Merchant, User, Agent where necessary. // Assuming $purchase_trx is the created PurchaseTransaction instance // Decode parameters and request if needed $trxParams = json_decode($purchase_trx->parameters, true); $reqParams = json_decode($purchase_trx->request, true); // Fetch related models $user = \App\Models\User::find($reqParams['user_id'] ?? null); $agent = isset($reqParams['agent_id']) ? \App\Models\Agent::find($reqParams['agent_id']) : null; $merchant = isset($reqParams['merchant_id']) ? \App\Models\Merchant::find($reqParams['merchant_id']) : null; $product = \App\Models\Product::find($reqParams['product_id'] ?? null); // Prepare data for New Transaction (all $fillable fields) $newTransactionData = [ 'purchase_id' => $purchase_trx->purchase_id ?? null, 'purchase_trx' => $purchase_trx->id, 'method' => $purchase_trx->method ?? null, 'category' => $purchase_trx->category, 'type' => $purchase_trx->type, 'status' => $purchase_trx->status, 'api_provider_id' => $purchase_trx->api_provider_id ?? null, 'api_provider' => $purchase_trx->api_provider, 'note' => $purchase_trx->note, 'details' => $purchase_trx->details ?? null, 'trx_time' => $purchase_trx->trx_time ?? null, 'transaction_id' => $purchase_trx->transaction_id ?? null, 'trx_number' => $purchase_trx->trx_number ?? null, 'trx_id' => $purchase_trx->trx_id ?? null, 'trx_group' => $purchase_trx->trx_group ?? null, 'trx_sequence' => $purchase_trx->trx_sequence ?? null, 'trx_priority' => $purchase_trx->trx_priority ?? null, 'phone_number' => $reqParams['phone'] ?? null, 'customer_id' => $reqParams['customer_id'] ?? null, 'customer_name' => $reqParams['customer_name'] ?? null, 'customer_email' => $reqParams['customer_email'] ?? null, 'customer_address' => $reqParams['customer_address'] ?? null, 'customer_city' => $reqParams['customer_city'] ?? null, 'customer_state' => $reqParams['customer_state'] ?? null, 'customer_zip' => $reqParams['customer_zip'] ?? null, 'customer_country' => $reqParams['customer_country'] ?? null, 'customer_ip' => $reqParams['customer_ip'] ?? null, 'customer_note' => $reqParams['customer_note'] ?? null, 'merchant_id' => $merchant->id ?? ($reqParams['merchant_id'] ?? null), 'merchant_name' => $merchant->name ?? ($reqParams['merchant_name'] ?? null), 'merchant_reference' => $reqParams['merchant_reference'] ?? null, 'merchant_details' => $reqParams['merchant_details'] ?? null, 'merchant_ip' => $reqParams['merchant_ip'] ?? null, 'merchant_note' => $reqParams['merchant_note'] ?? null, 'agent_id' => $agent->id ?? ($reqParams['agent_id'] ?? null), 'agent_name' => $agent->name ?? ($reqParams['agent_name'] ?? null), 'agent_reference' => $reqParams['agent_reference'] ?? null, 'agent_details' => $reqParams['agent_details'] ?? null, 'agent_ip' => $reqParams['agent_ip'] ?? null, 'agent_note' => $reqParams['agent_note'] ?? null, 'cm_name' => $reqParams['cm_name'] ?? null, 'cm_ip' => $reqParams['cm_ip'] ?? null, 'cm_note' => $reqParams['cm_note'] ?? null, 'api_provider_name' => $reqParams['api_provider_name'] ?? null, 'api_provider_ip' => $reqParams['api_provider_ip'] ?? null, 'api_provider_address' => $reqParams['api_provider_address'] ?? null, 'api_provider_note' => $reqParams['api_provider_note'] ?? null, 'purchase_transaction_id' => $purchase_trx->id, 'purchase_transaction_number' => $purchase_trx->trx_number ?? null, 'purchase_transaction_status' => $purchase_trx->status, 'purchase_transaction_date' => $purchase_trx->created_at, 'purchase_transaction_amount' => $trxParams['price'] ?? null, 'purchase_transaction_currency' => $trxParams['wallet_currency'] ?? null, 'purchase_transaction_type' => $purchase_trx->type, 'purchase_transaction_method' => $purchase_trx->method ?? null, 'purchase_transaction_note' => $purchase_trx->note, 'wallet_id' => $reqParams['wallet_id'] ?? null, 'wallet_name' => $reqParams['wallet_id_name'] ?? null, 'wallet_reference' => $reqParams['wallet_reference'] ?? null, 'wallet_country_currency' => $reqParams['wallet_country_currency'] ?? null, 'wallet_note' => $reqParams['wallet_note'] ?? null, 'previous_balance' => $trxParams['wallet_initial_balance'] ?? null, 'wallet_new_balance' => $trxParams['wallet_after_balance'] ?? null, 'wallet_trx_type' => $reqParams['wallet_trx_type'] ?? null, 'wallet_account_number' => $reqParams['wallet_account_number'] ?? null, 'product_id' => $product->id ?? ($reqParams['product_id'] ?? null), 'product_name' => $product->name ?? ($reqParams['product_name'] ?? null), 'product_sku' => $product->sku ?? ($reqParams['product_sku'] ?? null), 'product_code' => $product->code ?? ($reqParams['product_code'] ?? null), 'product_description' => $product->description ?? ($reqParams['product_description'] ?? null), 'product_category' => $product->category ?? ($reqParams['product_category'] ?? null), 'product_price' => $product->price ?? ($trxParams['price'] ?? null), 'product_country_currency' => $product->country_currency ?? ($trxParams['wallet_currency'] ?? null), 'product_type' => $product->type ?? ($reqParams['product_type'] ?? null), 'product_status' => $product->status ?? ($reqParams['product_status'] ?? null), 'product_image' => $product->image ?? ($reqParams['product_image'] ?? null), 'product_dimensions' => $product->dimensions ?? ($reqParams['product_dimensions'] ?? null), 'product_qty' => $product->qty ?? ($reqParams['product_qty'] ?? null), 'product_stock' => $product->stock ?? ($reqParams['product_stock'] ?? null), 'product_tags' => $product->tags ?? ($reqParams['product_tags'] ?? null), 'base_currency_id' => $reqParams['base_currency_id'] ?? null, 'base_currency_name' => $reqParams['base_currency_name'] ?? null, 'base_currency_multiplier' => $reqParams['base_currency_multiplier'] ?? null, 'admin_currency_id' => $reqParams['admin_currency_id'] ?? null, 'admin_currency_name' => $reqParams['admin_currency_name'] ?? null, 'admin_currency_multiplier' => $reqParams['admin_currency_multiplier'] ?? null, 'agent_currency_id' => $reqParams['agent_currency_id'] ?? null, 'agent_currency_name' => $reqParams['agent_currency_name'] ?? null, 'agent_currency_multiplier' => $reqParams['agent_currency_multiplier'] ?? null, 'merchant_currency_id' => $reqParams['merchant_currency_id'] ?? null, 'merchant_currency_name' => $reqParams['merchant_currency_name'] ?? null, 'merchant_currency_multiplier' => $reqParams['merchant_currency_multiplier'] ?? null, 'admin_product_cost' => $reqParams['admin_product_cost'] ?? null, 'admin_commission' => $reqParams['admin_commission'] ?? null, 'admin_product_price' => $reqParams['admin_product_price'] ?? null, 'agent_commission' => $reqParams['agent_commission'] ?? null, 'agent_commission_paid' => $reqParams['agent_commission_paid'] ?? null, 'agent_price' => $trxParams['agent_price'] ?? null, 'merchant_commission' => $reqParams['merchant_commission'] ?? null, 'merchant_commission_paid' => $reqParams['merchant_commission_paid'] ?? null, 'merchant_price' => $reqParams['merchant_price'] ?? null, 'service_charge' => $trxParams['service_charge'] ?? null, 'from_amount' => $reqParams['from_amount'] ?? null, 'to_amount' => $reqParams['to_amount'] ?? null, 'wallet_initial_balance' => $trxParams['wallet_initial_balance'] ?? null, 'wallet_after_balance' => $trxParams['wallet_after_balance'] ?? null, 'wallet_currency_exr' => $trxParams['wallet_currency_exr'] ?? null, 'wallet_currency' => $trxParams['wallet_currency'] ?? null, 'product_currency' => $trxParams['product_currency'] ?? null, 'price' => $trxParams['price'] ?? null, 'selling_rate' => $trxParams['selling_rate'] ?? null, 'selling_price' => $trxParams['selling_price'] ?? null, 'selling_price_pyrwlt' => $trxParams['selling_price_pyrwlt'] ?? null, 'agent_price_pyrwlt' => $trxParams['agent_price_pyrwlt'] ?? null, 'base_cost' => $trxParams['base_cost'] ?? null, 'force_price' => $trxParams['force_price'] ?? null, 'force_price_min' => $trxParams['force_price_min'] ?? null, 'force_price_max' => $trxParams['force_price_max'] ?? null, 'base_price' => $trxParams['base_price'] ?? null, 'base_price_min' => $trxParams['base_price_min'] ?? null, 'base_price_max' => $trxParams['base_price_max'] ?? null, 'base_rcv_min' => $trxParams['base_rcv_min'] ?? null, 'base_rcv_max' => $trxParams['base_rcv_max'] ?? null, 'cost' => $trxParams['cost'] ?? null, 'admin_cost' => $trxParams['admin_cost'] ?? null, 'admin_price' => $trxParams['admin_price'] ?? null, 'forced_price' => $trxParams['forced_price'] ?? null, 'commission_amount' => $trxParams['commission_amount'] ?? null, 'discount_amount' => $trxParams['discount_amount'] ?? null, 'sender_amount' => $trxParams['sender_amount'] ?? null, 'receiver_amount' => $trxParams['receiver_amount'] ?? null, 'final_price' => $trxParams['final_price'] ?? null, 'user_id' => $user->id ?? ($reqParams['user_id'] ?? null), 'user_name' => $user->name ?? ($reqParams['user_name'] ?? null), 'user_email' => $user->email ?? ($reqParams['user_email'] ?? null), 'user_phone' => $user->phone ?? ($reqParams['user_phone'] ?? null), 'user_country' => $user->country ?? ($reqParams['user_country'] ?? null), 'user_address' => $user->address ?? ($reqParams['user_address'] ?? null), 'user_note' => $reqParams['user_note'] ?? null, 'user_agent' => $reqParams['user_agent'] ?? null, 'user_ip' => $reqParams['user_ip'] ?? null, 'user_browser' => $reqParams['user_browser'] ?? null, 'user_os' => $reqParams['user_os'] ?? null, 'user_device' => $reqParams['user_device'] ?? null, ]; // Create the New Transaction $newTransaction = \App\Models\NewTransaction::create($newTransactionData);