Exemple #1
0
def update_transaction(request, reference_type, transaction_id):

    module = "payments.update_transaction"
    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()

    response = {}

    try:

        if reference_type == "payer_reference":
            trx_search = Transactions.objects.using(
                'payment_collections_client').filter(
                    payer_reference_number=transaction_id)
        elif reference_type == "mesika_reference":
            trx_search = Transactions.objects.using(
                'payment_collections_client').filter(
                    mesika_reference_number=transaction_id)
        elif reference_type == "provider_reference":
            trx_search = Transactions.objects.using(
                'payment_collections_client').filter(
                    provider_reference_number=transaction_id)
        else:
            trx_search = Transactions.objects.filter(
                transaction_id=transaction_id)

        LOG_HANDLER.writelog(module,
                             f"TRANSACTION RESULT ||| {trx_search} |||")

        if not trx_search:
            response['status'] = 404
            response[
                'reason'] = f"Transaction reference ||{reference_type}||::{trx_search} Does Not Exist"
        else:
            trx_search.update(status=pl['status'])
            response['status'] = 200
            response['reason'] = "Transaction Updated"

    except Transactions.DoesNotExist:
        response['status'] = 404
        response[
            'reason'] = f"Transaction ||{transaction_id}|| Does Not Exist in [[{reference_type}]]"

    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)
Exemple #2
0
def post_transaction(request):

    module = "payments.post_transaction"

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

    pl = json.loads(request.body)

    response = {}
    extra_usable_information = {}
    try:
        voucher_code = ""
        reference = pl['reference_number']
        narration = pl['narration']
        payer_msisdn = pl['payer_msisdn']
        account = pl['account_number']
        amount = pl['billed_amount']
        processing_fees = pl['processing_fees']

        try:
            voucher_code = pl["voucher_code"]
        except:
            LOG_HANDLER.writelog(module,
                                 f"NO VOUCHER CODE INCLUDED FOR THIS REQUEST")
            voucher_code = ""
        provider = pl['provider']
        stored = False

        # Extra
        extra_usable_information = pl['extra_details']
        LOG_HANDLER.writelog(module,
                             f"EXTRA DETAILS: {extra_usable_information}")
        extra_usable_information['voucher_code'] = voucher_code

        with transaction.atomic():
            trx = Transactions(
                payer_reference_number=reference,
                payer_msisdn=payer_msisdn,
                account_number=account,
                provider=provider,
                amount=amount,
                processing_charge=processing_fees,
            )
            #trx.save(using="payment_collections_client")
            trx.save()

            ed = ExtraDetails(transaction=trx,
                              details=extra_usable_information)
            #ed.save(using="payment_collections_client", force_insert=True)
            ed.save()
            stored = True

        if stored:
            LOG_HANDLER.writelog(module,
                                 f"SENDING {reference} TO PROCESSING QUEUE")
            # Forward to Celery Job For Processing
            pnum = process_payment_request.delay(provider, account, amount,
                                                 reference, narration,
                                                 payer_msisdn, voucher_code)

            LOG_HANDLER.writelog(module, f"Task Processing {pnum}")

            response['status'] = 201
            response['mesika_reference_number'] = trx.mesika_reference_number
            response['transaction_reference_number'] = trx.transaction_id
            response['processing_number'] = pnum.id
            response['payer_reference_number'] = trx.payer_reference_number
            response['reason'] = "Transaction Saved For Processing"
        else:
            transaction.rollback()
            response['status'] = 901
            response[
                'reason'] = "Database Could Not Store This Transaction.Please retry."

    except IntegrityError:
        transaction.rollback()
        ler = LOG_HANDLER.log_error_detailed(module, module)
        response['status'] = 900
        response['reason'] = f"Duplicate Transaction ID-{ler['error_value']}"

    except DatabaseError:
        transaction.rollback()
        ler = LOG_HANDLER.log_error_detailed(module, module)
        response['status'] = 901
        response[
            'reason'] = "Database Could Not Store This Transaction.Please retry."

    except KeyError:
        transaction.rollback()
        ler = LOG_HANDLER.log_error_detailed(module, module)
        response['status'] = 407
        response['reason'] = f"Missing Key {ler['error_value']}"

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

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

    return JsonResponse(response)
Exemple #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)