Flutterwave / Python-v2

Python library for Flutterwave for Business (F4B) v2 APIs
https://developer.flutterwave.com/
MIT License
20 stars 13 forks source link

RWMobile issues, not handling response correctly #75

Closed holayeah closed 4 years ago

holayeah commented 4 years ago

Checks for non existing key 'flwRef' in response

Describe the Bug

Traceback res = rave.RWMobile.charge(payload) wo8nb File "/usr/local/lib/python3.6/site-packages/rave_python/rave_rwmobile.py", line 31, in charge wo8nb return super(RWMobile, self).charge(accountDetails, requiredParameters, endpoint) wo8nb File "/usr/local/lib/python3.6/site-packages/rave_python/rave_payment.py", line 209, in charge wo8nb return self._handleChargeResponse(response, paymentDetails["txRef"]) wo8nb File "/usr/local/lib/python3.6/site-packages/rave_python/rave_payment.py", line 72, in _handleChargeResponse wo8nb flwRef = responseJson["data"]["flwRef"] wo8nb KeyError: 'flwRef'

Content of the response on rwmobile object.

{'_content': b'{"status":"success","message":"Momo initiated","data":{"code":"02","status":"pending","ts":1591965xxxx40,"link":"https://checkout.flutterwave.com/captcha/verify/57799:0dxxxxxxxxxxxxxxxxx"}}',

Contains a link to captcha verification.

Kyeza commented 4 years ago

Hello, I am facing this same issue with UGMobile, I have a backend that accept MTN Mobile money and it had been working but recently when a client initiate a payment they receive an OTP with this message inclusive "MOMO Verification Auth", yet the expected behaviour is initiate a pin input box where the user enters the Mobile money pin to initiate payment, @MaestroJolly, could you offer any help please

below is my implementation

from rave_python import Rave, RaveExceptions

rave = Rave(settings.RAVE_PUBLIC_KEY, settings.RAVE_SECRET_KEY, production=True)

payload = {
  "txRef": "67865",
  "amount": "34000",
  "email": "rave@rave.com",
  "user_phonenumber": "256784660058",
  "phonenumber": "256784660058",
  "firstname": "Jeffrey",
  "lastname": "Jeffrey",
  "redirect_url": "",
  "device_id": "sadsdadweqweqe",
  "IP": ""
}

try:
    try:
        response = rave.UGMobile.charge(payload)
    except RaveExceptions.TransactionChargeError as e:
        print(e.err)
        print(e.err["flwRef"])
        return Response(e.err, status=status.HTTP_400_BAD_REQUEST)
    except RaveExceptions.TransactionVerificationError as e:
        print(e.err["errMsg"])
        print(e.err["txRef"])
        return Response(e.err, status=status.HTTP_400_BAD_REQUEST)
except KeyError as e:
    print(f'missing key: {e.args}')

I keep get this as my error: "Traceback (most recent call last): File "", line 1, in File "/Users/kyeza/PycharmProjects/JeffreyApi/venv/lib/python3.8/site-packages/rave_python/rave_ugmobile.py", line 31, in charge return super(UGMobile, self).charge(accountDetails, requiredParameters, endpoint) File "/Users/kyeza/PycharmProjects/JeffreyApi/venv/lib/python3.8/site-packages/rave_python/rave_payment.py", line 209, in charge return self._handleChargeResponse(response, paymentDetails["txRef"]) File "/Users/kyeza/PycharmProjects/JeffreyApi/venv/lib/python3.8/site-packages/rave_python/rave_payment.py", line 72, in _handleChargeResponse flwRef = responseJson["data"]["flwRef"]"

holayeah commented 4 years ago

Theri implementation of the api now includes captcha verification.

I have ended up overwritting this method on RWMobile

class RWMobileCustom(RWMobile):
    def _handleChargeResponse(self, response, txRef, request=None, isMpesa=False):
        """ This handles transaction charge responses """

         # If we cannot parse the json, it means there is a server error 
        res = self._preliminaryResponseChecks(response, TransactionChargeError, txRef=txRef)

        responseJson = res["json"]

        return responseJson

And using it as

from .rave import RWMobileCustom as Rave
rave = Rave(os.environ['API_FLUTTERWAVE_PUBLIC_KEY'],
            os.environ['API_FLUTTERWAVE_SECRET_KEY'], production=True, usingEnv=False)

payload = {
        "amount": 100,
        "email": "test@test.testr",
        "phonenumber": 250788000xxx,
        "txRef": uuid.uuid4(),
        "redirect_url": "http://web.com",
        "IP": ""
    }

        res = rave.charge(payload)
        print(res) # includes the link to cptcha verification
Kyeza commented 4 years ago

Thank you, though this is a breaking change for an Official library that should have been communicated

MaestroJolly commented 4 years ago

Hi @upatricck @Kyeza, We sincerely apologize for this. @Corvus97 would help update this accordingly.

Kyeza commented 4 years ago

@MaestroJolly Thank you very much, and a little documentation would really be helpful on this specific issue. Again thanks for the response.