简体   繁体   中英

Adding User to Django Group via Stripe API Response

I am attempting to add a user to a group once a successful Order response is returned.

I have a group like below:

act_group = Group.objects.create(name='ACT Group')

There will most likely be multiple products, but for this specific group we will only add users to the group if their order contains an item called ACT Course

I've yet to write what the post purchase will look like, but I know I will be using Django-Oscar 1.6 to handle the orders so the user will have some type of order history such as below

class oscar.apps.order.processing.EventHandler({ User })

handle_shipping_event()

I am looking to see the best way to have the order signal adding that specific user to the ACT Group using the username with which they are currently logged in (users will be required to create an account since they will need this for access to digital materials).

I'm presuming you are already using Stripe's webhooks to catch the API responses?

If so, the easiest way to work with Stripe's webhooks is using a Django integration library for Stripe such as pinax-stripe or djstripe . It takes a bit of setting up, but it is worth it. These libraries give you Django models that provide mappings for Stripe's API JSON objects such as Customer and Subscription , and which include the ID strings that Stripe sends in its API responses. These models act as intermediaries, making it straightforward to link Stripe's JSON responses with your own app's models such as Users and Orders (with the added benefit that you can have all of Stripe's event data in your project database).

Then you can write a view that automatically updates your Django model instances when a Stripe API response hits your webhook URL:

# urls.py
...
path('webhooks/', myviews.webhooks, name='webhooks'),

# views.py
import json
from django.conf import settings
from pinax.stripe import Customer

def webhooks(request):
    import stripe
    stripe.api_key = settings.STRIPE_SECRET_KEY
    event_json = json.loads(request.body.decode())
    if event_json['type'] == 'order.created':
        ...
        # parse the JSON to get the customer's Stripe ID, 
        # and check if it has the relevant order item, then...
        Customer.objects.get(stripe_id='12345678')
        user = Customer.user  # Django user model
        user.groups.add(act_group)

There are probably more elegant ways to parse the different events, but you get the general idea. I also note that neither library I mentioned has a specific model for Stripe's Order responses. But you would presumably not need that as long as you can get the information that you need from Stripe's Order webhook to look up the relevant orders and users in your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM