def METHOD_NAME(payment, past_payments, amount: int | None = None):
    # Alllow at most three failures of current payment method
    rejected_payments = past_payments.filter(
        state=Payment.REJECTED, repeat=payment.repeat or payment
    )
    if rejected_payments.count() > 3:
        payment.recurring = ""
        payment.save()
        return
    # Create repeated payment
    repeated = payment.repeat_payment(amount=amount)
    # Backend does not support it
    if not repeated:
        # Remove recurring flag
        payment.recurring = ""
        payment.save()
        return
    # Remote trigger of the payment
    repeated.trigger_remotely()