diff --git a/CHANGE.md b/CHANGE.md index 4de6592..9c1622b 100644 --- a/CHANGE.md +++ b/CHANGE.md @@ -1,3 +1,10 @@ +version 1.0.3 +============= +**Date:** 23-Mar-2016 + +1. Added classes CreditCardCVCode, CreditCardExpiry, and CreditCardNumber. These extend from \kartik\base\InputWidget and + are used as per README documentation. + version 1.0.2 ============= **Date:** 29-Aug-2015 diff --git a/CreditCard.php b/CreditCard.php deleted file mode 100644 index 0460883..0000000 --- a/CreditCard.php +++ /dev/null @@ -1,168 +0,0 @@ - - */ -class CreditCard extends Widget -{ - /** @var ActiveForm the bootstrap/ActiveForm object */ - public $form; - - /** @var string Credit card number attribute name */ - public $numberAttribute = 'creditCard_number'; - - /** @var string Credit card expiry attribute name */ - public $expiryAttribute = 'creditCard_expirationDate'; - - /** @var string Credit card cvc/cvv/ccv attribute name */ - public $cvcAttribute = 'creditCard_cvv'; - - private $model; - - public function init() - { - parent::init(); - $this->model = new CreditCardModel([ - 'numberAttribute' => $this->numberAttribute, - 'expiryAttribute' => $this->expiryAttribute, - 'cvcAttribute' => $this->cvcAttribute, - ]); - $this->checkConfig(); - $this->registerTranslations(); - CreditCardAsset::register($this->view); - } - - public function registerTranslations() - { - Yii::setAlias("@creditcard", __DIR__); - Yii::$app->i18n->translations['creditcard'] = [ - 'class' => 'yii\i18n\PhpMessageSource', - 'sourceLanguage' => 'en-US', - 'basePath' => '@creditcard/messages', - ]; - } - - /** - * Generate credit card number form field - * @param array $fieldConfig any special config and overriding config for the form field - * @return $this form field html - */ - public function number($fieldConfig = []) - { - // default field configuration - $fieldConfig = ArrayHelper::merge([ - 'inputTemplate' => '
{input}
', - 'inputOptions' => [ - 'type' => 'tel', - 'autocomplete' => 'cc-number', - 'placeholder' => Yii::t('creditcard', 'Card number'), - ], - ], $fieldConfig); - // ensure id is correct and consistent with validation attribute in active form - $fieldConfig['inputOptions']['id'] = Html::getInputId($this->model, $this->numberAttribute); - - // bind event handlers - $this->view->registerJs("jQuery('#{$fieldConfig['inputOptions']['id']}').ccNumber();"); - - // build the field - return $this->form->field($this->model, $this->numberAttribute, $fieldConfig)->textInput(); - } - - /** - * Generate credit card expiry form field - * @param array $fieldConfig any special config and overriding config for the form field - * @return $this form field html - */ - public function expiry($fieldConfig = []) - { - // default field configuration - $fieldConfig = ArrayHelper::merge([ - 'inputOptions' => [ - 'autocomplete' => 'cc-exp', - 'placeholder' => Yii::t('creditcard', 'MM / YY'), - ], - ], $fieldConfig); - // ensure id is correct and consistent with validation attribute in active form - $fieldConfig['inputOptions']['id'] = Html::getInputId($this->model, $this->expiryAttribute); - - // Mask credit card expiry using stripes https://github.com/stripe/jquery.payment - $this->view->registerJs("jQuery('#{$fieldConfig['inputOptions']['id']}').payment('formatCardExpiry');"); - - // build the field - return $this->form->field($this->model, $this->expiryAttribute, $fieldConfig)->textInput(); - } - - /** - * Generate credit card cvc form field - * @param array $fieldConfig any special config and overriding config for the form field - * @return $this form field html - */ - public function cvc($fieldConfig = []) - { - // default field configuration - $fieldConfig = ArrayHelper::merge([ - 'inputOptions' => [ - 'placeholder' => Yii::t('creditcard', 'CV code'), - ], - ], $fieldConfig); - // ensure id is correct and consistent with validation attribute in active form - $fieldConfig['inputOptions']['id'] = Html::getInputId($this->model, $this->cvcAttribute); - - // Mask credit card CVC/CVV/CSC (security verification code) using stripes https://github.com/stripe/jquery.payment - $this->view->registerJs("jQuery('#{$fieldConfig['inputOptions']['id']}').payment('formatCardCVC');"); - - // build the field - return $this->form->field($this->model, $this->cvcAttribute, $fieldConfig)->textInput(); - } - - /** - * Check the configuration is valid - * - * @throws InvalidConfigException - */ - private function checkConfig() - { - /** - * config checks - */ - - if (!$this->form) { - throw new InvalidConfigException("The 'form' property must be defined and must be an instance of - yii\widgets\ActiveForm"); - } elseif (!$this->form instanceof \yii\widgets\ActiveForm) { - throw new InvalidConfigException("The 'form' property must be an instance of yii\widgets\ActiveForm"); - } - } -} \ No newline at end of file diff --git a/CreditCardCVCode.php b/CreditCardCVCode.php index c73ae6e..5796a85 100644 --- a/CreditCardCVCode.php +++ b/CreditCardCVCode.php @@ -4,7 +4,7 @@ * @package andrewblake1\yii2-credit-card * @license https://github.com/andrewblake1/yii2-credit-card/blob/master/LICENSE.md MIT License * @link https://github.com/andrewblake1/yii2-credit-card - * @version 1.0.2 + * @version 1.0.3 */ namespace andrewblake1\creditcard; diff --git a/CreditCardExpiry.php b/CreditCardExpiry.php index 369d75a..839ca11 100644 --- a/CreditCardExpiry.php +++ b/CreditCardExpiry.php @@ -4,7 +4,7 @@ * @package andrewblake1\yii2-credit-card * @license https://github.com/andrewblake1/yii2-credit-card/blob/master/LICENSE.md MIT License * @link https://github.com/andrewblake1/yii2-credit-card - * @version 1.0.2 + * @version 1.0.3 */ namespace andrewblake1\creditcard; diff --git a/CreditCardNumber.php b/CreditCardNumber.php index 2ba6b89..c391414 100644 --- a/CreditCardNumber.php +++ b/CreditCardNumber.php @@ -4,7 +4,7 @@ * @package andrewblake1\yii2-credit-card * @license https://github.com/andrewblake1/yii2-credit-card/blob/master/LICENSE.md MIT License * @link https://github.com/andrewblake1/yii2-credit-card - * @version 1.0.2 + * @version 1.0.3 */ namespace andrewblake1\creditcard; diff --git a/README.md b/README.md index 6c34107..dd796cb 100644 --- a/README.md +++ b/README.md @@ -4,22 +4,10 @@ yii2-credit-card [![Latest Stable Version](https://poser.pugx.org/andrewblake1/yii2-credit-card/v/stable)](https://packagist.org/packages/andrewblake1/yii2-credit-card) [![License](https://poser.pugx.org/andrewblake1/yii2-credit-card/license)](https://packagist.org/packages/andrewblake1/yii2-credit-card) -Yii2 Bootstrap 3 component, providing client validated and masked credit card number, expiry and cvc fields with credit card icon changing to match credit card type when detectable. +Yii2 Bootstrap 3 components, providing client validated and masked credit card number, expiry and cvc fields with credit card icon changing to match credit card type when detectable. Uses client validation courtesy of Stripe (https://github.com/stripe/jquery.payment) and works with validation in ActiveForm. -Options - -- **form** ActiveForm the bootstrap/ActiveForm object, *required*. - -- **numberAttribute** string Credit card number attribute name, *defaults to 'creditCard_number'*. - -- **expiryAttribute** string Credit card number expiry attribute name, *defaults to 'creditCard_expirationDate'*. - -- **cvcAttribute** string Credit card cvc/cvv/ccv attribute name, *defaults to 'creditCard_cvv'*. - -In addition, the methods number(), expiry(), and cvc() accept an optional field configuration array as used by yii\bootstrap\ActiveForm::field. - ### The future 1. Integrate first with Braintree and potentially Stripe e.g. any requirements to encode the fields when posting request etc. @@ -46,23 +34,34 @@ to the `require` section of your `composer.json` file. ## Latest Release -> NOTE: The latest version of the module is v1.0.2. Refer the [CHANGE LOG](https://github.com/andrewblake1/yii2-credit-card/blob/master/CHANGE.md) for details. +> NOTE: The latest version of the module is v1.0.3. Refer the [CHANGE LOG](https://github.com/andrewblake1/yii2-credit-card/blob/master/CHANGE.md) for details. ## Usage in view +Note that the input names here have been chosen to fit work with the tuyakhov\braintree extension + ```php -
- $form]);?> -
number() ?>
-
expiry() ?>
-
cvc() ?>
-
+
+
+
+ field($bookingForm, 'creditCard_number')->widget(CreditCardNumber::className(), ['name' => 'creditCard_number',]) ?> +
+
+ field($bookingForm, 'creditCard_expirationDate')->widget(CreditCardExpiry::className(), ['name' => 'creditCard_expirationDate',]) ?> +
+
+ field($bookingForm, 'creditCard_cvv')->widget(CreditCardCVCode::className(), ['name' => 'creditCard_cvv',]) ?> +
+
+
``` ## License diff --git a/assets/js/creditcard.js b/assets/js/creditcard.js index b501755..022659c 100644 --- a/assets/js/creditcard.js +++ b/assets/js/creditcard.js @@ -3,7 +3,7 @@ * @package andrewblake1\yii2-credit-card * @license http://opensource.org/licenses/MIT MIT License * @link https://github.com/andrewblake1/yii2-credit-card - * @version 1.0.2 + * @version 1.0.3 */ (function($) { diff --git a/models/CreditCardModel.php b/models/CreditCardModel.php index bf00630..d2e64f4 100644 --- a/models/CreditCardModel.php +++ b/models/CreditCardModel.php @@ -4,7 +4,7 @@ * @package andrewblake1\yii2-credit-card * @license http://opensource.org/licenses/MIT MIT License * @link https://github.com/andrewblake1/yii2-credit-card - * @version 1.0.2 + * @version 1.0.3 */ namespace andrewblake1\creditcard\models; diff --git a/validators/CCCVCodeValidator.php b/validators/CCCVCodeValidator.php index 3154dfe..519491f 100644 --- a/validators/CCCVCodeValidator.php +++ b/validators/CCCVCodeValidator.php @@ -4,7 +4,7 @@ * @package andrewblake1\yii2-credit-card * @license http://opensource.org/licenses/MIT MIT License * @link https://github.com/andrewblake1/yii2-credit-card - * @version 1.0.2 + * @version 1.0.3 */ namespace andrewblake1\creditcard\validators; diff --git a/validators/CCNumberValidator.php b/validators/CCNumberValidator.php index ac60e3c..5013659 100644 --- a/validators/CCNumberValidator.php +++ b/validators/CCNumberValidator.php @@ -4,7 +4,7 @@ * @package andrewblake1\yii2-credit-card * @license http://opensource.org/licenses/MIT MIT License * @link https://github.com/andrewblake1/yii2-credit-card - * @version 1.0.2 + * @version 1.0.3 */ namespace andrewblake1\creditcard\validators;