Question

The way I have my Stripe payment set up now it is a single monthly fee of $10. I want to create another fee of $25 that lets my users use the site for three months. I know how to create a customer, that is not my problem. I want the user to be able to click a link that would take them to either payment amount, or a simple way to let them choose in the registration page.

This is my provider (BillingServiceProvider.php):

<?php namespace Acme\Providers;

use Illuminate\Support\ServiceProvider;

class BillingServiceProvider extends ServiceProvider {

public function register()
{
    $this->app->bind('Acme\Billing\BillingInterface', 'Acme\Billing\StripeBilling');
}
}

The amount charged is declared in StripeBilling:

class StripeBilling implements BillingInterface {

public function __construct()
{
    Stripe::setApiKey(Config::get('stripe.secret_key'));
}

public function charge(array $data)
{
    try
    {
        return Stripe_Charge::create([
            'amount' => 1000, // $10
            'currency' => 'usd',
            'card' => $data['token'],
            'description' => $data['email']
        ]);

    }
}
}

BillingInterface has one function: charge(array $data)

I feel like I should make another file almost identical to StripeBilling with a different amount but I don't know how to access it in the provider.

Any help is appreciated, let me know if more information is needed.

Was it helpful?

Solution

What I would do is setup a table of subscription_types which would contain all your subscription types. It would be a simple table, probably just an id, name, and amount would be bare minimum. Then you can create your model for this... The users function here isn't too important. You really just need the class.

// models/SubscriptionType.php
class SubscriptionType extends Eloquent 
{
    public function users()
    {
        return $this->hasMany('User');
    }
}

Then to connect this table to your users, you can update your users table to also include a subscription_type_id and a subscription_status column. Then when your user picks a subscription they want to go with, you set that column.

$subscription = Subscription::find($idOfSubscriptionTheyWant);
Auth::user()->subscription()->associate($subscription);

Now that we know the user's desired subscription, during checkout, we know exactly how much to charge them...

$biller = App::make('Acme\Billing\BillingInterface');
$success = $biller->charge($data, Auth::user()->subscription->amount);
if($success) {
     $user = Auth::user();
     $user->subscription_status = 1;
     $user->save();
}

Lastly....

// Modify your charge function to something like this... 
public function charge(array $data, $amount)
{
    try
    {
        return Stripe_Charge::create([
            'amount' => $amount,
            'currency' => 'usd',
            'card' => $data['token'],
            'description' => $data['email']
        ]);

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top