from django.views.decorators.csrf import csrf_exemptfrom django.http import HttpResponsefrom stripe.api_resources.charge import Charge@csrf_exemptdefcreate_charge(request):# Get the payment token from the request token = request.POST.get('stripeToken')# Create a Stripe customer customer = stripe.Customer.create(email=request.POST.get('email'),source=token,)# Charge the customer for the specified amount charge = Charge.create(customer=customer.id,amount=request.POST.get('amount'),currency=request.POST.get('currency'),description=request.POST.get('description'),)returnHttpResponse(status=200)
2. Creating a Subscription:
3. Creating a Customer:
4. Updating a Customer:
5. Deleting a Customer:
6. Retrieving a Customer:
7. Listing Customers:
8. Creating a Payment Intent:
9. Retrieving a Payment Intent:
10. Confirming a Payment Intent:
11. Capturing a Payment:
12. Refunding a Payment:
13. Webhook Handling (Stripe Event):
14. Creating a Plan:
15. Updating a Plan:
16. Deleting a Plan:
17. Retrieving a Plan:
18. Listing Plans:
19. Creating a Product:
20. Updating a Product:
21. Deleting a Product:
22. Retrieving a Product:
23. Listing Products:
24. Invoicing:
25. Retrieving an Invoice:
26. Listing Invoices:
27. Sending an Invoice:
28. Marking an Invoice as Paid:
29. Updating an Invoice:
30. Retrieving Upcoming Invoice Items:
31. Creating an Invoice Item:
32. Updating an Invoice Item:
33. Deleting an Invoice Item:
34. Retrieving an Invoice Item:
35. Listing Invoice Items:
36. Setting a Stripe Subscription to Cancel at the End of the Billing Period:
from stripe.api_resources.payment_intent import PaymentIntent
payment_intent = PaymentIntent.retrieve(intent_id)
from stripe.api_resources.payment_intent import PaymentIntent
payment_intent = PaymentIntent.retrieve(intent_id)
payment_intent.confirm()
from stripe.api_resources.charge import Charge
charge = Charge.retrieve(charge_id)
charge.capture()
from stripe.api_resources.refund import Refund
refund = Refund.create(
charge=charge_id,
amount=refund_amount,
)
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from stripe import webhook
@csrf_exempt
def webhook_handler(request):
# Get the event data from the request
event_data = json.loads(request.body)
# Verify the webhook signature
if not webhook.construct_event(
request.body, request.META.get('HTTP_STRIPE_SIGNATURE'),
settings.STRIPE_WEBHOOK_SECRET
):
return HttpResponse(status=403)
# Handle the event
event = event_data['event']
type_ = event['type']
if type_ == 'charge.succeeded':
# Handle charge succeeded event
elif type_ == 'charge.failed':
# Handle charge failed event
return HttpResponse(status=200)
from stripe.api_resources.plan import Plan
plan = Plan.create(
amount=1000,
interval='month',
product='prod_test',
currency='usd',
name='Basic Plan',
)
from stripe.api_resources.plan import Plan
plan = Plan.retrieve(plan_id)
plan.update({
'amount': 2000,
})
from stripe.api_resources.plan import Plan
plan = Plan.retrieve(plan_id)
plan.delete()
from stripe.api_resources.plan import Plan
plan = Plan.retrieve(plan_id)
from stripe.api_resources.plan import Plan
plans = Plan.list()