pretix-stripe2/pretix_stripe2/signals.py

160 lines
6.0 KiB
Python
Raw Normal View History

2020-09-04 17:56:10 +10:00
import json
from collections import OrderedDict
from django import forms
from django.dispatch import receiver
from django.template.loader import get_template
from django.urls import resolve, reverse
from django.utils.translation import gettext_lazy as _
from pretix.base.forms import SecretKeySettingsField
from pretix.base.settings import settings_hierarkey
from pretix.base.signals import (
logentry_display, register_global_settings, register_payment_providers,
requiredaction_display,
)
from pretix.control.signals import nav_organizer
2020-09-04 17:58:25 +10:00
from pretix_stripe2.forms import StripeKeyValidator
2020-09-04 17:56:10 +10:00
from pretix.presale.signals import html_head
2020-09-04 17:58:25 +10:00
@receiver(register_payment_providers, dispatch_uid="payment_stripe2")
2020-09-04 17:56:10 +10:00
def register_payment_provider(sender, **kwargs):
from .payment import (
StripeSettingsHolder, StripeCC, StripeGiropay, StripeIdeal, StripeAlipay, StripeBancontact,
StripeSofort, StripeEPS, StripeMultibanco, StripePrzelewy24, StripeWeChatPay
)
return [
StripeSettingsHolder, StripeCC, StripeGiropay, StripeIdeal, StripeAlipay, StripeBancontact,
StripeSofort, StripeEPS, StripeMultibanco, StripePrzelewy24, StripeWeChatPay
]
2020-09-04 17:58:25 +10:00
@receiver(html_head, dispatch_uid="payment_stripe2_html_head")
2020-09-04 17:56:10 +10:00
def html_head_presale(sender, request=None, **kwargs):
from .payment import StripeSettingsHolder
provider = StripeSettingsHolder(sender)
url = resolve(request.path_info)
if provider.settings.get('_enabled', as_type=bool) and ("checkout" in url.url_name or "order.pay" in url.url_name):
2020-09-04 17:58:25 +10:00
template = get_template('pretixplugins/stripe2/presale_head.html')
2020-09-04 17:56:10 +10:00
ctx = {
'event': sender,
'settings': provider.settings,
'testmode': (
(provider.settings.get('endpoint', 'live') == 'test' or sender.testmode)
2020-09-04 17:58:25 +10:00
and provider.settings.test_publishable_key
2020-09-04 17:56:10 +10:00
)
}
return template.render(ctx)
else:
return ""
2020-09-04 17:58:25 +10:00
@receiver(signal=logentry_display, dispatch_uid="stripe2_logentry_display")
2020-09-04 17:56:10 +10:00
def pretixcontrol_logentry_display(sender, logentry, **kwargs):
2020-09-04 17:58:25 +10:00
if logentry.action_type != 'pretix_stripe2.event':
2020-09-04 17:56:10 +10:00
return
data = json.loads(logentry.data)
event_type = data.get('type')
text = None
plains = {
'charge.succeeded': _('Charge succeeded.'),
'charge.refunded': _('Charge refunded.'),
'charge.updated': _('Charge updated.'),
'charge.pending': _('Charge pending'),
'source.chargeable': _('Payment authorized.'),
'source.canceled': _('Payment authorization canceled.'),
'source.failed': _('Payment authorization failed.')
}
if event_type in plains:
text = plains[event_type]
elif event_type == 'charge.failed':
text = _('Charge failed. Reason: {}').format(data['data']['object']['failure_message'])
elif event_type == 'charge.dispute.created':
text = _('Dispute created. Reason: {}').format(data['data']['object']['reason'])
elif event_type == 'charge.dispute.updated':
text = _('Dispute updated. Reason: {}').format(data['data']['object']['reason'])
elif event_type == 'charge.dispute.closed':
text = _('Dispute closed. Status: {}').format(data['data']['object']['status'])
if text:
return _('Stripe reported an event: {}').format(text)
2020-09-04 17:58:25 +10:00
settings_hierarkey.add_default('payment_stripe2_method_cc', True, bool)
settings_hierarkey.add_default('payment_stripe2_reseller_moto', False, bool)
2020-09-04 17:56:10 +10:00
2020-09-04 17:58:25 +10:00
@receiver(register_global_settings, dispatch_uid='stripe2_global_settings')
2020-09-04 17:56:10 +10:00
def register_global_settings(sender, **kwargs):
return OrderedDict([
2020-09-04 17:58:25 +10:00
('payment_stripe2_secret_key', SecretKeySettingsField(
label=_('Stripe2: Secret key'),
2020-09-04 17:56:10 +10:00
required=False,
validators=(
StripeKeyValidator(['sk_live_', 'rk_live_']),
),
)),
2020-09-04 17:58:25 +10:00
('payment_stripe2_publishable_key', forms.CharField(
label=_('Stripe2: Publishable key'),
2020-09-04 17:56:10 +10:00
required=False,
validators=(
StripeKeyValidator('pk_live_'),
),
)),
2020-09-04 17:58:25 +10:00
('payment_stripe2_test_secret_key', SecretKeySettingsField(
label=_('Stripe2: Secret key (test)'),
2020-09-04 17:56:10 +10:00
required=False,
validators=(
StripeKeyValidator(['sk_test_', 'rk_test_']),
),
)),
2020-09-04 17:58:25 +10:00
('payment_stripe2_test_publishable_key', forms.CharField(
label=_('Stripe2: Publishable key (test)'),
2020-09-04 17:56:10 +10:00
required=False,
validators=(
StripeKeyValidator('pk_test_'),
),
2020-09-04 17:58:25 +10:00
))
2020-09-04 17:56:10 +10:00
])
2020-09-04 17:58:25 +10:00
@receiver(signal=requiredaction_display, dispatch_uid="stripe2_requiredaction_display")
2020-09-04 17:56:10 +10:00
def pretixcontrol_action_display(sender, action, request, **kwargs):
# DEPRECATED
2020-09-04 17:58:25 +10:00
if not action.action_type.startswith('pretix_stripe2'):
2020-09-04 17:56:10 +10:00
return
data = json.loads(action.data)
2020-09-04 17:58:25 +10:00
if action.action_type == 'pretix_stripe2.refund':
template = get_template('pretixplugins/stripe2/action_refund.html')
elif action.action_type == 'pretix_stripe2.overpaid':
template = get_template('pretixplugins/stripe2/action_overpaid.html')
elif action.action_type == 'pretix_stripe2.double':
template = get_template('pretixplugins/stripe2/action_double.html')
2020-09-04 17:56:10 +10:00
ctx = {'data': data, 'event': sender, 'action': action}
return template.render(ctx, request)
2020-09-04 17:58:25 +10:00
@receiver(nav_organizer, dispatch_uid="stripe2_nav_organizer")
2020-09-04 17:56:10 +10:00
def nav_o(sender, request, organizer, **kwargs):
if request.user.has_active_staff_session(request.session.session_key):
url = resolve(request.path_info)
return [{
'label': _('Stripe Connect'),
2020-09-04 17:58:25 +10:00
'url': reverse('plugins:pretix_stripe2:settings.connect', kwargs={
2020-09-04 17:56:10 +10:00
'organizer': request.organizer.slug
}),
'parent': reverse('control:organizer.edit', kwargs={
'organizer': request.organizer.slug
}),
'active': 'settings.connect' in url.url_name,
}]
return []