I Have Stucked In When I Enter The Card Details The Response is Come
It SHowing That I ENter The The Incorrect Card Number But I ENter Thr Correct Card Tell me Where I Making Mistake
{
"error": "Your card number is incorrect",
"status": 400,
"payment_intent": {
"id": "Null"
},
"payment_confirm": {
"status": "Failed"
}
}
def check_expiry_month(value):
if not 1 <= int(value) <= 12:
raise serializers.ValidationError("Invalid expiry month.")
def check_expiry_year(value):
today = datetime.datetime.now()
if not int(value) >= today.year:
raise serializers.ValidationError("Invalid expiry year.")
def check_cvc(value):
if not 3 <= len(value) <= 4:
raise serializers.ValidationError("Invalid cvc number.")
def check_payment_method(value):
payment_method = value.lower()
if payment_method not in ["card"]:
raise serializers.ValidationError("Invalid payment_method.")
class CardInformationSerializer(serializers.Serializer):
card_number = serializers.CharField(
max_length=150,
required=True,
)
expiry_month = serializers.CharField(
max_length=150,
required=True,
validators=[check_expiry_month],
)
expiry_year = serializers.CharField(
max_length=150,
required=True,
validators=[check_expiry_year],
)
cvc = serializers.CharField(
max_length=150,
required=True,
validators=[check_cvc],
)
My Views
class PaymentAPI(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
serializer_class = CardInformationSerializer
def post(self, request):
serializer = self.serializer_class(data=request.data)
response = {}
if serializer.is_valid():
data_dict = serializer.data
stripe.api_key = settings.STRIPE_SECRET_KEY
response = self.stripe_card_payment(data_dict=data_dict, request=request)
else:
response = {'errors': serializer.errors, 'status': status.HTTP_400_BAD_REQUEST}
return Response(response)
def stripe_card_payment(self, data_dict, request):
try:
card_details = {
"type": "card",
"card": {
"number": data_dict['card_number'],
"exp_month": data_dict['expiry_month'],
"exp_year": data_dict['expiry_year'],
"cvc": data_dict['cvc'],
},
}
engagement = Engagement.objects.filter(created_by=request.user).first()
time_track = TimeTrackModels.objects.filter(engagement=engagement).first()
if not (engagement and time_track):
# Handle missing engagement or time_track
return Response({'error': "Engagement or TimeTrack not found.", 'status': status.HTTP_400_BAD_REQUEST})
amount = engagement.rate * time_track.hour * 100
payment_intent = stripe.PaymentIntent.create(
amount=amount,
currency='usd',
payment_method_types=['card'],
# Add other necessary fields as needed by Stripe
)
payment_intent_modified = stripe.PaymentIntent.modify(
payment_intent['id'],
payment_method=card_details['id'],
)
stripe.PaymentIntent.confirm(
payment_intent['id'],
payment_method=card_details['id'],
)
try:
payment_confirm = stripe.PaymentIntent.confirm(
payment_intent['id']
)
payment_intent_modified = stripe.PaymentIntent.retrieve(payment_intent['id'])
except:
payment_intent_modified = stripe.PaymentIntent.retrieve(payment_intent['id'])
payment_confirm = {
"stripe_payment_error": "Failed",
"code": payment_intent_modified['last_payment_error']['code'],
"message": payment_intent_modified['last_payment_error']['message'],
'status': "Failed"
}
if payment_intent_modified and payment_intent_modified['status'] == 'succeeded':
response = {
'message': "Card Payment Success",
'status': status.HTTP_200_OK,
"card_details": card_details,
"payment_intent": payment_intent_modified,
"payment_confirm": payment_confirm
}
else:
response = {
'message': "Card Payment Failed",
'status': status.HTTP_400_BAD_REQUEST,
"card_details": card_details,
"payment_intent": payment_intent_modified,
"payment_confirm": payment_confirm
}
except:
response = {
'error': "Your card number is incorrect",
'status': status.HTTP_400_BAD_REQUEST,
"payment_intent": {"id": "Null"},
"payment_confirm": {'status': "Failed"}
}
return response