Exemple #1
0
def order_get(order_id):
    order = Order.get_or_none(Order.id == order_id)

    if order is None:
        raise ApiException(Errors.NOT_FOUND, "order")

    order_dict = model_to_dict(Order.get_by_id(order_id))
    order_dict["product"] = {
        "id": order.product.product.id,
        "quantity": order.product.quantity
    }

    return jsonify(order_dict)
Exemple #2
0
def order_put(order_id):
    order = Order.get_or_none(Order.id == order_id)

    if order is None:
        raise ApiException(Errors.NOT_FOUND, "order")

    req = request.json if 'order' not in request.json else request.json['order']

    req["product"] = None

    if 'shipping_information' in req and 'email' in req and 'credit_card' in req:
        raise ApiException(Errors.PAYMENT_REQUEST_NOT_UNIQUE, "order")

    is_client_info_missing_from_req = not field_exists(
        req, ["email"]) or not field_exists(
            req["shipping_information"],
            ["country", "address", "postal_code", "city", "province"])
    client_info_missing_from_model = order.email is None or order.shipping_information is None

    if client_info_missing_from_model and 'credit_card' in req:
        raise ApiException(
            Errors.MISSING_FIELD, 'order/credit_card',
            "Les informations du client sont nécessaire avant d'appliquer une carte de crédit"
        )

    if client_info_missing_from_model and is_client_info_missing_from_req:
        raise ApiException(Errors.MISSING_FIELD, "order")

    if "credit_card" in req and not field_exists(
            req['credit_card'],
        ['name', 'number', 'expiration_year', 'cvv', 'expiration_month']):
        raise ApiException(Errors.MISSING_FIELD, "order/credit_card")

    if 'shipping_information' in req:
        OrderService.add_client_info_from_req(req, order)
        return order_get(order_id)

    if order.paid:
        raise ApiException(Errors.ALREADY_PAID, 'order/credit_card')

    payment_api_response = PaymentService.request_payment_to_api(
        req['credit_card'], order.shipping_price)

    if 'success' in payment_api_response and not payment_api_response[
            'success']:
        raise ApiException(Errors.CARD_DECLINED, 'order/payment')

    OrderService.add_payment_from_res(payment_api_response, order)

    return order_get(order_id)