From 19edaa3a3a989b05f9a545dafc68768169f57035 Mon Sep 17 00:00:00 2001 From: Andy McKay Date: Fri, 15 Apr 2011 15:29:12 -0700 Subject: [PATCH] cope on the home and featured pages with paypal failures (bug 649022 comment 6) --- apps/addons/views.py | 8 +++++-- media/js/zamboni/contributions.js | 25 ++++++++++++-------- media/js/zamboni/tests/addon_tests.js | 33 +++++++++++++++++++++++++++ media/js/zamboni/tests/suite.json | 3 ++- templates/qunit.html | 6 +++++ 5 files changed, 63 insertions(+), 12 deletions(-) diff --git a/apps/addons/views.py b/apps/addons/views.py index a9276f46cf..0851425d02 100644 --- a/apps/addons/views.py +++ b/apps/addons/views.py @@ -484,7 +484,7 @@ def embedded_contribute(request, addon): name, paypal_id = addon.name, addon.paypal_id contrib_for = _(u'Contribution for {0}').format(jinja2.escape(name)) - paykey = None + paykey, nice_error = None, None try: paykey = paypal.get_paykey({ 'return_url': absolutify('%s?%s' % (reverse('addons.paypal', @@ -500,8 +500,10 @@ def embedded_contribute(request, addon): 'memo': contrib_for}) except paypal.AuthError, error: paypal_log.error('Authentication error: %s' % error) + nice_error = _('There was a problem communicating with Paypal.') except Exception, error: paypal_log.error('Error: %s' % error) + nice_error = _('There was a problem with that contribution.') if paykey: contrib = Contribution(addon_id=addon.id, @@ -523,7 +525,9 @@ def embedded_contribute(request, addon): if request.GET.get('result_type') == 'json' or request.is_ajax(): # If there was an error getting the paykey, then JSON will # not have a paykey and the JS can cope appropriately. - return http.HttpResponse(json.dumps({'url': url, 'paykey': paykey}), + return http.HttpResponse(json.dumps({'url': url, + 'paykey': paykey, + 'error': nice_error}), content_type='application/json') return http.HttpResponseRedirect(url) diff --git a/media/js/zamboni/contributions.js b/media/js/zamboni/contributions.js index 28ad9486fc..d2fc5c3f81 100644 --- a/media/js/zamboni/contributions.js +++ b/media/js/zamboni/contributions.js @@ -3,16 +3,23 @@ $(document).ready(function() { pointTo: "#contribute-more-info" }); if ($('body').attr('data-paypal-url')) { - $('div.contribute a.suggested-amount').bind('click', function(event) { - $.ajax({type: 'GET', - url: $(this).attr('href') + '&result_type=json', - success: function(json) { - $.getScript($('body').attr('data-paypal-url'), function() { - dgFlow = new PAYPAL.apps.DGFlow(); - dgFlow.startFlow(json.url); - }); + $('div.contribute a.suggested-amount').live('click', function(event) { + var el = this; + $.getJSON($(this).attr('href') + '&result_type=json', + function(json) { + if (json.paykey) { + $.getScript($('body').attr('data-paypal-url'), function() { + dgFlow = new PAYPAL.apps.DGFlow(); + dgFlow.startFlow(json.url); + }); + } else { + if (!$('#paypal-error').length) { + $(el).closest('div').append(''); + } + $('#paypal-error').text(json.error).popup(el, {pointTo:el}).render(); + } } - }); + ); return false; }); } diff --git a/media/js/zamboni/tests/addon_tests.js b/media/js/zamboni/tests/addon_tests.js index 095ac480cd..8b6c04e7c2 100644 --- a/media/js/zamboni/tests/addon_tests.js +++ b/media/js/zamboni/tests/addon_tests.js @@ -19,3 +19,36 @@ test('Buttons: Test backup button', function() { equals(backup_wrapper.hasClass('hidden'), true); equals(current_wrapper.hasClass('hidden'), false); }); + +var paypalFixtures = { + setup: function() { + this.sandbox = tests.createSandbox('#paypal'); + $.mockjaxSettings = { + status: 200, + responseTime: 0 + }; + }, + teardown: function() { + $.mockjaxClear(); + this.sandbox.remove(); + } +}; + +module('Contributions', paypalFixtures); + +asyncTest('Paypal failure', function() { + var self = this; + $.mockjax({ + url: '/paykey?src=direct&result_type=json', + dataType: 'json', + responseText: { paykey: '', url:'', error:'Error' } + }); + self.sandbox.find('div.contribute a.suggested-amount').trigger('click'); + tests.waitFor(function() { + // Note: popup.render moves the element outside the sandbox. + return $('#paypal-error').length === 1; + }).thenDo(function() { + equals($('#paypal-error').text(), 'Error'); + start(); + }); +}); diff --git a/media/js/zamboni/tests/suite.json b/media/js/zamboni/tests/suite.json index bdb52fad8f..a9afa2e9c0 100644 --- a/media/js/zamboni/tests/suite.json +++ b/media/js/zamboni/tests/suite.json @@ -9,6 +9,7 @@ "js/zamboni/storage.js", "js/zamboni/editors.js", "js/zamboni/upload.js", - "js/zamboni/files.js" + "js/zamboni/files.js", + "js/zamboni/contributions.js" ] } diff --git a/templates/qunit.html b/templates/qunit.html index bfce10f8ba..030f30f80d 100644 --- a/templates/qunit.html +++ b/templates/qunit.html @@ -12,6 +12,7 @@ data-anonymous="true" data-readonly="false" data-media-url="{{ MEDIA_URL }}" + data-paypal-url="/paypal" data-default-locale="en-us">

QUnit Test Suite ({{ suite.name }})

@@ -267,6 +268,11 @@ +
+ +