def pay_intent_complete(self, request, pk=None): """ Invoice Payment Endpoint --- omit_serializer: true omit_parameters: - query """ invoice = self.get_object() payload = request.data # Pay with Stripe paid_at = datetime.datetime.utcnow() stripe = stripe_utils.get_client() try: payment_intent = stripe.PaymentIntent.retrieve(payload['id']) if payment_intent.status == 'succeeded': # Save payment details Payment.objects.create( invoice=invoice, payment_method=PAYMENT_METHOD_STRIPE, amount=Decimal(payment_intent.amount) * Decimal(0.01), currency=(payment_intent.currency or CURRENCY_EUR).upper(), status=STATUS_COMPLETED, ref=payment_intent.id, paid_at=paid_at, created_by=request.user, extra=json.dumps(dict( paid=True, itent_id=payment_intent.id, email=payload.get('email', None) )) ) # Update invoice invoice.paid = True invoice.paid_at = paid_at invoice.save() notify_paid_invoice.delay(invoice) invoice_serializer = InvoiceSerializer(invoice, context={ 'request': request}) return Response(invoice_serializer.data) except: return Response(dict( message='We could not process your payment! Please contact [email protected]'), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def pay_intent(self, request, pk=None): """ Invoice Payment Endpoint --- omit_serializer: true omit_parameters: - query """ invoice = self.get_object() payload = request.data # Pay with Stripe paid_at = datetime.datetime.utcnow() stripe = stripe_utils.get_client() try: # Create customer customer = stripe.Customer.create(**dict(email=payload['email'])) # Create Payment Intent payment_intent = stripe.PaymentIntent.create( amount=payload['amount'], currency=payload.get('currency', CURRENCY_EUR), description=payload.get('description', invoice.title), payment_method_types=['card'], customer=customer.id, metadata=dict( invoice_id=invoice.id, project_id=invoice.project.id ) ) # invoice_serializer = InvoiceSerializer(invoice, context={'request': request}) return Response(payment_intent) except: return Response(dict( message='We could not process your payment! Please contact [email protected]'), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def pay(self, request, pk=None): """ Invoice Payment Endpoint --- omit_serializer: true omit_parameters: - query """ invoice = self.get_object() payload = request.data if payload['payment_method'] == PAYMENT_METHOD_STRIPE: # Pay with Stripe paid_at = datetime.datetime.utcnow() stripe = stripe_utils.get_client() try: # Create customer customer = stripe.Customer.create( **dict(source=payload['token'], email=payload['email'])) # Create Charge charge = stripe.Charge.create( idempotency_key=payload.get('idem_key', None), **dict(amount=payload['amount'], description=payload.get('description', invoice.title), currency=payload.get('currency', CURRENCY_EUR), customer=customer.id, metadata=dict(invoice_id=invoice.id, project_id=invoice.project.id))) if charge.paid: # Save payment details Payment.objects.create( invoice=invoice, payment_method=PAYMENT_METHOD_STRIPE, amount=Decimal(charge.amount) * Decimal(0.01), currency=(charge.currency or CURRENCY_EUR).upper(), status=STATUS_COMPLETED, ref=charge.id, paid_at=paid_at, created_by=request.user, extra=json.dumps( dict( paid=charge.paid, token=payload['token'], email=payload['email'], captured=charge.captured, ))) # Update invoice invoice.paid = True invoice.paid_at = paid_at invoice.save() notify_paid_invoice.delay(invoice) invoice_serializer = InvoiceSerializer( invoice, context={'request': request}) return Response(invoice_serializer.data) except InvalidRequestError: return Response(dict( message= 'We could not process your payment! Please contact [email protected]' ), status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return Response(dict( message= 'We could not process your payment! Please contact [email protected]' ), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def pay(self, request, pk=None): """ Invoice Payment Endpoint --- omit_serializer: true omit_parameters: - query """ invoice = self.get_object() payload = request.data if payload['payment_method'] == PAYMENT_METHOD_STRIPE: # Pay with Stripe paid_at = datetime.datetime.utcnow() stripe = stripe_utils.get_client() try: # Create customer customer = stripe.Customer.create(**dict(source=payload['token'], email=payload['email'])) # Create Charge charge = stripe.Charge.create( idempotency_key=payload.get('idem_key', None), **dict( amount=payload['amount'], description=payload.get('description', invoice.title), currency=payload.get('currency', CURRENCY_EUR), customer=customer.id, metadata=dict( invoice_id=invoice.id, project_id=invoice.project.id ) ) ) if charge.paid: # Save payment details Payment.objects.create( invoice=invoice, payment_method=PAYMENT_METHOD_STRIPE, amount=Decimal(charge.amount) * Decimal(0.01), currency=(charge.currency or CURRENCY_EUR).upper(), status=STATUS_COMPLETED, ref=charge.id, paid_at=paid_at, created_by=request.user, extra=json.dumps(dict( paid=charge.paid, token=payload['token'], email=payload['email'], captured=charge.captured, )) ) # Update invoice invoice.paid = True invoice.paid_at = paid_at invoice.save() notify_paid_invoice.delay(invoice) invoice_serializer = InvoiceSerializer(invoice, context={'request': request}) return Response(invoice_serializer.data) except InvalidRequestError: return Response(dict(message='We could not process your payment! Please contact [email protected]'), status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return Response(dict(message='We could not process your payment! Please contact [email protected]'), status=status.HTTP_500_INTERNAL_SERVER_ERROR)