Exemplo n.º 1
0
def create_checkout():
    global parsedItems
    result = transact({
        'amount': request.form['amount'],
        'payment_method_nonce': request.form['payment_method_nonce'],
        'options': {
            "submit_for_settlement": True
        }
    })
    for pair in parsedItems:
        query = mongo.db.paypay.find_one({"name" : pair[0].strip()})
        qty = query["quantity"]
        if int(qty) < pair[1]:
            return pair[0] + " is out of stock."
    if result.is_success or result.transaction:
        jsonItems = request.form['items']
        parsedItems = json.loads(jsonItems) #[[item1, qty], [item2, qty], [item3, qty]]
        #db operations: decrement stock, add new transaction entry
        for pair in parsedItems:
            query = mongo.db.paypay.find_one({"name" : pair[0].strip()})
            qty = query["quantity"]
            newqty = int(qty) - pair[1]
            newvalues = { "$set": {"quantity" : str(newqty)}}
            mongo.db.paypay.update_one(query, newvalues)
        page = "http://0.0.0.0:4567/checkouts/" + str(result.transaction.id)
        url = pyqrcode.create(page)
        url.png('tr.png', scale=8)

        pay123.main()
        return redirect(url_for('show_checkout',transaction_id=result.transaction.id))
    else:
        for x in result.errors.deep_errors: flash('Error: %s: %s' % (x.code, x.message))
        return redirect(url_for('new_checkout'))
Exemplo n.º 2
0
def create_checkout():
    nonce = request.json.get('payment_method_nonce')
    amount = request.json.get('amount')
    result = transact({
        'amount': amount,
        'payment_method_nonce': nonce,
        'options': {
            "submit_for_settlement": True
        }
    })
    # amount = int(request.form.get('amount'))
    # current_id= current_user.id
    # current_email = str(current_user.email)

    if result.is_success or result.transaction:
        # print(result.transaction.id[0])
        transaction = find_transaction(result.transaction.id)
        transac = {}
        transac['id'] = transaction.id
        transac['type'] = transaction.type
        transac['amount'] = str(transaction.amount)
        transac['status'] = transaction.status
        result = {}
        if transaction.status in TRANSACTION_SUCCESS_STATUSES:
            result = {
                'header':
                'Sweet Success!',
                'icon':
                'success',
                'message':
                'Your test transaction has been successfully processed. See the Braintree API response and try again.'
            }
        else:
            result = {
                'header':
                'Transaction Failed',
                'icon':
                'fail',
                'message':
                'Your test transaction has a status of ' + transaction.status +
                '. See the Braintree API response and try again.'
            }

        return jsonify(message=result, transac=transac)
    else:

        # for x in result.errors.deep_errors: flash('Error: %s: %s' % (x.code, x.message))
        # errormessage={}
        # errormessage['errormessage'] = result.errors.deep_errors
        return jsonify(error="error")
Exemplo n.º 3
0
def create_checkout():
    result = transact({
        'amount': request.form['amount'],
        'payment_method_nonce': request.form['payment_method_nonce'],
        'options': {
            "submit_for_settlement": True
        }
    })

    if result.is_success or result.transaction:
        return redirect(url_for('show_checkout',transaction_id=result.transaction.id))
    else:
        for x in result.errors.deep_errors: flash('Error: %s: %s' % (x.code, x.message))
        return redirect(url_for('new_checkout'))
Exemplo n.º 4
0
def create(transaction_request):
    '''
    Input: transaction_request-(request.form)
    '''


    fast_customer_id = transaction_request.form["customer_id"]

    # open database connection
    with MongoDB() as mongo_client:
        fast_customer_object = mongo_client.customers.find_by_id(fast_customer_id)

    # Use Fast customer id to get customer spending limit
    customer_object = find_customer(fast_customer_object['braintree']['customer_id'])
    customer_spending_limit = fast_customer_object['braintree']["customer_spending_limit"]

    # Check if request amount is within limits
    if float(transaction_request.form['amount']) > float(customer_spending_limit):
        error_dict = {
            "error_message": "Transaction Amount: {} Exceeds Limit: {}".format(transaction_request.form['amount'], customer_spending_limit),
            "error_code": "415"
            }
    
    else: 
        # transact uses braintree gateway to process the transaction
        data = transact({
            'amount': transaction_request.form['amount'],
            'payment_method_nonce': transaction_request.form['payment_method_nonce'],
            'options': {
                "submit_for_settlement": True
            }
        })

        if not data.is_success:
            errors_list = [[x.code, x.message] for x in data.errors.deep_errors]
            error_dict = {
                "error_message": errors_list[0][1], 
                "error_code": errors_list[0][0]
                }

        else:
            error_dict = {}
            braintree_transaction_id = data.transaction.id
            braintree_transaction_amount = float(data.transaction.amount)

            # open database connection
            with MongoDB() as mongo_client: 
                transaction_pair = {
                    "braintree":{
                        "braintree_transaction_id":braintree_transaction_id,
                        "braintree_transaction_amount":braintree_transaction_amount
                    }
                }
            #Store braintree's customer transaction to mongo
            transaction_object = mongo_client.transactions.insert_one(transaction_pair)
    

    transaction_response = {
        "fast_transaction_id": None if error_dict else str(transaction_object["_id"]),
        "braintree_transaction_id": {} if error_dict else data.transaction.id,
        "error": error_dict,
        "success": bool(not error_dict)
    }

    return transaction_response