Skip to content

Commit 503b012

Browse files
authored
Merge pull request #25 from Ekirapapaul/transactions
Transactions
2 parents c28338d + f85921e commit 503b012

File tree

7 files changed

+59
-9
lines changed

7 files changed

+59
-9
lines changed

mpesa/LipaNaMpesaOnline.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
PASS_KEY = api_settings.PASS_KEY
1616
SHORT_CODE = api_settings.SHORT_CODE
1717
SAFARICOM_API = api_settings.SAFARICOM_API
18+
TRANSACTION_TYPE = api_settings.TRANSACTION_TYPE
1819

1920

2021
# Applies for LipaNaMpesaOnline Payment method
@@ -26,7 +27,6 @@ def generate_pass_key():
2627

2728
def get_token():
2829
api_url = "{}/oauth/v1/generate?grant_type=client_credentials".format(SAFARICOM_API)
29-
print(api_url)
3030

3131
r = requests.get(api_url, auth=HTTPBasicAuth(consumer_key, consumer_secret))
3232
if r.status_code == 200:
@@ -38,7 +38,7 @@ def get_token():
3838
return False
3939

4040

41-
def sendSTK(phone_number, amount, orderId=0, transaction_id=None, shortcode=None):
41+
def sendSTK(phone_number, amount, orderId=0, transaction_id=None, shortcode=None, account_number=None):
4242
code = shortcode or SHORT_CODE
4343
access_token = get_token()
4444
if access_token is False:
@@ -54,18 +54,26 @@ def sendSTK(phone_number, amount, orderId=0, transaction_id=None, shortcode=None
5454
"Authorization": "Bearer %s" % access_token,
5555
"Content-Type": "application/json",
5656
}
57+
58+
transaction_type = TRANSACTION_TYPE or "CustomerBuyGoodsOnline"
59+
# If account number is set, change transaction type to paybill
60+
if account_number:
61+
transaction_type = "CustomerPayBillOnline"
62+
elif transaction_type == "CustomerPayBillOnline" and account_number == None:
63+
account_number = phone_number
64+
5765
request = {
5866
"BusinessShortCode": code,
5967
"Password": encoded,
6068
"Timestamp": time_now,
61-
"TransactionType": "CustomerPayBillOnline",
69+
"TransactionType": transaction_type,
6270
"Amount": str(int(amount)),
6371
"PartyA": phone_number,
6472
"PartyB": code,
6573
"PhoneNumber": phone_number,
6674
"CallBackURL": "{}/mpesa/confirm/".format(HOST_NAME),
67-
"AccountReference": code,
68-
"TransactionDesc": "Payment for {}".format(phone_number)
75+
"AccountReference": account_number or code,
76+
"TransactionDesc": "{}".format(phone_number)
6977
}
7078

7179
print(request)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Generated by Django 2.2.20 on 2022-01-08 10:33
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('contenttypes', '0002_remove_content_type_name'),
11+
('mpesa', '0005_auto_20200822_1645'),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name='paymenttransaction',
17+
name='content_type',
18+
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='contenttypes.ContentType'),
19+
),
20+
migrations.AddField(
21+
model_name='paymenttransaction',
22+
name='object_id',
23+
field=models.PositiveIntegerField(default=0),
24+
),
25+
migrations.AlterField(
26+
model_name='paymenttransaction',
27+
name='amount',
28+
field=models.DecimalField(decimal_places=2, default=0, max_digits=14),
29+
),
30+
]

mpesa/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from django.db import models
55
from django.conf import settings
66
import uuid
7+
from django.contrib.contenttypes.models import ContentType
8+
from django.contrib.contenttypes.fields import GenericForeignKey
79

810

911
# Create your models here.
@@ -21,7 +23,7 @@ class Meta:
2123

2224
class PaymentTransaction(models.Model):
2325
phone_number = models.CharField(max_length=30)
24-
amount = models.DecimalField(('amount'), max_digits=6, decimal_places=2, default=0)
26+
amount = models.DecimalField(max_digits=14, decimal_places=2, default=0)
2527
isFinished = models.BooleanField(default=False)
2628
isSuccessFull = models.BooleanField(default=False)
2729
trans_id = models.CharField(max_length=30)
@@ -30,6 +32,10 @@ class PaymentTransaction(models.Model):
3032
date_modified = models.DateTimeField(auto_now=True)
3133
date_created = models.DateTimeField(auto_now=False, auto_now_add=True)
3234

35+
content_type = models.ForeignKey(ContentType, null=True, blank=True, on_delete=models.SET_NULL)
36+
object_id = models.PositiveIntegerField(default=0)
37+
order_object = GenericForeignKey('content_type', 'object_id')
38+
3339
def __str__(self):
3440
return "{} {}".format(self.phone_number, self.amount)
3541

mpesa/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'PASS_KEY': None,
1212
'SAFARICOM_API': 'https://sandbox.safaricom.co.ke',
1313
'SHORT_CODE': None,
14+
'TRANSACTION_TYPE': 'CustomerBuyGoodsOnline',
1415
}
1516

1617
api_settings = APISettings(USER_SETTINGS, DEFAULTS, None)

mpesa/views.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ def post(self, request):
3535
if data.get('entity_id'):
3636
entity_id = data.get('entity_id')
3737

38-
transactionId = sendSTK(phone_number, amount, entity_id)
38+
paybill_account_number = None
39+
if data.get('paybill_account_number'):
40+
paybill_account_number = data.get('paybill_account_number')
41+
42+
transaction_id = sendSTK(phone_number, amount, entity_id, account_number=paybill_account_number)
3943
# b2c()
40-
message = {"status": "ok", "transaction_id": transactionId}
44+
message = {"status": "ok", "transaction_id": transaction_id}
4145
return Response(message, status=HTTP_200_OK)
4246

4347

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
setup(
2424
name='django-mpesa',
25-
version='2.0.6',
25+
version='2.0.9',
2626
description='A python library that interfaces safaricoms mpesa apis',
2727
long_description=open('README.rst', 'r', encoding='utf-8').read(),
2828
url='https://www.vorane.com/',

tests/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
'PASS_KEY': '',
6060
'SAFARICOM_API': 'https://sandbox.safaricom.co.ke',
6161
'SHORT_CODE': '174379',
62+
'TRANSACTION_TYPE': 'CustomerBuyGoodsOnline',
6263
}
6364

6465
ROOT_URLCONF = 'tests.urls'

0 commit comments

Comments
 (0)