# Django Stripe

***

**1. Creating a Charge:**

```python
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from stripe.api_resources.charge import Charge

@csrf_exempt
def create_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'),
    )

    return HttpResponse(status=200)
```

**2. Creating a Subscription:**

```python
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from stripe.api_resources.subscription import Subscription

@csrf_exempt
def create_subscription(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,
    )

    # Create a Stripe subscription
    subscription = Subscription.create(
        customer=customer.id,
        plan=request.POST.get('plan'),
    )

    return HttpResponse(status=200)
```

**3. Creating a Customer:**

```python
from stripe.api_resources.customer import Customer

customer = Customer.create(
    email=request.POST.get('email'),
    source=request.POST.get('token'),
)
```

**4. Updating a Customer:**

```python
from stripe.api_resources.customer import Customer

customer = Customer.retrieve(customer_id)
customer.update({
    'email': 'new_email@example.com',
})
```

**5. Deleting a Customer:**

```python
from stripe.api_resources.customer import Customer

customer = Customer.retrieve(customer_id)
customer.delete()
```

**6. Retrieving a Customer:**

```python
from stripe.api_resources.customer import Customer

customer = Customer.retrieve(customer_id)
```

**7. Listing Customers:**

```python
from stripe.api_resources.customer import Customer

customers = Customer.list()
```

**8. Creating a Payment Intent:**

```python
from stripe.api_resources.payment_intent import PaymentIntent

payment_intent = PaymentIntent.create(
    amount=request.POST.get('amount'),
    currency=request.POST.get('currency'),
    customer=request.POST.get('customer'),
)
```

**9. Retrieving a Payment Intent:**

```python
from stripe.api_resources.payment_intent import PaymentIntent

payment_intent = PaymentIntent.retrieve(intent_id)
```

**10. Confirming a Payment Intent:**

```python
from stripe.api_resources.payment_intent import PaymentIntent

payment_intent = PaymentIntent.retrieve(intent_id)
payment_intent.confirm()
```

**11. Capturing a Payment:**

```python
from stripe.api_resources.charge import Charge

charge = Charge.retrieve(charge_id)
charge.capture()
```

**12. Refunding a Payment:**

```python
from stripe.api_resources.refund import Refund

refund = Refund.create(
    charge=charge_id,
    amount=refund_amount,
)
```

**13. Webhook Handling (Stripe Event):**

```python
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)
```

**14. Creating a Plan:**

```python
from stripe.api_resources.plan import Plan

plan = Plan.create(
    amount=1000,
    interval='month',
    product='prod_test',
    currency='usd',
    name='Basic Plan',
)
```

**15. Updating a Plan:**

```python
from stripe.api_resources.plan import Plan

plan = Plan.retrieve(plan_id)
plan.update({
    'amount': 2000,
})
```

**16. Deleting a Plan:**

```python
from stripe.api_resources.plan import Plan

plan = Plan.retrieve(plan_id)
plan.delete()
```

**17. Retrieving a Plan:**

```python
from stripe.api_resources.plan import Plan

plan = Plan.retrieve(plan_id)
```

**18. Listing Plans:**

```python
from stripe.api_resources.plan import Plan

plans = Plan.list()
```

**19. Creating a Product:**

```python
from stripe.api_resources.product import Product

product = Product.create(
    name='Your Amazing Product',
)
```

**20. Updating a Product:**

```python
from stripe.api_resources.product import Product

product = Product.retrieve(product_id)
product.update({
    'description': 'This product is amazing!',
})
```

**21. Deleting a Product:**

```python
from stripe.api_resources.product import Product

product = Product.retrieve(product_id)
product.delete()
```

**22. Retrieving a Product:**

```python
from stripe.api_resources.product import Product

product = Product.retrieve(product_id)
```

**23. Listing Products:**

```python
from stripe.api_resources.product import Product

products = Product.list()
```

**24. Invoicing:**

```python
from stripe.api_resources.invoice import Invoice

invoice = Invoice.create(
    customer=customer_id,
)
```

**25. Retrieving an Invoice:**

```python
from stripe.api_resources.invoice import Invoice

invoice = Invoice.retrieve(invoice_id)
```

**26. Listing Invoices:**

```python
from stripe.api_resources.invoice import Invoice

invoices = Invoice.list()
```

**27. Sending an Invoice:**

```python
from stripe.api_resources.invoice import Invoice

invoice.send_invoice()
```

**28. Marking an Invoice as Paid:**

```python
from stripe.api_resources.invoice import Invoice

invoice.mark_as_paid()
```

**29. Updating an Invoice:**

```python
from stripe.api_resources.invoice import Invoice

invoice.update({
    'description': 'Updated description',
})
```

**30. Retrieving Upcoming Invoice Items:**

```python
from stripe.api_resources.invoice_item import InvoiceItem

upcoming_invoice_items = InvoiceItem.list(
    customer=customer_id,
)
```

**31. Creating an Invoice Item:**

```python
from stripe.api_resources.invoice_item import InvoiceItem

invoice_item = InvoiceItem.create(
    customer=customer_id,
    amount=1000,
    currency='usd',
)
```

**32. Updating an Invoice Item:**

```python
from stripe.api_resources.invoice_item import InvoiceItem

invoice_item = InvoiceItem.retrieve(invoice_item_id)
invoice_item.update({
    'description': 'Updated description',
})
```

**33. Deleting an Invoice Item:**

```python
from stripe.api_resources.invoice_item import InvoiceItem

invoice_item = InvoiceItem.retrieve(invoice_item_id)
invoice_item.delete()
```

**34. Retrieving an Invoice Item:**

```python
from stripe.api_resources.invoice_item import InvoiceItem

invoice_item = InvoiceItem.retrieve(invoice_item_id)
```

**35. Listing Invoice Items:**

```python
from stripe.api_resources.invoice_item import InvoiceItem

invoice_items = InvoiceItem.list(
    customer=customer_id,
)
```

**36. Setting a Stripe Subscription to Cancel at the End of the Billing Period:**

```python
from stripe.subscription import Subscription

subscription = Subscription.retrieve("sub_12345")

subscription.cancel_at_period_end = True

subscription.save()
```
