card action

This commit is contained in:
Kavalar 2025-01-26 14:42:47 +03:00
parent 08cdf44b67
commit 824130df26
27 changed files with 430 additions and 53 deletions

View File

@ -12,11 +12,16 @@ class TgBotRestController extends \kernel\app_modules\tgbot\controllers\TgBotRes
public function actionGetScanBtn(int $id): void
{
$dialog = Tgbot::where("dialog_id", $id)->first();
$html = "";
if ($dialog){
$this->renderApi([
'html' => '<a class="btn btn-primary" href="/miniapp/scanner">Сканировать</a>',
]);
if ($dialog->status === Tgbot::ADMIN_STATUS){
$html = '<a class="btn btn-primary" href="/miniapp/scanner">Сканировать</a>';
}
}
$this->renderApi([
'html' => $html,
]);
}

View File

@ -2,14 +2,19 @@
namespace app\modules\tgbot\controllers;
use app\modules\tgbot\models\forms\CardActionStep2Form;
use app\modules\tgbot\models\Tgbot;
use app\modules\tgbot\services\TgBotService;
use Cassandra\Decimal;
use kernel\app_modules\card\conditions\CashbackCondition;
use kernel\app_modules\card\models\Card;
use kernel\app_modules\card\services\CardService;
use kernel\app_modules\tag\service\TagService;
use kernel\Controller;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\modules\post\models\Post;
use kernel\Request;
class TgMainController extends Controller
{
@ -47,11 +52,49 @@ class TgMainController extends Controller
$this->cgView->render("scanner.php");
}
public function actionCardAction(int $cardId): void
public function actionCardActionStep1(int $cardId): void
{
$card = Card::where("id", $cardId)->first();
$this->cgView->render("card_action.php", ['card' => $card]);
$this->cgView->render("card_action_step_1.php", ['card' => $card]);
}
public function actionCardActionStep2(): void
{
$params = new CardActionStep2Form();
$request = new Request();
$params->load($request->get());
$this->cgView->render("card_action_step_2.php", ['params' => $params]);
}
public function actionCardActionStep3(): void
{
$params = new CardActionStep2Form();
$request = new Request();
$params->load($request->post());
$card = CardService::getCardById($params->getItem("card_id"));
if ($params->getItem('type') === 'add_money'){
Flash::setMessage("success", "Баланс пополнен на " . $params->getItem('amount'));
$transaction = CardService::addMoneyToCard($card, (int)$params->getItem('amount'));
}
if ($params->getItem('type') === 'withdraw'){
Flash::setMessage("success", "С карты списано " . $params->getItem('amount'));
$transaction = CardService::withdrawMoneyFromCard($card, (int)$params->getItem('amount'));
}
if ($params->getItem('type') === 'add_purchase'){
// Flash::setMessage("success", "С карты списано " . $params->getItem('amount'));
// CardService::withdrawMoneyFromCard($card, (int)$params->getItem('amount'));
$cashback = new CashbackCondition();
$transaction = $cashback->handler($card, (int)$params->getItem('amount'));
Flash::setMessage("success", "Начислено кешбэк " . $transaction->amount);
}
$card = $card->fresh();
$this->cgView->render("card_action_step_3.php", ['card' => $card, 'transaction' => $transaction ?? null]);
}
}

View File

@ -9,7 +9,7 @@ use kernel\Middleware;
class TgBotAuthMiddleware extends Middleware
{
function handler(): void
public function handler(): void
{
if(isset($_COOKIE['dialog_id']))
{
@ -23,4 +23,14 @@ class TgBotAuthMiddleware extends Middleware
exit();
}
public function isTgAdmin(): void
{
if (TgBotService::isAdmin()){
return;
}
echo "Доступ запрещен";
exit();
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace app\modules\tgbot\models\forms;
use kernel\FormModel;
/**
* @property string $type
* @property integer $card_id
*/
class CardActionStep2Form extends FormModel
{
public function rules(): array
{
return [
'type' => 'required|min-str-len:3',
'card_id' => 'required|alpha-numeric',
'amount' => 'alpha-numeric',
];
}
}

View File

@ -6,13 +6,18 @@ use kernel\CgRouteCollector;
include KERNEL_APP_MODULES_DIR . "/tgbot/routs/tgbot.php";
App::$collector->filter("tg_bot_auth", [\app\modules\tgbot\middlewares\TgBotAuthMiddleware::class, "handler"]);
App::$collector->filter("tg_bot_is_admin", [\app\modules\tgbot\middlewares\TgBotAuthMiddleware::class, "isTgAdmin"]);
App::$collector->group(["prefix" => "miniapp"], function (CGRouteCollector $router) {
App::$collector->get('/', [\app\modules\tgbot\controllers\TgMainController::class, 'actionMain']);
App::$collector->group(["before" => "tg_bot_auth"], function (CGRouteCollector $router){
App::$collector->get('/news', [\app\modules\tgbot\controllers\TgMainController::class, 'actionNews']);
App::$collector->get('/promo', [\app\modules\tgbot\controllers\TgMainController::class, 'actionPromo']);
App::$collector->get('/scanner', [\app\modules\tgbot\controllers\TgMainController::class, 'actionScanner']);
App::$collector->get('/card_action/{cardId}', [\app\modules\tgbot\controllers\TgMainController::class, 'actionCardAction']);
App::$collector->group(["before" => "tg_bot_is_admin"], function (CGRouteCollector $router){
App::$collector->get('/scanner', [\app\modules\tgbot\controllers\TgMainController::class, 'actionScanner']);
App::$collector->get('/card_action/{cardId}', [\app\modules\tgbot\controllers\TgMainController::class, 'actionCardActionStep1']);
App::$collector->get('/card_action_step_2', [\app\modules\tgbot\controllers\TgMainController::class, 'actionCardActionStep2']);
App::$collector->post('/card_action_step_3', [\app\modules\tgbot\controllers\TgMainController::class, 'actionCardActionStep3']);
});
});
});

View File

@ -12,6 +12,4 @@ use kernel\services\ModuleService;
class TgBotService extends \kernel\app_modules\tgbot\services\TgBotService
{
public static null|Tgbot $currentDialog = null;
}

View File

@ -5,7 +5,7 @@
* @var string $title
* @var \kernel\CgView $view
*/
\Josantonius\Session\Facades\Session::start();
\kernel\Flash::start();
?>
<!doctype html>
<html lang="en">

View File

@ -1,23 +0,0 @@
<?php
/**
* @var \kernel\app_modules\card\models\Card $card
*/
use kernel\helpers\Html;
?>
<div class="row">
<div class="col-12 m-1">
<?php
echo Html::img(src: "data:image/png;base64, " . \kernel\app_modules\card\services\CardFileService::createCardPNG($card, true));
?>
</div>
</div>
<div class="row">
<div class="col-12 m-1">
<a class="btn btn-primary" href="/miniapp/add_purchase">Добавить покупку</a>
</div>
</div>

View File

@ -0,0 +1,38 @@
<?php
/**
* @var \kernel\app_modules\card\models\Card $card
*/
use kernel\helpers\Html;
?>
<div class="row">
<div class="col-12 m-1">
<?php
echo Html::img(src: "data:image/png;base64, " . \kernel\app_modules\card\services\CardFileService::createCardPNG($card, true));
?>
</div>
</div>
<div class="row">
<div class="col-12 m-1" style="font-size: 20px;">
Баланс: <?= $card->balance ?>
</div>
</div>
<div class="row" style="margin-top: 20px;">
<div class="col-12 m-1">
<a class="btn btn-primary w-100" href="/miniapp/card_action_step_2?type=add_purchase&card_id=<?=$card->id?>">Добавить покупку</a>
</div>
</div>
<div class="row" style="margin-top: 20px;">
<div class="col-12 m-1">
<a class="btn btn-primary w-100" href="/miniapp/card_action_step_2?type=add_money&card_id=<?=$card->id?>">Начислить</a>
</div>
</div>
<div class="row" style="margin-top: 20px;">
<div class="col-12 m-1">
<a class="btn btn-primary w-100" href="/miniapp/card_action_step_2?type=withdraw&card_id=<?=$card->id?>">Списать</a>
</div>
</div>

View File

@ -0,0 +1,51 @@
<?php
/**
* @var \app\modules\tgbot\models\forms\CardActionStep2Form $params;
*/
$form = new \itguild\forms\ActiveForm();
$form->beginForm( "/miniapp/card_action_step_3");
$form->field(class: \itguild\forms\inputs\Hidden::class, name: "card_id", params: [
'value' => $params->getItem("card_id")
])
->render();
$form->field(class: \itguild\forms\inputs\Hidden::class, name: "type", params: [
'value' => $params->getItem("type")
])
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "amount", params: [
'class' => "form-control",
'placeholder' => 'Количество',
])
->setLabel("Количество")
->render();
?>
<div class="row">
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ",
'value' => 'Отправить',
'typeInput' => 'submit'
])
->render();
?>
</div>
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [
'class' => "btn btn-warning",
'value' => 'Сбросить',
'typeInput' => 'reset'
])
->render();
?>
</div>
</div>
<?php
$form->endForm();

View File

@ -0,0 +1,30 @@
<?php
/**
* @var Card $card
* @var CardTransaction $transaction
*/
use kernel\app_modules\card\models\Card;
use kernel\app_modules\card\models\CardTransaction;
use kernel\app_modules\card\services\CardFileService;
use kernel\helpers\Html;
?>
<div class="row">
<div class="col-12 m-1">
<?php
echo Html::img(src: "data:image/png;base64, " . CardFileService::createCardPNG($card, true));
?>
</div>
</div>
<div class="row">
<div class="col-12 m-1" style="font-size: 20px;">
Баланс: <?= $card->balance ?>
</div>
</div>
<div class="row" style="margin-top: 20px;">
<div class="col-12 m-1">
<a class="btn btn-primary w-100" href="/miniapp/scanner">Сканировать</a>
</div>
</div>

View File

@ -9,12 +9,13 @@ class Flash
public static function setMessage(string $type, string $msg): void
{
Session::start();
self::start();
Session::set($type, $msg);
}
public static function getMessage(string $type): string
{
self::start();
$msg = Session::get($type, false);
Session::remove($type);
@ -23,7 +24,16 @@ class Flash
public static function hasMessage(string $type): bool
{
self::start();
return Session::has($type);
}
public static function start()
{
if (!Session::isStarted()){
Session::start();
}
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace kernel\app_modules\card\conditions;
use kernel\app_modules\card\models\Card;
use kernel\app_modules\card\models\forms\CreateCardTransactionForm;
use kernel\app_modules\card\services\CardTransactionService;
use kernel\helpers\Debug;
class CashbackCondition
{
public function handler(Card $card, int $amount): \kernel\app_modules\card\models\CardTransaction|false
{
$conditions = $card->cardProgram->cardProgramConditions;
if ($conditions){
foreach ($conditions as $condition){
if ($condition->type === "cashback"){
$transactionForm = new CreateCardTransactionForm();
$transactionForm->load([
'from' => 1001,
'to' => $card->id,
'amount' => $this->calcPercent($amount, $condition->value),
'type' => 1,
'status' => 1,
]);
$transactionService = new CardTransactionService();
return $transactionService->create($transactionForm);
}
}
}
return false;
}
private function calcPercent($price, $percent): int
{
return round($price * ($percent / 100));
}
}

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public string $migration;
/**
* Run the migrations.
*/
public function up(): void
{
\kernel\App::$db->schema->table('card', function(Blueprint $table) {
$table->renameColumn('program', 'card_program_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
\kernel\App::$db->schema->table('card', function(Blueprint $table) {
$table->renameColumn('card_program_id', 'program');
});
}
};

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public string $migration;
/**
* Run the migrations.
*/
public function up(): void
{
\kernel\App::$db->schema->table('card_transaction', function(Blueprint $table) {
$table->integer("from_balance")->after("from")->default(0);
$table->integer("to_balance")->after("to")->default(0);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
\kernel\App::$db->schema->table('card_transaction', function(Blueprint $table) {
$table->dropColumn("from_balance");
$table->dropColumn("to_balance");
});
}
};

View File

@ -14,7 +14,7 @@ use kernel\modules\user\models\User;
* @property int $payment_type
* @property int $bank_id
* @property int $info
* @property int $program
* @property int $card_program_id
* @property int $cvc
* @property int $pin
* @property int $card_template_id
@ -28,7 +28,7 @@ class Card extends Model
protected $table = 'card';
protected $fillable = ['user_id', 'payment_type', 'balance', 'bank_id', 'info', 'program', 'cvc', 'pin', 'username', 'card_template_id', 'card_file_id', 'status'];
protected $fillable = ['user_id', 'payment_type', 'balance', 'bank_id', 'info', 'card_program_id', 'cvc', 'pin', 'username', 'card_template_id', 'card_file_id', 'status'];
public static function labels(): array
{
@ -46,7 +46,7 @@ class Card extends Model
'balance' => 'Баланс',
'bank_id' => 'ID банка',
'info' => 'Информация о банке',
'program' => 'Программа',
'card_program_id' => 'Программа',
'cvc' => 'CVC',
'pin' => 'PIN',
'username' => 'Username',
@ -72,6 +72,11 @@ class Card extends Model
return $this->belongsTo(CardTemplate::class);
}
public function cardProgram(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(related: CardProgram::class);
}
public function cardFile(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(CardFile::class);

View File

@ -37,6 +37,11 @@ class CardProgram extends Model
];
}
public function cardProgramConditions(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(CardProgramConditions::class);
}
/**
* @return string[]
*/

View File

@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Model;
/**
* @property integer $from
* @property integer $to
* @property integer $from_balance
* @property integer $to_balance
* @property integer $amount
* @property integer $type
* @property integer $status
@ -20,7 +22,7 @@ class CardTransaction extends Model
protected $table = 'card_transaction';
protected $fillable = ['from', 'to', 'amount', 'type', 'status'];
protected $fillable = ['from', 'from_balance', 'to', 'to_balance', 'amount', 'type', 'status'];
public static function labels(): array
{

View File

@ -23,7 +23,7 @@ class CreateCardForm extends FormModel
'balance' => 'alpha-numeric',
'bank_id' => 'required|alpha-numeric',
'info' => 'required|alpha-numeric',
'program' => 'required|alpha-numeric',
'card_program_id' => 'required|alpha-numeric',
'cvc' => 'required|alpha-numeric',
'pin' => 'required|alpha-numeric',
'username' => 'required|min-str-len:5|max-str-len:20',

View File

@ -19,7 +19,9 @@ class CreateCardTransactionForm extends FormModel
// ];
return [
'from' => 'required|alpha-numeric',
'from_balance' => 'alpha-numeric',
'to' => 'required|alpha-numeric',
'to_balance' => 'alpha-numeric',
'amount' => 'required|alpha-numeric',
'type' => 'required|alpha-numeric',
'status' => ''

View File

@ -57,7 +57,7 @@ class CardFileService
if ($card->cardTemplate) {
$formatter = BankFormatter::create();
$customer = BankFactory::create()->paymentType($card->payment_type)->bank($card->bank_id, $card->info, $card->program)->client($card->id);
$customer = BankFactory::create()->paymentType($card->payment_type)->bank($card->bank_id, $card->info, $card->card_program_id)->client($card->id);
$cardNumber = CardNumber::generate($customer, $formatter);
//Card

View File

@ -5,6 +5,8 @@ namespace kernel\app_modules\card\services;
use DragonCode\CardNumber\CardNumber;
use DragonCode\CardNumber\Factories\BankFactory;
use DragonCode\CardNumber\Formatters\BankFormatter;
use kernel\app_modules\card\models\CardTransaction;
use kernel\app_modules\card\models\forms\CreateCardTransactionForm;
use kernel\helpers\Debug;
use kernel\app_modules\card\models\Card;
use kernel\FormModel;
@ -14,10 +16,12 @@ use kernel\modules\user\models\User;
class CardService
{
protected CardFileService $cardFileService;
protected CardTransactionService $cardTransactionService;
public function __construct()
{
$this->cardFileService = new CardFileService();
$this->cardTransactionService = new CardTransactionService();
}
public function create(FormModel $form_model): false|Card
@ -28,7 +32,7 @@ class CardService
$model->payment_type = $form_model->getItem('payment_type') ?? 2;
$model->bank_id = $form_model->getItem('bank_id') ?? 232;
$model->info = $form_model->getItem('info') ?? 42;
$model->program = $form_model->getItem('program') ?? 71;
$model->card_program_id = $form_model->getItem('card_program_id') ?? 71;
$model->balance = $form_model->getItem('balance') ?? 0;
$model->cvc = $form_model->getItem('cvc') ?? 101;
$model->pin = $form_model->getItem('pin') ?? 1111;
@ -56,7 +60,7 @@ class CardService
$card->payment_type = $form_model->getItem('payment_type');
$card->bank_id = $form_model->getItem('bank_id');
$card->info = $form_model->getItem('info');
$card->program = $form_model->getItem('program');
$card->card_program_id = $form_model->getItem('card_program_id');
$card->balance = $form_model->getItem('balance');
$card->cvc = $form_model->getItem('cvc');
$card->pin = $form_model->getItem('pin');
@ -81,12 +85,44 @@ class CardService
return false;
}
public static function addMoneyToCard(Card $card, int $amount): CardTransaction
{
$transactionForm = new CreateCardTransactionForm();
$transactionForm->load([
'from' => 1001,
'to' => $card->id,
'amount' => $amount,
'type' => 1,
'status' => 1,
]);
$transactionService = new CardTransactionService();
return $transactionService->create($transactionForm);
}
public static function withdrawMoneyFromCard(Card $card, int $amount): CardTransaction
{
$transactionForm = new CreateCardTransactionForm();
$transactionForm->load([
'from' => $card->id,
'to' => 1001,
'amount' => $amount,
'type' => 1,
'status' => 1,
]);
$transactionService = new CardTransactionService();
return $transactionService->create($transactionForm);
}
public static function createCardPNG(Card $card): false|string
{
if ($card->cardTemplate) {
$formatter = BankFormatter::create();
$customer = BankFactory::create()->paymentType($card->payment_type)->bank($card->bank_id, $card->info, $card->program)->client($card->id);
$customer = BankFactory::create()->paymentType($card->payment_type)->bank($card->bank_id, $card->info, $card->card_program_id)->client($card->id);
$cardNumber = CardNumber::generate($customer, $formatter);
//Card
@ -110,6 +146,16 @@ class CardService
return false;
}
public static function getCardById(int $id)
{
$card = Card::find($id);
if ($card) {
return $card;
}
return false;
}
public static function userHasCard(int $userId): bool
{
$card = Card::where("user_id", $userId)->first();

View File

@ -24,6 +24,7 @@ class CardTransactionService
$this->errors[] = ["error_id" => 3, "error_msg" => "Sender not found"];
return false;
}
$model->from_balance = $fromCard->balance;
}
if ($toId !== 1001){
@ -32,6 +33,7 @@ class CardTransactionService
$this->errors[] = ["error_id" => 3, "error_msg" => "Recipient not found"];
return false;
}
$model->to_balance = $toCard->balance;
}
if ($fromId !== 1001){
@ -49,11 +51,11 @@ class CardTransactionService
// $model->slug = Slug::createSlug($form_model->getItem('title'), Card::class); // Генерация уникального slug
if ($model->save()) {
if ($fromCard){
if (isset($fromCard)){
$fromCard->balance = $fromCard->balance - (int)$form_model->getItem('amount');
$fromCard->save();
}
if ($toCard){
if (isset($toCard)){
$toCard->balance = $toCard->balance + (int)$form_model->getItem('amount');
$toCard->save();
}

View File

@ -58,9 +58,9 @@ $form->field(\itguild\forms\inputs\TextInput::class, 'info', [
->setLabel("Информация")
->render();
$form->field(class: \itguild\forms\inputs\Select::class, name: "program", params: [
$form->field(class: \itguild\forms\inputs\Select::class, name: "card_program_id", params: [
'class' => "form-control",
'value' => $model->program ?? ''
'value' => $model->card_program_id ?? ''
])
->setLabel("Программа")
->setOptions(\kernel\app_modules\card\services\CardProgramService::getProgramList())

View File

@ -18,6 +18,7 @@ class Tgbot extends Model
{
const DISABLE_STATUS = 0;
const ACTIVE_STATUS = 1;
const ADMIN_STATUS = 9;
protected $table = 'tgbot';

View File

@ -11,6 +11,8 @@ use kernel\services\ModuleService;
class TgBotService
{
public static null|Tgbot $currentDialog = null;
public function create(FormModel $form_model): false|Tgbot
{
$model = new Tgbot();
@ -45,4 +47,13 @@ class TgBotService
return false;
}
public static function isAdmin(): bool
{
if (self::$currentDialog->status === Tgbot::ADMIN_STATUS){
return true;
}
return false;
}
}

View File

@ -71,10 +71,15 @@ class MigrationController extends ConsoleController
$dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration');
$m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), new Filesystem());
if (\kernel\App::$db->schema->hasTable('option')) {
$migrationPaths = array_merge($this->moduleService->getModulesMigrationsPaths(), [ROOT_DIR . '/migrations']);
} else {
$migrationPaths = [ROOT_DIR . '/migrations'];
if (isset($this->argv['path'])){
$migrationPaths = [ROOT_DIR . $this->argv['path']];
}
else {
if (\kernel\App::$db->schema->hasTable('option')) {
$migrationPaths = array_merge($this->moduleService->getModulesMigrationsPaths(), [ROOT_DIR . '/migrations']);
} else {
$migrationPaths = [ROOT_DIR . '/migrations'];
}
}
$res = $m->run($migrationPaths);
@ -94,7 +99,13 @@ class MigrationController extends ConsoleController
$m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), new Filesystem());
//$migrationPaths = array_merge(App::$migrationsPaths, [WORKSPACE_DIR . '/console/migrations']);
$migrationPaths = [ROOT_DIR . '/migrations'];
if (isset($this->argv['path'])){
$migrationPaths = [ROOT_DIR . $this->argv['path']];
}
else {
$migrationPaths = [ROOT_DIR . '/migrations'];
}
$res = $m->rollback($migrationPaths, ['step' => $step]);
print_r($step);
foreach ($res as $re) {