Controller Home For Exchange

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.7 kB
9
Indexable
Never
 public function exchange(Request $request)
    {
        session()->forget('Track');

        $receive = Currency::find($request->receive);
        $send = Currency::find($request->send);

       

        if ($receive == null) {
            $notify[] = ['error', 'Select any method that we send u the money'];
            return back()->withNotify($notify);
        }

        if ($send == null) {
            $notify[] = ['error', 'Select any method that we get money'];
            return back()->withNotify($notify);
        }

        $field = json_decode($receive->user_input);

        $validate_array = [
            'send' => 'required|numeric',
            'send_amount' => 'required|numeric|gt:0',
            'receive' => 'required|numeric',
            'receive_amount' => 'required|numeric|gt:0',

        ];
        foreach ($field as $value) {
            if (strtolower($value->type) === 'email') {
                $validate_array[$value->field_name] = "sometimes|{$value->validation}|email";
                continue;
            }

            $validate_array[$value->field_name] = "sometimes|{$value->validation}";
        }

        $this->validate($request, $validate_array);


        // new Calculation for covert amount 

        $percentCharge = ($request->send_amount * $send->percent_charge) / 100;

        $totalCharge = $percentCharge + $send->fixed_charge;

        $totalSendAmount = $request->send_amount - $totalCharge;

        $sendAmountConvertInBaseCurrency =  $totalSendAmount * $send->buy_at;

        $userReceiveAmount = $sendAmountConvertInBaseCurrency / $receive->sell_at;