dev-master
9999999-dev http://bitapp.plSymfony WalletBundle
MIT
The Requires
- php >=5.3.9
- doctrine/orm ^2.4.8
by Michał Kurcewicz
symfony money wallet
Symfony WalletBundle
The bundle added wallet into Users and allow to managed it. This bundle is for Symfony Framework., (*1)
Require the bundle with composer:, (*3)
$ composer require mkurc1/wallet-bundle
Enable the bundle in the kernel:, (*4)
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new WalletBundle\WalletBundle(), // ... ); }
Create your Wallet class:, (*5)
<?php namespace AppBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use WalletBundle\Entity\Wallet as BaseWallet; /** * @ORM\Entity * @ORM\Table(name="wallet") */ class Wallet extends BaseWallet { /** * @var integer * * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var ArrayCollection * * @ORM\OneToMany(targetEntity="AppBundle\Entity\WalletHistory", mappedBy="wallet", cascade={"persist"}, orphanRemoval=true) * @ORM\OrderBy({"createdAt"="DESC"}) */ protected $histories; public function __construct() { parent::__construct(); // your own logic } }
Create your WalletHistory class:, (*6)
<?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use WalletBundle\Entity\WalletInterface; use WalletBundle\Entity\WalletHistory as BaseWalletHistory; /** * @ORM\Entity * @ORM\Table(name="wallet_history") */ class WalletHistory extends BaseWalletHistory { /** * @var integer * * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var WalletInterface * * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Wallet", inversedBy="histories") * @ORM\JoinColumn(nullable=false) */ protected $wallet; public function __construct($type = null) { parent::__construct($type); // your own logic } }
Implement WalletBundle\Entity\UserWalletInterface on User entity:, (*7)
<?php namespace AppBundle\Entity; use WalletBundle\Entity\WalletInterface; use WalletBundle\Entity\UserWalletInterface; class User implement UserWalletInterface { // your user entity logic /** * @var WalletInterface * * @ORM\OneToOne(targetEntity="AppBundle\Entity\Wallet", cascade={"persist"}) */ protected $wallet; /** * @return WalletInterface */ public function getWallet() { return $this->wallet; } /** * @param WalletInterface $wallet * @return User */ public function setWallet($wallet) { $this->wallet = $wallet; return $this; } public function __construct() { parent::__construct($type); $this->wallet = new Wallet(); // your own logic } }
Configure your application:, (*8)
# app/config/config.yml wallet: classes: wallet: AppBundle\Entity\Wallet # your wallet class wallet_history: AppBundle\Entity\WalletHistory # your wallet history class
Update your database schema:, (*9)
$ php app/console doctrine:schema:update --force
Usages:, (*10)
<?php $entityManager = $this->container->get('doctrine.orm.entity_manager'); $userRepository = $entityManager->getRepository('AppBundle:User'); $transaction = $this->container->get('wallet.transaction'); // add money to user account $user = $userRepository->findOneByName('foo'); $amount = 199; $transactionName = 'Bonus for your activity' $transaction->addMoney($user, $amount, $transactionName); // subtract money from user account $user = $userRepository->findOneByName('foo'); $amount = 400; $transactionName = 'Payment for subscription' $transaction->subMoney($user, $amount, $transactionName); // if account don't have enough money, method throw exception WalletBundle\Exception\NotEnoughMoneyException // freeze money on user account (account don't need to have enough money to freeze it) $user = $userRepository->findOneByName('foo'); $amount = 50; $transactionName = 'Reclamation' $transaction->freezeMoney($user, $amount, $transactionName); // move freeze money to another user account $fromUser = $userRepository->findOneByName('foo'); $toUser = $userRepository->findOneByName('bar'); $amount = 50; $transactionName = 'Refund' $transaction->moveFreezeMoneyToUser($fromUser, $toUser, $amount, $transactionName); // if account don't have enough freeze money in wallet, method throw exception WalletBundle\Exception\NotEnoughMoneyException
You now can use your wallet system!, (*11)
The bundle is released under the MIT License., (*12)
Symfony WalletBundle
MIT
symfony money wallet