redooor / redminportal

A Laravel package as a backend administrating tool for Content Management and Ecommerce sites.
MIT License
43 stars 16 forks source link

Use laravel cashier #64

Closed ulshamim closed 9 years ago

ulshamim commented 9 years ago

Can I use laravel cashier with this package? If yes then somehow $user->subscription('basic')->create(Input::get('stripeToken')); code is not working. I have added the code after $user->addGroup($adminGroup); in UserController.php file.

kongnir commented 9 years ago

Hi @ulshamim , I'm not sure about Laravel Cashier because we don't use it in Redminportal. But technically there shouldn't be any problem.

I think your problem may be due to how you call the User model.

We're using Cartalyst/Sentry for user management, that's why you see:

$user = Sentry::findUserById(1);

I'm not sure if Sentry supports Laravel Cashier directly.

How about try to get User model directly by using:

$user = User::find(1);

This calls the User model in Eloquent, so it should work with most Laravel's built-in functions.

Important Do note that if you get User model this way all the other codes with $user after that cannot be used directly because Sentry has its own methods. For example, $user->addGroup() is a Sentry specific method.

ulshamim commented 9 years ago

hmm, I tried cashier without using redminportal package and it's working. But somehow with this package it's not working. It shows an error: Call to undefined method Illuminate\Database\Query\Builder::subscription() . Error file path: C:\xampp\htdocs\inspeckd\workbench\redooor\redminportal\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php . Can you please help me to short-out this?

kongnir commented 9 years ago

Hi @ulshamim , are you using Laravel 4.2?

Try adding the dependency to workbench/redooor/redminportal/composer.json

"laravel/cashier": "~2.0"

Then edit workbench/redooor/redminportal/src/Redooor/Redminportal/RedminportalServiceProvider.php

Edit boot() function to this

public function boot()
{
        $this->package('redooor/redminportal');

        $this->app->register('Maatwebsite\Excel\ExcelServiceProvider');
        $this->app->register('Cartalyst\Sentry\SentryServiceProvider');
        $this->app->register('Laravel\Cashier\CashierServiceProvider');

        include __DIR__.'/../../routes.php';
        include __DIR__.'/../../filters.php';
}

Question: Is this feature essential to the Redminportal package? If it doesn't affect or change the functionality of the Redminportal package, you should do this at the app/ folder instead of workbench/.

ulshamim commented 9 years ago

Yes I am laravel 4.2 . I followed your instructions but there was same error. Should I run composer update in workbench/redooor/redminportal? If yes then how can I do so?

kongnir commented 9 years ago

Hi @ulshamim , yes you need to run composer update again.

?> cd workbench/redooor/redminportal
?> composer update --prefer-dist -vvv --profile
?> php ../../../artisan dump-autoload

The additional argument "--profile" will save the packages in your computer so it's faster next time when you run composer update again. "-vvv" is verbose mode, so you'll see what's going on. "--prefer-dist" just tells composer to use stable version whenever possible.

If you have composer.phar downloaded on the root folder, use this command instead.

?> cd workbench/redooor/redminportal
?> php ../../../composer.phar update --prefer-dist -vvv --profile
?> php ../../../artisan dump-autoload
ulshamim commented 9 years ago

Same issue. these are the file: 1) workbench/redooor/redminportal/composer.json

{
    "name": "redooor/redminportal",
    "description": "RedminPortal is a backend administrating tool for Content Management and Ecommerce sites.",
    "keywords": ["package", "laravel", "redooor", "redmin", "portal", "ecommerce", "backend"],
    "license": "MIT",
    "authors": [
        {
            "name": "Redooor LLP",
            "email": "support@redooor.com",
            "homepage": "http://www.redooor.com"
        }
    ],
    "require": {
        "php": ">=5.3.0",
        "cartalyst/sentry": "2.1.*",
        "doctrine/dbal": "~2.3",
        "imagine/imagine": "0.5.*",
        "maatwebsite/excel": "1.*",
        "laravel/cashier": "~2.0"
    },
    "require-dev": {
        "twbs/bootstrap": "3.1.*",
        "phpunit/phpunit": "4.0.*",
        "mockery/mockery": "0.9.*",
        "orchestra/testbench": "~2.2.0@dev"
    },
    "autoload": {
        "classmap": [
            "src/controllers",
            "src/migrations",
            "src/models",
            "src/helpers",
            "src/config",
            "src/seeds",
            "src/lang",
            "tests/bases",
            "tests/RedminTestCase.php"
        ],
        "psr-0": {
            "Redooor\\Redminportal": "src/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable" : true
}

2) workbench/redooor/redminportal/src/Redooor/Redminportal/RedminportalServiceProvider.php:

<?php namespace Redooor\Redminportal;

use Illuminate\Support\ServiceProvider;

class RedminportalServiceProvider extends ServiceProvider {

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
    * Bootstrap the application events.
    *
    * @return void
    */
    public function boot()
    {
        $this->package('redooor/redminportal');

        $this->app->register('Maatwebsite\Excel\ExcelServiceProvider');
        $this->app->register('Cartalyst\Sentry\SentryServiceProvider');
                $this->app->register('Laravel\Cashier\CashierServiceProvider');

        include __DIR__.'/../../routes.php';
        include __DIR__.'/../../filters.php';
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app['redminportal'] = $this->app->share(function($app)
        {
            return new Redminportal;
        });

        $this->app->booting(function()
        {
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
            $loader->alias('Redminportal', 'Redooor\Redminportal\Facades\Redminportal');
            $loader->alias('Excel', 'Maatwebsite\Excel\Facades\Excel');
            $loader->alias('Sentry', 'Cartalyst\Sentry\Facades\Laravel\Sentry');
        });

        // Get config loader
        $loader = $this->app['config']->getLoader();

        // Get environment name
        $env = $this->app['config']->getEnvironment();

        // Add package namespace with path set, override package if app config exists
        if (file_exists(app_path() .'/config/packages/redooor/redminportal')) {
            $loader->addNamespace('redminportal', app_path() .'/config/packages/redooor/redminportal');
        } else {
            $loader->addNamespace('redminportal',__DIR__.'/../../config');
        }

        // Load package override config file
        $menu = $loader->load($env,'menu','redminportal');
        $image = $loader->load($env,'image','redminportal');
        $translation = $loader->load($env,'translation','redminportal');

        // Override value
        $this->app['config']->set('redminportal::menu',$menu);
        $this->app['config']->set('redminportal::image',$image);
        $this->app['config']->set('redminportal::translation',$translation);
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array('redminportal');
    }

}
  1. this is function post function App/controller/UserController.php
<?php

use Redooor\Redminportal\User;

class UserController extends BaseController {

    protected $model;

    public function __construct(User $user) {
        $this->model = $user;
    }

    public function getCreate() {
        $groups = Sentry::getGroupProvider()->findAll();
        $roles = array();

        foreach ($groups as $group) {
            $roles[$group->id] = $group->name;
        }

        return View::make('users.signup')->with('roles', $roles);
    }

    public function postStore() {

        $sid = Input::get('id');

        $rules = array(
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required'
        );

        if (isset($sid)) {
            $rules['password'] = 'confirmed|min:6';
        } else {
            $rules['password'] = 'required|confirmed|min:6';
        }

        $validation = Validator::make(Input::all(), $rules);

        if (!$validation->passes()) {
            if (isset($sid)) {
                return Redirect::to('users/edit/' . $sid)->withErrors($validation)->withInput();
            } else {
                return Redirect::to('users/create')->withErrors($validation)->withInput();
            }
        }

        $first_name = Input::get('first_name');
        $last_name = Input::get('last_name');
        $email = Input::get('email');
        $password = Input::get('password');
        $role = 4;
        $activated = true;
        $subscriptiont_type = Input::get('subscriptiont_type');

        if (isset($sid)) {
            // Edit existing
            try {
                $user = Sentry::findUserById($sid);

                $user->email = $email;
                if ($password != '') {
                    $user->password = $password;
                }
                $user->first_name = $first_name;
                $user->last_name = $last_name;
                $user->activated = $activated;

                // Find user's group
                $old_group = $user->getGroups()->first();
                $new_group = Sentry::findGroupById($role);

                // Assign the group to the user
                if ($old_group->id != $new_group->id) {
                    $user->removeGroup($old_group);
                    $user->addGroup($new_group);
                }

                // Update the user
                if (!$user->save()) {
                    $errors = new \Illuminate\Support\MessageBag;
                    $errors->add(
                            'editError', "The user cannot be updated due to some problem. Please try again."
                    );
                    return Redirect::to('users/edit/' . $sid)->withErrors($errors)->withInput();
                }
            } catch (Exception $exp) {
                $errors = new \Illuminate\Support\MessageBag;
                $errors->add(
                        'editError', "The user cannot be found because it does not exist or may have been deleted."
                );
                return Redirect::to('redminportal::pages/404');
            }
        } else {
            try {
                // Create the user
                $user = Sentry::getUserProvider()->create(array(
                    'email' => $email,
                    'password' => $password,
                    'first_name' => $first_name,
                    'last_name' => $last_name,
                    'activated' => $activated,
                ));

                // Find the group using the group id
                $adminGroup = Sentry::getGroupProvider()->findById($role);

                // Assign the group to the user
                $user->addGroup($adminGroup);

                $user->subscription('basic')->create(Input::get('stripeToken'));
            } catch (Exception $exp) {
                $errors = new \Illuminate\Support\MessageBag;
                $errors->add(
                        'editError', "The user cannot be created due to some problem. Please try again."
                );
                return Redirect::to('users/create')->withErrors($errors)->withInput();
            }
        }

        return Redirect::to('/');
    }

}

4) App/model/User

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Laravel\Cashier\BillableTrait;
use Laravel\Cashier\BillableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface, BillableInterface {

    use UserTrait,
        BillableTrait,
        RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');
    protected $dates = ['trial_ends_at', 'subscription_ends_at'];

}
kongnir commented 9 years ago

workbench/redooor/redminportal/composer.json and workbench/redooor/redminportal/src/Redooor/Redminportal/RedminportalServiceProvider.php look right.

For the User model, can you add the interface to the User model in the package instead? It should be under workbench/redooor/redminportal/src/models/User.php

As for UserController.php, I'm not sure if you can use it with Sentry. But you try changing the User model first. Let's see.

ulshamim commented 9 years ago

Same issue. Here is the workbench/redooor/redminportal/src/models/User.php code:

<?php namespace Redooor\Redminportal;

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Database\Eloquent\Model;
use Laravel\Cashier\BillableTrait;
use Laravel\Cashier\BillableInterface;

class User extends Model implements UserInterface, RemindableInterface, BillableInterface {
    use BillableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password');
         protected $dates = ['trial_ends_at', 'subscription_ends_at'];

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }

}
kongnir commented 9 years ago

Now in UserController.php, can you change this line:

$user->subscription('basic')->create(Input::get('stripeToken'));

to

$user_model = User::find($user->id);
$user_model->subscription('basic')->create(Input::get('stripeToken'));

What is the error message?

ulshamim commented 9 years ago

Yesssssssssssssssssssssssssssss .It's working. Great.

kongnir commented 9 years ago

Cool, I'm closing this issue then. :)