Beispiel #1
0
def process_payment_request(provider, account, amount, billed_amount,
                            payer_ref, narration, payer_msisdn, voucher_code):
    module = "task.process_payment_request"
    response = {}
    valid_provider = False
    LOG_HANDLER.writelog(
        module, f"RECEIVED ::provider:{provider},account:{account},"
        f"amount:{amount},payer_ref:{payer_ref},"
        f"narration:{narration},payer_msisdn:{payer_msisdn}")

    # Get Provider

    # Load Providers END POINTS from JSON FILE

    # Post Payment Debit Request+Callback and wait for response on update page
    # when successful

    # Update Transaction table as required

    try:

        LOG_HANDLER.writelog(
            module, f"and SMS:{settings.SMS_SOURCE_ADDRESS} and "
            f"{settings.TRANSACTION_CALLBACK}")

        # pload = {
        #     "account_number": account,
        #     "amount": amount,
        #     "reference_number": payer_ref,
        #     "narration": narration,
        #     "callback": settings.TRANSACTION_CALLBACK
        # }

        # LOG_HANDLER.writelog(module, f"PAYLOAD SET TO {pload}")

        #provider_list = settings.PAYMENT_PROVIDER_ENDPOINT
        provider_list = [{
            "MTN":
            "https://10.85.85.65:24766/api/prymo-momo/debit-wallet/"
        }, {
            "VODAFONE":
            "https://10.85.85.65:24766/api/vodafonecash/debit-wallet/"
        }, {
            "AIRTELTIGO":
            "https://10.85.85.65:24766/api/prymo-momo/debit-wallet/"
        }]
        LOG_HANDLER.writelog(module,
                             f"PAMENT PROVIDERS ENDPOINT::: {provider_list}")
        sender_id = settings.SMS_SOURCE_ADDRESS

        endpoint = ""
        call_provider = ""
        status_sms = ""

        for providers in provider_list:
            for kv in providers:
                LOG_HANDLER.writelog(module, f"Provider {kv}")
                if kv == provider:
                    endpoint = providers[kv]
                    valid_provider = True
                    break

        if valid_provider and provider == "VODAFONE":
            pload = {
                "account_number": account,
                "amount": billed_amount,
                "reference_number": payer_ref,
                "narration": narration,
                "voucher_code": voucher_code,
                "callback": settings.TRANSACTION_CALLBACK
            }

            LOG_HANDLER.writelog(module, f"POSTING ::: {pload} to {endpoint}")
            call_provider = requests.post(endpoint,
                                          json=pload,
                                          verify=False,
                                          timeout=40)
            response = call_provider.json()
            provider_ref = response['provider_reference_number']
            LOG_HANDLER.writelog(
                module, f"PROVIDER REFERENCE NUMBER ::: {provider_ref}")

            time.sleep(5)
            record = main(provider_ref)
            status_sms = record[0]

            if status_sms == "PAID":
                status_sms = "SUCCESSFUL"

            elif status_sms == "UNPAID":
                status_sms = "FAILED"

        elif valid_provider and provider == "MTN":
            pload = {
                "account": account,
                "amount": billed_amount,
                "trxid": payer_ref,
            }
            LOG_HANDLER.writelog(module, f"POSTING ::: {pload} to {endpoint}")
            call_provider = requests.post(endpoint, params=pload, verify=False)

            LOG_HANDLER.writelog(
                module, f"CALL RESPONSE CODE ::: {call_provider.status_code}")
            LOG_HANDLER.writelog(module,
                                 f"CALL RESPONSE ::: {call_provider.text}")
            status_split = call_provider.text.split('|')
            status_sms = status_split[0].split(':')
            status_sms = status_sms[1]

            if status_sms == "COMPLETED":
                status_sms = "SUCCESSFUL"

        elif valid_provider and provider == "AIRTELTIGO":
            pload = {
                "account": account,
                "amount": billed_amount,
                "trxid": payer_ref,
            }
            newone = 'https://10.85.85.65:24766/api/prymo-momo/debit-wallet/'
            LOG_HANDLER.writelog(module, f"POSTING ::: {pload} to {newone}")
            call_provider = requests.post(newone, params=pload, verify=False)

            LOG_HANDLER.writelog(
                module, f"CALL RESPONSE CODE ::: {call_provider.status_code}")
            LOG_HANDLER.writelog(module,
                                 f"CALL RESPONSE ::: {call_provider.text}")
            status_split = call_provider.text.split('|')
            status_sms = status_split[0].split(':')
            status_sms = status_sms[1]

            if status_sms == "COMPLETED":
                status_sms = "SUCCESSFUL"

    #    elif valid_provider and provider == "AIRTELTIGO" or provider == "AirtelTigo":
    #       pload = {
    #          "msisdn": account,
    #         "amount": billed_amount,
#                "product_name": narration,
#               "misika247_transaction_id": payer_ref,
#              "narration": narration,
#         }
#        LOG_HANDLER.writelog(module, f"POSTING ::: {pload} to {endpoint}")
#       call_provider = requests.post(endpoint, json=pload, verify=False, timeout=40)
#      LOG_HANDLER.writelog(module, f"CALL RESPONSE ::: {call_provider.text}")

        else:
            response['status'] = 303
            response['reason'] = f"Provider {provider} UNKNOWN"

        text = f"""Dear customer, your payment of GHS.{amount} via {provider} to {sender_id} {status_sms} -Ref:{payer_ref}."""

        try:
            sm = ""
            #post_sms.delay(sender_id, text, payer_msisdn, payer_ref)
            # post_sms_via_mesika.delay(sender_id, text, payer_msisdn, payer_ref)
            sm = post_sms_via_mesika.delay(sender_id, text, payer_msisdn)
            LOG_HANDLER.writelog(module, f"DELIVERING ACCT MSG: {sm.id}")

        except:
            LOG_HANDLER.log_error_detailed(module, module)

        response = {
            "status": 200,
            "delivery_id": sm.id,
            "reason": "Processing Completed"
        }

        try:
            trx_search = Transactions.objects.filter(
                payer_reference_number=payer_ref)
            LOG_HANDLER.writelog(module,
                                 f"TRANSACTION RESULT ||| {trx_search} |||")

            if not trx_search:
                trx_search.update(status='TRXN NOT FOUND')
                LOG_HANDLER.writelog(module, f"TRANSACTION NOT FOUND")

            else:
                trx_search.update(status=status_sms)
                LOG_HANDLER.writelog(module,
                                     f"TRANSACTION UPDATED TO {status_sms}")

        except:
            ler = LOG_HANDLER.log_error_detailed(module, module)

            LOG_HANDLER.writelog(module, f"Exception due to {ler}")

    except:
        ler = LOG_HANDLER.log_error_detailed(module, module)
        response['status'] = 502
        response[
            'reason'] = f"Exception Processing Payment Request of GHS.{amount} {ler}"

    LOG_HANDLER.writelog(module, f"RESPONSE ::: {response}")

    return response
Beispiel #2
0
def process_payment_request(provider, account, amount, billed_amount,
                            payer_ref, narration, payer_msisdn, voucher_code):
    module = "task.process_payment_request"
    response = {}
    valid_provider = False
    LOG_HANDLER.writelog(
        module, f"RECEIVED ::provider:{provider},account:{account},"
        f"amount:{amount},payer_ref:{payer_ref},"
        f"narration:{narration},payer_msisdn:{payer_msisdn}")

    # Get Provider

    # Load Providers END POINTS from JSON FILE

    # Post Payment Debit Request+Callback and wait for response on update page
    # when successful

    # Update Transaction table as required

    try:

        LOG_HANDLER.writelog(
            module,
            f"PROCESSING WITH PROVIDERS:{settings.PAYMENT_PROVIDER_ENDPOINT} "
            f"and SMS:{settings.SMS_SOURCE_ADDRESS} and "
            f"{settings.TRANSACTION_CALLBACK}")

        # pload = {
        #     "account_number": account,
        #     "amount": amount,
        #     "reference_number": payer_ref,
        #     "narration": narration,
        #     "callback": settings.TRANSACTION_CALLBACK
        # }

        # LOG_HANDLER.writelog(module, f"PAYLOAD SET TO {pload}")

        provider_list = settings.PAYMENT_PROVIDER_ENDPOINT
        sender_id = settings.SMS_SOURCE_ADDRESS

        endpoint = ""
        call_provider = ""

        for providers in provider_list:
            for kv in providers:
                LOG_HANDLER.writelog(module, f"{kv}")
                if kv == provider:
                    endpoint = providers[kv]
                    valid_provider = True
                    break

        if valid_provider and provider == "VODAFONE":
            pload = {
                "account_number": account,
                "amount": billed_amount,
                "reference_number": payer_ref,
                "narration": narration,
                "voucher_code": voucher_code,
                "callback": settings.TRANSACTION_CALLBACK
            }

            LOG_HANDLER.writelog(module, f"POSTING ::: {pload} to {endpoint}")
            call_provider = requests.post(endpoint,
                                          json=pload,
                                          verify=False,
                                          timeout=40)

        elif valid_provider and provider == "MTN":
            pload = {
                "account": account,
                "amount": billed_amount,
                "trxid": payer_ref,
            }
            LOG_HANDLER.writelog(module, f"POSTING ::: {pload} to {endpoint}")
            call_provider = requests.post(endpoint, params=pload, verify=False)

        elif valid_provider and provider == "AIRTELTIGO":
            pload = {
                "account_number": account,
                "amount": billed_amount,
                "reference_number": payer_ref,
                "narration": narration,
                "callback": settings.TRANSACTION_CALLBACK
            }
            LOG_HANDLER.writelog(module, f"POSTING ::: {pload} to {endpoint}")
            call_provider = requests.post(endpoint,
                                          json=pload,
                                          verify=False,
                                          timeout=40)

        else:
            response['status'] = 303
            response['reason'] = f"Provider {provider} UNKNOWN"

        LOG_HANDLER.writelog(
            module, f"CALL RESPONSE CODE ::: {call_provider.status_code}")
        LOG_HANDLER.writelog(module, f"CALL RESPONSE ::: {call_provider.text}")
        status_split = call_provider.text.split('|')
        status_sms = status_split[0].split(':')
        statussms = status_sms[1]

        text = f"""Dear customer, your payment of GHS.{amount} via {provider} to {sender_id} {statussms} -Ref:{payer_ref}."""

        # if provider == "MTN":
        #     text = f"{text}dial *170# to approve."

        try:
            sm = ""
            #post_sms.delay(sender_id, text, payer_msisdn, payer_ref)
            # post_sms_via_mesika.delay(sender_id, text, payer_msisdn, payer_ref)
            sm = post_sms_via_mesika.delay(sender_id, text, payer_msisdn)
            LOG_HANDLER.writelog(module, f"DELIVERING ACCT MSG: {sm.id}")

        except:
            LOG_HANDLER.log_error_detailed(module, module)

        response = {
            "status": 200,
            "delivery_id": sm.id,
            "reason": "Processing Completed"
        }

    except:
        ler = LOG_HANDLER.log_error_detailed(module, module)
        response['status'] = 502
        response[
            'reason'] = f"Exception Processing Payment Request of GHS.{amount} {ler}"

    LOG_HANDLER.writelog(module, f"RESPONSE ::: {response}")

    return response
Beispiel #3
0
def callback_receiver(request):

    module = "callback"
    LOG_HANDLER.writelog(module, f"INBOUND PAYLOAD >>> {request.body}")

    pl = json.loads(request.body)

    LOG_HANDLER.writelog(module, f"INBOUND DICT >>> {pl}")

    # trx_status = (pl['status']).upper()
    our_reference_number = ""
    response = {}

    try:

        our_reference_number = pl['payer_reference_number']
        transaction_status = pl["transaction_status"]
        status_description = pl["status_description"]
        provider_reference = pl["provider_reference_number"]

        find_transaction = Transactions.objects.using(
            'payment_collections_client').filter(
                payer_reference_number=our_reference_number)

        if not find_transaction:
            response['status'] = 404
            response[
                'reason'] = f"Transaction with Payer Reference {our_reference_number} Not Found"
        else:
            if transaction_status == "PAID":
                trx_update = find_transaction.update(
                    status=transaction_status,
                    status_description=status_description,
                    provider_reference_number=provider_reference)

                LOG_HANDLER.writelog(module,
                                     f"TRANSACTION UPDATE DB:{trx_update}")
            else:
                trx_update = find_transaction.update(
                    status=transaction_status,
                    status_description=status_description)
                LOG_HANDLER.writelog(module,
                                     f"TRANSACTION UPDATE DB:{trx_update}")

            try:
                LOG_HANDLER.writelog(
                    module, f"ATTEMPTING SMS DELIVERY AFTER CALLBACK")
                get_for_sms = find_transaction.values()[0]
                payer_msisdn = get_for_sms['payer_msisdn']
                pay_amount = get_for_sms['amount']

                if transaction_status == "PAID":

                    sms_message = f"Dear customer, your payment of GHS.{pay_amount} was successful"
                else:
                    sms_message = f"Dear customer, your payment of GHS.{pay_amount} was not successful.Your payment provider did not approve your payment."

                sender_id = settings.SMS_SOURCE_ADDRESS

                payer_ref = generate_sso_token()

                #post_sms.delay(sender_id, sms_message, payer_msisdn, payer_ref)
                sm = post_sms_via_mesika.delay(f"{settings.CLIENT}",
                                               sms_message, payer_msisdn,
                                               payer_ref)
                LOG_HANDLER.writelog(module, f"DELIVERING ACCT MSG: {sm.id}")
            except:
                LOG_HANDLER.log_error_detailed(module, module)
                LOG_HANDLER.writelog(
                    module, f"Could not send SMS on Call Back Receipt")

            response['status'] = 200
            response[
                'reason'] = "Transaction Updated With Callback Response Successfully"

    except Transactions.DoesNotExist:
        response['status'] = 404
        response[
            'reason'] = f"Transaction with Payer Reference {our_reference_number} Not Found"

    except:
        ler = LOG_HANDLER.log_error_detailed(module, module)
        response['status'] = 502
        response['reason'] = f"Exception Updating Transaction {ler}"

    LOG_HANDLER.writelog(module, f"UPDATE RESPONSE ::: {response}")

    return JsonResponse(response)