mirror of
https://github.com/pretix/pretix.git
synced 2026-11-26 03:51:56 +00:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
facec4fc14 | ||
|
|
a27f2db92c | ||
|
|
5d1f4d073c | ||
|
|
47c7abbc7f | ||
|
|
a05c3d6eee | ||
|
|
3b2cd96c6f | ||
|
|
3d68b7db11 |
No files matched your search
@@ -45,6 +45,7 @@ at :ref:`plugin-docs`.
|
||||
carts
|
||||
teams
|
||||
devices
|
||||
gates
|
||||
webhooks
|
||||
seatingplans
|
||||
exporters
|
||||
|
||||
@@ -1065,6 +1065,7 @@ Creating orders
|
||||
charge will be created), this is just informative in case you *handled the payment already*.
|
||||
* ``payment_date`` (optional) – Date and time of the completion of the payment.
|
||||
* ``tax_rounding_mode`` (optional)
|
||||
* ``total`` (optional, will be computed from backend and operation will fail if the computation has a different result)
|
||||
* ``comment`` (optional)
|
||||
* ``custom_followup_at`` (optional)
|
||||
* ``checkin_attention`` (optional)
|
||||
@@ -1095,6 +1096,9 @@ Creating orders
|
||||
* ``item``
|
||||
* ``variation`` (optional)
|
||||
* ``price`` (optional, if set to ``null`` or missing the price will be computed from the given product)
|
||||
* ``tax_rule`` (optional, if set to ``null`` or missing the tax rule will be selected from the given product)
|
||||
* ``tax_rate`` (optional, only works together with a tax rule, if set to ``null`` or missing the tax rate will be computed from the given tax rule)
|
||||
* ``tax_code`` (optional, only works together with a tax rule, if set to ``null`` or missing the tax code will be computed from the given tax rule)
|
||||
* ``seat`` (The ``seat_guid`` attribute of a seat. Required when the specified ``item`` requires a seat, otherwise must be ``null``.)
|
||||
* ``attendee_name`` **or** ``attendee_name_parts`` (optional)
|
||||
* ``voucher`` (optional, the ``code`` attribute of a valid voucher)
|
||||
|
||||
@@ -991,6 +991,7 @@ class DeviceEventSettingsSerializer(EventSettingsSerializer):
|
||||
'system_question_order',
|
||||
'tax_rule_payment',
|
||||
'tax_rule_cancellation',
|
||||
'tax_rounding',
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
@@ -608,7 +608,6 @@ class OrderPositionSerializer(I18nAwareModelSerializer):
|
||||
fields = ('id', 'order', 'positionid', 'item', 'variation', 'price', 'attendee_name', 'attendee_name_parts',
|
||||
'company', 'street', 'zipcode', 'city', 'country', 'state', 'discount',
|
||||
'attendee_email', 'voucher', 'tax_rate', 'tax_value', 'secret', 'addon_to', 'subevent', 'checkins',
|
||||
'print_logs', 'downloads', 'answers', 'tax_rule', 'pseudonymization_id', 'pdf_data', 'seat', 'canceled',
|
||||
'print_logs', 'downloads', 'answers', 'tax_rule', 'tax_code', 'pseudonymization_id', 'pdf_data', 'seat',
|
||||
'canceled', 'valid_from', 'valid_until', 'blocked', 'voucher_budget_use', 'plugin_data')
|
||||
read_only_fields = (
|
||||
@@ -1028,18 +1027,56 @@ class OrderFeeCreateSerializer(I18nAwareModelSerializer):
|
||||
_treat_value_as_percentage = serializers.BooleanField(default=False, required=False)
|
||||
_split_taxes_like_products = serializers.BooleanField(default=False, required=False)
|
||||
|
||||
tax_rate = serializers.DecimalField(required=False, decimal_places=4, max_digits=7)
|
||||
tax_value = serializers.DecimalField(required=False, decimal_places=2, max_digits=13)
|
||||
value_includes_rounding_correction = serializers.DecimalField(required=False, decimal_places=2, max_digits=13)
|
||||
tax_value_includes_rounding_correction = serializers.DecimalField(required=False, decimal_places=2, max_digits=13)
|
||||
|
||||
class Meta:
|
||||
model = OrderFee
|
||||
fields = ('fee_type', 'value', 'description', 'internal_type', 'tax_rule',
|
||||
'_treat_value_as_percentage', '_split_taxes_like_products')
|
||||
fields = (
|
||||
'fee_type', 'value', 'description', 'internal_type', 'tax_rule',
|
||||
'_treat_value_as_percentage', '_split_taxes_like_products',
|
||||
# The following fields should usually not be set by the client but computed by the backend. However, it makes
|
||||
# sense to set them when the decision on the tax rate is made irrevertably on the client side, such as with
|
||||
# pretixPOS.
|
||||
'tax_rate', 'tax_code',
|
||||
# The following fields are undocumented and only used by pretixPOS. They do *NOT* override the calculations
|
||||
# by the backends but are checked against them and must match exactly, otherwise the entire operation is
|
||||
# aborted to avoid diverging tax values between POS reports and backend reports.
|
||||
'tax_value', 'value_includes_rounding_correction', 'tax_value_includes_rounding_correction',
|
||||
)
|
||||
|
||||
def validate_tax_rule(self, tr):
|
||||
if tr and tr.event != self.context['event']:
|
||||
raise ValidationError(
|
||||
'The specified tax rate does not belong to this event.'
|
||||
'The specified tax rule does not belong to this event.'
|
||||
)
|
||||
return tr
|
||||
|
||||
def validate(self, attrs):
|
||||
if attrs.get('_treat_value_as_percentage'):
|
||||
for k in ('tax_value', 'value_includes_rounding_correction', 'tax_value_includes_rounding_correction'):
|
||||
if k in attrs:
|
||||
raise ValidationError({
|
||||
k: ['You cannot ask for the value to be treated as a percentage if you already computed taxes.']
|
||||
})
|
||||
if attrs.get('_split_taxes_like_products'):
|
||||
for k in ('tax_rate', 'tax_value', 'value_includes_rounding_correction', 'tax_value_includes_rounding_correction'):
|
||||
if k in attrs:
|
||||
raise ValidationError({
|
||||
k: ['You cannot ask for the value to be distributed over tax rates if you already set the tax rate or computed taxes.']
|
||||
})
|
||||
if 'tax_rate' in attrs and 'tax_rule' not in attrs:
|
||||
raise ValidationError({
|
||||
'tax_rate': ['Tax rate cannot be stored without tax rule.']
|
||||
})
|
||||
|
||||
if 'value_includes_rounding_correction' in attrs:
|
||||
# Make all computations like discounts on the price before rounding, later compare against rounding again
|
||||
attrs['value'] = attrs['value'] - attrs['value_includes_rounding_correction']
|
||||
return attrs
|
||||
|
||||
|
||||
class OrderPositionCreateSerializer(I18nAwareModelSerializer):
|
||||
answers = AnswerCreateSerializer(many=True, required=False)
|
||||
@@ -1055,13 +1092,27 @@ class OrderPositionCreateSerializer(I18nAwareModelSerializer):
|
||||
requested_valid_from = serializers.DateTimeField(required=False, allow_null=True)
|
||||
use_reusable_medium = serializers.PrimaryKeyRelatedField(queryset=ReusableMedium.objects.none(),
|
||||
required=False, allow_null=True)
|
||||
tax_rate = serializers.DecimalField(required=False, decimal_places=4, max_digits=7)
|
||||
tax_value = serializers.DecimalField(required=False, decimal_places=2, max_digits=13)
|
||||
price_includes_rounding_correction = serializers.DecimalField(required=False, decimal_places=2, max_digits=13)
|
||||
tax_value_includes_rounding_correction = serializers.DecimalField(required=False, decimal_places=2, max_digits=13)
|
||||
|
||||
class Meta:
|
||||
model = OrderPosition
|
||||
fields = ('positionid', 'item', 'variation', 'price', 'attendee_name', 'attendee_name_parts', 'attendee_email',
|
||||
'company', 'street', 'zipcode', 'city', 'country', 'state', 'is_bundled',
|
||||
'secret', 'addon_to', 'subevent', 'answers', 'seat', 'voucher', 'valid_from', 'valid_until',
|
||||
'requested_valid_from', 'use_reusable_medium', 'discount')
|
||||
fields = (
|
||||
'positionid', 'item', 'variation', 'price', 'attendee_name', 'attendee_name_parts', 'attendee_email',
|
||||
'company', 'street', 'zipcode', 'city', 'country', 'state', 'is_bundled',
|
||||
'secret', 'addon_to', 'subevent', 'answers', 'seat', 'voucher', 'valid_from', 'valid_until',
|
||||
'requested_valid_from', 'use_reusable_medium', 'discount',
|
||||
# The following fields should usually not be set by the client but computed by the backend. However, it makes
|
||||
# sense to set them when the decision on the tax rate is made irrevertably on the client side, such as with
|
||||
# pretixPOS.
|
||||
'tax_rule', 'tax_rate', 'tax_code',
|
||||
# The following fields are undocumented and only used by pretixPOS. They do *NOT* override the calculations
|
||||
# by the backends but are checked against them and must match exactly, otherwise the entire operation is
|
||||
# aborted to avoid diverging tax values between POS reports and backend reports.
|
||||
'tax_value', 'price_includes_rounding_correction', 'tax_value_includes_rounding_correction',
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -1099,6 +1150,13 @@ class OrderPositionCreateSerializer(I18nAwareModelSerializer):
|
||||
)
|
||||
return item
|
||||
|
||||
def validate_tax_rule(self, tax_rule):
|
||||
if tax_rule.event != self.context['event']:
|
||||
raise ValidationError(
|
||||
'The specified tax rule does not belong to this event.'
|
||||
)
|
||||
return tax_rule
|
||||
|
||||
def validate_subevent(self, subevent):
|
||||
if self.context['event'].has_subevents:
|
||||
if not subevent:
|
||||
@@ -1162,6 +1220,15 @@ class OrderPositionCreateSerializer(I18nAwareModelSerializer):
|
||||
{'discount': ['You can only specify a discount if you do the price computation, but price is not set.']}
|
||||
)
|
||||
|
||||
if 'tax_rate' in data and 'tax_rule' not in data and not data['item'].tax_rule_id:
|
||||
raise ValidationError({
|
||||
'tax_rate': ['Tax rate cannot be stored without tax rule.']
|
||||
})
|
||||
|
||||
if 'price_includes_rounding_correction' in data:
|
||||
# Make all computations like discounts on the price before rounding, later compare against rounding again
|
||||
data['price'] = data['price'] - data['price_includes_rounding_correction']
|
||||
|
||||
return data
|
||||
|
||||
|
||||
@@ -1219,6 +1286,7 @@ class OrderCreateSerializer(I18nAwareModelSerializer):
|
||||
tax_rounding_mode = serializers.ChoiceField(choices=ROUNDING_MODES, allow_null=True, required=False,)
|
||||
locale = serializers.ChoiceField(choices=[], required=False, allow_null=True)
|
||||
use_gift_cards = serializers.ListField(child=serializers.CharField(required=False), required=False)
|
||||
total = serializers.DecimalField(required=False, decimal_places=2, max_digits=13)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -1233,7 +1301,7 @@ class OrderCreateSerializer(I18nAwareModelSerializer):
|
||||
model = Order
|
||||
fields = ('code', 'status', 'testmode', 'email', 'phone', 'locale', 'payment_provider', 'fees', 'comment', 'sales_channel',
|
||||
'invoice_address', 'positions', 'checkin_attention', 'checkin_text', 'payment_info', 'payment_date',
|
||||
'consume_carts', 'force', 'send_email', 'simulate', 'customer', 'custom_followup_at',
|
||||
'consume_carts', 'force', 'send_email', 'simulate', 'customer', 'custom_followup_at', 'total',
|
||||
'require_approval', 'valid_if_pending', 'expires', 'api_meta', 'tax_rounding_mode', 'use_gift_cards')
|
||||
|
||||
def validate_payment_provider(self, pp):
|
||||
@@ -1573,6 +1641,7 @@ class OrderCreateSerializer(I18nAwareModelSerializer):
|
||||
if validated_data.get('locale', None) is None:
|
||||
validated_data['locale'] = self.context['event'].settings.locale
|
||||
|
||||
expected_total = validated_data.pop('total', None)
|
||||
order = Order(event=self.context['event'], **validated_data)
|
||||
if not validated_data.get('expires'):
|
||||
order.set_expires(subevents=[p.get('subevent') for p in positions_data])
|
||||
@@ -1604,7 +1673,14 @@ class OrderCreateSerializer(I18nAwareModelSerializer):
|
||||
pos_data['attendee_name_parts'] = {
|
||||
'_legacy': attendee_name
|
||||
}
|
||||
pos = OrderPosition(**{k: v for k, v in pos_data.items() if k not in ('answers', '_quotas', 'use_reusable_medium')})
|
||||
pos = OrderPosition(**{k: v for k, v in pos_data.items() if k not in (
|
||||
'answers',
|
||||
'_quotas',
|
||||
'use_reusable_medium',
|
||||
'tax_value',
|
||||
'price_includes_rounding_correction',
|
||||
'tax_value_includes_rounding_correction',
|
||||
)})
|
||||
if simulate:
|
||||
pos.order = order._wrapped
|
||||
else:
|
||||
@@ -1647,7 +1723,12 @@ class OrderCreateSerializer(I18nAwareModelSerializer):
|
||||
if not pos.item.tax_rule or pos.item.tax_rule.price_includes_tax:
|
||||
price_after_voucher = max(pos.price, pos.voucher.calculate_price(listed_price))
|
||||
else:
|
||||
pos._calculate_tax(invoice_address=ia)
|
||||
pos._calculate_tax(
|
||||
invoice_address=ia,
|
||||
tax_rule=pos_data.get('tax_rule'),
|
||||
override_tax_rate=pos_data.get('tax_rate'),
|
||||
override_tax_code=pos_data.get('tax_code'),
|
||||
)
|
||||
price_after_voucher = max(pos.price - pos.tax_value, pos.voucher.calculate_price(listed_price))
|
||||
else:
|
||||
price_after_voucher = listed_price
|
||||
@@ -1679,7 +1760,12 @@ class OrderCreateSerializer(I18nAwareModelSerializer):
|
||||
answers_data = pos_data.pop('answers', [])
|
||||
use_reusable_medium = pos_data.pop('use_reusable_medium', None)
|
||||
pos = pos_data['__instance']
|
||||
pos._calculate_tax(invoice_address=ia)
|
||||
pos._calculate_tax(
|
||||
invoice_address=ia,
|
||||
tax_rule=pos_data.get('tax_rule', None),
|
||||
override_tax_rate=pos_data.get('tax_rate'),
|
||||
override_tax_code=pos_data.get('tax_code'),
|
||||
)
|
||||
|
||||
if simulate:
|
||||
pos = WrappedModel(pos)
|
||||
@@ -1778,7 +1864,11 @@ class OrderCreateSerializer(I18nAwareModelSerializer):
|
||||
for tr, val in fee_values:
|
||||
fee_data['tax_rule'] = tr
|
||||
fee_data['value'] = val
|
||||
f = OrderFee(**fee_data)
|
||||
f = OrderFee(**{k: v for k, v in fee_data.items() if k not in (
|
||||
'tax_value',
|
||||
'value_includes_rounding_correction',
|
||||
'tax_value_includes_rounding_correction'
|
||||
)})
|
||||
f.order = order._wrapped if simulate else order
|
||||
f._calculate_tax()
|
||||
fees.append(f)
|
||||
@@ -1787,9 +1877,17 @@ class OrderCreateSerializer(I18nAwareModelSerializer):
|
||||
else:
|
||||
f.save()
|
||||
else:
|
||||
f = OrderFee(**fee_data)
|
||||
f = OrderFee(**{k: v for k, v in fee_data.items() if k not in (
|
||||
'tax_value',
|
||||
'value_includes_rounding_correction',
|
||||
'tax_value_includes_rounding_correction'
|
||||
)})
|
||||
f.order = order._wrapped if simulate else order
|
||||
f._calculate_tax()
|
||||
f._calculate_tax(
|
||||
override_tax_rate=fee_data.get('tax_rate'),
|
||||
override_tax_code=fee_data.get('tax_code'),
|
||||
)
|
||||
fee_data['__instance'] = f
|
||||
fees.append(f)
|
||||
if simulate:
|
||||
f.id = 0
|
||||
@@ -1821,7 +1919,47 @@ class OrderCreateSerializer(I18nAwareModelSerializer):
|
||||
"value", "value_includes_rounding_correction", "tax_value", "tax_value_includes_rounding_correction"
|
||||
])
|
||||
|
||||
for i, pos_data in enumerate(positions_data):
|
||||
pos = pos_data['__instance']
|
||||
if 'tax_value' in pos_data and pos_data['tax_value'] != pos.tax_value:
|
||||
raise ValidationError(f'Tax value {pos_data["tax_value"]} in position {pos.positionid} does not match backend computation of {pos.tax_value}.')
|
||||
if 'price_includes_rounding_correction' in pos_data and pos_data['price_includes_rounding_correction'] != pos.price_includes_rounding_correction:
|
||||
raise ValidationError(
|
||||
f'Price rounding {pos_data["price_includes_rounding_correction"]} in position {pos.positionid} does not '
|
||||
f'match backend computation of {pos.price_includes_rounding_correction}.'
|
||||
)
|
||||
if ('tax_value_includes_rounding_correction' in pos_data and
|
||||
pos_data['tax_value_includes_rounding_correction'] != pos.tax_value_includes_rounding_correction):
|
||||
raise ValidationError(
|
||||
f'Tax rounding {pos_data["tax_value_includes_rounding_correction"]} in position {pos.positionid} does not '
|
||||
f'match backend computation of {pos.tax_value_includes_rounding_correction}.'
|
||||
)
|
||||
|
||||
for i, fee_data in enumerate(fees_data):
|
||||
if '__instance' not in fee_data: # split fee not supported for this, validated in OrderFeeCreateSerializer
|
||||
continue
|
||||
fee = fee_data['__instance']
|
||||
if 'tax_value' in fee_data and fee_data['tax_value'] != fee.tax_value:
|
||||
raise ValidationError(f'Tax value {fee_data["tax_value"]} in fee does not match backend computation of {fee.tax_value}.')
|
||||
if 'value_includes_rounding_correction' in fee_data and fee_data['value_includes_rounding_correction'] != fee.value_includes_rounding_correction:
|
||||
raise ValidationError(
|
||||
f'Price rounding {fee_data["value_includes_rounding_correction"]} in fee does not '
|
||||
f'match backend computation of {fee.value_includes_rounding_correction}.'
|
||||
)
|
||||
if ('tax_value_includes_rounding_correction' in fee_data and
|
||||
fee_data['tax_value_includes_rounding_correction'] != fee.tax_value_includes_rounding_correction):
|
||||
raise ValidationError(
|
||||
f'Tax rounding {fee_data["tax_value_includes_rounding_correction"]} in fee does not '
|
||||
f'match backend computation of {fee.tax_value_includes_rounding_correction}.'
|
||||
)
|
||||
|
||||
order.total = sum([c.price for c in pos_map.values()]) + sum([f.value for f in fees])
|
||||
|
||||
if expected_total is not None and expected_total != order.total:
|
||||
raise ValidationError({
|
||||
'total': [f'Order total of {expected_total} does not match backend computation of {order.total}.']
|
||||
})
|
||||
|
||||
if simulate:
|
||||
order.fees = fees
|
||||
order.positions = pos_map.values()
|
||||
|
||||
@@ -287,7 +287,7 @@ class NamePartsFormField(forms.MultiValueField):
|
||||
v = value.get(fname)
|
||||
if not v:
|
||||
continue
|
||||
special_chars = re.findall('[$€/%§{}<>~]', v)
|
||||
special_chars = re.findall('[$€/%§{}<>~=]', v)
|
||||
if special_chars:
|
||||
raise forms.ValidationError(
|
||||
_('The field "%(label)s" may not contain special characters such as "%(chars)s".'),
|
||||
|
||||
@@ -2431,7 +2431,7 @@ class OrderFee(RoundingCorrectionMixin, models.Model):
|
||||
self.fee_type, self.value
|
||||
)
|
||||
|
||||
def _calculate_tax(self, tax_rule=None, invoice_address=None, event=None):
|
||||
def _calculate_tax(self, tax_rule=None, invoice_address=None, event=None, override_tax_rate=None, override_tax_code=None):
|
||||
if tax_rule:
|
||||
self.tax_rule = tax_rule
|
||||
|
||||
@@ -2450,7 +2450,8 @@ class OrderFee(RoundingCorrectionMixin, models.Model):
|
||||
self.tax_rule = event.cached_default_tax_rule
|
||||
|
||||
if self.tax_rule:
|
||||
tax = self.tax_rule.tax(self.value, base_price_is='gross', invoice_address=ia, force_fixed_gross_price=True)
|
||||
tax = self.tax_rule.tax(self.value, base_price_is='gross', invoice_address=ia, force_fixed_gross_price=True,
|
||||
override_tax_rate=override_tax_rate, override_tax_code=override_tax_code)
|
||||
self.tax_rate = tax.rate
|
||||
self.tax_code = tax.code
|
||||
self.tax_value = tax.tax
|
||||
@@ -2813,14 +2814,15 @@ class OrderPosition(AbstractPosition):
|
||||
self.item.id, self.variation.id if self.variation else 0, self.order_id
|
||||
)
|
||||
|
||||
def _calculate_tax(self, tax_rule=None, invoice_address=None):
|
||||
def _calculate_tax(self, tax_rule=None, invoice_address=None, override_tax_rate=None, override_tax_code=None):
|
||||
self.tax_rule = tax_rule or self.item.tax_rule
|
||||
try:
|
||||
ia = invoice_address or self.order.invoice_address
|
||||
except InvoiceAddress.DoesNotExist:
|
||||
ia = None
|
||||
if self.tax_rule:
|
||||
tax = self.tax_rule.tax(self.price, invoice_address=ia, base_price_is='gross', force_fixed_gross_price=True)
|
||||
tax = self.tax_rule.tax(self.price, invoice_address=ia, base_price_is='gross', force_fixed_gross_price=True,
|
||||
override_tax_rate=override_tax_rate, override_tax_code=override_tax_code)
|
||||
self.tax_rate = tax.rate
|
||||
self.tax_code = tax.code
|
||||
self.tax_value = tax.tax
|
||||
@@ -3358,11 +3360,12 @@ class CartPosition(AbstractPosition):
|
||||
bundled_sum=sum([b.price_after_voucher for b in bundled_positions]),
|
||||
is_bundled=self.is_bundled,
|
||||
)
|
||||
if line_price.gross != self.line_price_gross or line_price.rate != self.tax_rate:
|
||||
|
||||
if line_price.gross != self.line_price_gross or line_price.rate != self.tax_rate or line_price.code != self.tax_code:
|
||||
self.line_price_gross = line_price.gross
|
||||
self.tax_rate = line_price.rate
|
||||
self.tax_code = line_price.code
|
||||
self.save(update_fields=['line_price_gross', 'tax_rate'])
|
||||
self.save(update_fields=['line_price_gross', 'tax_rate', 'tax_code'])
|
||||
|
||||
@property
|
||||
def discount_percentage(self):
|
||||
|
||||
@@ -32,9 +32,9 @@
|
||||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations under the License.
|
||||
|
||||
import csv
|
||||
import io
|
||||
|
||||
from defusedcsv import csv
|
||||
from django.contrib import messages
|
||||
from django.db import transaction
|
||||
from django.db.models import F, Max, Min, Q, Sum
|
||||
|
||||
@@ -55,13 +55,14 @@ from django.utils.safestring import mark_safe
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import gettext, gettext_lazy as _, pgettext
|
||||
from django_countries import countries
|
||||
from phonenumber_field.formfields import PhoneNumberField
|
||||
from text_unidecode import unidecode
|
||||
|
||||
from pretix import __version__
|
||||
from pretix.base.decimal import round_decimal
|
||||
from pretix.base.forms import SecretKeySettingsField
|
||||
from pretix.base.forms.questions import (
|
||||
guess_country, guess_country_from_request,
|
||||
WrappedPhoneNumberPrefixWidget, guess_country, guess_country_from_request,
|
||||
)
|
||||
from pretix.base.models import (
|
||||
Event, InvoiceAddress, Order, OrderPayment, OrderRefund, Quota,
|
||||
@@ -116,7 +117,7 @@ logger = logging.getLogger('pretix.plugins.stripe')
|
||||
# - Stablecoins and Crypto: ✗
|
||||
# - Kakao Pay: ✗
|
||||
# - Naver Pay: ✗
|
||||
# - MB Way: ✗
|
||||
# - MB Way: ✓
|
||||
# - Satis Pay: ✗
|
||||
# - WeChat Pay: ✓
|
||||
# - PAYCO: ✗
|
||||
@@ -537,6 +538,14 @@ class StripeSettingsHolder(BasePaymentProvider):
|
||||
'before they work properly.'),
|
||||
required=False,
|
||||
)),
|
||||
('method_mb_way',
|
||||
forms.BooleanField(
|
||||
label=_('MB WAY'),
|
||||
disabled=self.event.currency not in 'EUR',
|
||||
help_text=_('Some payment methods might need to be enabled in the settings of your Stripe account '
|
||||
'before they work properly.'),
|
||||
required=False,
|
||||
)),
|
||||
] + extra_fields + list(super().settings_form_fields.items()) + moto_settings
|
||||
)
|
||||
if not self.settings.connect_client_id or self.settings.secret_key:
|
||||
@@ -1947,3 +1956,63 @@ class StripeWero(StripeRedirectMethod):
|
||||
'This payment method is available to European online banking users, whose banking institutions support WERO '
|
||||
'either through their native banking apps or through the WERO wallet app. Please have you app ready.'
|
||||
)
|
||||
|
||||
|
||||
class StripeRedirectWithPhoneNumberMethod(StripeRedirectMethod):
|
||||
def payment_form_render(self, request) -> str:
|
||||
template = get_template('pretixplugins/stripe/checkout_payment_form_simple.html')
|
||||
ctx = {
|
||||
'request': request,
|
||||
'event': self.event,
|
||||
'settings': self.settings,
|
||||
'explanation': self.explanation,
|
||||
'form': self.payment_form(request)
|
||||
}
|
||||
return template.render(ctx)
|
||||
|
||||
@property
|
||||
def payment_form_fields(self):
|
||||
return OrderedDict([
|
||||
('phone', PhoneNumberField(
|
||||
label=_('Phone'),
|
||||
required=True,
|
||||
widget=WrappedPhoneNumberPrefixWidget()
|
||||
)),
|
||||
])
|
||||
|
||||
def execute_payment(self, request: HttpRequest, payment: OrderPayment):
|
||||
try:
|
||||
return super().execute_payment(request, payment)
|
||||
finally:
|
||||
if f'payment_stripe_{self.method}_phone' in request.session:
|
||||
del request.session[f'payment_stripe_{self.method}_phone']
|
||||
|
||||
def checkout_prepare(self, request, cart):
|
||||
form = self.payment_form(request)
|
||||
if form.is_valid():
|
||||
request.session[f"payment_stripe_{self.method}_payment_method_id"] = None
|
||||
request.session[f'payment_stripe_{self.method}_phone'] = form.cleaned_data['phone'].as_e164
|
||||
return True
|
||||
return False
|
||||
|
||||
def _payment_intent_kwargs(self, request, payment):
|
||||
breakpoint()
|
||||
return {
|
||||
"payment_method_data": {
|
||||
"type": self.method,
|
||||
"billing_details": {
|
||||
"phone": request.session.get(f"payment_stripe_{self.method}_phone")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class StripeMBWay(StripeRedirectWithPhoneNumberMethod):
|
||||
identifier = 'stripe_mb_way'
|
||||
verbose_name = _('MB WAY via Stripe')
|
||||
public_name = 'MB WAY'
|
||||
method = 'mb_way'
|
||||
confirmation_method = 'automatic'
|
||||
explanation = _(
|
||||
'This payment method is available to users of the MB WAY App in Portugal. Please have your app ready.'
|
||||
)
|
||||
@@ -45,7 +45,7 @@ from pretix.presale.signals import html_head, process_response
|
||||
def register_payment_provider(sender, **kwargs):
|
||||
from .payment import (
|
||||
StripeAffirm, StripeAlipay, StripeBancontact, StripeCC, StripeEPS,
|
||||
StripeGiropay, StripeIdeal, StripeKlarna, StripeMobilePay,
|
||||
StripeGiropay, StripeIdeal, StripeKlarna, StripeMBWay, StripeMobilePay,
|
||||
StripeMultibanco, StripePayByBank, StripePayPal, StripePromptPay,
|
||||
StripePrzelewy24, StripeRevolutPay, StripeSEPADirectDebit,
|
||||
StripeSettingsHolder, StripeSofort, StripeSwish, StripeTwint,
|
||||
@@ -56,7 +56,7 @@ def register_payment_provider(sender, **kwargs):
|
||||
StripeSettingsHolder, StripeCC, StripeGiropay, StripeIdeal, StripeAlipay, StripeBancontact,
|
||||
StripeSofort, StripeEPS, StripeMultibanco, StripePayByBank, StripePrzelewy24, StripePromptPay, StripeRevolutPay,
|
||||
StripeWeChatPay, StripeSEPADirectDebit, StripeAffirm, StripeKlarna, StripePayPal, StripeSwish,
|
||||
StripeTwint, StripeMobilePay, StripeWero
|
||||
StripeTwint, StripeMobilePay, StripeWero, StripeMBWay
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -1012,6 +1012,58 @@ def test_order_create_tax_rules(token_client, organizer, event, item, quota, que
|
||||
assert pos.tax_rule == taxrule
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_order_create_tax_rule_explicit(token_client, organizer, event, item, quota, question, taxrule):
|
||||
item.tax_rule = None
|
||||
item.save()
|
||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||
res['fees'][0]['tax_rule'] = taxrule.pk
|
||||
res['positions'][0]['item'] = item.pk
|
||||
res['positions'][0]['tax_rule'] = taxrule.pk
|
||||
res['positions'][0]['answers'][0]['question'] = question.pk
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||
organizer.slug, event.slug
|
||||
), format='json', data=res
|
||||
)
|
||||
assert resp.status_code == 201
|
||||
with scopes_disabled():
|
||||
o = Order.objects.get(code=resp.data['code'])
|
||||
pos = o.positions.first()
|
||||
assert pos.item == item
|
||||
assert pos.tax_rate == Decimal('19.00')
|
||||
assert pos.tax_value == Decimal('3.67')
|
||||
assert pos.tax_rule == taxrule
|
||||
assert pos.tax_code == "S/standard"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_order_create_tax_rule_custom_rate_and_code(token_client, organizer, event, item, quota, question, taxrule):
|
||||
item.tax_rule = None
|
||||
item.save()
|
||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||
res['fees'][0]['tax_rule'] = taxrule.pk
|
||||
res['positions'][0]['item'] = item.pk
|
||||
res['positions'][0]['tax_rule'] = taxrule.pk
|
||||
res['positions'][0]['tax_code'] = 'S/reduced'
|
||||
res['positions'][0]['tax_rate'] = "7.00"
|
||||
res['positions'][0]['answers'][0]['question'] = question.pk
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||
organizer.slug, event.slug
|
||||
), format='json', data=res
|
||||
)
|
||||
assert resp.status_code == 201
|
||||
with scopes_disabled():
|
||||
o = Order.objects.get(code=resp.data['code'])
|
||||
pos = o.positions.first()
|
||||
assert pos.item == item
|
||||
assert pos.tax_rate == Decimal('7.00')
|
||||
assert pos.tax_value == Decimal('1.50')
|
||||
assert pos.tax_rule == taxrule
|
||||
assert pos.tax_code == "S/reduced"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_order_create_fee_type_validation(token_client, organizer, event, item, quota, question):
|
||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||
@@ -1107,6 +1159,75 @@ def test_order_create_fee_with_auto_tax(token_client, organizer, event, item, qu
|
||||
assert o.total == Decimal('25.30')
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_order_create_fee_with_explicit_tax_and_percentage(token_client, organizer, event, item, quota, question, taxrule):
|
||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||
res['fees'][0]['tax_rate'] = '7.00'
|
||||
res['fees'][0]['tax_rule'] = taxrule.pk
|
||||
res['fees'][0]['_split_taxes_like_products'] = False
|
||||
res['fees'][0]['_treat_value_as_percentage'] = True
|
||||
res['fees'][0]['value'] = '10.00'
|
||||
res['positions'][0]['item'] = item.pk
|
||||
res['positions'][0]['answers'][0]['question'] = question.pk
|
||||
item.tax_rule = taxrule
|
||||
item.save()
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||
organizer.slug, event.slug
|
||||
), format='json', data=res
|
||||
)
|
||||
assert resp.status_code == 201
|
||||
with scopes_disabled():
|
||||
o = Order.objects.get(code=resp.data['code'])
|
||||
fee = o.fees.first()
|
||||
assert fee.value == Decimal('2.30')
|
||||
assert fee.tax_rate == Decimal('7.00')
|
||||
assert fee.tax_value == Decimal('0.15')
|
||||
assert o.total == Decimal('25.30')
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_order_create_fee_with_explicit_tax_and_split_requested(token_client, organizer, event, item, quota, question, taxrule):
|
||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||
res['fees'][0]['tax_rate'] = '7.00'
|
||||
res['fees'][0]['tax_rule'] = taxrule.pk
|
||||
res['fees'][0]['_split_taxes_like_products'] = True
|
||||
res['fees'][0]['_treat_value_as_percentage'] = True
|
||||
res['fees'][0]['value'] = '10.00'
|
||||
res['positions'][0]['item'] = item.pk
|
||||
res['positions'][0]['answers'][0]['question'] = question.pk
|
||||
item.tax_rule = taxrule
|
||||
item.save()
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||
organizer.slug, event.slug
|
||||
), format='json', data=res
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
assert resp.data == {
|
||||
'fees': [{'tax_rate': ['You cannot ask for the value to be distributed over tax rates if you already set the tax rate or computed taxes.']}],
|
||||
}
|
||||
|
||||
res['fees'][0]['tax_rate'] = '7.00'
|
||||
res['fees'][0]['tax_value'] = '1.50'
|
||||
res['fees'][0]['tax_rule'] = taxrule.pk
|
||||
res['fees'][0]['_treat_value_as_percentage'] = True
|
||||
res['fees'][0]['value'] = '10.00'
|
||||
res['positions'][0]['item'] = item.pk
|
||||
res['positions'][0]['answers'][0]['question'] = question.pk
|
||||
item.tax_rule = taxrule
|
||||
item.save()
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||
organizer.slug, event.slug
|
||||
), format='json', data=res
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
assert resp.data == {
|
||||
'fees': [{'tax_value': ['You cannot ask for the value to be treated as a percentage if you already computed taxes.']}],
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_order_create_negative_fee_with_auto_tax(token_client, organizer, event, item, quota, question, taxrule):
|
||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||
@@ -1136,6 +1257,7 @@ def test_order_create_tax_rule_wrong_event(token_client, organizer, event, item,
|
||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||
res['fees'][0]['tax_rule'] = taxrule2.pk
|
||||
res['positions'][0]['item'] = item.pk
|
||||
res['positions'][0]['tax_rule'] = taxrule2.pk
|
||||
res['positions'][0]['answers'][0]['question'] = question.pk
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||
@@ -1143,7 +1265,10 @@ def test_order_create_tax_rule_wrong_event(token_client, organizer, event, item,
|
||||
), format='json', data=res
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
assert resp.data == {'fees': [{'tax_rule': ['The specified tax rate does not belong to this event.']}]}
|
||||
assert resp.data == {
|
||||
'positions': [{'tax_rule': ['The specified tax rule does not belong to this event.']}],
|
||||
'fees': [{'tax_rule': ['The specified tax rule does not belong to this event.']}],
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@@ -3693,3 +3818,119 @@ def test_order_create_use_gift_card_invalid_secret(token_client, organizer, even
|
||||
assert o.payments.count() == 2
|
||||
assert o.payments.all()[0].state == OrderPayment.PAYMENT_STATE_FAILED
|
||||
assert o.payments.all()[1].state == OrderPayment.PAYMENT_STATE_CONFIRMED
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.parametrize("rounding_mode,data,error", [
|
||||
("line", {"tax_value": "2.40"}, "Tax value 2.40 in position 1 does not match backend computation of 2.39."),
|
||||
("line", {"tax_value": "2.39"}, None),
|
||||
("line", {"price_includes_rounding_correction": "0.01"}, "Price rounding 0.01 in position 1 does not match backend computation of 0.00."),
|
||||
("line", {"tax_value_includes_rounding_correction": "0.01"}, "Tax rounding 0.01 in position 1 does not match backend computation of 0.00."),
|
||||
("sum_by_net",
|
||||
{"tax_value": "2.39"},
|
||||
"Tax value 2.39 in position 1 does not match backend computation of 2.40."),
|
||||
("sum_by_net",
|
||||
{"tax_value": "2.40", "price": "15.01", "price_includes_rounding_correction": "0.01"},
|
||||
None),
|
||||
("sum_by_net",
|
||||
{"tax_value": "2.40", "price": "15.00", "price_includes_rounding_correction": "0.00"},
|
||||
"Price rounding 0.00 in position 1 does not match backend computation of 0.01."),
|
||||
("sum_by_net",
|
||||
{"tax_value": "2.40", "price": "15.01", "price_includes_rounding_correction": "0.01", "tax_value_includes_rounding_correction": "0.00"},
|
||||
"Tax rounding 0.00 in position 1 does not match backend computation of 0.01."),
|
||||
])
|
||||
def test_order_tax_match_validation(token_client, organizer, event, item, quota, question, taxrule,
|
||||
rounding_mode, data, error):
|
||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||
del res['fees']
|
||||
res['tax_rounding_mode'] = rounding_mode
|
||||
res['positions'][0]['item'] = item.pk
|
||||
res['positions'][0]['price'] = "15.00"
|
||||
res['positions'][0]['tax_rule'] = taxrule.pk
|
||||
res['positions'][0]['answers'][0]['question'] = question.pk
|
||||
res['positions'][0].update(data)
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||
organizer.slug, event.slug
|
||||
), format='json', data=res
|
||||
)
|
||||
if error:
|
||||
assert resp.status_code == 400
|
||||
assert resp.data == [error]
|
||||
else:
|
||||
assert resp.status_code == 201
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.parametrize("rounding_mode,data,error", [
|
||||
("line", {"tax_value": "1.61"}, "Tax value 1.61 in fee does not match backend computation of 1.60."),
|
||||
("line", {"tax_value": "1.60"}, None),
|
||||
("line", {"value_includes_rounding_correction": "0.01"},
|
||||
"Price rounding 0.01 in fee does not match backend computation of 0.00."),
|
||||
("line", {"tax_value_includes_rounding_correction": "0.01"},
|
||||
"Tax rounding 0.01 in fee does not match backend computation of 0.00."),
|
||||
("sum_by_net",
|
||||
{"tax_value": "1.60"},
|
||||
"Tax value 1.60 in fee does not match backend computation of 1.59."),
|
||||
("sum_by_net",
|
||||
{"tax_value": "1.59", "value": "9.99", "value_includes_rounding_correction": "-0.01"},
|
||||
None),
|
||||
("sum_by_net",
|
||||
{"tax_value": "1.59", "value": "10.00", "value_includes_rounding_correction": "0.00"},
|
||||
"Price rounding 0.00 in fee does not match backend computation of -0.01."),
|
||||
("sum_by_net",
|
||||
{"tax_value": "1.59", "value": "9.99", "value_includes_rounding_correction": "-0.01",
|
||||
"tax_value_includes_rounding_correction": "0.00"},
|
||||
"Tax rounding 0.00 in fee does not match backend computation of -0.01."),
|
||||
])
|
||||
def test_order_tax_match_validation_of_fee(token_client, organizer, event, item, quota, question, taxrule,
|
||||
rounding_mode, data, error):
|
||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||
res['tax_rounding_mode'] = rounding_mode
|
||||
res['positions'][0]['item'] = item.pk
|
||||
res['positions'][0]['price'] = "5.00"
|
||||
res['positions'][0]['tax_rule'] = taxrule.pk
|
||||
res['positions'][0]['answers'][0]['question'] = question.pk
|
||||
res['fees'][0]['value'] = "10.00"
|
||||
res['fees'][0]['tax_rule'] = taxrule.pk
|
||||
res['fees'][0].update(data)
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||
organizer.slug, event.slug
|
||||
), format='json', data=res
|
||||
)
|
||||
if error:
|
||||
assert resp.status_code == 400
|
||||
assert resp.data == [error]
|
||||
else:
|
||||
assert resp.status_code == 201
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_order_expect_total(token_client, organizer, event, item, quota, question, taxrule):
|
||||
# Force a rounding difference to make sure it's the last step of total computation that is compared
|
||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||
del res['fees']
|
||||
res['tax_rounding_mode'] = "sum_by_net"
|
||||
res['positions'][0]['item'] = item.pk
|
||||
res['positions'][0]['price'] = "15.00"
|
||||
res['positions'][0]['tax_rule'] = taxrule.pk
|
||||
res['positions'][0]['answers'][0]['question'] = question.pk
|
||||
res['total'] = "15.00"
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||
organizer.slug, event.slug
|
||||
), format='json', data=res
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
assert resp.data == {
|
||||
'total': ['Order total of 15.00 does not match backend computation of 15.01.']
|
||||
}
|
||||
|
||||
res['total'] = "15.01"
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||
organizer.slug, event.slug
|
||||
), format='json', data=res
|
||||
)
|
||||
assert resp.status_code == 201
|
||||
@@ -1798,6 +1798,7 @@ class OrderChangeManagerTests(BaseOrderChangeManagerTestCase, TestCase):
|
||||
assert self.op1.item == self.shirt
|
||||
assert self.op1.price == Decimal('23.00')
|
||||
assert self.op1.tax_rate == self.shirt.tax_rule.rate
|
||||
assert self.op1.tax_code == self.shirt.tax_rule.code
|
||||
assert round_decimal(self.op1.price * (1 - 100 / (100 + self.op1.tax_rate))) == self.op1.tax_value
|
||||
assert self.order.total == self.op1.price + self.op2.price
|
||||
|
||||
@@ -1811,6 +1812,7 @@ class OrderChangeManagerTests(BaseOrderChangeManagerTestCase, TestCase):
|
||||
assert self.op1.item == self.shirt
|
||||
assert self.op1.price == Decimal('12.00')
|
||||
assert self.op1.tax_rate == self.shirt.tax_rule.rate
|
||||
assert self.op1.tax_code == self.shirt.tax_rule.code
|
||||
assert round_decimal(self.op1.price * (1 - 100 / (100 + self.op1.tax_rate))) == self.op1.tax_value
|
||||
assert self.order.total == self.op1.price + self.op2.price
|
||||
|
||||
|
||||
@@ -168,6 +168,45 @@ class CartTest(CartTestMixin, TestCase):
|
||||
self.assertIsNone(objs[0].variation)
|
||||
self.assertEqual(objs[0].price, 23)
|
||||
|
||||
def _test_simple_tax_code(self, tr):
|
||||
self.ticket.tax_rule = tr
|
||||
self.ticket.save()
|
||||
|
||||
response = self.client.post('/%s/%s/cart/add' % (self.orga.slug, self.event.slug), {
|
||||
'item_%d' % self.ticket.id: '1'
|
||||
}, follow=True)
|
||||
self.assertRedirects(response, '/%s/%s/?require_cookie=true' % (self.orga.slug, self.event.slug),
|
||||
target_status_code=200)
|
||||
doc = BeautifulSoup(response.rendered_content, "lxml")
|
||||
self.assertIn('Early-bird', doc.select('.cart .cart-row')[0].select('strong')[0].text)
|
||||
self.assertIn('1', doc.select('.cart .cart-row')[0].select('.count')[0].text)
|
||||
self.assertIn('23', doc.select('.cart .cart-row')[0].select('.price')[0].text)
|
||||
self.assertIn('23', doc.select('.cart .cart-row')[0].select('.price')[1].text)
|
||||
with scopes_disabled():
|
||||
objs = list(CartPosition.objects.filter(cart_id=self.session_key, event=self.event))
|
||||
self.assertEqual(len(objs), 1)
|
||||
self.assertEqual(objs[0].item, self.ticket)
|
||||
self.assertIsNone(objs[0].variation)
|
||||
self.assertEqual(objs[0].price, 23)
|
||||
assert objs[0].tax_code == tr.code
|
||||
assert objs[0].tax_rate == tr.rate
|
||||
|
||||
def test_simple_tax_standard(self):
|
||||
tr_standard = self.event.tax_rules.create(
|
||||
name='VAT',
|
||||
rate=Decimal('19.00'),
|
||||
code="S/standard",
|
||||
)
|
||||
self._test_simple_tax_code(tr_standard)
|
||||
|
||||
def test_simple_tax_outside(self):
|
||||
tr_o = self.event.tax_rules.create(
|
||||
name='VAT',
|
||||
rate=Decimal('0'),
|
||||
code="O",
|
||||
)
|
||||
self._test_simple_tax_code(tr_o)
|
||||
|
||||
def test_widget_data_post(self):
|
||||
self.event.settings.attendee_names_asked = True
|
||||
self.event.settings.attendee_emails_asked = True
|
||||
|
||||
Reference in new issue
Block a user