Exemple #1
0
def lipa_na_mpesa():

    formatted_time = get_timestamp()
    decoded_password = generate_password(formatted_time)
    access_token = generate_access_token()

    api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"

    headers = {"Authorization": "Bearer %s" % access_token}
    request = {
        "BusinessShortCode": keys.business_shortCode,
        "Password": decoded_password,
        "Timestamp": formatted_time,
        "TransactionType": "CustomerPayBillOnline",
        "Amount": "5",
        "PartyA": keys.phone_number,
        "PartyB": keys.business_shortCode,
        "PhoneNumber": keys.phone_number,
        "CallBackURL": "https://fullstackdjango.com/lipanampesa/",
        "AccountReference": "12345678",
        "TransactionDesc": "Pay School Fees"
    }

    response = requests.post(api_url, json=request, headers=headers)

    print(response.text)
Exemple #2
0
def register_url():

    my_access_token = generate_access_token()

    api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl"

    headers = {"Authorization": "Bearer %s" % my_access_token}

    request = {
        "ShortCode":
        keys.shortcode,
        "ResponseType":
        "Completed",
        "ConfirmationURL":
        "https://mysterious-oasis-16355.herokuapp.com/api/payments/c2b-confirmation/",
        "ValidationURL":
        "https://mysterious-oasis-16355.herokuapp.com/api/payments/c2b-validation/",
    }
    #register url above
    try:
        response = requests.post(api_url, json=request, headers=headers)
    except:
        response = requests.post(api_url,
                                 json=request,
                                 headers=headers,
                                 verify=False)

    print(response.text)
def lipa_na_mpesa():

    timestamp = get_timestamp()
    password = generate_password(timestamp)
    access_token = generate_access_token()

    api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {
        "BusinessShortCode": keys.business_short_code,
        "Password": password,
        "Timestamp": timestamp,
        "TransactionType": "CustomerPayBillOnline",
        "Amount": "1",
        "PartyA": keys.phone_number,
        "PartyB": keys.business_short_code,
        "PhoneNumber": keys.phone_number,
        "CallBackURL": "https://edwinthedjangodev.com/lnm/",
        "AccountReference": "11568",
        "TransactionDesc": "pay school fees"
    }

    response = requests.post(api_url, json=request, headers=headers)

    print(response.text)
Exemple #4
0
def register_url ():
    my_access_token = generate_access_token()
    api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl"
    headers = {"Authorization": "Bearer %s" % my_access_token}
    request = { 
      "ShortCode": keys.c2b_short_code,
      "ResponseType": "Completed",
      "ConfirmationURL": "https://asininefatuity.com/confirmation",
      "ValidationURL": "https://asininefatuity.com/validation_url"}
  
    response = requests.post(api_url, json = request, headers=headers)
  
    print (response.text)
Exemple #5
0
def simulatetransaction_c2b():
    api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate"
    headers = {"Authorization": "Bearer %s" % generate_access_token()}
    request = {
        "ShortCode": keys.shortcode,
        "CommandID": "CustomerPayBillOnline",
        "Amount": "1",
        "Msisdn": keys.test_msisdn,
        "BillRefNumber": "cit2210182016"
    }

    response = requests.post(api_url, json=request, headers=headers)

    print(response.text)
Exemple #6
0
def register_url():
    access_token = generate_access_token()
    api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {
        "ShortCode": important.shortcode,
        "ResponseType": " Completed",
        "ConfirmationURL": "https://fullstackdjango.com/confirmation",
        "ValidationURL": "https://fullstackdjango.com/validation_url"
    }

    response = requests.post(api_url, json=request, headers=headers)

    print(response.text)
Exemple #7
0
def simulate_transaction_c2b ():
        my_access_token = generate_access_token()
        api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate"
        headers = {"Authorization": "Bearer %s" % my_access_token}
        request = { 
        "ShortCode":keys.c2b_short_code,
        "CommandID":"CustomerPayBillOnline", # can be this or buy goods online
        "Amount":"2",
        "Msisdn": keys.test_msisdn,
        "BillRefNumber":"35842347" #can be invoice/ id number etc, will be used in future for validation
        }

        response = requests.post(api_url, json = request, headers=headers)

        print (response.text)
Exemple #8
0
def register_url():
    generated_access_token = generate_access_token()

    api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl"
    headers = {"Authorization": "Bearer %s" % generated_access_token}
    request = {
        "ShortCode": keys.shortcode,
        "ResponseType": "Success",
        "ConfirmationURL": "https://matlynventures.com/confirmation",
        "ValidationURL": "https://matlynventures.com/validation_url"
    }

    response = requests.post(api_url, json=request, headers=headers)

    print(response.text)
Exemple #9
0
def simulate_transaction():

    my_access_token = generate_access_token()
    api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate"
    headers = {"Authorization": "Bearer %s" % my_access_token}
    request = {
        "ShortCode": keys.shortcode,
        "CommandID": "CustomerPayBillOnline",  #could be CustomerBuyGoodsOnline
        "Amount": "2",
        "Msisdn": keys.test_msisdn,
        "BillRefNumber": "12345"
    }

    response = requests.post(api_url, json=request, headers=headers)

    print(response.text)
Exemple #10
0
def register_url():
    access_token = generate_access_token()
    api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {
        "ShortCode": mpesa_keys.shortcode,
        "ResponseType": "Completed",
        # This is what will happen incase safaricom sends you a response of your transaction and your server is not online. Options Canceled or Completed
        "ConfirmationURL": mpesa_keys.mpesa_c2b_confirmation_url,
        "ValidationURL": mpesa_keys.mpesa_c2b_validation_url
    }

    try:
        response = requests.post(api_url, json=request, headers=headers)
    except:
        response = requests.post(api_url,
                                 json=request,
                                 headers=headers,
                                 verify=False)
    print(response.text)
Exemple #11
0
def simulate_c2b_transaction(amount, bill_ref_number, msisdn):
    access_token = generate_access_token()
    api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {
        "ShortCode": mpesa_keys.shortcode,
        "CommandID": "CustomerPayBillOnline",
        "Amount": amount,
        "Msisdn": str(msisdn),
        "BillRefNumber": str(bill_ref_number)
    }
    try:
        response = requests.post(api_url, json=request, headers=headers)
    except:
        response = requests.post(api_url,
                                 json=request,
                                 headers=headers,
                                 verify=False)

    print(response.text)
def Lipa_na_mpesa():
    my_formatted_time = get_timestamp()
    my_password = generate_password(my_formatted_time)
    access_token = generate_access_token()
    api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {
        "BusinessShortCode": keys.business_short_code,
        "Password": my_password,
        "Timestamp": my_formatted_time,
        "TransactionType": "CustomerPayBillOnline",
        "Amount": "1",
        "PartyA": keys.phone_number,
        "PartyB": keys.business_short_code,
        "PhoneNumber": keys.phone_number,
        "CallBackURL": "https://asininefatuity.com",
        "AccountReference": "f17/81870/2017 ",
        "TransactionDesc": "pay compliments"
    }
    response = requests.post(api_url, json=request, headers=headers)
    print(response.text)
Exemple #13
0
def lipa_na_mpesa():

    access_token = generate_access_token()
    api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {
        "BusinessShortCode": important.business_shortCode,
        "Password": password,
        "Timestamp": date,
        "TransactionType": "CustomerPayBillOnline",
        "Amount": "5",
        "PartyA": important.partyA,
        "PartyB": important.business_shortCode,
        "PhoneNumber": important.partyA,
        "CallBackURL": "https://ben.com/callback",
        "AccountReference": "12345678",
        "TransactionDesc": "school fees "
    }

    response = requests.post(api_url, json=request, headers=headers)

    print(response.text)
Exemple #14
0
def lipa_na_mpesa(amount, account_reference, phone_number):
    formatted_time = get_timestamp()
    decoded_password = generate_password(formatted_time)
    access_token = generate_access_token()
    api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {
        "BusinessShortCode": mpesa_keys.businessShortCode,
        "Password": decoded_password,
        "Timestamp": formatted_time,
        "TransactionType": "CustomerPayBillOnline",
        "Amount": amount,
        "PartyA": mpesa_keys.phone_no,
        "PartyB": mpesa_keys.businessShortCode,
        "PhoneNumber": str(phone_number),
        "CallBackURL": mpesa_keys.lipa_na_mpesa_online_callback_url,
        "AccountReference": account_reference,
        "TransactionDesc": mpesa_keys.lipa_na_mpesa_online_transaction_description
    }

    response = requests.post(api_url, json=request, headers=headers)

    print(response.text)
        package_pr_id = sys.argv[2]

    except:
        print('Not enough arguments')
    print('Environment:', environment)
    print("PR ID:", package_pr_id)

    #if you want to test it from jenkins use line number :20 and comment line numbers :  22
    package_file_path = "/var/www/html/prpackages/module_" + package_pr_id + "_modules_1.zip"

    #if you want to test it from local use line number :25 and comment line number : 22
    #package_file_path = os.getcwd() + "/" + package_pr_id +".zip"
    json_path = os.getcwd() + "/environments.json"
    json_open = open(json_path)
    data_json = json.load(json_open)

    envi_info = data_json[environment]
    url = envi_info["env_url"]
    payload = envi_info["credentials"]

    access_token = generate_access_token(url=url, payload=payload)

    file_hash, unfile_hash = upload_package(url=url,
                                            access_code=access_token,
                                            pkg_path=package_file_path)

    install_package(url,
                    access_code=access_token,
                    file_install_hash=file_hash,
                    unfile_hash=unfile_hash)
Exemple #16
0
import requests

from datetime import datetime
from access_token import generate_access_token
from encode import generate_password
import keys

formatted_time = datetime.now().strftime('%Y%m%d%H%M%S')
# print(formatted_time)

decode_password = generate_password(formatted_time)

my_access_token = generate_access_token()


def lipa_na_mpesa():

    access_token = my_access_token
    api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {
        "BusinessShortCode": keys.business_shortcode,
        "Password": decode_password,
        "Timestamp": formatted_time,
        "TransactionType": "CustomerPayBillOnline",
        "Amount": "1",
        "PartyA": keys.phone_number,
        "PartyB": keys.business_shortcode,
        "PhoneNumber": keys.phone_number,
        "CallBackURL": "https://fullstackdjango.com/lipanampesa/",
        "AccountReference": " 123456 ",
#if you want to test it from local use line numbers : 21 and comment line numbers : 18
pkg_name = pr_number


json_path = os.getcwd() + "/environments.json"
json_open = open(json_path)
data_json = json.load(json_open)

envi_info = data_json[envi]
envi_url = envi_info["env_url"]
envi_payload = envi_info["credentials"]

print("Environment : ", envi)
print("PR ID : ", pr_number)

access_response_token = generate_access_token(envi_url ,envi_payload)

if access_response_token == None :
    exit()

pkg_id = installed_pkg_api(envi_url ,access_response_token ,pkg_name)

if pkg_id == None :
    print("Package doesn't exist in the environment, will install the package")
    exit()
else :
    print("Package already installed.. Deleting the Package")

    uninstall_pkg_api(envi_url ,access_response_token, pkg_id)
    unFile_hash = staged_pkg_api(envi_url ,access_response_token ,pkg_name)