168 lines
5.7 KiB
PHP
168 lines
5.7 KiB
PHP
<?php
|
|
|
|
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;
|
|
use kernel\helpers\ImageGD;
|
|
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
|
|
{
|
|
$model = new Card();
|
|
// Пример заполнения:
|
|
$model->user_id = $form_model->getItem('user_id');
|
|
$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->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;
|
|
$model->username = $form_model->getItem('username');
|
|
$model->card_template_id = $form_model->getItem('card_template_id');
|
|
$model->status = $form_model->getItem('status');
|
|
|
|
if ($model->save()) {
|
|
$cardFile = $this->cardFileService->create($model);
|
|
if ($cardFile) {
|
|
$model->card_file_id = $cardFile->id;
|
|
$model->save();
|
|
}
|
|
|
|
return $model;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function update(FormModel $form_model, Card $card): false|Card
|
|
{
|
|
// Пример обновления:
|
|
$card->user_id = $form_model->getItem('user_id');
|
|
$card->payment_type = $form_model->getItem('payment_type');
|
|
$card->bank_id = $form_model->getItem('bank_id');
|
|
$card->info = $form_model->getItem('info');
|
|
$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');
|
|
$card->username = $form_model->getItem('username');
|
|
$card->status = $form_model->getItem('status');
|
|
|
|
if (
|
|
$card->card_template_id !== (int)$form_model->getItem('card_template_id')
|
|
or $card->program !== (int)$form_model->getItem('program')
|
|
) {
|
|
$card->card_template_id = $form_model->getItem('card_template_id');
|
|
$cardFile = $this->cardFileService->create($card);
|
|
if ($cardFile) {
|
|
$card->card_file_id = $cardFile->id;
|
|
}
|
|
}
|
|
|
|
if ($card->save()) {
|
|
return $card;
|
|
}
|
|
|
|
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->card_program_id)->client($card->id);
|
|
|
|
$cardNumber = CardNumber::generate($customer, $formatter);
|
|
//Card
|
|
$img = ROOT_DIR . "/" . $card->cardTemplate->path; // Ссылка на файл
|
|
$font = RESOURCES_DIR . "/tmp/arialmt.ttf"; // Ссылка на шрифт
|
|
|
|
$img = new ImageGD($img);
|
|
|
|
$pngFilePath = RESOURCES_DIR . "/tmp/" . $card->id . ".png";
|
|
$pngFileUrl = "/resources/tmp/" . $card->id . ".png";
|
|
|
|
$img->addText(font_size: 14, degree: 0, x: 15, y: 25, color: "#000000", font: $font, text: "KO coin");
|
|
$img->addText(font_size: 14, degree: 0, x: 15, y: 45, color: "#000000", font: $font, text: "BGroup\ITGuild");
|
|
$img->addText(font_size: 18, degree: 0, x: 15, y: 180, color: "#ffffff", font: $font, text: $cardNumber);
|
|
$img->addText(font_size: 12, degree: 0, x: 15, y: 200, color: "#ffffff", font: $font, text: $card->username);
|
|
$img->save($pngFilePath);
|
|
|
|
return $pngFileUrl;
|
|
}
|
|
|
|
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();
|
|
if ($card) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |