PHP code example of yii2mod / yii2-cashier

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

    

yii2mod / yii2-cashier example snippets


$tableOptions = null;

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

$this->createTable('subscriptions', [
    '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_ends_at' => $this->timestamp()->null(),
    'ends_at' => $this->timestamp()->null(),
    'created_at' => $this->timestamp()->null(),
    'updated_at' => $this->timestamp()->null()
], $tableOptions);

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

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

use yii2mod\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_ends_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' => 'yii2mod\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