From 56ff29df2e80a39bd1dddf679e659014d683eed3 Mon Sep 17 00:00:00 2001 From: muneneevans Date: Wed, 26 Aug 2020 15:46:46 +0300 Subject: [PATCH] Add model fields to PaymentTransaction Admin View (#4) * TCW add extra fields to PaymentTransaction Admin View * Move safaricom-api to .env (#1) --- .gitignore | 1 + api.rest | 37 +++++++++++++++++++++++++++++++++++++ env.example | 3 ++- mpesa/admin.py | 7 ++++++- mpesa/functions.py | 9 +++++---- 5 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 api.rest diff --git a/.gitignore b/.gitignore index 259c233..a9bc2e7 100644 --- a/.gitignore +++ b/.gitignore @@ -103,3 +103,4 @@ venv.bak/ # mypy .mypy_cache/ .idea* +.vscode \ No newline at end of file diff --git a/api.rest b/api.rest new file mode 100644 index 0000000..3713790 --- /dev/null +++ b/api.rest @@ -0,0 +1,37 @@ +@host =http://localhost:8000 +@phoneNumber=2547XXXXXXXX + + +### +# @name requestSTKPush +POST {{host}}/mpesa/submit/ HTTP/1.1 +content-type: application/json + +{ + "phone_number":{{phoneNumber}}, + "amount": 1, + "entity_id":1 +} + + +### +# @name checkTransactionFromMPESA +POST {{host}}/mpesa/check-online/ HTTP/1.1 +content-type: application/json + +{ + "transaction_id" : 4 +} + + +### +# @name checkTransactionlocally +POST {{host}}/mpesa/check-transaction/ HTTP/1.1 +content-type: application/json + +{ + "transaction_id" : 4 +} + + + diff --git a/env.example b/env.example index d5465b2..7e01cf4 100644 --- a/env.example +++ b/env.example @@ -1,4 +1,5 @@ CONSUMER_KEY=[consumer_key] CONSUMER_SECRET=[consumer secret from daraja] HOST_NAME=[host name] -CERTIFICATE_FILE=[path to certifiate file] \ No newline at end of file +CERTIFICATE_FILE=[path to certifiate file] +SAFARICOM_API=http://sandbox.safaricom.co.ke \ No newline at end of file diff --git a/mpesa/admin.py b/mpesa/admin.py index a230034..5a794a1 100644 --- a/mpesa/admin.py +++ b/mpesa/admin.py @@ -5,5 +5,10 @@ from .models import PaymentTransaction, Wallet # Register your models here. -admin.site.register(PaymentTransaction) + +class PaymentTransactionAdmin(admin.ModelAdmin): + list_display = ("phone_number", "amount", "isFinished", + "isSuccessFull", "trans_id", 'date_created','date_modified') + +admin.site.register(PaymentTransaction, PaymentTransactionAdmin) admin.site.register(Wallet) \ No newline at end of file diff --git a/mpesa/functions.py b/mpesa/functions.py index 7b49309..1f7ec17 100644 --- a/mpesa/functions.py +++ b/mpesa/functions.py @@ -26,6 +26,7 @@ HOST_NAME = os.environ.get('HOST_NAME') PASS_KEY = os.environ.get('PASS_KEY') shortcode = os.environ.get('SHORT_CODE') +SAFARICOM_API = os.environ.get('SAFARICOM_API') def encryptInitiatorPassword(): @@ -49,7 +50,7 @@ def generate_pass_key(cipher): def get_token(): - api_URL = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials" + api_URL = "{}/oauth/v1/generate?grant_type=client_credentials".format(SAFARICOM_API) r = requests.get(api_URL, auth=HTTPBasicAuth(consumer_key, consumer_secret)) jonresponse = json.loads(r.content) @@ -59,7 +60,7 @@ def get_token(): def register_url(access_token): - api_url = "http://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl" + api_url = "{}/mpesa/c2b/v1/registerurl".format(SAFARICOM_API) headers = { "Authorization": "Bearer %s" % access_token, "Content-Type": "application/json", @@ -83,7 +84,7 @@ def sendSTK(phone_number, amount, orderId=0, transaction_id=None): s = shortcode + PASS_KEY + time_now encoded = b64encode(s.encode('utf-8')).decode('utf-8') - api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest" + api_url = "{}/mpesa/stkpush/v1/processrequest".format(SAFARICOM_API) headers = { "Authorization": "Bearer %s" % access_token, "Content-Type": "application/json", @@ -128,7 +129,7 @@ def check_payment_status(checkout_request_id): s = shortcode + PASS_KEY + time_now encoded = b64encode(s.encode('utf-8')).decode('utf-8') - api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpushquery/v1/query" + api_url = "{}/mpesa/stkpushquery/v1/query".format(SAFARICOM_API) headers = { "Authorization": "Bearer %s" % access_token, "Content-Type": "application/json",