def process_payment(): lead_id = request.form['lead_id'] payment_id = request.form['payment_id'] logging.debug('###### BACKGROUND PROCESSING!') logging.debug('Lead Id: %s' % lead_id) logging.debug('Payment Id: %s' % payment_id) lead = _get_lead(int(lead_id)) payment = Payment.get_by_id(int(payment_id)) plan = payment.plan.get() property_owner = lead.property_owner.get() customer_data = property_owner.to_json() email = customer_data['email'] credit_card_brand = customer_data['credit_card_brand'] logging.debug('trying to find customer %s' % email) fc_result = superlogica.find_customer(email) payment.update_status('PENDING', 'Creating customer') customer_id = None if fc_result is not None: if len(fc_result['body']) > 0: pprint.pprint(fc_result) customer_id = fc_result['body'][0]['id_sacado_sac'] logging.debug('customer %s found' % customer_id) else: logging.debug('customer not found.') if customer_id is None: logging.debug('customer not found. lets create a new one') customer_result = _try_create_customer(customer_data) customer_id = customer_result['id_sacado_sac'] logging.debug('customer %s created' % customer_id) payment.update_status('PENDING', 'Creating billing') if customer_id is not None: logging.debug('creating billing') bl_result = superlogica.create_billing(customer_id, plan.price, payment.instalments, plan.erp_id) logging.debug('registering creditcard') billing = Billing() billing.erp_result = bl_result billing.lead = lead.key billing.put() payment.update_status('PENDING', 'Creating credit card') callback_url = URL_CALLBACK % plan.slug cc_result = superlogica.register_creditcard(email, credit_card_brand.lower(), callback_url) url = cc_result['body']['url'] payment.iframe_url = url payment.put() payment.update_status('SUCCESS', 'Done') lead.status = 'PENDING_PAYMENT' lead.put() create_activity('LEAD_PENDING_PAYMENT', lead=lead) else: payment.update_status('ERROR', 'Cannot create customer or billing at ERP') return '', 200