PHP code example of c975l / purchasecredits-bundle

1. Go to this page and download the library: Download c975l/purchasecredits-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

c975l / purchasecredits-bundle example snippets


//Your entity file
namespace App\Entity;

//Example is made using Doctrine, as the common one, but you can use any entity manager
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User
{
//...
    /**
     * Number of credits for User
     * @var int
     *
     * @ORM\Column(name="credits", type="integer", nullable=true)
     */
    protected $credits;

//...
    /**
     * Set credits
     * @param int
     * @return User
     */
    public function setCredits($credits)
    {
        $this->credits = $credits;

        return $this;
    }

    /**
     * Get credits
     * @return int
     */
    public function getCredits()
    {
        return $this->credits;
    }

    /**
     * Add credits (or subtracts if $credits is negative)
     * @param int
     * @return User
     */
    public function addCredits($credits)
    {
        $this->credits += $credits;

        return $this;
    }


//In your controller file
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use c975L\PurchaseCreditsBundle\Service\TransactionServiceInterface;

class PaymentController extends AbstractController
{
    /**
     * @Route("/YOUR_ROUTE",
     *    name="YOUR_ROUTE_NAME",
     *    methods={"HEAD", "GET"})
     */
    public function YOUR_METHOD_NAME(Request $request, TransactionServiceInterface $transactionService)
    {
        //Your stuff...

        //Gets the manager
        $em = $this->getDoctrine()->getManager();

        //Adds transaction, to keep trace of it and for user to see it in its list of transactions
        //You can call create() without argument, TransactionService will add an orderId built on the same scheme as Payment's one
        //The only restriction is that your orderId MUST NOT start with 'pmt' as this string is added to the Payment orderId, to provide a link to the payment
        $transaction = $this->transactionService->add('YOUR_OWN_ORDER_ID_OR_NULL', +-CREDITS, $description(), $this->getUser());

        //You need to flush DB as $transaction and $user are persisted but not flushed
        $em->flush();
    }
}