|
15 | 15 | CurrentCustomerSerializer,
|
16 | 16 | SubscriptionSerializer,
|
17 | 17 | CardSerializer,
|
| 18 | + CardTokenSerializer, |
18 | 19 | CancelSerializer,
|
19 | 20 | ChargeSerializer,
|
20 | 21 | InvoiceSerializer,
|
@@ -120,6 +121,42 @@ def post(self, request, *args, **kwargs):
|
120 | 121 | return Response(error_data, status=status.HTTP_400_BAD_REQUEST)
|
121 | 122 |
|
122 | 123 |
|
| 124 | +class ChangeCardTokenView(StripeView): |
| 125 | + """ |
| 126 | + Add or update customer card token |
| 127 | +
|
| 128 | + This is useful if you are planing to use strip.js to |
| 129 | + retrieve the card token. This isolates the full credit |
| 130 | + card number from your server. |
| 131 | + """ |
| 132 | + serializer_class = CardTokenSerializer |
| 133 | + |
| 134 | + def post(self, request, *args, **kwargs): |
| 135 | + try: |
| 136 | + serializer = self.serializer_class(data=request.data) |
| 137 | + |
| 138 | + if serializer.is_valid(): |
| 139 | + validated_data = serializer.validated_data |
| 140 | + |
| 141 | + customer = self.get_customer() |
| 142 | + |
| 143 | + token = validated_data['token'] |
| 144 | + customer.update_card(token) |
| 145 | + send_invoice = customer.card_fingerprint == "" |
| 146 | + |
| 147 | + if send_invoice: |
| 148 | + customer.send_invoice() |
| 149 | + customer.retry_unpaid_invoices() |
| 150 | + |
| 151 | + return Response(validated_data, status=status.HTTP_201_CREATED) |
| 152 | + else: |
| 153 | + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
| 154 | + |
| 155 | + except stripe.StripeError as e: |
| 156 | + error_data = {u'error': smart_str(e) or u'Unknown error'} |
| 157 | + return Response(error_data, status=status.HTTP_400_BAD_REQUEST) |
| 158 | + |
| 159 | + |
123 | 160 | class CancelView(StripeView):
|
124 | 161 | """ Cancel customer subscription """
|
125 | 162 | serializer_class = CancelSerializer
|
|
0 commit comments