Skip to content

Instantly share code, notes, and snippets.

@homanp
Created March 23, 2016 20:57
Show Gist options
  • Save homanp/03303ebb8dffc9b6029b to your computer and use it in GitHub Desktop.
Save homanp/03303ebb8dffc9b6029b to your computer and use it in GitHub Desktop.
import settings
from core.models import Company
import stripe
def create_customer(instance, token):
stripe.api_key = settings.STRIPE_SECRET_KEY
customer_name = instance.request.user.userprofile.company.name
customer = stripe.Customer.create(source=token,
plan=settings.STRIPE_DEFAULT_PLAN,
email=instance.request.user,
description=customer_name)
company_pk = instance.request.user.userprofile.company.id
company = Company.objects.get(pk=company_pk)
company.stripe_customer_id = customer.id
company.save()
return company
class BillingCard(generics.UpdateAPIView):
permission_classes = (IsAuthenticated,)
def post(self, request):
cc_token = request.DATA.get('cc_token', None)
if cc_token:
company = helpers.create_customer(self, cc_token)
serializer = CompanySerializer(company)
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response('No cc_token in request',
status=status.HTTP_400_BAD_REQUEST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment