Exemplo n.º 1
0
def checkout():
    current_id = User.get_by_id(get_jwt_identity())
    not_enough_items = Cart.select().join(Item).where(
        Cart.user == current_id, Cart.payment_status == False,
        Cart.amount > Item.stock)

    if len(not_enough_items) > 0:
        return jsonify("Message: There are not enough of these items in stock",
                       [{
                           "item": {
                               "id": item.item.id,
                               "stock": item.item.stock,
                               "color": item.item.color,
                               "name": item.item.name,
                               "product_type": item.item.product_type,
                               "image": item.item.image_url,
                               "price": item.item.price,
                               "size": item.item.size
                           }
                       } for item in not_enough_items], "Please reduce amount",
                       {"Status": "Failed"}), 400

    else:
        print(request.form.get('paymentMethodNonce'))
        data = request.get_json()
        amount_input = data['amount']
        pmNonce_input = data['paymentMethod']

        result = gateway.transaction.sale({
            "amount": amount_input,
            "payment_method_nonce": pmNonce_input,
            "options": {
                "submit_for_settlement": True
            }
        })

        Payment(user=current_id,
                Braintree_Transaction_id=result.transaction.id,
                Total_amount=result.transaction.amount).save()
        cart_amount = Cart.select(Cart.amount).where(Cart.item_id == Item.id)

        item = (Item.update(stock=Item.stock - cart_amount).where(
            Item.id.in_(
                Cart.select(
                    Cart.item_id).where((Cart.user == current_id)
                                        & (Cart.payment_status == False)))))
        item.execute()

        Cart.update(payment_status=True).where(
            Cart.user == current_id, Cart.payment_status == False).execute()

    print(result.transaction)
    print(result.transaction.id)
    print(result.transaction.amount)
    send_after_payment(current_id.email)

    return jsonify({'message': 'Success', 'status': 'success'}), 200
Exemplo n.º 2
0
def delete_by_name():
    data = request.get_json()
    item_name = data['name']
    name = Item.select().where(Item.name == item_name)
    cart_unpaid = Cart.delete().where((Cart.item.in_(name))
                                      & (Cart.payment_status == False))
    cart_paid = Cart.update(item_id=None).where((Cart.item.in_(name))
                                                & (Cart.payment_status == True))
    cart_paid.execute()
    cart_unpaid.execute()
    item = Item.get_or_none(Item.name == item_name)
    item.delete().where(Item.name == item_name).execute()
    return jsonify({"name": item.name, "message": ["item is deleted"]})