feat: Implement SMS sending functionality with KavehNegar and Rangineh providers
- Add SendSmsMessage class for encapsulating SMS message data. - Create KavehNegarProvider and RanginehProvider classes implementing SmsProviderInterface for sending SMS. - Implement SmsLogRepository and SmsTemplateRepository for managing SMS logs and templates. - Develop SendSmsHandler for handling SMS sending messages. - Create SmsService to manage SMS dispatching and logging. - Add UserProfileController for managing user profiles with CRUD operations. - Implement UserProfile entity and repository for user profile data management. - Update symfony.lock and bootstrap.php for project dependencies and environment setup.
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace App\Settlement\Controller;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Settlement\Entity\Settlement;
|
||||
use App\Settlement\Entity\WalletTransaction;
|
||||
use App\Settlement\Repository\SettlementRepository;
|
||||
use App\Settlement\Repository\WalletTransactionRepository;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Controller\BaseController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\CurrentUser;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
class SettlementController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SettlementRepository $settlementRepo,
|
||||
private readonly WalletTransactionRepository $walletRepo,
|
||||
) {}
|
||||
|
||||
// ── Wallet ────────────────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/wallet/balance', methods: ['GET'])]
|
||||
public function balance(#[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$balance = $this->settlementRepo->getWalletBalance($user);
|
||||
$transactions = array_map(
|
||||
fn(WalletTransaction $t) => $t->toArray(),
|
||||
$this->walletRepo->findByUser($user, 10)
|
||||
);
|
||||
|
||||
return $this->success([
|
||||
'balance_rials' => $balance,
|
||||
'recent_transactions' => $transactions,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/wallet/transactions', methods: ['GET'])]
|
||||
public function transactions(#[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$transactions = array_map(
|
||||
fn(WalletTransaction $t) => $t->toArray(),
|
||||
$this->walletRepo->findByUser($user)
|
||||
);
|
||||
|
||||
return $this->success(['data' => $transactions]);
|
||||
}
|
||||
|
||||
// ── Settlement Requests ───────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/settlement', methods: ['POST'])]
|
||||
public function request(Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$amountRials = (int) ($data['amount_rials'] ?? 0);
|
||||
$bankAccount = $data['bank_account'] ?? null;
|
||||
|
||||
if ($amountRials <= 0) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'مبلغ برداشت باید بیشتر از صفر باشد', 422);
|
||||
}
|
||||
|
||||
$balance = $this->settlementRepo->getWalletBalance($user);
|
||||
if ($amountRials > $balance) {
|
||||
return $this->error(ErrorCodes::ERR_CONFLICT_001, 'موجودی کافی نیست', 422);
|
||||
}
|
||||
|
||||
$settlement = new Settlement($user, $amountRials, $bankAccount);
|
||||
$this->settlementRepo->save($settlement);
|
||||
|
||||
// Reserve amount by debit transaction
|
||||
$tx = new WalletTransaction($user, $amountRials, WalletTransaction::TYPE_DEBIT, $balance - $amountRials);
|
||||
$tx->setDescription('درخواست برداشت ' . $settlement->getUuid());
|
||||
$this->walletRepo->save($tx);
|
||||
|
||||
return $this->success(['data' => $settlement->toArray()], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/settlement', methods: ['GET'])]
|
||||
public function listMine(#[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$settlements = array_map(
|
||||
fn(Settlement $s) => $s->toArray(),
|
||||
$this->settlementRepo->findByUser($user)
|
||||
);
|
||||
|
||||
return $this->success(['data' => $settlements]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/settlement/{uuid}', methods: ['GET'])]
|
||||
public function get(string $uuid, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$settlement = $this->settlementRepo->findByUuid($uuid);
|
||||
if ($settlement === null) {
|
||||
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'درخواست یافت نشد', 404);
|
||||
}
|
||||
|
||||
if ($settlement->getUser()->getId() !== $user->getId() && !$user->hasRole('ROLE_ADMIN')) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
|
||||
}
|
||||
|
||||
return $this->success(['data' => $settlement->toArray()]);
|
||||
}
|
||||
|
||||
// ── Admin Actions ─────────────────────────────────────────────────────────
|
||||
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
#[Route('/api/v1/settlement/{uuid}/approve', methods: ['POST'])]
|
||||
public function approve(string $uuid, Request $request, #[CurrentUser] User $admin): JsonResponse
|
||||
{
|
||||
$settlement = $this->settlementRepo->findByUuid($uuid);
|
||||
if ($settlement === null) {
|
||||
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'درخواست یافت نشد', 404);
|
||||
}
|
||||
|
||||
if ($settlement->getStatus() !== Settlement::STATUS_PENDING) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'این درخواست قابل تأیید نیست', 422);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$settlement->approve($admin->getId(), $data['note'] ?? null);
|
||||
$this->settlementRepo->save($settlement);
|
||||
|
||||
return $this->success(['data' => $settlement->toArray()]);
|
||||
}
|
||||
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
#[Route('/api/v1/settlement/{uuid}/reject', methods: ['POST'])]
|
||||
public function reject(string $uuid, Request $request, #[CurrentUser] User $admin): JsonResponse
|
||||
{
|
||||
$settlement = $this->settlementRepo->findByUuid($uuid);
|
||||
if ($settlement === null) {
|
||||
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'درخواست یافت نشد', 404);
|
||||
}
|
||||
|
||||
if ($settlement->getStatus() !== Settlement::STATUS_PENDING) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'این درخواست قابل رد نیست', 422);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$note = trim($data['note'] ?? '');
|
||||
if (empty($note)) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دلیل رد الزامی است', 422);
|
||||
}
|
||||
|
||||
$settlement->reject($admin->getId(), $note);
|
||||
$this->settlementRepo->save($settlement);
|
||||
|
||||
// Refund the reserved amount back to wallet
|
||||
$balance = $this->settlementRepo->getWalletBalance($settlement->getUser());
|
||||
$tx = new WalletTransaction(
|
||||
$settlement->getUser(),
|
||||
$settlement->getAmountRials(),
|
||||
WalletTransaction::TYPE_CREDIT,
|
||||
$balance + $settlement->getAmountRials()
|
||||
);
|
||||
$tx->setDescription('برگشت برداشت رد شده ' . $settlement->getUuid());
|
||||
$this->walletRepo->save($tx);
|
||||
|
||||
return $this->success(['data' => $settlement->toArray()]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Settlement\Entity;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'settlements')]
|
||||
#[ORM\Index(columns: ['user_id', 'status'], name: 'idx_settlements_user_status')]
|
||||
class Settlement
|
||||
{
|
||||
public const STATUS_PENDING = 'pending';
|
||||
public const STATUS_APPROVED = 'approved';
|
||||
public const STATUS_REJECTED = 'rejected';
|
||||
public const STATUS_PAID = 'paid';
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 36, unique: true)]
|
||||
private string $uuid;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: User::class)]
|
||||
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false, onDelete: 'RESTRICT')]
|
||||
private User $user;
|
||||
|
||||
#[ORM\Column(name: 'amount_rials', type: 'integer')]
|
||||
private int $amountRials;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 20)]
|
||||
private string $status = self::STATUS_PENDING;
|
||||
|
||||
#[ORM\Column(name: 'bank_account', type: 'json', nullable: true)]
|
||||
private ?array $bankAccount = null;
|
||||
|
||||
#[ORM\Column(name: 'admin_note', type: 'string', length: 500, nullable: true)]
|
||||
private ?string $adminNote = null;
|
||||
|
||||
#[ORM\Column(name: 'reviewed_by', type: 'integer', nullable: true)]
|
||||
private ?int $reviewedBy = null;
|
||||
|
||||
#[ORM\Column(name: 'reviewed_at', type: 'integer', nullable: true)]
|
||||
private ?int $reviewedAt = null;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: 'integer')]
|
||||
private int $createdAt;
|
||||
|
||||
#[ORM\Column(name: 'updated_at', type: 'integer')]
|
||||
private int $updatedAt;
|
||||
|
||||
public function __construct(User $user, int $amountRials, ?array $bankAccount = null)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->user = $user;
|
||||
$this->amountRials = $amountRials;
|
||||
$this->bankAccount = $bankAccount;
|
||||
$this->createdAt = time();
|
||||
$this->updatedAt = time();
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
public function getUuid(): string { return $this->uuid; }
|
||||
public function getUser(): User { return $this->user; }
|
||||
public function getAmountRials(): int { return $this->amountRials; }
|
||||
public function getStatus(): string { return $this->status; }
|
||||
public function getBankAccount(): ?array { return $this->bankAccount; }
|
||||
public function getAdminNote(): ?string { return $this->adminNote; }
|
||||
|
||||
public function approve(int $adminUserId, ?string $note = null): self
|
||||
{
|
||||
$this->status = self::STATUS_APPROVED;
|
||||
$this->reviewedBy = $adminUserId;
|
||||
$this->reviewedAt = time();
|
||||
$this->adminNote = $note;
|
||||
$this->updatedAt = time();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function reject(int $adminUserId, string $note): self
|
||||
{
|
||||
$this->status = self::STATUS_REJECTED;
|
||||
$this->reviewedBy = $adminUserId;
|
||||
$this->reviewedAt = time();
|
||||
$this->adminNote = $note;
|
||||
$this->updatedAt = time();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function markPaid(): self
|
||||
{
|
||||
$this->status = self::STATUS_PAID;
|
||||
$this->updatedAt = time();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'uuid' => $this->uuid,
|
||||
'amount_rials' => $this->amountRials,
|
||||
'status' => $this->status,
|
||||
'bank_account' => $this->bankAccount,
|
||||
'admin_note' => $this->adminNote,
|
||||
'reviewed_at' => $this->reviewedAt,
|
||||
'created_at' => $this->createdAt,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Settlement\Entity;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Payment\Entity\Payment;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'wallet_transactions')]
|
||||
#[ORM\Index(columns: ['user_id', 'created_at'], name: 'idx_wallet_user_date')]
|
||||
class WalletTransaction
|
||||
{
|
||||
public const TYPE_CREDIT = 'credit';
|
||||
public const TYPE_DEBIT = 'debit';
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 36, unique: true)]
|
||||
private string $uuid;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: User::class)]
|
||||
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false, onDelete: 'RESTRICT')]
|
||||
private User $user;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: Payment::class)]
|
||||
#[ORM\JoinColumn(name: 'payment_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
|
||||
private ?Payment $payment = null;
|
||||
|
||||
#[ORM\Column(name: 'amount_rials', type: 'integer')]
|
||||
private int $amountRials;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 10)]
|
||||
private string $type;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private ?string $description = null;
|
||||
|
||||
#[ORM\Column(name: 'balance_after', type: 'integer')]
|
||||
private int $balanceAfter;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: 'integer')]
|
||||
private int $createdAt;
|
||||
|
||||
public function __construct(User $user, int $amountRials, string $type, int $balanceAfter)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->user = $user;
|
||||
$this->amountRials = $amountRials;
|
||||
$this->type = $type;
|
||||
$this->balanceAfter = $balanceAfter;
|
||||
$this->createdAt = time();
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
public function getUuid(): string { return $this->uuid; }
|
||||
public function getUser(): User { return $this->user; }
|
||||
public function getPayment(): ?Payment { return $this->payment; }
|
||||
public function getAmountRials(): int { return $this->amountRials; }
|
||||
public function getType(): string { return $this->type; }
|
||||
public function getDescription(): ?string { return $this->description; }
|
||||
public function getBalanceAfter(): int { return $this->balanceAfter; }
|
||||
|
||||
public function setPayment(?Payment $p): self { $this->payment = $p; return $this; }
|
||||
public function setDescription(?string $d): self { $this->description = $d; return $this; }
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'uuid' => $this->uuid,
|
||||
'amount_rials' => $this->amountRials,
|
||||
'type' => $this->type,
|
||||
'description' => $this->description,
|
||||
'balance_after' => $this->balanceAfter,
|
||||
'created_at' => $this->createdAt,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Settlement\Repository;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Settlement\Entity\Settlement;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class SettlementRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Settlement::class);
|
||||
}
|
||||
|
||||
public function findByUuid(string $uuid): ?Settlement
|
||||
{
|
||||
return $this->findOneBy(['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
/** @return Settlement[] */
|
||||
public function findByUser(User $user): array
|
||||
{
|
||||
return $this->findBy(['user' => $user], ['createdAt' => 'DESC']);
|
||||
}
|
||||
|
||||
/** Balance = sum of credits - sum of debits from wallet_transactions */
|
||||
public function getWalletBalance(User $user): int
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
|
||||
$credit = (int) ($em->createQuery(
|
||||
'SELECT SUM(w.amountRials) FROM App\Settlement\Entity\WalletTransaction w
|
||||
WHERE w.user = :user AND w.type = :type'
|
||||
)->setParameters(['user' => $user, 'type' => 'credit'])->getSingleScalarResult() ?? 0);
|
||||
|
||||
$debit = (int) ($em->createQuery(
|
||||
'SELECT SUM(w.amountRials) FROM App\Settlement\Entity\WalletTransaction w
|
||||
WHERE w.user = :user AND w.type = :type'
|
||||
)->setParameters(['user' => $user, 'type' => 'debit'])->getSingleScalarResult() ?? 0);
|
||||
|
||||
return $credit - $debit;
|
||||
}
|
||||
|
||||
public function save(Settlement $entity, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($entity);
|
||||
if ($flush) $this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Settlement\Repository;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Settlement\Entity\WalletTransaction;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class WalletTransactionRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, WalletTransaction::class);
|
||||
}
|
||||
|
||||
/** @return WalletTransaction[] */
|
||||
public function findByUser(User $user, int $limit = 50): array
|
||||
{
|
||||
return $this->findBy(['user' => $user], ['createdAt' => 'DESC'], $limit);
|
||||
}
|
||||
|
||||
public function save(WalletTransaction $entity, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($entity);
|
||||
if ($flush) $this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user