PHP code example of yii2mod / yii2-braintree

1. Go to this page and download the library: Download yii2mod/yii2-braintree 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-braintree 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(),
    'userId' => $this->integer()->notNull(),
    'name' => $this->string()->notNull(),
    'braintreeId' => $this->string()->notNull(),
    'braintreePlan' => $this->string()->notNull(),
    'quantity' => $this->integer()->notNull(),
    'trialEndAt' => $this->timestamp()->null(),
    'endAt' => $this->timestamp()->null(),
    'createdAt' => $this->timestamp()->null(),
    'updatedAt' => $this->timestamp()->null()
], $tableOptions);

$this->addColumn('user', 'braintreeId', $this->string());
$this->addColumn('user', 'paypalEmail', $this->string());
$this->addColumn('user', 'cardBrand', $this->string());
$this->addColumn('user', 'cardLastFour', $this->string());
$this->addColumn('user', 'trialEndAt', $this->timestamp()->null());

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

use yii2mod\braintree\Billable;

class User extends ActiveRecord implements IdentityInterface
{
    use Billable;
}

'bootstrap' => [
    // your other bootstrap components
    [
       'class' => 'yii2mod\braintree\BraintreeBootstrap',
       'environment' => 'braintree env',
       'merchantId' => 'braintree merchantId',
       'publicKey' => 'braintree public key',
       'privateKey' => 'braintree private 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');

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...
    'trialEndAt' => 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' => [
        //Braintree webhook
        'webhook' => 'yii2mod\braintree\controllers\WebhookController',
    ],

// Braintree Accepts Charges In Dollars...
$user->charge(1);

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

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

// Braintree Accepts Charges In Dollars...

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

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

$invoices = $user->invoices(); // yii2mod\collection\Collection object

$dataProvider = new \yii\data\ArrayDataProvider([
            'allModels' => $invoices->all(),
            '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',
    ]);
}

php composer.phar