This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

mjones1018466
mjones1018466

Can any one help, I am trying to create a plugin with Laravel Cashier to allow users to signup with a plan to access content.

I have Registered the Service Provider for Laravel Cashier in the boot function of the plugin.

I am also using the RainLab.User plugin user model to setStripeKey but when i do that i get an error.

Call to undefined method October\Rain\Database\QueryBuilder::setStripeKey()

I have added below Plugin.php below

 namespace MichaelJones\Stripe;

use App;
use MichaelJones\Stripe\Models\Settings;
use MichaelJones\Stripe\Models\Plans;
use System\Classes\PluginBase;
use RainLab\User\Models\User as UserModel;

/**
 * Stripe Plugin Information File
 */
class Plugin extends PluginBase
{

    public $require = ['RainLab.User'];

    public function pluginDetails()
    {
        return [
            'name'        => 'Stripe',
            'description' => 'No description provided yet...',
            'author'      => 'MichaelJones',
            'icon'        => 'icon-leaf'
        ];
    }

    public function registerNavigation()
    {

    }

    public function registerSettings()
    {
        return [
            'settings' => [
                'label'       => 'Stripe Payment Settings',
                'description' => 'Manage payment settings.',
                'category'    => 'Stripe',
                'icon'        => 'icon-cc-stripe',
                'class'       => 'MichaelJones\Stripe\Models\Settings',
                'order'       => 500
            ]
        ];
    }

    public function register()
    {
    }

    public function boot()
    {

        // Register ServiceProviders
        App::register('\Laravel\Cashier\CashierServiceProvider');

        if (Settings::get('live_mode')) {

            $secret_key = (Settings::get('live_mode') == 1 ? Settings::get('live_secret_key') : Settings::get('test_secret_key'));

            UserModel::setStripeKey($secret_key);
        }
    }

}

Last updated

daftspunky
daftspunky

You need to ensure the model, in this case "UserModel" implements the Billable trait, as per the docs:

use Laravel\Cashier\Billable;
use Laravel\Cashier\Contracts\Billable as BillableContract;

class User extends Model implements BillableContract {

    use Billable;

    protected $dates = ['trial_ends_at', 'subscription_ends_at'];

}

Unfortunately traits cannot be applied dynamically (this is why October uses behaviors instead) so you cannot apply this to the RainLab.User model. You will need to create your own "StripeProfile" model, implement the Billable trait and interface on it, then make the model "belongTo" the RainLab.User model.

This process is described in more detail in the following screencast:

https://vimeo.com/108040919

mjones1018466
mjones1018466

Thank you for the help i got it working.

mjones1018466
mjones1018466

new problem i can t seem to add the subscription to the user. I have added the stripe fields to the users table. Can you help Daftspunk

chris10207
chris10207

Did you find a solution to your problem ?

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.