Giter Club home page Giter Club logo

walletbundle's Introduction

WalletBundle

The bundle added wallet into Users and allow to managed it. This bundle is for Symfony Framework.

SensioLabsInsight

Configure

Require the bundle with composer:

$ composer require mkurc1/wallet-bundle

Enable the bundle in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new WalletBundle\WalletBundle(),
        // ...
    );
}

Create your Wallet class:

<?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:

<?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:

<?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:

# 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:

$ php app/console doctrine:schema:update --force

Usages:

<?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!

License

The bundle is released under the MIT License.

walletbundle's People

Contributors

mkurc1 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.