Esempio n. 1
0
    def post(self, request, *args, **kwargs):
        form = self.get_form()

        if form.is_valid():
            context = self.get_context_data()
            token = form.cleaned_data.get("token")
            donation_amount = form.cleaned_data.get("donation_amount")

            # Get or create stripe customer
            customer = StripeCustomer.get_or_create(email=self.request.user.email, token=token)
            if not customer:
                form.add_error("__all__", "Invalid credit card")
                return self.render_to_response(self.get_context_data(form=form))

            # Create Billing Address
            billing_address = form.save()

            # Make stripe charge to a customer
            stripe_utils = StripeUtils()
            stripe_utils.CURRENCY = "usd"
            charge_response = stripe_utils.make_charge(amount=donation_amount, customer=customer.stripe_id)
            charge = charge_response.get("response_object")

            # Check if the payment was approved
            if not charge:
                context.update({"paymentError": charge_response.get("error"), "form": form})
                return render(request, self.template_name, context)

            # Create a donation
            charge = charge_response.get("response_object")
            donation_data = request.POST.copy()
            donation_data.update(
                {
                    "cc_brand": charge.source.brand,
                    "stripe_charge_id": charge.id,
                    "last4": charge.source.last4,
                    "billing_address": billing_address.id,
                    "donator": customer.id,
                    "donation": donation_amount,
                }
            )
            donation_form = DonationForm(donation_data)
            if donation_form.is_valid():

                # reactivate donation status
                donation = donation_form.save()

                try:
                    donator_status = DonatorStatus.objects.get(user=self.request.user)
                    donator_status.set_active()
                except DonatorStatus.DoesNotExist:
                    pass

                donation = donation_form.save()

                context = {"donation": donation, "base_url": "{0}://{1}".format(request.scheme, request.get_host())}
                email_data = {
                    "subject": "Your donation have been charged",
                    "to": request.user.email,
                    "context": context,
                    "template_name": "donation_charge",
                    "template_path": "nosystemd/emails/",
                }
                email = BaseEmail(**email_data)
                email.send()

                return HttpResponseRedirect(reverse("nosystemd:donations", kwargs={"pk": donation.id}))
            else:
                self.form_invalid(donation_form)

        else:
            return self.form_invalid(form)
Esempio n. 2
0
    def handle(self, *args, **options):
        donators = DonatorStatus.objects.filter(status=DonatorStatus.ACTIVE)
        current_month = datetime.now().month
        current_year = datetime.now().year

        print("--------- STARTING DONATIONS SCRIPT  ---------")
        print("Donations date: %s-%s" % (current_month, current_year))

        for donator_status in donators:
            donator = donator_status.user.stripecustomer
            try:
                Donation.objects.get(created_at__month=current_month,
                                     created_at__year=current_year,
                                     donator=donator)
            except Donation.DoesNotExist:
                try:
                    # Get donator last donation amount
                    last_donation = Donation.objects.filter(donator=donator).last()
                    donation_amount = last_donation.donation

                    # Make stripe charge to a customer
                    stripe_utils = StripeUtils()
                    stripe_utils.CURRENCY = self.CURRENCY
                    charge_response = stripe_utils.make_charge(amount=donation_amount,
                                                               customer=donator.stripe_id)
                    charge = charge_response.get('response_object')

                    # Check if the payment was approved
                    if not charge:
                        # There is an error trying to creating the stripe charge
                        context = {
                            'paymentError': charge_response.get('error'),
                        }
                        print("--------- STRIPE PAYMENT ERROR ---------")
                        print(context)
                        print("-------------------------")
                        continue
                    # Create a donation
                    charge = charge_response.get('response_object')
                    donation_data = {
                        'cc_brand': charge.source.brand,
                        'stripe_charge_id': charge.id,
                        'last4': charge.source.last4,
                        'billing_address': last_donation.billing_address.id,
                        'donator': donator.id,
                        'donation': donation_amount
                    }
                    donation_form = DonationForm(donation_data)
                    if donation_form.is_valid():
                        donation = donation_form.save()

                        context = {
                            'donation': donation,
                            'base_url': "{0}://{1}".format('https', 'dynamicweb.ungleich.ch')

                        }
                        email_data = {
                            'subject': 'Your donation have been charged',
                            'to': donation.donator.user.email,
                            'context': context,
                            'template_name': 'donation_charge',
                            'template_path': 'nosystemd/emails/'
                        }
                        email = BaseEmail(**email_data)
                        email.send()

                        print("--------- PAYMENT DONATION SUCCESSFULL ---------")
                        print("Donator: %s" % donation.donator.user.email)
                        print("Amount: %s %s" % (donation.donation, self.CURRENCY))
                        print("-----------------------------------------------")
                except Exception as e:
                    print("--------- ERROR ---------")
                    print(e)
                    print("-------------------------")
                    continue
    def handle(self, *args, **options):
        translation.activate('en-us')
        memberships_orders = MembershipOrder.objects.filter(membership__active=True)
        current_month = datetime.now().month
        current_year = datetime.now().year

        print("--------- STARTING MEMBERSHIP CHARGING SCRIPT  ---------")
        print("Memberhips date: %s-%s" % (current_month, current_year))

        for membership_order in memberships_orders:
            member = membership_order.customer
            try:
                MembershipOrder.objects.get(created_at__month=current_month,
                                            created_at__year=current_year,
                                            customer=member)
            except MembershipOrder.DoesNotExist:
                try:
                    current_membership_price = membership_order.membership.type.price

                    last_membership_order = MembershipOrder.objects.filter(customer=member).last()

                    # Make stripe charge to a customer
                    stripe_utils = StripeUtils()
                    stripe_utils.CURRENCY = self.CURRENCY
                    charge_response = stripe_utils.make_charge(amount=current_membership_price,
                                                               customer=member.stripe_id)
                    charge = charge_response.get('response_object')
                    # Check if the payment was approved
                    if not charge:
                        # There is an error trying to creating the stripe charge
                        context = {
                            'paymentError': charge_response.get('error'),
                        }
                        print("--------- STRIPE PAYMENT ERROR ---------")
                        print(context)
                        print("-------------------------")
                        continue

                    # Create a donation
                    charge = charge_response.get('response_object')
                    membership_order_data = {
                        'cc_brand': charge.source.brand,
                        'stripe_charge_id': charge.id,
                        'last4': charge.source.last4,
                        'membership': last_membership_order.membership.id,
                        'billing_address': last_membership_order.billing_address.id,
                        'customer': member.id,
                        'amount': current_membership_price
                    }
                    membership_order_form = MembershipOrderForm(membership_order_data)
                    if membership_order_form.is_valid():
                        membership_order = membership_order_form.save()

                        context = {
                            'order': membership_order,
                            'base_url': "{0}://{1}".format('https', 'dynamicweb.ungleich.ch')

                        }
                        email_data = {
                            'subject': 'Your monthly membership has been charged',
                            'to': member.user.email,
                            'context': context,
                            'template_name': 'membership_monthly_charge',
                            'template_path': 'digitalglarus/emails/'
                        }
                        email = BaseEmail(**email_data)
                        email.send()

                        print("--------- PAYMENT DONATION SUCCESSFULL ---------")
                        print("Member: %s" % member.user.email)
                        print("Amount: %s %s" % (current_membership_price, self.CURRENCY))
                        print("-----------------------------------------------")

                except Exception as e:
                    print("--------- ERROR ---------")
                    print(e)
                    print("-------------------------")
                    continue
Esempio n. 4
0
    def handle(self, *args, **options):
        donators = DonatorStatus.objects.filter(status=DonatorStatus.ACTIVE)
        current_month = datetime.now().month
        current_year = datetime.now().year

        print("--------- STARTING DONATIONS SCRIPT  ---------")
        print("Donations date: %s-%s" % (current_month, current_year))

        for donator_status in donators:
            donator = donator_status.user.stripecustomer
            try:
                Donation.objects.get(created_at__month=current_month,
                                     created_at__year=current_year,
                                     donator=donator)
            except Donation.DoesNotExist:
                try:
                    # Get donator last donation amount
                    last_donation = Donation.objects.filter(
                        donator=donator).last()
                    donation_amount = last_donation.donation

                    # Make stripe charge to a customer
                    stripe_utils = StripeUtils()
                    stripe_utils.CURRENCY = self.CURRENCY
                    charge_response = stripe_utils.make_charge(
                        amount=donation_amount, customer=donator.stripe_id)
                    charge = charge_response.get('response_object')

                    # Check if the payment was approved
                    if not charge:
                        # There is an error trying to creating the stripe charge
                        context = {
                            'paymentError': charge_response.get('error'),
                        }
                        print("--------- STRIPE PAYMENT ERROR ---------")
                        print(context)
                        print("-------------------------")
                        continue
                    # Create a donation
                    charge = charge_response.get('response_object')
                    donation_data = {
                        'cc_brand': charge.source.brand,
                        'stripe_charge_id': charge.id,
                        'last4': charge.source.last4,
                        'billing_address': last_donation.billing_address.id,
                        'donator': donator.id,
                        'donation': donation_amount
                    }
                    donation_form = DonationForm(donation_data)
                    if donation_form.is_valid():
                        donation = donation_form.save()

                        context = {
                            'donation':
                            donation,
                            'base_url':
                            "{0}://{1}".format('https',
                                               'dynamicweb.ungleich.ch')
                        }
                        email_data = {
                            'subject': 'Your donation have been charged',
                            'to': donation.donator.user.email,
                            'context': context,
                            'template_name': 'donation_charge',
                            'template_path': 'nosystemd/emails/'
                        }
                        email = BaseEmail(**email_data)
                        email.send()

                        print(
                            "--------- PAYMENT DONATION SUCCESSFULL ---------")
                        print("Donator: %s" % donation.donator.user.email)
                        print("Amount: %s %s" %
                              (donation.donation, self.CURRENCY))
                        print(
                            "-----------------------------------------------")
                except Exception as e:
                    print("--------- ERROR ---------")
                    print(e)
                    print("-------------------------")
                    continue
Esempio n. 5
0
    def post(self, request, *args, **kwargs):
        form = self.get_form()

        if form.is_valid():
            context = self.get_context_data()
            token = form.cleaned_data.get('token')
            donation_amount = form.cleaned_data.get('donation_amount')

            # Get or create stripe customer
            customer = StripeCustomer.get_or_create(email=self.request.user.email,
                                                    token=token)
            if not customer:
                form.add_error("__all__", "Invalid credit card")
                return self.render_to_response(self.get_context_data(form=form))

            # Create Billing Address
            billing_address = form.save()

            # Make stripe charge to a customer
            stripe_utils = StripeUtils()
            stripe_utils.CURRENCY = 'usd'
            charge_response = stripe_utils.make_charge(amount=donation_amount,
                                                       customer=customer.stripe_id)
            charge = charge_response.get('response_object')

            # Check if the payment was approved
            if not charge:
                context.update({
                    'paymentError': charge_response.get('error'),
                    'form': form
                })
                return render(request, self.template_name, context)

            # Create a donation
            charge = charge_response.get('response_object')
            donation_data = request.POST.copy()
            donation_data.update({
                'cc_brand': charge.source.brand,
                'stripe_charge_id': charge.id,
                'last4': charge.source.last4,
                'billing_address': billing_address.id,
                'donator': customer.id,
                'donation': donation_amount
            })
            donation_form = DonationForm(donation_data)
            if donation_form.is_valid():

                # reactivate donation status
                donation = donation_form.save()

                try:
                    donator_status = DonatorStatus.objects.get(user=self.request.user)
                    donator_status.set_active()
                except DonatorStatus.DoesNotExist:
                    pass

                donation = donation_form.save()

                context = {
                    'donation': donation,
                    'base_url': "{0}://{1}".format(request.scheme, request.get_host())

                }
                email_data = {
                    'subject': 'Your donation have been charged',
                    'to': request.user.email,
                    'context': context,
                    'template_name': 'donation_charge',
                    'template_path': 'nosystemd/emails/'
                }
                email = BaseEmail(**email_data)
                email.send()

                return HttpResponseRedirect(reverse('nosystemd:donations',
                                                    kwargs={'pk': donation.id}))
            else:
                self.form_invalid(donation_form)

        else:
            return self.form_invalid(form)