175 lines
6.0 KiB
Plaintext
175 lines
6.0 KiB
Plaintext
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Domain\Points\Models\Point;
|
||
use App\Domain\Payments\Models\Withdrawal;
|
||
use App\Domain\Points\Enums\DirectionEnum;
|
||
use App\Domain\PaymentGateway\Services\QiwiService;
|
||
use App\Domain\PaymentGateway\Services\UnitpayService;
|
||
use App\Domain\PaymentGateway\Models\PaymentGatewayOrder;
|
||
use App\Domain\PaymentGateway\Services\InterkassaService;
|
||
use App\Domain\Subscriptions\Service\SubscriptionService;
|
||
|
||
class MoneyController extends Controller
|
||
{
|
||
|
||
public function unitpay()
|
||
{
|
||
$amount = filter_input(INPUT_GET, 'ik_am', FILTER_VALIDATE_INT);
|
||
|
||
if(empty($amount)){
|
||
return redirect()->back();
|
||
}
|
||
|
||
$order = PaymentGatewayOrder::qiwi($amount);
|
||
return redirect(QiwiService::payments_link($amount, $order, auth()->user()));
|
||
}
|
||
|
||
public function qiwiStatus($number)
|
||
{
|
||
|
||
$SECRET_KEY = 'eyJ2ZXJzaW9uIjoiUDJQIiwiZGF0YSI6eyJwYXlpbl9tZXJjaGFudF9zaXRlX3VpZCI6InAwOGxmMS0wMCIsInVzZXJfaWQiOiI3OTAyOTI0MDM2NCIsInNlY3JldCI6ImE1MmJhZWEwZDBiMGVlMzI2ZTU4OWU1MGNiZDY1YzRkYjE0MTFhMmViYWJjNGVlNDA3ZjMwYjE1NGY3NzI0ZGYifX0=';
|
||
|
||
$order = PaymentGatewayOrder::where('number', $number)->firstOrFail();
|
||
if($order->status !== 0){
|
||
return redirect()->to('/');
|
||
}
|
||
|
||
$billPayments = new \Qiwi\Api\BillPayments($SECRET_KEY);
|
||
$response = $billPayments->getBillInfo($number);
|
||
|
||
if ($response['status']['value'] === "PAID")
|
||
{
|
||
$order->status = 1;
|
||
$order->save();
|
||
|
||
$point = new Point;
|
||
$point->user_id = auth()->user()->id;
|
||
$point->point = $order->amount;
|
||
$point->type = 'Пополнение счета: ' . $order->type . ' # ' . $order->id;
|
||
$point->direction = DirectionEnum::COMING();
|
||
$point->save();
|
||
|
||
return redirect()->route('setting.money')->with('success', 'Счет успешно пополнен!');
|
||
}
|
||
else if ($response['status']['value'] === "WAITING")
|
||
{
|
||
echo "Ваш платеж в обработке \n Не закрывайте страницу и попробуйте обновить страницу позже";
|
||
die;
|
||
}
|
||
|
||
|
||
PaymentGatewayOrder::destroy($order->id);
|
||
|
||
$msg_error = 'Заказ завершен с ошибкой';
|
||
if($response['status']['value'] === "REJECTED"){
|
||
$msg_error .= ' - Счет отклонен';
|
||
}
|
||
return redirect()->route('setting.money')->with('error', $msg_error);
|
||
|
||
}
|
||
|
||
public function unitpayStatus()
|
||
{
|
||
dd(request()->all());
|
||
}
|
||
|
||
public function interkassa()
|
||
{
|
||
$amount = filter_input(INPUT_GET, 'ik_am', FILTER_VALIDATE_INT);
|
||
|
||
if(empty($amount)){
|
||
return redirect()->back();
|
||
}
|
||
|
||
$order = PaymentGatewayOrder::interkassa($amount);
|
||
|
||
return redirect(InterkassaService::payments_link($amount, $order->id));
|
||
}
|
||
|
||
public function interkassaStatus()
|
||
{
|
||
$ik_co_id = request()->ik_co_id; // Идентификатор кассы
|
||
$ik_inv_id = request()->ik_inv_id; // Идентификатор платежа в системе Интеркасса
|
||
$ik_inv_st = request()->ik_inv_st; // Состояние платежа. - success; fail
|
||
$ik_pm_no = request()->ik_pm_no; // Номер заказа. example - ID_4233
|
||
|
||
if($ik_inv_st === 'success'){
|
||
$order = PaymentGatewayOrder::findOrFail($ik_pm_no);
|
||
if($order->status === 1){
|
||
return redirect()->back();
|
||
}
|
||
|
||
$order->status = 1;
|
||
$order->system_payment_id = $ik_inv_id;
|
||
$order->save();
|
||
|
||
$point = new Point;
|
||
$point->user_id = auth()->user()->id;
|
||
$point->point = $order->amount;
|
||
$point->type = 'Пополнение счета: ' . $order->type . ' # ' . $order->id;
|
||
$point->direction = DirectionEnum::COMING();
|
||
$point->save();
|
||
|
||
return redirect()->route('setting.money')->with('success', 'Счет успешно пополнен!');
|
||
}
|
||
|
||
return $this->fails($ik_pm_no, $ik_inv_st);
|
||
|
||
}
|
||
|
||
protected function fails($order_id, $status)
|
||
{
|
||
PaymentGatewayOrder::destroy($order_id);
|
||
|
||
$msg_error = 'Заказ завершен с ошибкой';
|
||
if($status === 'canceled'){
|
||
$msg_error = 'Заказ отменен';
|
||
}
|
||
return redirect()->route('setting.money')->with('error', $msg_error);
|
||
}
|
||
|
||
|
||
public function payouts()
|
||
{
|
||
$amount = request()->amount;
|
||
$user = auth()->user();
|
||
$bankRequisites = $user->bank_requisites->first();
|
||
|
||
if(empty($bankRequisites)){
|
||
return redirect()->back()->with('error', 'Заполните платежные реквизиты');
|
||
}
|
||
|
||
$balance = SubscriptionService::calculate(auth()->user()->id);
|
||
if ($amount > $balance) {
|
||
return redirect()->back()->with('error', 'Недостаточно средств для вывода');
|
||
}
|
||
|
||
$requisites = $bankRequisites->requisites;
|
||
|
||
$detailsDescr[] = 'Тип: Банк';
|
||
$detailsDescr[] = 'Номер карты: ' . $bankRequisites->number;
|
||
$detailsDescr = implode(PHP_EOL, $detailsDescr);
|
||
|
||
$point = new Point;
|
||
$point->user_id = auth()->user()->id;
|
||
$point->point = $amount;
|
||
$point->type = 'Вывод средств (ожидает обработки)'; //YSV ENUM!
|
||
$point->direction = DirectionEnum::EXPENSE();
|
||
$point->save();
|
||
|
||
$withdrawal = new Withdrawal;
|
||
$withdrawal->point()->associate($point);
|
||
$withdrawal->user()->associate(auth()->user());
|
||
$withdrawal->requisites()->associate($requisites);
|
||
$withdrawal->amount = $amount;
|
||
$withdrawal->history_payment_details = $detailsDescr;
|
||
$withdrawal->save();
|
||
|
||
|
||
|
||
return redirect()->back()->with('success', 'Запрос на вывод денег успешно отправлен!');
|
||
}
|
||
}
|