def get_card_token(data): result = mp.post("/v1/card_tokens", data) print("Token de tarjeta: ", result) if result['status'] != 201: raise ResourceConflict('Problemas obteniendo el token de tarjeta') return result['response']['id']
def check_schedule_exists_by_warehouse(warehouse, day): if not warehouse: raise InvalidArgument('El campo warehouse no puede estar vacio o nulo') if not day: raise InvalidArgument('El campo day no puede estar vacio o nulo') if s.query(StoreHours).filter( and_(StoreHours.store_id == warehouse, StoreHours.day == day)).first(): raise ResourceConflict( 'Este horario ya se encuentra asociado al almacen') return {'success': True}, 200, {'ContentType': 'application/json'}
def store_card(email, token): customer_id = get_customer_id(email) card = mp.post("/v1/customers/" + customer_id + "/cards", {"token": token}) if card['status'] != 200: return ResourceConflict('Esta tarjeta ya existe') print("Estado creacion tarjeta:", card['status'], "id tarjeta:", card['response']['id']) return {'success': True}, 200, {'ContentType': 'application/json'}
def get_customer_id(email): filters = {"email": email} try: customer = mp.get("/v1/customers/search", filters) if customer['response']['paging']['total'] == 0: raise ResourceConflict( 'Este usuario no se encuentra resgistrado en la base de datos de Mercado Libre' ) except ConnectionError: print("Conexion rechazada") return customer["response"]["results"][0]["id"]
def process_order(user_id, parameters, payment_info, location): basket = get_basket(user_id, parameters, location) order = ProductOrder(user_id) order.add_item() order.total = 0 for basket_item in basket: stock_quantity = get_inventory_quantity_by_id( basket_item.stock_item_id) if stock_quantity - basket_item.standard_basket_quantity >= 0: order_item = ProductOrderItem( order.product_order_id, basket_item.stock_item_id, int(basket_item.standard_basket_quantity), basket_item.inventory.stock_price) order.total += (basket_item.standard_basket_quantity * basket_item.inventory.stock_price) s.add(order_item) else: order_item_error = ProductOrderItemError( order.product_order_id, basket_item.inventory.product_id, int(basket_item.standard_basket_quantity)) s.add(order_item_error) s.commit() if order.total <= 0: order.cancelled = True s.commit() raise ResourceConflict( 'Orden no procesada. El valor total debe ser mayor a 0') payment_info['transaction_amount'] = order.total transaccion = ProductTransaction(order.product_order_id, payment_info) transaccion.add_item() error, response = payment(payment_info) if error: transaccion.id_estado_transaccion = 3 s.commit() raise ResourceConflict('Pago rechazado') transaccion.referencia_pago = response transaccion.id_estado_transaccion = 2 s.commit() empty_basket(user_id) return {'success': True}, 200, {'ContentType': 'application/json'}
def check_warehouse_not_exists_by_name(name): if not name or '': raise InvalidArgument('El campo name no puede estar vacio') if s.query(Store).filter(Store.store_name == name).first(): raise ResourceConflict('Este nombre de tienda ya se encuentra en uso')
def check_manufacturer_exists_by_name(name): if s.query(Brand).filter(Brand.brand_name == name).first(): raise ResourceConflict('Este fabricante ya existe') return False