Show price/method data for all available payment regions (bug 884443)

This commit is contained in:
Stuart Colville 2013-06-20 16:31:14 +00:00
Родитель 987e6e6e08
Коммит f46ba0e373
4 изменённых файлов: 59 добавлений и 29 удалений

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

@ -2,8 +2,13 @@ define('payments', [], function() {
'use strict';
var currentPrice;
var $regions = $('.regions');
var $regions = $('#region-list');
var $regionsIsland = $('#regions');
var apiErrorMsg = $regions.data('apiErrorMsg');
var disabledRegions = $regions.data('disabledRegions');
var freeWithInAppId = $regions.data('freeWithInappId');
var paymentMethods = $regions.data('paymentMethods') || {};
var pricesApiEndpoint = $regions.data('pricelistApiUrl') + '{0}/';
function getOverlay(opts) {
@ -77,29 +82,24 @@ define('payments', [], function() {
/*jshint validthis:true */
var $this = $(this);
var selectedPrice = $this.val() || '';
var apiUrl = format(pricesApiEndpoint, parseInt(selectedPrice, 10));
var disabledRegions = $regions.data('disabledRegions');
var freeWithInAppId = $regions.data('freeWithInappId');
var apiErrorMsg = $regions.data('apiErrorMsg');
var apiUrl = format(pricesApiEndpoint, +selectedPrice);
if (currentPrice == selectedPrice) {
return;
}
// If free with in-app is selected then make the 'No' radio disabled
// and hide it and make the allow_inapp a hidden field.
// If free with in-app is selected, check "Yes" then make the 'No' radio
// disabled and hide it.
if (selectedPrice == freeWithInAppId) {
$('input[name=allow_inapp][value=True]').attr('type', 'hidden');
$('input[name=allow_inapp][value=True]').prop('checked', true);
$('input[name=allow_inapp][value=False]').prop('disabled', true)
.parent('label').hide();
} else {
$('input[name=allow_inapp][value=True]').attr('type', 'radio');
$('input[name=allow_inapp][value=False]').prop('disabled', false)
.parent('label').show();
}
// Clear out existing price data.
$regions.find('.local-retail').text('');
$.ajax({
url: apiUrl,
@ -114,25 +114,41 @@ define('payments', [], function() {
for (var i=0, j=prices.length; i<j; i++) {
var price = prices[i];
var region = price.region;
var billingMethodText = paymentMethods[+price.method] || '';
var $chkbox = $regions.find('input:checkbox[value=' + region + ']');
// Skip if over regions that should be disabled e.g games app in Brazil.
if (disabledRegions.indexOf(region) > -1) {
continue;
}
// Enable checkboxes for those that we have price info for.
$chkbox.prop('disabled', false)
.parent('label').removeClass('disabled')
.closest('tr').find('.local-retail')
.text(price.price +' '+ price.currency)
.toggle($chkbox.prop('checked'));
.closest('label').removeClass('disabled');
var $tr = $chkbox.closest('tr');
// Display local currency for price.
$tr.find('.local-retail')
.text(price.price + ' ' + price.currency);
// Display local billing method.
$tr.find('.local-method')
.text(billingMethodText);
seen.push($chkbox[0]);
}
// Disable everything else.
$regions.find('input[type=checkbox]').not(seen)
.prop('checked', false)
$regions.find('input[type=checkbox]').not(seen).each(function() {
var $this = $(this);
var $tr = $this.closest('tr');
$this.prop('checked', false)
.prop('disabled', true)
.parent('label').addClass('disabled')
.trigger('change');
.closest('label').addClass('disabled');
// Remove the text, where it shouldn't be displayed.
$tr.find('.local-retail, .local-method').text('');
});
},
dataType: "json"
}).fail(function() {
@ -144,12 +160,6 @@ define('payments', [], function() {
currentPrice = selectedPrice;
}
function handleCheckboxChange() {
/*jshint validthis:true */
var $this = $(this);
$this.closest('tr').find('.local-retail').toggle($this.prop('checked'));
}
function init() {
$('#regions').trigger('editLoaded');
@ -167,7 +177,6 @@ define('payments', [], function() {
$('#id_price').on('change', updatePrices)
.each(updatePrices);
$('.regions').on('change', 'input[type=checkbox]', handleCheckboxChange);
}
return {

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

@ -173,10 +173,11 @@
</tr>
<tr>
<td colspan="2" class="region-container">
<div class="checkbox-choices regions"
<div id="region-list" class="checkbox-choices regions"
data-api-error-msg="{{ _('A server error occurred. Please try again later.') }}"
data-disabled-regions="{{ region_form.disabled_regions|json }}"
data-free-with-inapp-id="{{ free_with_in_app_id }}"
data-payment-methods="{{ payment_methods|json }}"
data-pricelist-api-url="{{ api_pricelist_url }}">
{{ region_form.regions.errors }}
<table>
@ -185,6 +186,7 @@
<th><span class="region-heading">{{ _('Region') }}</span></th>
<th>{{ _('Retail price') }}
<span class="local-currency-heading">({{ _('local currency') }})</span></th>
<th>{{ _('Billing method') }}</th>
</tr>
</thead>
<tbody>
@ -198,6 +200,7 @@
name="regions" value="{{ value }}">{{ text }}</label>
</td>
<td><span class="local-retail"></span></td>
<td><span class="local-method"></span></td>
</tr>
{% endif %}
{% endfor %}

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

@ -12,6 +12,9 @@ import amo.tests
from amo.urlresolvers import reverse
from addons.models import (Addon, AddonCategory, AddonDeviceType, AddonUser,
Category)
from constants.payments import (PAYMENT_METHOD_ALL,
PAYMENT_METHOD_CARD,
PAYMENT_METHOD_OPERATOR)
from mkt.constants.payments import ACCESS_PURCHASE, ACCESS_SIMULATE
from market.models import Price
from users.models import UserProfile
@ -293,7 +296,7 @@ class TestPayments(amo.tests.TestCase):
eq_(len(pqr('#regions-island')), 0),
eq_(len(pqr('#paid-regions-island')), 1),
def test_free_with_in_app_tier_id_in_context(self):
def test_free_with_in_app_tier_id_in_content(self):
free_tier = Price.objects.create(price='0.00')
self.webapp.update(premium_type=amo.ADDON_PREMIUM)
res = self.client.get(self.url)
@ -301,6 +304,13 @@ class TestPayments(amo.tests.TestCase):
eq_(len(pqr('.regions[data-free-with-inapp-id]')), 1),
eq_(int(pqr('.regions').attr('data-free-with-inapp-id')), free_tier.pk)
def test_pay_method_ids_in_context(self):
self.webapp.update(premium_type=amo.ADDON_PREMIUM)
res = self.client.get(self.url)
self.assertSetEqual(res.context['payment_methods'].keys(),
[PAYMENT_METHOD_ALL, PAYMENT_METHOD_CARD,
PAYMENT_METHOD_OPERATOR])
def test_premium_in_app_passes(self):
self.webapp.update(premium_type=amo.ADDON_FREE)
res = self.client.post(

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

@ -19,6 +19,9 @@ from access import acl
from amo import messages
from amo.decorators import json_view, login_required, post_required, write
from amo.urlresolvers import reverse
from constants.payments import (PAYMENT_METHOD_ALL,
PAYMENT_METHOD_CARD,
PAYMENT_METHOD_OPERATOR)
from lib.crypto import generate_key
from lib.pay_server import client
@ -145,7 +148,12 @@ def payments(request, addon_id, addon, webapp=False):
not waffle.switch_is_active('disabled-payments'),
'api_pricelist_url':
reverse('api_dispatch_list', kwargs={'resource_name': 'prices',
'api_name': 'webpay'})})
'api_name': 'webpay'}),
'payment_methods': {
PAYMENT_METHOD_ALL: _('All'),
PAYMENT_METHOD_CARD: _('Credit card'),
PAYMENT_METHOD_OPERATOR: _('Carrier'),
}})
@login_required