Exemplo n.º 1
0
def process_payment(payment):
    if payment.state != Payment.ACCEPTED:
        raise ValueError('Can not process not accepted payment')
    if payment.repeat:
        # Update existing
        donation = Donation.objects.get(payment=payment.repeat.pk)
        donation.expires += get_period_delta(payment.repeat.recurring)
        donation.save()
    else:
        user = User.objects.get(pk=payment.customer.user_id)
        reward = None
        if 'reward' in payment.extra:
            reward = Reward.objects.get(pk=payment.extra['reward'])
        # Calculate expiry
        expires = timezone.now()
        if reward:
            expires += get_period_delta(reward.recurring)
        elif payment.recurring:
            expires += get_period_delta(payment.recurring)
        # Create new
        donation = Donation.objects.create(
            user=user,
            payment=payment.pk,
            reward=reward,
            expires=expires,
            active=True,
        )
    # Flag payment as processed
    payment.state = Payment.PROCESSED
    payment.save()
    return donation
Exemplo n.º 2
0
def process_donation(payment):
    if payment.state != Payment.ACCEPTED:
        raise ValueError('Can not process not accepted payment')
    if payment.repeat:
        # Update existing
        donation = Donation.objects.get(payment=payment.repeat.pk)
        donation.expires += get_period_delta(payment.repeat.recurring)
        donation.save()
    else:
        user = User.objects.get(pk=payment.customer.user_id)
        reward = payment.extra.get('reward', 0)
        # Calculate expiry
        expires = timezone.now()
        if payment.recurring:
            expires += get_period_delta(payment.recurring)
        elif reward:
            expires += get_period_delta('y')
        # Create new
        donation = Donation.objects.create(
            user=user,
            payment=payment.pk,
            reward=reward,
            expires=expires,
            active=True,
        )
    # Flag payment as processed
    payment.state = Payment.PROCESSED
    payment.save()
    return donation
Exemplo n.º 3
0
def process_subscription(payment):
    if payment.state != Payment.ACCEPTED:
        raise ValueError('Can not process not accepted payment')
    if payment.repeat:
        # Update existing
        subscription = Subscription.objects.get(payment=payment.repeat.pk)
        subscription.expires += get_period_delta(payment.repeat.recurring)
        subscription.save()
    elif isinstance(payment.extra['subscription'], int):
        subscription = Subscription.objects.get(pk=payment.extra['subscription'])
        if subscription.payment:
            subscription.pastpayments_set.create(payment=subscription.payment)
        subscription.expires += get_period_delta('y')
        subscription.payment = payment.pk
        subscription.save()
    else:
        user = User.objects.get(pk=payment.customer.user_id)
        # Calculate expiry
        expires = timezone.now() + get_period_delta('y')
        # Create new
        subscription = Subscription.objects.create(
            user=user,
            payment=payment.pk,
            status=payment.extra['subscription'],
            expires=expires,
        )
    # Flag payment as processed
    payment.state = Payment.PROCESSED
    payment.save()
    return subscription
Exemplo n.º 4
0
def process_subscription(payment):
    if payment.state != Payment.ACCEPTED:
        raise ValueError("Can not process not accepted payment")
    if payment.repeat:
        # Update existing
        subscription = Subscription.objects.get(payment=payment.repeat.pk)
        subscription.expires += get_period_delta(payment.repeat.recurring)
        subscription.save()
    elif isinstance(payment.extra["subscription"], int):
        subscription = Subscription.objects.get(
            pk=payment.extra["subscription"])
        if subscription.payment:
            subscription.pastpayments_set.create(payment=subscription.payment)
        subscription.expires += get_period_delta(subscription.get_repeat())
        subscription.payment = payment.pk
        subscription.save()
    else:
        user = User.objects.get(pk=payment.customer.user_id)
        package = Package.objects.get(name=payment.extra["subscription"])
        # Calculate expiry
        repeat = package.get_repeat()
        if repeat:
            expires = timezone.now() + get_period_delta(repeat)
        else:
            expires = timezone.now()
        # Create new
        subscription = Subscription.objects.create(
            service=get_service(payment, user),
            payment=payment.pk,
            package=package.name,
            expires=expires,
        )
    # Flag payment as processed
    payment.state = Payment.PROCESSED
    payment.save()
    return subscription
Exemplo n.º 5
0
def end_interval(payment, start):
    return start + get_period_delta(payment.extra['period'])