cope on the home and featured pages with paypal failures (bug 649022 comment 6)

This commit is contained in:
Andy McKay 2011-04-15 15:29:12 -07:00
Родитель 36562f7c55
Коммит 19edaa3a3a
5 изменённых файлов: 63 добавлений и 12 удалений

Просмотреть файл

@ -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)

Просмотреть файл

@ -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('<div id="paypal-error" class="popup"></div>');
}
$('#paypal-error').text(json.error).popup(el, {pointTo:el}).render();
}
}
});
);
return false;
});
}

Просмотреть файл

@ -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();
});
});

Просмотреть файл

@ -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"
]
}

Просмотреть файл

@ -12,6 +12,7 @@
data-anonymous="true"
data-readonly="false"
data-media-url="{{ MEDIA_URL }}"
data-paypal-url="/paypal"
data-default-locale="en-us">
<h1 id="qunit-header">QUnit Test Suite ({{ suite.name }})</h1>
<h2 id="qunit-banner"></h2>
@ -267,6 +268,11 @@
</li>
</ul>
</div>
<div id="paypal">
<div class="contribute">
<a href="/paykey?src=direct" class="suggested-amount">Contribute</a>
</div>
</div>
<script src="{{ url('jsi18n') }}/build:{{ BUILD_ID_JS }}"></script>