forked from uncloud/uncloud
Replace legacy Stripe Charge API by Payment{setup, intent}
This commit is contained in:
parent
47148454f6
commit
bf83b750de
10 changed files with 250 additions and 50 deletions
|
|
@ -1,9 +1,12 @@
|
|||
from django.shortcuts import render
|
||||
from django.db import transaction
|
||||
from django.contrib.auth import get_user_model
|
||||
from rest_framework import viewsets, permissions, status
|
||||
from rest_framework import viewsets, permissions, status, views
|
||||
from rest_framework.renderers import TemplateHTMLRenderer
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.reverse import reverse
|
||||
from rest_framework.decorators import renderer_classes
|
||||
|
||||
import json
|
||||
|
||||
|
|
@ -66,7 +69,6 @@ class PaymentMethodViewSet(viewsets.ModelViewSet):
|
|||
else:
|
||||
return PaymentMethodSerializer
|
||||
|
||||
|
||||
def get_queryset(self):
|
||||
return PaymentMethod.objects.filter(owner=self.request.user)
|
||||
|
||||
|
|
@ -75,29 +77,32 @@ class PaymentMethodViewSet(viewsets.ModelViewSet):
|
|||
def create(self, request):
|
||||
serializer = self.get_serializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
payment_method = PaymentMethod.objects.create(owner=request.user, **serializer.validated_data)
|
||||
|
||||
# Retrieve Stripe customer ID for user.
|
||||
customer_id = uncloud_stripe.get_customer_id_for(request.user)
|
||||
if customer_id == None:
|
||||
return Response(
|
||||
if serializer.validated_data['source'] == "stripe":
|
||||
# Retrieve Stripe customer ID for user.
|
||||
customer_id = uncloud_stripe.get_customer_id_for(request.user)
|
||||
if customer_id == None:
|
||||
return Response(
|
||||
{'error': 'Could not resolve customer stripe ID.'},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
# Register card under stripe customer.
|
||||
credit_card = uncloud_stripe.CreditCard(**serializer.validated_data.pop('credit_card'))
|
||||
card_request = uncloud_stripe.create_card(customer_id, credit_card)
|
||||
if card_request['error']:
|
||||
return Response({'stripe_error': card_request['error']}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
card_id = card_request['response_object']['id']
|
||||
# TODO: handle error
|
||||
setup_intent = uncloud_stripe.create_setup_intent(customer_id)
|
||||
payment_method = PaymentMethod.objects.create(
|
||||
owner=request.user,
|
||||
stripe_setup_intent_id=setup_intent['response_object']['id'],
|
||||
**serializer.validated_data)
|
||||
|
||||
# Save payment method locally.
|
||||
serializer.validated_data['stripe_card_id'] = card_request['response_object']['id']
|
||||
payment_method = PaymentMethod.objects.create(owner=request.user, **serializer.validated_data)
|
||||
# TODO: find a way to use reverse properly:
|
||||
# https://www.django-rest-framework.org/api-guide/reverse/
|
||||
query= "payment-method/{}/register-stripe-cc".format(
|
||||
payment_method.uuid
|
||||
)
|
||||
stripe_registration_url = reverse('api-root', request=request) + query
|
||||
return Response({'please_visit': stripe_registration_url})
|
||||
|
||||
# We do not want to return the credit card details sent with the POST
|
||||
# request.
|
||||
output_serializer = PaymentMethodSerializer(payment_method)
|
||||
return Response(output_serializer.data)
|
||||
return Response(serializer.data)
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def charge(self, request, pk=None):
|
||||
|
|
@ -112,6 +117,39 @@ class PaymentMethodViewSet(viewsets.ModelViewSet):
|
|||
except Exception as e:
|
||||
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
@action(detail=True, methods=['get'], url_path='register-stripe-cc', renderer_classes=[TemplateHTMLRenderer])
|
||||
def register_stripe_cc(self, request, pk=None):
|
||||
payment_method = self.get_object()
|
||||
setup_intent = uncloud_stripe.get_setup_intent(
|
||||
payment_method.stripe_setup_intent_id)
|
||||
|
||||
# Render stripe card registration form.
|
||||
template_args = {
|
||||
'client_secret': setup_intent["response_object"]["client_secret"],
|
||||
'stripe_pk': uncloud_stripe.public_api_key
|
||||
}
|
||||
return Response(template_args, template_name='stripe-payment.html.j2')
|
||||
|
||||
@action(detail=True, methods=['post'], url_path='register-stripe-cc')
|
||||
def register_stripe_cc(self, request, pk=None):
|
||||
payment_method = self.get_object()
|
||||
setup_intent = uncloud_stripe.get_setup_intent(
|
||||
payment_method.stripe_setup_intent_id)
|
||||
|
||||
# Card had been registered, fetching payment method.
|
||||
payment_method_id = setup_intent["response_object"].payment_method
|
||||
if payment_method_id:
|
||||
payment_method.stripe_payment_method_id = payment_method_id
|
||||
payment_method.save()
|
||||
|
||||
return Response({
|
||||
'uuid': payment_method.uuid,
|
||||
'activated': payment_method.active})
|
||||
else:
|
||||
error = 'Could not fetch payment method from stripe. Please try again.'
|
||||
return Response({'error': error})
|
||||
|
||||
|
||||
###
|
||||
# Admin views.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue