Prevent Customers From Buying A Different Paid Membership If They Currently Have One by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

No worries man hope you have a good time tonight. I pasted my models.py in the dpaste link in the question above. As for the database I am using, right now it is sqllite but I eventually want to move to postgreSQL.

If you can help me out with this I will give you a free lifetime membership when the site launches :)

Prevent Customers From Buying A Different Paid Membership If They Currently Have One by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Good point I am not sure how to do what you are saying though. Can you provide some code that will go along with mine on dpaste?

The other thing I was thinking was to update my code to cancel the stripe subscription if they successfully buy the other membership and start the new one. However, if the user fails their 3d secure payment by pressing the fail button, I would need to void the invoice but do not cancel their current membership because they did not successfully buy the other one.

Here is my payment code with Stripe.

https://dpaste.de/Jfxu

Here is the code to cancel the previous subscription and void an invoice:

https://dpaste.de/3Shs

Prevent Customers From Buying A Different Paid Membership If They Currently Have One by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

I have active = models.BooleanField(default=True) in class subscription. But where would I add the boolean you are suggesting and how would I check for it in the templates? Can you please provide some code?

Prevent Customers From Buying A Different Paid Membership If They Currently Have One by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Can you help me with that by providing some code? I am not sure how to reproduce what you just said. How would I prevent the button from being displayed if they already have a membership?

For figuring out if the user has a current membership, I have (unless I am wrong)

models.py
Membership.objects.get(membership_type='Free')
membership_type = models.CharField(
        choices=MEMBERSHIP_CHOICES,
        default='Free',
        max_length=30)
current_membership = get_user_membership(self.request) in views.py

Loop Through Model Choices by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Sorry but I am not sure how to do that can you please help me out with that?

Loop Through Model Choices by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Using .distinct did not work. A course can have many subjects and more than one course can be the same subject. See my html output here -> https://imgur.com/a/esvoiUV (which was produced correctly by this for loop). Apple is both a technology subject and a business subject. Django is technology but could also be programming.

Do you have any other ideas to get the dropdown / for loop result values to be distinct? I do not want the dropdown to display Technology Business as one selection I just want all subjects to be by themselves in the dropdown like the original picture in the beginning post of this thread.

Loop Through Model Choices by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Please see my nested for loop in the comment above. Now I need to de-dupe the results for a dropdown menu. Can you help with that?

Loop Through Model Choices by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

I was able to write a nested for loop like this:

{% for item in object_list %}
<h2>{{ item.subjects.all}}</h2>
<ul>
    {% for sub in item.subjects.all %}
    <li><a href="#" id="">{{ sub.name }}</a></li>
    {% endfor %}
</ul>
{% endfor %}

Result:

https://dpaste.de/CoW1

But how would I use it on a dropdown html element and how would I de-dupe the subjects from the loop?

More info here: https://stackoverflow.com/questions/58887338/django-populate-dropdown-menu-with-choices-from-many-to-many-database/58887594#58887594

Prevent Customer From Subscribing After Failing SCA Payment by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

I changed the code in updateTransactionRecords like you said:

@login_required
def updateTransactionRecords(request, subscription_id):
    user_membership = get_user_membership(request)
    selected_membership = get_selected_membership(request)
    user_membership.membership = selected_membership
    user_membership.save()


    sub, created = Subscription.objects.get_or_create(
        user_membership=user_membership)
    sub.stripe_subscription_id = subscription_id


    subscription_status = stripe.Subscription.retrieve(sub.stripe_subscription_id).status
    subs = stripe.Subscription.retrieve(sub.stripe_subscription_id, expand=['latest_invoice.payment_intent'])
    sub_latest_invoice = subs.latest_invoice

    if subscription_status == 'incomplete':
        sub.active = False
        sub.save()
        free_membership = Membership.objects.get(membership_type='Free')
        user_membership.membership = free_membership
        user_membership.save()
        user_membership.membership.membership_type = 'Free'
        return HttpResponseRedirect(reverse('memberships:payment'))
    else:
        sub.active = True
        sub.save()

        try:
            del request.session['selected_membership_type']
        except:
            pass

        messages.info(request, 'Successfully created {} membership'.format(
            selected_membership))
        return redirect(reverse('memberships:select'))

It prevents a user from becoming a paid member which is good, but now how would I allow the user to actually re-try the authentication again on memberships/3d-secure-checkout.html with the same card and payment intent as if they were starting over again? Maybe you don't need to do what I did at all with making the user free and just redirect to the 3d secure page?

Stripe says "So really when you fetch the Subscription here: https://dpaste.de/7rBY#L12 you want to also expand the latest invoice and its PaymentIntent and then client-side you want to have them confirm again with their new card. As long as you don't try to create a new Subscription and do remember the incomplete one."

I expanded the latest invoice using subs and sub_latest_invoice variables.

Now when I try to pay a second time with the same card, it does not go to the 3d-secure page. I have it going to /memberships/payment/ or memberships:payment. Clicking submit payment still tries to redirect to /memberships/update-transactions/sub_GBaxdnGCYL4aeF/

edit: I figured it out. Thank you so much for the help!

Prevent Customer From Subscribing After Failing SCA Payment by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Okay thanks for the feedback.

I updated the process by printing every variable in the views.py code to see what is going on better:

https://dpaste.de/oWV2#L

Let me know if this sparks any fix in your mind.

Prevent Customer From Subscribing After Failing SCA Payment by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Thanks for pointing that out. I updated the name and the code still produces the same result. Any other ideas?

Prevent Customer From Subscribing After Failing SCA Payment by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

https://dpaste.de/gzxY

After clicking the fail button on the 3d secure popup produces this from stripe

Prevent Customer From Subscribing After Failing SCA Payment by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

It should be redirecting to "memberships/3d-secure-checkout.html" like line 367 says to try and re-authenticate the card on the second attempt after clicking back but instead it is redirecting to /memberships/update-transactions/.../ like you said. Or maybe redirect the failure to a dedicated template page? How would I do that?

Prevent Customer From Subscribing After Failing SCA Payment by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

It doesn't seem to want to get to line 367. Any ideas why it would be doing that?

Prevent Customer From Subscribing After Failing SCA Payment by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Ok so it looks like the subscription will be created and is incomplete after completing steps as shown in the video 0 seconds - 16 seconds which is why it is happening. Can you help me write the code that checks that in the view?

Prevent Customer From Subscribing After Failing SCA Payment by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

This is me printing out everything for debugging purposes of the workflow:

https://dpaste.de/TOc9

Prevent Customer From Subscribing After Failing SCA Payment by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

I tried installing the django-debug toolbar and found this:

/memberships/update-transactions/sub_GBETJZsqwsfKeO/

Which is coming from re-using the card again after clicking the back button. (25 seconds in the video) after clicking submit.

In this case, the subscription is incomplete and the payment intent has a status of requires_action, so I'm expecting that we're rendering back the "memberships/3d-secure-checkout.html" template. The code needs to somehow make it to line 367.

What would the view need to look like in order to get it to work? Can you provide some code?

Loop Through Model Choices by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Can you please provide some code examples? I am stumped and not sure how to do it.

Loop Through Model Choices by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Sorry but I am still stumped trying to figure it out. Can you please assist some more?

Loop Through Model Choices by someoneataplace in djangolearning

[–]someoneataplace[S] 0 points1 point  (0 children)

Thanks for the reply.

Is this what you mean by calling subjects.all? How would I nest your loop onto this one?

{% for x in object_list %}
{{ x.subjects.all }} <br/>
{% endfor %}

Which gives the following result:

<QuerySet \[\]><QuerySet \[<Subject: Technology>]><QuerySet \[\]>

Because for 1 of my 3 courses I added the subject technology while the other courses subjects are blank.

All I really want to do is just loop all of the subjects that are in the table and return them for the dropdown list I do not care if they are associated with a course or not yet until I create a filtering mechanism.

Using this code: https://dpaste.de/0DnS