Skip to content

Commit

Permalink
Adds custom directive for CVV validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
wjames111 committed Nov 5, 2024
1 parent 2fff68d commit 64cfd40
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import cartService from 'common/services/api/cart.service'
import { validPaymentMethod } from 'common/services/paymentHelpers/validPaymentMethods'
import giveModalWindowTemplate from 'common/templates/giveModalWindow.tpl.html'
import { SignInEvent } from 'common/services/session/session.service'
import creditCardCvv from '../../../../common/directives/creditCardCvv.directive'

import template from './existingPaymentMethods.tpl.html'

Expand Down Expand Up @@ -144,7 +145,8 @@ export default angular
paymentMethodFormModal.name,
coverFees.name,
orderService.name,
cartService.name
cartService.name,
creditCardCvv.name
])
.component(componentName, {
controller: ExistingPaymentMethodsController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@
</label>
</div>

<form name="$ctrl.form" class="loading-overlay-parent">
<div class="form-group" ng-class="{'has-success': $ctrl.form.cvv.$valid, 'has-error': ($ctrl.form.cvv | showErrors) }">
<label for="cvv" translate>CVV</label>
<input id="cvv"
name="cvv"
type="cvv"
class="form-control form-control-subtle"
ng-model="$ctrl.cvv"
credit-card-cvv="cvv"
required
>
<div ng-if="($ctrl.form.cvv | showErrors)" ng-messages="$ctrl.form.cvv.$error">
<span class="help-block" ng-message="required" translate>CVV is required.</span>
</div>
</div>
<loading type="overlay" ng-if="$ctrl.isLoading"></loading>
</form>

<div class="panel panel-default tab-toggle mb0 mt"
ng-if="(($ctrl.cartData && $ctrl.cartData.items) || $ctrl.brandedCheckoutItem) && $ctrl.selectedPaymentMethod && $ctrl.selectedPaymentMethod['card-type']">
<div class="panel-title panel-heading" translate>{{'OPTIONAL'}}</div>
Expand Down
30 changes: 30 additions & 0 deletions src/common/directives/creditCardCvv.directive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import angular from 'angular'
import * as cruPayments from '@cruglobal/cru-payments/dist/cru-payments'

const directiveName = 'creditCardCvv'

const creditCardCvv = /* @ngInject */ function () {
const directiveDefinitionObject = {
restrict: 'A',
require: ['^ngModel', '^form'],
link: function (scope, element, attributes, controllers) {
const ngModel = controllers[0]
const formController = controllers[1]
const valueModel = formController[attributes.creditCardCvv]

scope.$watch(() => valueModel.$viewValue, function () {
ngModel.$validate()
})

ngModel.$validators.creditCardCvv = function (modelValue) {
console.log(modelValue)
return (cruPayments.creditCard.cvv.validate.minLength(modelValue)) && (cruPayments.creditCard.cvv.validate.maxLength(modelValue))
}
}
}
return directiveDefinitionObject
}

export default angular
.module(directiveName, [])
.directive(directiveName, creditCardCvv)

0 comments on commit 64cfd40

Please sign in to comment.