Created
March 23, 2016 20:57
-
-
Save homanp/03303ebb8dffc9b6029b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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