dmitry-kulikov/yii2-braintree

View on GitHub
README.md

Summary

Maintainability
Test Coverage
# yii2-braintree

Integrate a credit card payment form with Braintree's API into Yii 2.
Inspired by [braintreeapi](https://www.yiiframework.com/extension/braintreeapi).

[![License](https://poser.pugx.org/kdn/yii2-braintree/license)](https://packagist.org/packages/kdn/yii2-braintree)
[![Latest Stable Version](https://poser.pugx.org/kdn/yii2-braintree/v/stable)](https://packagist.org/packages/kdn/yii2-braintree)
[![Code Coverage](https://scrutinizer-ci.com/g/dmitry-kulikov/yii2-braintree/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/dmitry-kulikov/yii2-braintree/?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/dmitry-kulikov/yii2-braintree/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/dmitry-kulikov/yii2-braintree/?branch=master)
[![Code Climate](https://codeclimate.com/github/dmitry-kulikov/yii2-braintree/badges/gpa.svg)](https://codeclimate.com/github/dmitry-kulikov/yii2-braintree)

## Requirements

- PHP 7.2 or later;
- Yii framework 2.

## Installation

The preferred way to install this extension is through [Composer](https://getcomposer.org).

To install, either run

```sh
php composer.phar require kdn/yii2-braintree "*"
```

or add

```text
"kdn/yii2-braintree": "*"
```

to the `require` section of your `composer.json` file.

## Usage

You should add Braintree component to your Yii configuration first:

```php
'components' => [
    'braintree' => [
        'class' => 'tuyakhov\braintree\Braintree',
        'merchantId' => 'YOUR_MERCHANT_ID',
        'publicKey' => 'YOUR_PUBLIC_KEY',
        'privateKey' => 'YOUR_PRIVATE_KEY',
    ],
]
```

`BraintreeForm` provides all basic operations for sales and stores customer info. Operation name equals scenario name.
Available scenarios:

- `address` - create an address -
  [API documentation](https://developer.paypal.com/braintree/docs/reference/request/address/create)
- `creditCard` - create a credit card -
  [API documentation](https://developer.paypal.com/braintree/docs/reference/request/credit-card/create)
- `customer` - create a customer -
  [API documentation](https://developer.paypal.com/braintree/docs/reference/request/customer/create)
- `sale` - create a transaction -
  [API documentation](https://developer.paypal.com/braintree/docs/reference/request/transaction/sale)
- `saleFromVault` - create a transaction from the vault -
  [API documentation](https://developers.braintreepayments.com/ios+php/reference/request/transaction/sale)

Action example:

```php
public function actionSale() {
    $model = new BraintreeForm();
    $model->setScenario('sale');
    if ($model->load(Yii::$app->request->post()) && $model->send()) {
        // do something
    }

    return $this->render('purchase', ['model' => $model]);
}
```

Form widget for a view:

```php
use tuyakhov\braintree\ActiveForm;
use yii\helpers\Html;
use yii\widgets\MaskedInput;

$form = ActiveForm::begin();
?>

<?= $form->field($model, 'creditCard_number'); ?>
<?= $form->field($model, 'creditCard_cvv'); ?>
<?=
$form->field($model, 'creditCard_expirationDate')
    ->widget(MaskedInput::class, ['mask' => '99/9999']);
?>
<?= $form->field($model, 'amount'); ?>
<?= Html::submitButton(); ?>

<?php
ActiveForm::end();
```