Package: djangoldp-stripe
djangoldp-stripeA DjangoLDP package supporting Stripe payments. Uses dependencydj-stripeto achieve much of the functionality, a library for handling Stripe payments in DjangoRequirementsdjangoldp 2.1Python 3.6.5Requires the installation of aDjangoLDP serverYou will need to set up aStripe clientInstallationYou will need to set the following settings to your settings.yml:dependencies:# ...-djangoldp-stripeldp_packages:# ...-djangoldp_stripeserver:# ...STRIPE_LIVE_MODE:True# Should be False when testing, True in productionDJSTRIPE_USE_NATIVE_JSONFIELD:True# if using SQLite, set this to FalseDJSTRIPE_FOREIGN_KEY_TO_FIELD:"id"SetSTRIPE_LIVE_SECRET_KEYandSTRIPE_TEST_SECRET_KEYin your OS environment variables where the server is run. These settings will then be securely imported when you next configure. Alternatively, you can add them to the settings.yml:server:# ...STRIPE_LIVE_SECRET_KEY:""STRIPE_TEST_SECRET_KEY:""You can find these settings via theStripe dashboardOptionally you can change the settingLDP_STRIPE_URL_PATHto change the path where Django Stripe's urls will be accessible. By default it is"stripe/", i.e.http://localhost:8080/stripe/. These urls are provided byDjStripeand implement all of the necessary webhooks to keep your Stripe data sync'ed with your Django dataIn the Stripe dashboard, register awebhooklistening to all events (or just those you want to keep up-to-date) and point them to your API url + your webhook endpoint. So if your API was hosted onhttps://yoursite.com/and you didn't change theLDP_STRIPE_URL_PATH, then the endpoint would behttps://yoursite.com/stripe/webhooks/In the created webhooks, find your webhook secret, and set this in the OS variable (or server setting)DJSTRIPE_WEBHOOK_SECRET. It is a string which will startwhsec_Optionally adddjangoldp_stripe.middleware.StripeSubscriptionRequiredMiddlewareto yourMIDDLEWAREsettings insettings.yml. When this middleware is included then it will restrict access to certain requests so that they are only accessible by users with an active subscription. Add a list of all paths which should be restricted to the settingINCLUDE_FROM_GLOBAL_STRIPE_SUBSCRIPTION_REQserver:# ...MIDDLEWARE:-"djangoldp_stripe.middleware.StripeSubscriptionRequiredMiddleware"INCLUDE_FROM_GLOBAL_STRIPE_SUBSCRIPTION_REQ:-'/my-custom-path/'Note that this middleware does not check that an anonymous user is subscribed. To prevent this being a problem, make sure that your server does not give anonymous users permissions that they shouldn't have, so that they must login before accessing the applicationOptionally add the settingREDIRECT_URL_NO_SUBSCRIPTIONif you plan to useUserHasValidSubscriptionViewand wish to modify its' behaviour, e.g.http://localhost:8000/checkout-session/?lookup_key=test-subscriptionorhttp://localhost:8000/checkout-session/?price_id=price_1KOPEhIK99puQD1xtbSN94fkRundjangoldp installRundjangoldp configureRunpython manage.py djstripe_sync_models. Anytime you run this command your database will be manually synchronised with your Stripe records. If for example your site went down for a time, you should re-run this command## Migrating existing customersAfter configuration, you can do this by running:djangoldp configure
python manage.py djstripe_init_customers
python manage.py djstripe_sync_plans_from_stripeStripeSubscriptionPermissionsThis package can be used to provide custom permissions on your models and views. To do this you can use the permissions classStripeSubscriptionPermissionsor the utility functions inpermissions.pySet up the required product permissions on your model:classMyModel(Model):# ...classMeta(Model.Meta):# ...PERMS_REQUIRED_STRIPE_SUBSCRIPTIONS=['prod_xxxx',]When applying either the permission class to aLDPViewSetor the utility function, you can now control access based on the products a requesting user is subscribed toViewsMaking a GET request to/user-valid-subscriptions/will return in JSON-LD the serialized products which the authenticated user is subscribed to. It will only return those which are valid subscriptions (e.g. excluding those which have expired){"@context":[...],"@type":"ldp:Container","@id":"http://yourserver/user-valid-subscriptions/","ldp:contains":[{"name":"My Awesome Product","id":"prod_xxxx"}]}