PHP code example of tmyers273 / laravel-stripe-billing

1. Go to this page and download the library: Download tmyers273/laravel-stripe-billing 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/ */

    

tmyers273 / laravel-stripe-billing example snippets

 artisan migrate

StripeBilling::createTestToken();
StripeBilling::setApiKey($apiKey);
StripeBilling::setCurrency($currency);

// Create a customer from token (new default card will be created)
$user->retrieveOrCreateStripeCustomer($token);
$user->retrieveStripeCustomer($token);

// Check if user is already subscribed to product
// Accepts Price object, Product object, string (name of Product or Price) e.g. basic, basic_yearly_90
$user->isSubscribedTo($product);

// Check if user is subscribed to a specific $price
$user->isSubscribedStrictlyTo($price);

// true or false
$user->hasActiveSubscriptions();

// or for subscription
// accepts Price|Product|string (name of Product or Price)
$subscription->isFor($product);

$user->getSubscriptionFor($product)->isActive();
$user->getSubscriptionFor('basic-monthly-10')->cancelNow();

// in the vast majority of cases your users will be only allowed
// to have one active subscription, so use this method when applicable
$user->getFirstActiveSubscription();

// Create the plans
$bronzePlan= Plan::create([
    'description' => 'Bronze Plan',
    'name' => 'bronze',
]);

// Create the Price
$bronzeMonthly = Price::create([
    'product_id' => $bronzePlan->id, // parent plan id
    'description' => 'Monthly Bronze Plan',
    'name' => 'bronze_monthly_50.00',
    'interval' => 'month',
    'stripe_product_id' => 'bronze_monthly', // this needs to be created in Stripe first
    'price' => 5000,
    'active' => true,
]);

// Accepts Plan object or string representing Plan name e.g. bronze_monthly_50.00
$user->subscribeTo($bronzeMonthly); // for already existing stripe customer
$user->subscribeTo($bronzeMonthly, $token); // for user without created customer

$user->subscriptions;
$user->activeSubscriptions;

$subscription->cancelAtPeriodEnd();
$subscription->cancelNow();

$subscription->resume();

$subscription->trialEndAt($unixTimestamp);

$days = 10;
$subscription->addDaysToTrial($days);

// Accepts Plan object
$subscription->changeTo($basicMonthlyPlan);

Subscription::active()->get(); // get all active subscriptions
Subscription::canceledAndArchived()->get(); // get all canceled and non active subscriptions

$user->defaultCard;

$card = $user->addCardFromToken($token);

$user->setCardAsDefault($card);

$user->hasDefaultCard(); //true or false

$user->hasDefaultCard($card); //true or false

$user->removeCard($card);

$card->isOwnedBy($user); // true or false
$card->isDefault(); // true or false

$user->charge(2500); // Charge 25$

$user->chargeByToken(1799, 'some token from stripe.js'); // Charge 17.99$

/**
* @param int $amount
* @param Card $card
* @param array $params
* @return mixed
*/
$user->chargeCard(1799, $card); // Charge 17.99$

$user->applyCoupon($coupon);

'subscription' => \TMyers\StripeBilling\Middleware\SubscriptionMiddleware::class,

'models' => [
        'owner' => 'App\User',
        'subscription' => \TMyers\StripeBilling\Models\Subscription::class,
        'pricing_plan' => \TMyers\StripeBilling\Models\Price::class,
        'plan' => \TMyers\StripeBilling\Models\Plan::class,
        'card' => \TMyers\StripeBilling\Models\Card::class,
    ],
    
    'tables' => [
        'owner' => 'users',
        'subscriptions' => 'subscriptions',
        'pricing_plans' => 'pricing_plans',
        'plans' => 'plans',
        'cards' => 'cards',
    ],
    
    'unique_active_subscription' => false,
];