def post(cls): """ Expect a token and a list of item ids from the request body. Construct an order and talk to the Strip API to make a charge. """ data = request.get_json( ) # token + list of item ids [1, 2, 3, 5, 5, 5] items = [] item_id_quantities = Counter(data["item_ids"]) # [(5, 3), (3, 1), (2, 1)] for _id, count in item_id_quantities.most_common( ): #most common give u how many items in the order they happen item = ItemModel.find_by_id(_id) if not item: return { "message": gettext("order_item_by_id_not_found").format(_id) }, 404 # items.append(item) items.append(ItemsInOrder(item_id=_id, quantity=count)) order = OrderModel(items=items, status="pending") order.save_to_db() #THIS DOES NOT SUBMIT TO STRIPE order.set_status( "failed") # assume the order would fail until it's completed order.charge_with_stripe(data["token"]) order.set_status("complete") # charge succeeded return order_schema.dump(order), 200
def post(cls): """ Expect a token and a list of item ids from the request body. Construct an order and talk to the Strip API to make a charge. """ data = request.get_json( ) # token + list of item ids [1, 2, 4, 4, 5, 2, 1, 1, 4] items = [] item_id_quantities = Counter(data["item_ids"]) # Iterate over items and retrieve them from the database for _id, count in item_id_quantities.most_common( ): # [(1,3),(2,2),(4,3),(5,1)] item = ItemModel.find_by_id(_id) if not item: return {"message": "Item {} Not Found.".format(_id)}, 404 items.append(ItemsInOrder(item_id=_id, quantity=count)) order = OrderModel(items=items, status="pending") order.save_database() # this does not submit to Stripe try: order.set_status( "failed") # assume the order would fail until it's completed order.charge_stripe(data["token"]) order.set_status("complete") # charge succeeded except stripe.error.CardError as e: # Since it's a decline, stripe.error.CardError will be caught print('Status is: %s' % e.http_status) print('Type is: %s' % e.error.type) print('Code is: %s' % e.error.code) # param is '' in this case print('Param is: %s' % e.error.param) print('Message is: %s' % e.error.message) return {"message": e.error.message}, e.http_status except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly return {"message": e.error.message}, e.http_status except stripe.error.InvalidRequestError as e: # Invalid parameters were supplied to Stripe's API return {"message": e.error.message}, e.http_status except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) return {"message": e.error.message}, e.http_status except stripe.error.APIConnectionError as e: # Network communication with Stripe failed return {"message": e.error.message}, e.http_status except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email return {"message": e.error.message}, e.http_status except Exception as e: # Something else happened, completely unrelated to Stripe return {"message": e.error.message}, e.http_status return order_schema.dump(order), 200
def post(cls): data = request.get_json() items = [] item_id_quantities = Counter(data["item_ids"]) customer_id = get_jwt_identity() if OrderModel.find_customer_pending_order(customer_id): return {"message": HAS_ONE_ORDER_ALREADY}, 400 for _id, count in item_id_quantities.most_common(): item = ItemModel.find_by_id(_id) if not item: return {"message": ITEM_ID_NOT_FOUND.format(_id)}, 404 items.append(ItemsInOrder(item_id=_id, quantity=count)) message = data['message'] order = OrderModel(items=items, status="pending", customer_id=customer_id, message=message) order.charge_with_stripe(data["token"]) order.save_to_db() return order_schema.dump(order)
def post(cls): """Expect a token and a list of item ids from the request body. Construct an order and talk to the Strip API to make a charge.""" data = request.get_json() # token ,item_ids [1, 3, 3, 5, 5, 5] items = [] item_id_quantities = Counter(data["item_ids"]) for _id, count in item_id_quantities.most_common( ): # [(5,3),(3,2),(1,1)] item = ItemModel.find_by_id(_id) if not item: return { "message": gettext("order_item_by_id_not_found").format(_id) }, 404 """ItemsInOrder get item_id and quantity, however order_id will be set later on, when items is passed into OrderModel, because back_populates="order" it goes over to order column of ItemsInOrder table, and set order_id for each of those item in OrderModel to be the order to which you have added those items""" items.append(ItemsInOrder(item_id=_id, quantity=count)) # items is a list of ItemsInOrder obj order = OrderModel(items=items, status="pending") # pending until send to Stripe order.save_to_db() # this does not submit to Stripe try: order.set_status( "failed") # assume the order would fail until it's completed order.charge_with_stripe(data["token"]) order.set_status("complete") # charge succeeded return order_schema.dump(order), 200 # the following error handling is advised by Stripe, although the handling implementations are identical, # we choose to specify them separately just to give the students a better idea what we can expect except error.CardError as e: # Since it's a decline, stripe.error.CardError will be caught return e.json_body, e.http_status except error.RateLimitError as e: # Too many requests made to the API too quickly return e.json_body, e.http_status except error.InvalidRequestError as e: # Invalid parameters were supplied to Stripe's API return e.json_body, e.http_status except error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) return e.json_body, e.http_status except error.APIConnectionError as e: # Network communication with Stripe failed return e.json_body, e.http_status except error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email return e.json_body, e.http_status except Exception as e: # Something else happened, completely unrelated to Stripe print(e) return {"message": gettext("order_error")}, 500
def post(cls): """ Expect a token and a list of item ids from the request body Construct an order and talk to the Stripe API to make a charge """ data = request.get_json() # token + list of item ids items = [] item_id_quantities = Counter(data["item_idss"]) for _id, count in item_id_quantities.most_common(): item = ItemModel.find_by_id(_id) if not item: return { "message": gettext("order_item_by_id_not_found").format(_id) }, 404 items.append(ItemsInOrder(item_id=_id, quantity=count)) order = OrderModel(items=items, status="pending") order.save_to_db() order.set_status("failed") order.charge_with_stripe(data["token"]) order.set_status("complete") return order_schema.dump(order)
def post(cls): """ Expect a token and a list of item ids from the request body. Construct an order and talk to the Strip API to make a charge. """ data = request.get_json( ) # token + list of item ids [1, 2, 3, 5, 5, 5] items = [] item_id_quantities = Counter(data["item_ids"]) # Iterate over items and retrieve them from the database for _id, count in item_id_quantities.most_common( ): # -> [(5,3), (3,1), (2,1), (1,1)] item = ItemModel.find_by_id(_id) if not item: return { "message": gettext("order_item_by_id_not_found").format(_id) }, 404 items.append(ItemsInOrder(item_id=_id, quantity=count)) order = OrderModel(items=items, status="pending") order.save_to_db() # this does not submit to Stripe order.set_status("failed") order.charge_with_stripe(data['token']) order.set_status("completed") return order_schema.dump(order)
def post(cls): """ Expect a token and a list of item ids from the request body. Construct an order and talk to the Strip API to make a charge. :return: """ data = request.get_json() # token + list of item ids items = [] item_id_quantities = Counter(data["item_ids"]) #iterate over items and retrieve them from the database for _id, count in item_id_quantities.most_common(): item = ItemModel.find_by_id(_id) if not item: return {"message": gettext("order_item_by_id_not_found").format(_id)}, 404 items.append(ItemsInOrder(item_id=_id, quantity=count)) order = OrderModel(items=items, status="pending") order.save_to_db() # this does not submit to Stripe try: order.set_status("failed") order.charge_with_stripe(data["token"]) order.set_status("complete") return order_schema.dump(order), 200 except(error.CardError, error.RateLimitError, error.InvalidRequestError, error.AuthenticationError, error.APIConnectionError, error.StripeError) as e: return e.json_body, e.http_status except Exception as e: print(e) return {"message": gettext("order_error")}, 500
def post(cls): """ Expect a token and a list of item ids from the request body. Construct an order an talk to the Stripe API to make a charge. """ data = request.get_json() # token + list of item ids items = [] item_id_quantities = Counter( data['item_ids'] ) # you pass a list to the Counter and it will count for you # Iterate over items and retrieve them from the db for _id, count in item_id_quantities.most_common(): item = ItemModel.find_by_id(_id) if not item: return { 'message': gettext('order_item_by_id_not_found').format(_id) }, 404 items.append(ItemsInOrder(item_id=_id, quantity=count)) order = OrderModel(items=items, status='pending') order.save_to_db() # submit to Stripe try: order.set_status( "failed") # assume the order would fail until it's completed order.charge_with_stripe(data["token"]) order.set_status("complete") # charge succeeded return order_schema.dump(order), 200 # the following error handling is advised by Stripe, although the handling implementations are identical, # we choose to specify them separately just to give the students a better idea what we can expect except error.CardError as e: # Since it's a decline, stripe.error.CardError will be caught return e.json_body, e.http_status except error.RateLimitError as e: # Too many requests made to the API too quickly return e.json_body, e.http_status except error.InvalidRequestError as e: # Invalid parameters were supplied to Stripe's API return e.json_body, e.http_status except error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) return e.json_body, e.http_status except error.APIConnectionError as e: # Network communication with Stripe failed return e.json_body, e.http_status except error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email return e.json_body, e.http_status except Exception as e: # Something else happened, completely unrelated to Stripe print(e) return {"message": gettext("order_error")}, 500
def post(cls): """ Expects a token and a list of item ids from the request body. Constructs an order and talks to the Stripe API to make a charge. """ data = request.get_json() items = [] item_id_quantities = Counter(data["items_id"]) for _id, count in item_id_quantities.most_common(): item = ItemModel.find_by_id(_id) if not item: return {"message": f"Item {_id} Not Found."}, 404 items.append(ItemsInOrder(item_id=_id, quantity=count)) order = OrderModel(items=items, status="pending") order.save_to_db() try: order.set_status("payment failed") order.charge_with_stripe(data["token"]) order.set_status("payment completed") return orderSchema.dump(order) # the following error handling is advised by Stripe, although the handling implementations are identical, # we choose to specify them separately just to give the students a better idea what we can expect except error.CardError as e: # Since it's a decline, stripe.error.CardError will be caught return e.json_body, e.http_status except error.RateLimitError as e: # Too many requests made to the API too quickly return e.json_body, e.http_status except error.InvalidRequestError as e: # Invalid parameters were supplied to Stripe's API return e.json_body, e.http_status except error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) return e.json_body, e.http_status except error.APIConnectionError as e: # Network communication with Stripe failed return e.json_body, e.http_status except error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email return e.json_body, e.http_status except Exception as e: # Something else happened, completely unrelated to Stripe print(e) return {"message": "Order Error"}, 500
def post(cls): """ - Expect a token - list of item IDs from the request body #eg: {'token':'zxc', 'item_ids': [1, 2, 3, 3, 5, 5]} - Construct an order, connect to Stripe, make a charge """ data = request.get_json() items = [] item_id_quantities = Counter(data['item_ids']) # item_id_quantities.most_common(), returns list items_ids with no of occurences # = [(1,1), (2, 1), (3, 2), (5,2)] for _id, count in item_id_quantities.most_common(): item = ItemModel.find_by_id(_id) if not item: return {'message': ITEM_NOT_FOUND.format(_id)}, 404 items.append(ItemsInOrder(item_id=_id, quantity=count)) order = OrderModel(items=items, status='pending') order.save_to_db() # stripe try: order.set_status('failed') order.charge_with_stripe(data['token']) order.set_status('completed') # charge succeeded return order_schema.dump(order), 200 except error.CardError as e: return e.json_body, e.http_status except error.RateLimitError as e: return e.json_body, e.http_status except error.InvalidRequestError as e: return e.json_body, e.http_status except error.AuthenticationError as e: return e.json_body, e.http_status except error.APIConnectionError as e: return e.json_body, e.http_status except error.StripeError as e: return e.json_body, e.http_status except Exception as e: return {"message": ORDER_ERROR}, 500
def post(cls): """ Expect a token and a list of item ids from the request body. Construct a order to talk with Strip API to make a charge. """ data = request.get_json() # token + list of item ids items = [] item_id_quantities = Counter(data["item_ids"]) # iterate from list of items and retrieve from the database for _id, count in item_id_quantities.most_common(): item = ItemModel.find_by_id(_id) if not item: return { "message": gettext("order_by_id+not_found").format(_id) }, 404 items.append(ItemsInOrder(item_id=_id, quantity=count)) order = OrderModel(items=items, status="pending") order.save_to_db() # this doesn't submit to Stripe try: order.set_status("failed") order.charge_with_stripe(data["token"]) order.set_status("complete") return order_schema.dump(order) except error.CardError as e: # Since it is a decline, stripe.error.CardError will be caught return e.json_body, e.http_status # vezi in document restul de errori except error.StripeError as e: # Display a very generic error to the user, and maybe send yourself # an email return e.json_body, e.http_status except Exception as e: print(e) return {"message": gettext("order_error")}, 500
def post(cls): """ Expect a token and a list of items from the request body Construct an order and talk to the Stripe API to make charge """ data = request.get_json() order = OrderModel.find_pending_order_by_user_id(data["user_id"]) if order: items = [] item_id_quantities = Counter(data["item_ids"]) # [3,3,4,1,6,6,6] for _id, count in item_id_quantities.most_common(): # [6,3],[1,1],[4,1],3,2] most common item = ItemModel.find_by_id(_id) if not item: return {"message": "Order Item by id not found"}, 404 itemsInOrder = ItemsInOrder.is_item_available(_id, order.id) if itemsInOrder: itemsInOrder.set_quantity(count) else: orderItems = ItemsInOrder(item_id=_id, quantity=count, order_id=order.id) orderItems.save_to_db() return {"message": "Order saved to Items in Order"}, 200 items = [] item_id_quantities = Counter(data["item_ids"]) # [3,3,4,1,6,6,6] for _id, count in item_id_quantities.most_common(): # [6,3],[1,1],[4,1],3,2] most common item = ItemModel.find_by_id(_id) if not item: return {"message": "Order Item by id not found"}, 404 items.append(ItemsInOrder(item_id=_id, quantity=count)) order = OrderModel(items=items, status="pending", user_id=data["user_id"]) order.save_to_db() return order_schema.dump(order), 200
def get(cls, order_id: int): return {"items_in_order":items_in_order_schema.dump(ItemsInOrder.get_items_for_order(order_id), many=True)}, 200