braintreeapi Extension for easy implementation of Braintree's Payment Gateway

  1. Requirements
  2. Installation
  3. Usage
  4. Resources
  5. Change Log

Easily integrate a CC payment form with Braintree's API into Yii.

Version 1.2 - Small update just to update to Braintree's newest version of API (2.22.2)

Version 1.1 - I have added the ability to save Customers, Credit Cards and Addresses in the Vault. See change log for more info.

I plan on adding more functionality like setting up Recurring Payments.

Requirements

Yii 1.1 or + Requires PHP version 5.2 or higher and the PHP cURL extension (Braintree Requirements)

A sandbox or production account from BrainTree Payment Gateway, need following api acount info:

  • merchant_id
  • public_key
  • private_key
  • Client-Side Encryption Key

Installation

Download the extension here or from Repo below and extract to 'protected/extensions/BraintreeApi'. Modify your configuration file and add fields to the param array with the appropriate info:

// application-level parameters that can be accessed
    'params'=>array(
        // this is used in contact page
        'adminEmail'=>'your@email.com',
        'braintreeapi' => array(
            'class' => 'ext.BraintreeApi.BraintreeApi',
            'environment' => 'sandbox', //'sandbox' or 'production'
            'merchant_id' => '',
            'public_key' => '',
            'private_key' => '',
            'clientside_key' => '',
        ),
    ),

Usage

On your controller action import the extension and create a new instance of BraintreeApi and pass it to your form. You can then use the Widget to add all the fields to your form easily if you want.

Any Controller (Just an example):

public function actionPayment() {
        $model = User::model()->findByPk(Yii::app()->user->id);
        Yii::import('ext.BraintreeApi.models.BraintreeCCForm');
        $payment = new BraintreeCCForm('charge'); //also option for 'customer', 'address', and 'creditcard'
        
        if(isset($_POST['BraintreeCCForm'])) {
            $payment->setAttributes($_POST['BraintreeCCForm']);
            //$payment->customerId = '5534438'; //needed for 'address' and 'creditcard' scenarios
            if($payment->validate()) {
                $result = $payment->send();
                if($result) {
                    //Success
                }
            }
        }
        
        $this->render('payment',array('model'=>$model,'payment'=>$payment));
    }

Inside a View:

The fields array in the widget lets you specify what fields you want to show up on the form. If you remove it completely it will default to showing just credit card number, cvv, and expiration month/year.

//start $form widget
$this->widget('ext.BraintreeApi.widgets.CCForm', array(
        'form' => $form,
        'form_id' => 'payment-form', //Now optional as can be retrieved in widget from $form
        'model' => $payment,
        'values' => $payment->attributes,  //Now optional as can be retrieved in widget from $payment
        //'type' => 'customer', //can use this instead of fields array below, options are 'customer', 'creditcard', 'charge_min', 'address'
        'fields' => array(
            'amount',
            'orderId',
            'creditCard' => array('number','cvv','month','year','name'),
            'merchantAccountId',
            'customer' => array('firstName','lastName','company','phone','fax','website','email'),
            'billing' => array('firstName','lastName','company','streetAddress','extendedAddress','locality','region','postalCode','countryCodeAlpha2'),
            'shipping' => array('firstName','lastName','company','streetAddress','extendedAddress','locality','region','postalCode','countryCodeAlpha2'),
        ),
        
    ));

Resources

Change Log

Version 1.2

  • Updated to version 2.22.2 of Braintree's PHP API code

Version 1.1

  • Created a new model BraintreeCCForm that extends CFormModel for managing form input
  • Added default types for widget.
  • Reduced required inputs for widget
  • $this->values can be pulled from $this->model
  • $this->form_id can be pulled from $this->form
  • Added option to save Customers, Credit Cards and Addresses in Vault
  • Added option to submitforsettlement and storetovault on singleCharge
2 0
8 followers
496 downloads
Yii Version: Unknown
License: BSD-2-Clause
Category: Web Service
Developed by: pitchinnate
Created on: Jul 12, 2013
Last updated: 10 years ago

Downloads

show all

Related Extensions