From 1f8186f05eb6f31e286e272e2937ec025af6c0f7 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Tue, 28 Jan 2014 19:40:21 -0200 Subject: [PATCH] partial #2308 - balanced payment --- include/library/Crunchbutton/App.php | 5 + .../default/crunchbutton/bundle/js.phtml | 1 + .../crunchbutton/layout/html.bottom.phtml | 19 +-- .../default/crunchbutton/layout/html.phtml | 16 +-- include/views/default/seven/layout/html.phtml | 3 + www/assets/js/app.js | 14 ++ www/assets/js/tokenizeCard.balanced.js | 55 -------- www/assets/js/tokenizeCard.js | 125 ++++++++++++++++++ www/assets/js/tokenizeCard.stripe.js | 51 ------- 9 files changed, 154 insertions(+), 135 deletions(-) delete mode 100644 www/assets/js/tokenizeCard.balanced.js create mode 100644 www/assets/js/tokenizeCard.js delete mode 100644 www/assets/js/tokenizeCard.stripe.js diff --git a/include/library/Crunchbutton/App.php b/include/library/Crunchbutton/App.php index 9107522ff..9161b6fc9 100755 --- a/include/library/Crunchbutton/App.php +++ b/include/library/Crunchbutton/App.php @@ -398,6 +398,11 @@ class Crunchbutton_App extends Cana_App { $config['env'] = $this->env(); $config['ab'] = json_decode($this->auth()->get('ab')); + // export the processor info + $config[ 'processor' ][ 'type' ] = Crunchbutton_User_Payment_Type::processor(); + $config[ 'processor' ][ 'stripe' ] = c::config()->stripe->{c::getEnv()}->{'public'}; + $config[ 'processor' ][ 'balanced' ] = c::balanced()->uri; + if (!$this->auth()->get('loc_lat')) { $geo = new Crunchbutton_Geo([ 'adapter' => 'Geoip_Binary', diff --git a/include/views/default/crunchbutton/bundle/js.phtml b/include/views/default/crunchbutton/bundle/js.phtml index d8f3c8c99..cb6c850aa 100644 --- a/include/views/default/crunchbutton/bundle/js.phtml +++ b/include/views/default/crunchbutton/bundle/js.phtml @@ -38,6 +38,7 @@ + diff --git a/include/views/default/crunchbutton/layout/html.bottom.phtml b/include/views/default/crunchbutton/layout/html.bottom.phtml index 10355b173..890e4a97b 100644 --- a/include/views/default/crunchbutton/layout/html.bottom.phtml +++ b/include/views/default/crunchbutton/layout/html.bottom.phtml @@ -88,21 +88,4 @@ $(function() { var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); - - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/include/views/default/crunchbutton/layout/html.phtml b/include/views/default/crunchbutton/layout/html.phtml index 97ae795a0..f04cc853d 100755 --- a/include/views/default/crunchbutton/layout/html.phtml +++ b/include/views/default/crunchbutton/layout/html.phtml @@ -4,17 +4,11 @@ - + + + + + bundle) : ?> diff --git a/include/views/default/seven/layout/html.phtml b/include/views/default/seven/layout/html.phtml index 40123f18a..18925a457 100644 --- a/include/views/default/seven/layout/html.phtml +++ b/include/views/default/seven/layout/html.phtml @@ -4,6 +4,9 @@ + + + bundle) : ?> diff --git a/www/assets/js/app.js b/www/assets/js/app.js index 78b3cde25..7cc128c2b 100644 --- a/www/assets/js/app.js +++ b/www/assets/js/app.js @@ -727,6 +727,20 @@ App.init = function(config) { },10); } + // Init the processor + var processor = ( App.config.processor && App.config.processor.type ) ? App.config.processor.type : false; + switch( processor ){ + case 'stripe': + Stripe.setPublishableKey( App.config.processor.stripe ); + break; + case 'balanced': + balanced.init( App.config.processor.balanced ); + break; + default: + console.log( 'Processor error::', App.config.processor ); + break; + } + $(window).trigger('nginit'); }; diff --git a/www/assets/js/tokenizeCard.balanced.js b/www/assets/js/tokenizeCard.balanced.js deleted file mode 100644 index 35f1c3ae0..000000000 --- a/www/assets/js/tokenizeCard.balanced.js +++ /dev/null @@ -1,55 +0,0 @@ -App.tokenizeCard = function(card, complete) { - balanced.card.create(card, function(response) { - var res = { - status: false - }; - switch (response.status) { - case 201: - res.status = true; - res.id = response.data.id; - res.uri = response.data.uri; - res.card_type = response.data.card_type; - res.lastfour = response.data.last_four; - res.month = card.expiration_month; - res.year = card.expiration_year; - break; - - case 400: - res.error = 'Missing fields'; - break; - - case 402: - res.error = 'Unable to authorize'; - break; - - case 404: - res.error = 'Unexpected error'; - break; - - case 409: - res.error = 'Unable to validate'; - break; - - case 500: - res.error = 'Error processing payment'; - break; - - // a lack of any response from the ios sdk - case 999: - res.error = 'Unable to reach payment server'; - break; - - // a response from the ios sdk that was both ilformated and didnt not validate - case 666: - res.error = 'Unable to validate your card'; - break; - - // who knows wtf this is - default: - res.error = 'Unable to validate your card at this time'; - break; - } - console.debug('Balanced tokenization response',response); - complete(res); - } ); -}; diff --git a/www/assets/js/tokenizeCard.js b/www/assets/js/tokenizeCard.js new file mode 100644 index 000000000..385502623 --- /dev/null +++ b/www/assets/js/tokenizeCard.js @@ -0,0 +1,125 @@ +App.tokenizeCard = function( card, complete ) { + var processor = ( App.config.processor && App.config.processor.type ) ? App.config.processor.type : false; + switch( processor ){ + case 'stripe': + App.tokenizeCard_stripe( card, complete ); + break; + case 'balanced': + App.tokenizeCard_balanced( card, complete ); + break; + default: + console.log( 'Processor error::', App.config.processor ); + break; + } +}; + +App.tokenizeCard_stripe = function( card, complete ) { + var res = { status: false }; + var card = { number: card.card_number, exp_month: card.expiration_month, exp_year: card.expiration_year }; + Stripe.card.createToken( card , function( status, response ){ + if ( response.error ) { + switch( response.error.code ){ + case 'incorrect_number': + res.error = 'This card number looks invalid'; + break; + case 'invalid_number': + res.error = 'The card number is not a valid credit card number.'; + break; + case 'invalid_expiry_month': + res.error = 'The card\'s expiration month is invalid.'; + break; + case 'invalid_expiry_year': + res.error = 'The card\'s expiration year is invalid.'; + break; + case 'invalid_cvc': + res.error = 'The card\'s security code is invalid.'; + break; + case 'expired_card': + res.error = 'The card has expired.'; + break; + case 'incorrect_cvc': + res.error = 'The card\'s security code is incorrect.'; + break; + case 'card_declined': + res.error = 'The card was declined.'; + break; + case 'processing_error': + res.error = 'An error occurred while processing the card.'; + break; + default: + res.error = 'Unable to validate your card at this time'; + break; + } + } else { + res = { + id : response.card.id, + uri: response.id, + lastfour: response.card.last4, + card_type: response.card.type, + month: response.card.exp_month, + year: response.card.exp_year, + status : true + } + } + complete( res ); + } ); +}; + +App.tokenizeCard_balanced = function(card, complete) { + + console.log( 'call App.tokenizeCard_balanced' ); + + balanced.card.create(card, function(response) { + var res = { + status: false + }; + + switch (response.status) { + case 201: + res.status = true; + res.id = response.data.id; + res.uri = response.data.uri; + res.card_type = response.data.card_type; + res.lastfour = response.data.last_four; + res.month = card.expiration_month; + res.year = card.expiration_year; + break; + + case 400: + res.error = 'Missing fields'; + break; + + case 402: + res.error = 'Unable to authorize'; + break; + + case 404: + res.error = 'Unexpected error'; + break; + + case 409: + res.error = 'Unable to validate'; + break; + + case 500: + res.error = 'Error processing payment'; + break; + + // a lack of any response from the ios sdk + case 999: + res.error = 'Unable to reach payment server'; + break; + + // a response from the ios sdk that was both ilformated and didnt not validate + case 666: + res.error = 'Unable to validate your card'; + break; + + // who knows wtf this is + default: + res.error = 'Unable to validate your card at this time'; + break; + } + complete(res); + } ); +}; diff --git a/www/assets/js/tokenizeCard.stripe.js b/www/assets/js/tokenizeCard.stripe.js deleted file mode 100644 index eb68a4640..000000000 --- a/www/assets/js/tokenizeCard.stripe.js +++ /dev/null @@ -1,51 +0,0 @@ -App.tokenizeCard = function( card, complete ) { - var res = { status: false }; - var card = { number: card.card_number, exp_month: card.expiration_month, exp_year: card.expiration_year }; - Stripe.card.createToken( card , function( status, response ){ - if ( response.error ) { - switch( response.error.code ){ - case 'incorrect_number': - res.error = 'This card number looks invalid'; - break; - case 'invalid_number': - res.error = 'The card number is not a valid credit card number.'; - break; - case 'invalid_expiry_month': - res.error = 'The card\'s expiration month is invalid.'; - break; - case 'invalid_expiry_year': - res.error = 'The card\'s expiration year is invalid.'; - break; - case 'invalid_cvc': - res.error = 'The card\'s security code is invalid.'; - break; - case 'expired_card': - res.error = 'The card has expired.'; - break; - case 'incorrect_cvc': - res.error = 'The card\'s security code is incorrect.'; - break; - case 'card_declined': - res.error = 'The card was declined.'; - break; - case 'processing_error': - res.error = 'An error occurred while processing the card.'; - break; - default: - res.error = 'Unable to validate your card at this time'; - break; - } - } else { - res = { - id : response.card.id, - uri: response.id, - lastfour: response.card.last4, - card_type: response.card.type, - month: response.card.exp_month, - year: response.card.exp_year, - status : true - } - } - complete( res ); - } ); -}; \ No newline at end of file