PHPackages                             c975l/purchasecredits-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. c975l/purchasecredits-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

c975l/purchasecredits-bundle
============================

Bundle to purchase and use credits within a website

v4.0(4y ago)299MITPHPPHP \*

Since Mar 16Pushed 4y ago1 watchersCompare

[ Source](https://github.com/975L/PurchaseCreditsBundle)[ Packagist](https://packagist.org/packages/c975l/purchasecredits-bundle)[ Docs](https://github.com/975L/PurchaseCreditsBundle)[ Fund](https://buymeacoff.ee/laurentmarquet)[ Fund](https://opencollective.com/laurent-marquet)[ RSS](/packages/c975l-purchasecredits-bundle/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (16)Versions (48)Used By (0)

PurchaseCreditsBundle
=====================

[](#purchasecreditsbundle)

PurchaseCreditsBundle does the following:

- Allows to purchase and use credits within your website,
- Interfaces with Stripe via [c975LPaymentBundle](https://github.com/975L/PaymentBundle) for its payment,
- Integrates with [c975LToolbarBundle](https://github.com/975L/ToolbarBundle),
- Emails the user about purchased credits and joins your Terms of sales as PDF to the email,

This Bundle relies on the use of [c975LPaymentBundle](https://github.com/975L/PaymentBundle), [Stripe](https://stripe.com/) and its [PHP Library](https://github.com/stripe/stripe-php). **So you MUST have a Stripe account.**

It is also recomended to use this with a SSL certificat to reassure the user.

As the Terms of sales MUST be sent to the user with the Gift-Voucher, you MUST provide a Route or url for this PDF file. If you don't have such, you may consider using [c975LSiteBundle](https://github.com/975L/SiteBundle) for its pre-defined models and [c975LPageEditBundle](https://github.com/975L/PageEditBundle) for its ability to create a PDF.

[PurchaseCreditsBundle dedicated web page](https://975l.com/en/pages/purchase-credits-bundle).

[PurchaseCreditsBundle API documentation](https://975l.com/apidoc/c975L/PurchaseCreditsBundle.html).

Bundle installation
-------------------

[](#bundle-installation)

### Step 1: Download the Bundle

[](#step-1-download-the-bundle)

**v3.x works with Symfony 4.x. Use v2.x for Symfony 3.x**Use [Composer](https://getcomposer.org) to install the library

```
    composer require c975l/purchasecredits-bundle
```

### Step 2: Configure the Bundle

[](#step-2-configure-the-bundle)

Check dependencies for their configuration:

- [Doctrine](https://github.com/doctrine/DoctrineBundle)
- [KnpPaginatorBundle](https://github.com/KnpLabs/KnpPaginatorBundle)
- [c975LPaymentBundle](https://github.com/975L/PaymentBundle)
- [c975LEmailBundle](https://github.com/975L/EmailBundle)
- [Stripe PHP Library](https://github.com/stripe/stripe-php)

c975LPurchaseCreditsBundle uses [c975L/ConfigBundle](https://github.com/975L/ConfigBundle) to manage configuration parameters. Use the Route "/purchase-credits/config" with the proper user role to modify them.

### Step 3: Enable the Routes

[](#step-3-enable-the-routes)

Then, enable the routes by adding them to the `/config/routes.yaml` file of your project:

```
c975_l_purchase_credits:
    resource: "@c975LPurchaseCreditsBundle/Controller/"
    type: annotation
    prefix: /
    #Multilingual website use the following
    #prefix: /{_locale}
    #defaults:   { _locale: '%locale%' }
    #requirements:
    #    _locale: en|fr|es
```

### Step 4: User entity

[](#step-4-user-entity)

Your User entity **MUST** have a property `credits` with proper and getter and setter, plus a `addCredits()` one, notice the `+=`, this method is used to add and subtract credits:

```
//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;
    }
```

### Step 5: Create MySql tables

[](#step-5-create-mysql-tables)

You can use `php bin/console make:migration` to create the migration file as documented in [Symfony's Doctrine docs](https://symfony.com/doc/current/doctrine.html) OR use Use `/Resources/sql/purchase-credits.sql` to create the table `user_transactions`. The `DROP TABLE` is commented to avoid dropping by mistake.

### Step 6: Override templates

[](#step-6-override-templates)

It is strongly recommended to use the [Override Templates from Third-Party Bundles feature](http://symfony.com/doc/current/templating/overriding.html) to integrate fully with your site.

For this, simply, create the following structure `/templates/bundles/c975LPurchaseCreditsBundle/` in your app and then duplicate the file `layout.html.twig` in it, to override the existing Bundle file.

In `layout.html.twig`, it will mainly consist to extend your layout and define specific variables, i.e. :

```
{% extends 'layout.html.twig' %}

{% block content %}
    {% block purchaseCredits_content %}
    {% endblock %}
{% endblock %}
```

### How to use

[](#how-to-use)

All the process for purchase and payment is managed via the bundle. All you have to implement on your side is the use of credits. You can do so with the following code:

```
