예제 #1
0
def welcome_subscription(request, person_id):

    member = get_object_or_404(AIn7Member, person__pk=person_id)
    configuration = SubscriptionConfiguration.objects.get(
        type=SubscriptionConfiguration.TYPE_STUDENT_3Y,
        year=timezone.now().date().year,
    )

    SubscriptionForm = modelform_factory(Subscription, fields=('tender_type',))
    form = SubscriptionForm(request.POST or None)

    if request.method == 'POST' and form.is_valid():
        subscription = form.save(commit=False)
        subscription.member = member
        subscription.dues_amount = configuration.dues_amount
        subscription.newspaper_amount = 0
        subscription.date = timezone.now().date()
        subscription.start_year = timezone.now().date().year
        subscription.end_year = timezone.now().date().year + 3
        subscription.save()

        payment = Payment()
        payment.amount = subscription.dues_amount
        payment.type = subscription.tender_type
        payment.person = member.person
        payment.date = timezone.now().date()
        payment.save()

        systempay = {}
        systempay_signature = ''

        if subscription.tender_type == 4:

            # payment amount in cents
            systempay['vads_amount'] = str(subscription.dues_amount*100)
            # 978 is code for Euros
            systempay['vads_currency'] = '978'
            systempay['vads_site_id'] = str(settings.SYSTEM_PAY_SITE_ID)
            systempay['vads_trans_id'] = "%06d" % (payment.id % 900000)
            systempay['vads_trans_date'] = timezone.now().strftime("%Y%m%d%H%M%S")
            systempay['vads_version'] = 'V2'
            systempay['vads_payment_config'] = 'SINGLE'
            systempay['vads_page_action'] = 'PAYMENT'
            systempay['vads_action_mode'] = 'INTERACTIVE'
            systempay['vads_ctx_mode'] = str(settings.SYSTEM_PAY_MODE)
            systempay['vads_order_id'] = str(payment.id)
            systempay['vads_cust_name'] = member.person.complete_name
            systempay['vads_cust_email'] = member.person.mail_favorite()

            systempay_string = '+'.join([v.encode('utf-8') for k, v in sorted(systempay.items())])+'+'+settings.SYSTEM_PAY_CERTIFICATE
            systempay_signature = hashlib.sha1(systempay_string).hexdigest()

        return render(request, 'adhesions/informations.html', {
            'payment': payment,
            'systempay': systempay,
            'systempay_signature': systempay_signature,
            'systempay_url': settings.SYSTEM_PAY_URL
            }
        )

    return render(request, 'adhesions/welcome.html', {
        'form': form,
        'member': member,
        }
    )
예제 #2
0
def subscription_add(request, user_id=None, key_id=None, config_id=None):
    """add user subscription"""

    if key_id:
        key = get_object_or_404(SubscriptionKey, key=key_id)
        user_id = key.person.pk

    if user_id:
        person = get_object_or_404(Person, user=user_id)
    elif request.user.is_authenticated():
        person = request.user.person
        user_id = person.pk

    if config_id:
        subscription_configuration = get_object_or_404(
            SubscriptionConfiguration, pk=config_id
        )

    year_current = timezone.now().date().year

    subscription_fields = ()
    if not config_id:
        subscription_fields += ('configuration',)

    subscription_fields += ('tender_type', )

    if not user_id:
        subscription_fields += ('member',)

    SubscriptionForm = autocomplete_light.modelform_factory(
        Subscription,
        fields=subscription_fields,
    )
    form = SubscriptionForm(request.POST or None)

    # filldb should be launched before!
    default_configuration = SubscriptionConfiguration.objects.get(
        year=year_current,
        type=SubscriptionConfiguration.TYPE_REGULAR,
    )

    if not request.POST and (not key_id or not config_id):
        form = SubscriptionForm(initial={
            'configuration': default_configuration,
            'tender_type': Subscription.TENDER_TYPE_CARD,
            }
        )

    if not config_id:
        form.fields['configuration'].queryset = SubscriptionConfiguration.objects.filter(year=year_current)

    # 2e passage : sauvegarde et redirection
    if request.method == 'POST' and form.is_valid():

        subscription = form.save(commit=False)

        if user_id:
            subscription.member = person.ain7member

        current_subscription = subscription.member.current_subscription()
        can_subscribe_again = False
        if current_subscription is not None:
            can_subscribe_again = current_subscription.end_date < datetime.timedelta(days=60) + timezone.now().date()
        if subscription.member.is_subscriber() and not can_subscribe_again:
            return render(request, 'adhesions/already_subscriber.html', {
                'person': subscription.member.person,
                }
            )

        if not subscription.configuration:
            subscription.configuration = default_configuration

        if config_id:
            subscription.configuration = subscription_configuration
        subscription.dues_amount = subscription.configuration.dues_amount
        subscription.newspaper_amount = 0
        subscription.tender_type = subscription.tender_type
        subscription.set_timeslot()
        subscription.date = timezone.now().date()
        subscription.user_authenticated = request.user.is_authenticated()
        # TODO:
        # subscription.registered_by_himself = request.user == subscription.member.person.user
        subscription.save()

        payment = Payment()
        payment.amount = subscription.dues_amount
        if subscription.newspaper_subscription:
            payment.amount += subscription.newspaper_amount
        payment.type = subscription.tender_type
        payment.person = subscription.member.person
        payment.date = timezone.now().date()
        payment.save()

        subscription.payment = payment
        subscription.save()

        subscription.member.person.send_mail(_(u'AIn7 subscription request registered'), \
_(u"""Hi %(firstname)s,

We have registered your subscription request for the next year to the
association AIn7. Your subscription will be validated as soon as we have
received your payment.

All the AIn7 Team would like to thanks you for you support. See you on
the website or at one of our events.

Cheers,

AIn7 Team

""") % { 'firstname': subscription.member.person.first_name })

        systempay = {}
        systempay_signature = ''

        if payment.type == 4:

            # payment amount in cents
            systempay['vads_amount'] = str(payment.amount*100)
            # 978 is code for Euros
            systempay['vads_currency'] = '978'
            systempay['vads_site_id'] = str(settings.SYSTEM_PAY_SITE_ID)
            systempay['vads_trans_id'] = "%06d" % (payment.id % 900000)
            systempay['vads_trans_date'] = timezone.now().strftime("%Y%m%d%H%M%S")
            systempay['vads_version'] = 'V2'
            systempay['vads_payment_config'] = 'SINGLE'
            systempay['vads_page_action'] = 'PAYMENT'
            systempay['vads_action_mode'] = 'INTERACTIVE'
            systempay['vads_ctx_mode'] = str(settings.SYSTEM_PAY_MODE)
            systempay['vads_order_id'] = str(payment.id)
            systempay['vads_cust_name'] = subscription.member.person.complete_name
            systempay['vads_cust_email'] = subscription.member.person.mail_favorite()

            systempay_string = '+'.join([v.encode('utf-8') for k, v in sorted(systempay.items())])+'+'+settings.SYSTEM_PAY_CERTIFICATE
            systempay_signature = hashlib.sha1(systempay_string).hexdigest()

        return render(request, 'adhesions/informations.html', {
            'payment': payment,
            'systempay': systempay,
            'systempay_signature': systempay_signature,
            'systempay_url': settings.SYSTEM_PAY_URL
            }
        )

    return render(request, 'adhesions/subscribe_form.html', {
        'form': form,
        }
    )