def makeRefund(ref):

    rave = FlutterWave()

    response = rave.refund(ref)

    return response
def makeCardPayment(data, amount):

    error = validatePaymentDetails(data)

    if error['status'] == 'bad':
        return jsonify(error), 400

    cardno = data['cardno']
    cvv = data['cvv']
    pin = data['pin']
    amount = amount
    userId = data['userId']
    expiryyear = data['expiryyear']
    expirymonth = data['expirymonth']

    try:
        user = Users.objects(pk=userId).first()
        rave = FlutterWave(cardno, cvv, expirymonth, expiryyear, pin, amount,
                           user)

        res = rave.pay_via_card()

        return res
    except Exception as e:
        print(e)
        return {
            "status":
            "failed",
            "msg":
            "something happened server cannot  \
            handle this request at the moment try again later"
        }
def handleVerification():

    amount = None
    ref = None

    if request.method == 'POST':
        amount = request.json.get('amount')
        ref = request.json.get('ref')

    print(request.get_json())

    if ref == None or amount == None:
        return jsonify({
            "status":
            "failed",
            "msg":
            "Bad request some fields are missing from request body"
        }), 400

    rave = FlutterWave()

    verify = rave.verifyPayment(ref, amount)

    if verify == True:
        return jsonify({
            'msg': "amount charged match provided amount",
            'status': 'success'
        }), 200
    else:
        return jsonify({
            'msg': "amount charged  does not match provided amount",
            'status': 'success'
        }), 200
def handleOTP():
    
    otp = None
    ref = None
    amount = None
    userId = None
    wallet = None

    if request.method == 'POST':
        otp = request.json.get('otp')
        ref = request.json.get('ref')
        userId = request.json.get('userId')
        amount = request.json.get('amount')
    print(request.get_json())
    if otp == None or ref == None or userId == None or amount == None:
        return jsonify({"status": "failed", "msg": "Bad request some fields are missing from request body"}), 400

    rave = FlutterWave()

    res = rave.validatePayment(ref,otp)

    if 'data' in res['data'] and res['status'] == "success" and res['data']['data']['responsecode'] == '00':
        verify = rave.verifyPayment(res['data']['tx']['txRef'],amount)

        if verify:
            try:
                wallet = Wallets.objects(user=userId).first()

                if wallet:
                    wallet.update(
                        inc__amount=amount,
                        set__paymentRef=res['data']['data']['transactionreference']
                    )
                    wallet.save()
                    wallet.reload()
                else:
                    wallet = Wallets(user=userId, paymentRef=res['data']['data']['transactionreference'], amount=amount)
                    wallet.save()
                    wallet.reload()

                transaction = Transactions(
                    user=wallet.user,
                    wallet=wallet.pk,
                    amount=amount,
                    reference=wallet.paymentRef,
                    operation="deposit"
                )
                transaction.save()

                return jsonify({"status": "ok", "wallet": wallet, 'msg': 'Wallet funded successfully'}), 201
            except Exception as e:
                print(e)
                res = rave.refund(ref)
                return jsonify({"status": "failed", "wallet": wallet, 'msg': 'something happened cannot fund wallet'}), 500
        else:
            return jsonify({"status": "failed", "wallet": wallet, 'msg': 'Charge amount does not match enter amount'}), 402

    else:
        return jsonify({"status": "failed", "wallet": wallet, 'msg': res['data']['data']['responsemessage']}), 402
def makePayment():
    data = None
    if request.method == 'POST':
        data = request.get_json()

    print(request.get_json())

    error = validatePaymentDetails(data)

    print(error)

    if error['status'] == 'bad':
        return jsonify(error), 400

    cardno = data['cardno']
    cvv = data['cvv']
    pin = data['pin']
    amount = data['amount']
    userId = ObjectId(data['userId'])
    expiryyear = data['expiryyear']
    expirymonth = data['expirymonth']

    try:
        user = Users.objects(pk=userId).first()
        rave = FlutterWave(cardno, cvv, expirymonth, expiryyear, pin, amount,
                           user)
        wallet = None

        res = rave.pay_via_card()

        if res['status'] == 'success':
            if res['data']["chargeResponseCode"] == "02":
                return jsonify(res), 200
        else:
            return jsonify({
                "status":
                "failed",
                'msg':
                "Something happened cannot\
                            initiate payment please try again later",
            }), 400

    except Exception as e:
        print(e)
        return jsonify({
            "status":
            "failed",
            "msg":
            "something happened server cannot  \
            handle this request at the moment try again later"
        }), 500
def handleOTP():

    otp = None
    ref = None

    if request.method == 'POST':
        otp = request.json.get('otp')
        ref = request.json.get('ref')

    print(request.get_json())

    if otp == None or ref == None:
        return jsonify({
            "status":
            "failed",
            "msg":
            "Bad request some fields are missing from request body"
        }), 400

    rave = FlutterWave()

    res = rave.validatePayment(ref, otp)

    return jsonify(res), 200