def _create_or_replace_payment(lead, plan_slug, instalments): plan = Plan.find_by_slug(plan_slug) payment = Payment.find_open(lead.key, plan.key) if payment is None: payment = Payment() payment.lead = lead.key payment.plan = plan.key payment.status = 'PENDING' payment.message = 'Pending processing' payment.instalments = instalments payment.iframe_url = '' payment.put() _enqueue_payment_message(lead, payment) return payment