PHP code example of bigdropinc / yii2-cashier

1. Go to this page and download the library: Download bigdropinc/yii2-cashier 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/ */

    

bigdropinc / yii2-cashier example snippets


$tableOptions = null;

if ($this->db->driverName === 'mysql') {
    $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}

$this->createTable('subscription', [
    'id' => $this->primaryKey(),
    'user_id' => $this->integer()->notNull(),
    'name' => $this->string()->notNull(),
    'stripe_id' => $this->string()->notNull(),
    'stripe_plan' => $this->string()->notNull(),
    'quantity' => $this->integer()->notNull(),
    'trial_end_at' => $this->timestamp()->null(),
    'end_at' => $this->timestamp()->null(),
    'created_at' => $this->dateTime()->null(),
    'updated_at' => $this->dateTime()->null()
], $tableOptions);

$this->addColumn('user', 'stripe_id', $this->string());
$this->addColumn('user', 'card_brand', $this->string());
$this->addColumn('user', 'card_last_four', $this->string());
$this->addColumn('user', 'trial_end_at', $this->timestamp()->null());

php yii migrate --migrationPath=@vendor/bigdropinc/yii2-cashier/migrations

use bigdropinc\cashier\Billable;

class User extends ActiveRecord implements IdentityInterface
{
    use Billable;
}

'params' => [
        .....
        'stripe' => [
            'apiKey' => 'Your Secret Api Key'
        ],
    ],


$user = User::findOne(1);

$user->newSubscription('main', 'monthly')->create($creditCardToken);

$user->newSubscription('main', 'monthly')->create($creditCardToken, [
    'description' => 'Customer for [email protected]'
]);

$user->newSubscription('main', 'monthly')
     ->withCoupon('code')
     ->create($creditCardToken);

if ($user->subscribed('main')) {
    //
}

if ($user->subscription('main')->onTrial()) {
    //
}

if ($user->subscribedToPlan('monthly', 'main')) {
    //
}

if ($user->subscription('main')->cancelled()) {
    //
}

if ($user->subscription('main')->onGracePeriod()) {
    //
}

$user = User::findOne(1);

$user->subscription('main')->swap('provider-plan-id');

$user->subscription('main')->swap('provider-plan-id');

$user->subscription('main')
        ->skipTrial()
        ->swap('provider-plan-id');

$user = User::findOne(1);

$user->subscription('main')->incrementQuantity();

// Add five to the subscription's current quantity...
$user->subscription('main')->incrementQuantity(5);

$user->subscription('main')->decrementQuantity();

// Subtract five to the subscription's current quantity...
$user->subscription('main')->decrementQuantity(5);

$user->subscription('main')->updateQuantity(10);

public function taxPercentage() {
    return 20;
}

$user->subscription('main')->cancel();

if ($user->subscription('main')->onGracePeriod()) {
    //
}

$user->subscription('main')->resume();

$user = User::findOne(1);

$user->newSubscription('main', 'monthly')
            ->trialDays(10)
            ->create($creditCardToken);

if ($user->onTrial('main')) {
    //
}

if ($user->subscription('main')->onTrial()) {
    //
}

$user = new User([
    // Populate other user properties...
    'trial_end_at' => Carbon::now()->addDays(10),
]);

if ($user->onTrial()) {
    // User is within their trial period...
}

if ($user->onGenericTrial()) {
    // User is within their "generic" trial period...
}

$user = User::findOne(1);

$user->newSubscription('main', 'monthly')->create($creditCardToken);

'controllerMap' => [
        //Stripe webhook
        'webhook' => 'bigdropinc\cashier\controllers\WebhookController',
    ],

// Stripe Accepts Charges In Cents...
$user->charge(100);

$user->charge(100, [
    'custom_option' => $value,
]);

try {
    $response = $user->charge(100);
} catch (Exception $e) {
    //
}

// Stripe Accepts Charges In Cents...

$user->invoiceFor('One Time Fee', 500);

$user->invoiceFor('One Time Fee', 500, [
    'custom-option' => $value,
]);

$invoices = $user->invoices();

$dataProvider = new \yii\data\ArrayDataProvider([
            'allModels' => $invoices,
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);

 echo yii\grid\GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            [
                'label' => 'Invoice Date',
                'value' => function ($model) {
                    return $model->date()->toFormattedDateString();
                }
            ],
            [
                'label' => 'Total',
                'value' => function ($model) {
                    return $model->total();
                }
            ],
            [
                'header' => 'Action',
                'class' => 'yii\grid\ActionColumn',
                'template' => '{download}',
                'buttons' => [
                    'download' => function ($url, $model, $key) {
                        $options = [
                            'title' => Yii::t('yii', 'Download Invoice'),
                            'data-pjax' => '0',
                        ];
                        $url = ['download-invoice', 'invoiceId' => $model->id];
                        return \yii\helpers\Html::a('<span class="glyphicon glyphicon-download"></span>', $url, $options);
                    }
                ],
            ],
        ],
 ]);

public function actionDownloadInvoice($invoiceId)
{
    return $user->downloadInvoice($invoiceId, [
        'vendor' => 'Your Company',
        'product' => 'Your Product',
    ]);
}

// Create Invoice
$invoice = $user->invoiceFor('Invoice Description', 1000);
 
// Create the refund
$refund = $user->refund($invoice->charge);

var_dump($refund->amount); // 1000

php composer.phar