all(); $transactionType = $data['method'] == 'cash' ? 'ریالی' : 'طلایی'; $customer_id = Customer::where('mobile', $request->mobile)->first()?->id; if (!$customer_id) { throw new UnprocessableEntityHttpException('کاربری با این شماره موبایل یافت نشد.'); } $data['customer_id'] = $customer_id; $latestPrice = Price::latest()->first(); $data['price'] = $latestPrice->price; $data['price_id'] = $latestPrice->id; $data['difference'] = 0; $data['service_charge_percentage'] = 0; $data['pay_at'] = now(); $data['sts'] = 2; $data['type'] = 2; $data['payment_sts'] = 4; $data['price_original'] = $data['price']; $data['total_price_original'] = $data['amount'] * ($data['price_original'] ?? 0); $data['payment_remind'] = 0; $data['fee'] = $data['difference']; $data['total_fee'] = $data['amount'] * $data['difference']; $myCustomer = Customer::find($customer_id); $ownerType = (int) ($myCustomer?->type ?? 2); $wallet_interface = new WalletInterface($customer_id, $ownerType); $wallet = $wallet_interface->wallet; $response = []; // === اینجا قبلاً return بود و همه چیز قطع می‌شد === if ($data['method'] == 'cash') { $response['cash'] = $this->processCashTransaction($data, $wallet, $wallet_interface, $myCustomer); } else if ($data['method'] == 'gold') { $response['gold'] = $this->processGoldTransaction($data, $wallet, $wallet_interface, $myCustomer); } $response['Notifications'] = $this->sendTransactionNotifications($data, $transactionType); return response()->json($response); } public function getWallet(Request $request) { $customer_id = Customer::where('mobile', $request->mobile)->first()?->id; if($customer_id && $customer_id !== null){ $wallet = Wallet::select(['owner','gold','cash']) ->where('owner', $customer_id) ->whereNull('fund_id') ->whereNull('product_id') ->first(); }else{ throw new UnprocessableEntityHttpException('کاربری با این شماره موبایل یافت نشد.'); } return $wallet; } //for orders private function processCashTransaction(&$data, $wallet, $wallet_interface,$myCustomer) { $response = []; // $data['price'] += $data['difference']; $data['total_price'] = $data['amount'] /** $data['price']*/; $data['payment_amount'] = $data['total_price']; // $data['service_charge'] = $data['service_charge_percentage'] * $data['amount']; // $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; // $data['amount'] -= $data['service_charge']; if ($wallet->cash < $data['payment_amount']) { throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است'); } // $data['order'] = $order = Order::create($data); new TransactionInterface($wallet_interface, [ "cash" => $data['payment_amount'], "gold" => 0, 'expire_at' => now(), 'active' => true, // 'order_id' => $order->id, 'start_at' => now(), ], 4, true, false); $response['1'] = 2; return $response; } private function processGoldTransaction(&$data, $wallet, $wallet_interface,$myCustomer) { // if ($data['amount'] < 0.06){ // throw new UnprocessableEntityHttpException('کمترین مقدار مجاز برای فروش 60 سوت است.'); // // } // $data['price'] -= $data['difference']; $montifyCompanyName = $this->Config('montify_name'); $data['total_price'] = $data['amount'] * $data['price']; $data['payment_amount'] = $data['total_price']; $data['service_charge'] = $data['service_charge_percentage'] * $data['amount']; $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; $data['total_price'] = $data['amount'] * $data['price'] -$data['service_charge_price']; $data['payment_amount'] = $data['total_price']; $data['payment_amount'] -= $data['service_charge_price']; if ($wallet->gold < $data['amount']) { throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است'); } $data['direction'] = 2; $data['description'] = "فروش {$data['amount']} گرم طلا برای $montifyCompanyName توسط کاربر {$myCustomer->name} با شماره موبایل {$myCustomer->mobile}"; $data['order'] = $order = Order::create($data); new TransactionInterface($wallet_interface, [ "gold" => $data['amount'], "cash" => 0, 'expire_at' => now(), 'active' => true, 'order_id' => $order->id ], 10, false, true); } private function sendTransactionNotifications(array $data, string $transactionType) { $customer = Customer::findOrFail($data['customer_id']); $companyName = $this->Config('name_fa'); $montifyCompanyName = $this->Config('montify_name'); if($data['method'] == 'cash') { $message = "کاربر {$customer->name} با شماره موبایل {$customer->mobile} اقدام به برداشت موجودی از طرف $montifyCompanyName به مقدار " . "{$data['amount']} ریال " . " کرد. با تشکر، $companyName"; }elseif ($data['method'] == 'gold') { $message = "کاربر {$customer->name} با شماره موبایل {$customer->mobile} اقدام به $transactionType طلا به مقدار " . "{$data['amount']} گرم به مبلغ " . number_format($data['total_price']) . " ریال کرد. با تشکر، $companyName"; } User::where('send_sms', 1)->get()->each(function ($admin) use ($message, $data, $transactionType, $companyName) { if (self::isSmsNotifEnabled('sms_notif_fund_withdraw_admin')) { $this->send($admin->mobile, $message); } }); // User::whereNotNull('telegram')->get()->each(function ($admin) use ($message, $data, $transactionType, $companyName) { // $this->SendTelegram($admin->telegram, $message); // }); // $messageold = "کاربر {$customer->name} با شماره موبایل {$customer->mobile} اقدام به $transactionType طلا به مقدار " . // "{$data['amount']} گرم به مبلغ " . number_format($data['total_price']) . // " ریال کرد. با تشکر، $companyName"; // $azOrTa = $data['direction'] == 1 ? 'از' : 'به'; // $messageExam = " // {$transactionType} قلک طلا $azOrTa شما // {$data['amount']} گرم // در مظنه // " . number_format($data['price_original']) . " ریال"; // // //// User::whereNotNull('telegram')->get()->each(function ($admin) use ($messageExam) { //// $this->SendTelegram($admin->telegram, $messageExam); //// }); // User::where('send_sms', 1)->get()->each(function ($admin) use ($messageExam) { // $this->send($admin->mobile, $messageExam); // }); $wallet = Wallet::where('owner', $data['customer_id']) ->whereNull('fund_id') ->whereNull('product_id') ->first(); if($data['method'] == 'cash') { $pricePerGram = $data['total_price']; }elseif ($data['method'] == 'gold') { $pricePerGram = $data['total_price'] / $data['amount']; } $goldBalance = $wallet ? ($wallet->gold ?? 0) : 0; $cashBalance = $wallet ? ($wallet->cash ?? 0) : 0; if($data['method'] == 'cash') { $customerMessage = "$companyName\nبرداشت ریالی \nمبلغ: {$data['amount']}\n" . "\nموجودی طلایی: {$goldBalance}\n" . "مانده ریالی: " . number_format($cashBalance) . "\n" . $this->Config('app_url'); }elseif ($data['method'] == 'gold') { $customerMessage = "$companyName\nبرداشت طلایی \nگرم: {$data['amount']}\nقیمت گرم: " . number_format($pricePerGram) . "\nمبلغ کل: " . number_format($data['total_price']) . "\nموجودی طلایی: {$goldBalance}\n" . "مانده ریالی: " . number_format($cashBalance) . "\n" . $this->Config('app_url'); } // $customerMessage = "$companyName\n$transactionType آبشده\nگرم: {$data['amount']}\nقیمت گرم: " . number_format($pricePerGram) . // "\nمبلغ کل: " . number_format($data['total_price']) . "\n\nموجودی طلایی: {$wallet->gold}\n" . // "مانده ریالی: " . number_format($wallet->cash) . "\n" . $this->Config('app_url'); if (self::isSmsNotifEnabled('sms_notif_fund_withdraw_customer')) { $this->send($customer->mobile, $customerMessage); } } }