gaf-mobile
Version:
GAF mobile Web site
142 lines (126 loc) • 4.34 kB
JavaScript
'use strict';
angular.module('gafMobileApp')
/**
* @ngdoc controller
* @name gafMobileApp.PaymentVerificationCtrl
* @description
* Controller for adding a verified payment method
*/
.controller('PaymentVerificationCtrl', function($q, $scope, $location,
$window, $sce, Threatmetrix, Deposits, FlInterFrameService,
GAF_BASE_URL) {
var _this = this;
// Iframe ID for credit card
var ccIframeId = 'iframe-payment-method-cc';
var defaultVerifyData = {
amount: 0.01,
currency: {
id: 1,
code: 'USD'
}
};
var params = $location.search();
/**
* @ngdoc property
* @name gafMobileApp.PaymentVerificationCtrl#loading
* @propertyOf gafMobileApp.PaymentVerificationCtrl
* @description
* Stores the current `state` of the page. It can be either `view` or `edit`
*/
this.loading = true;
/**
* @ngdoc property
* @name gafMobileApp.PaymentVerificationCtrl#errors
* @propertyOf gafMobileApp.PaymentVerificationCtrl
* @description
* Hold the backend errors when setting up the payment verification
*/
this.errors = {};
/**
* @ngdoc method
* @name gafMobileApp.PaymentVerificationCtrl#init
* @methodOf gafMobileApp.PaymentVerificationCtrl
* @description
* Setup CC deposit gateway for payment verification with set
* parameters
*/
function init() {
// Setup parameters for payment verify source types and tokens
var sourceType = params.type || '';
var sourceSubType = params.subtype || '';
var trackingToken = 'API|' + sourceType + '|' + sourceSubType +
'|gaf-mobile';
// Payment Verification Parameters
var verifyParams = {
amount: defaultVerifyData.amount,
tracking_token: trackingToken,
threatmetrix_session: Threatmetrix.getSession(),
currency_id: defaultVerifyData.currency.id,
deposit_method: 'fln_billing',
action: 'verifyPayment',
credit_card_gateway: 'gc'
};
Deposits.setupDeposit(verifyParams).then(function(response) {
_this.iframe = $sce.trustAsResourceUrl(response.get().url);
}).catch(function(error) {
if (error.code === 'DEPOSIT_API_FAIL') {
_this.errors.verifySetupFailure = true;
} else {
_this.errors.unknownError = error.data.message;
}
}).finally(function() {
_this.loading = false;
});
// Get membership fee from URL params
// TODO: Put this logic inside membership module, this can
// be implemented via a modal and redirect to the payment verification
// page only when needed
_this.membershipFee = params.membership;
_this.isPostProject = params.postProject;
}
/**
* @ngdoc method
* @name gafMobileApp.PaymentVerificationCtrl#verifyPaymentMethod
* @methodOf gafMobileApp.PaymentVerificationCtrl
* @description
* Do process to the verification when the form is submitted
*/
this.verifyPaymentMethod = function() {
this.errors.confirmError = false;
FlInterFrameService.interFrameSubmit(ccIframeId, defaultVerifyData.amount,
defaultVerifyData.currency.code);
};
/**
* @ngdoc method
* @name gafMobileApp.PaymentVerificationCtrl#receiveMessage
* @methodOf gafMobileApp.PaymentVerificationCtrl
* @description
* Listener callback for receiving post messages
* @param {Object} event Event for listener
*/
this.receiveMessage = function(event) {
if (event.origin !== GAF_BASE_URL) { return; }
if (event.data.type === 'success') {
if (params.return) {
$location.url(params.return += '?verified=true');
} else {
$location.url('/dashboard');
}
// Apply digest to propagate changes to model
$scope.$apply();
} else {
_this.errors.confirmError = true;
init();
$scope.$apply();
}
};
// Setup listeners for postmessage
if ($window.addEventListener) {
$window.addEventListener('message', function(e) {
_this.receiveMessage(e);
}, false);
} else if ($window.attachEvent) {
$window.attachEvent('message', _this.receiveMessage);
}
init();
});