Skip to main content

A python package for connecting to payments platforms like MPESA,PayPal,RazorPay

Project description

koros_python_payments

A python package for connecting to payments platforms like MPESA,PayPal,RazorPay

Installation

pip install koros-python-payments

Configuration

Create an env file and add the following: Update the configuration according to your needs

MPESA_ENV=0 #0 means the sandbox credentails will be used while 1 means live credentations will be used

#Test
MPESA_TEST_CONSUMER_KEY=""
MPESA_TEST_CONSUMER_SECRET=""
MPESA_TEST_TOKEN_URL="https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"
MPESA_TEST_INITIATOR="testapi"
MPESA_TEST_PASSWORD="Safaricom999!*!"
MPESA_TEST_B2C_PARTY_A=600997
MPESA_TEST_B2C_RESULT_URL=""
MPESA_TEST_B2C_QUEUE_TIMEOUT_URL=""
MPESA_TEST_B2C_URL="https://sandbox.safaricom.co.ke/mpesa/b2c/v1/paymentrequest"
MPESA_TEST_B2C_COMMAND_ID = "BusinessPayment"
MPESA_TEST_BUSINESS_SHORTCODE = 174379
MPESA_TEST_PASSKEY = ""
MPESA_TEST_CUSTOMER_TO_BUSINESS_URL = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
MPESA_TEST_CALLBACK_URL = "http://localhost:8000"
MPESA_TEST_TRANSACTION_QUERY_URL = "https://sandbox.safaricom.co.ke/mpesa/transactionstatus/v1/query"
MPESA_TEST_TRANSACTION_QUERY_RESULT_URL="http://localhost:8000/callback/transaction-query/"
MPESA_TEST_TRANSACTION_QUERY_QUEUE_TIMEOUT_URL="https://localhost/callback/transaction-query"
MPESA_TEST_TRANSACTION_QUERY_IDENTIFIER_TYPE='4'
MPESA_TEST_TRANSACTION_QUERY_COMMAND_ID = "TransactionStatusQuery"
MPESA_TEST_TRANSACTION_QUERY_TRANSACTION_CODE = "OEI2AK4Q16"
MPESA_TEST_TRANSACTION_QUERY_PARTY_A= 600987
MPESA_TEST_TRANSACTION_ORIGINATOR_CONVERSATION_ID = ""
MPESA_TEST_CERT_PATH='./certs/SandboxCertificate.cer' #certificate is already uploaded in the package


#Live
MPESA_LIVE_CONSUMER_KEY=""
MPESA_LIVE_CONSUMER_SECRET=""
MPESA_LIVE_TOKEN_URL="https://api.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"
MPESA_LIVE_INITIATOR=""
MPESA_LIVE_PASSWORD=""
MPESA_LIVE_B2C_PARTY_A=""
MPESA_LIVE_B2C_RESULT_URL=""
MPESA_LIVE_B2C_QUEUE_TIMEOUT_URL=""
MPESA_LIVE_B2C_URL="https://api.safaricom.co.ke/mpesa/b2c/v1/paymentrequest"
MPESA_LIVE_B2C_COMMAND_ID = "BusinessPayment"
MPESA_LIVE_BUSINESS_SHORTCODE =""
MPESA_LIVE_PASSKEY = ""
MPESA_LIVE_CUSTOMER_TO_BUSINESS_URL = "https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
MPESA_LIVE_CALLBACK_URL = ""
MPESA_LIVE_TRANSACTION_QUERY_URL = "https://api.safaricom.co.ke/mpesa/transactionstatus/v1/query"
MPESA_LIVE_TRANSACTION_QUERY_RESULT_URL=""
MPESA_LIVE_TRANSACTION_QUERY_QUEUE_TIMEOUT_URL=""
MPESA_LIVE_TRANSACTION_QUERY_IDENTIFIER_TYPE='4'
MPESA_LIVE_TRANSACTION_QUERY_COMMAND_ID = "TransactionStatusQuery"
MPESA_LIVE_TRANSACTION_QUERY_TRANSACTION_CODE = "OEI2AK4Q16"
MPESA_LIVE_TRANSACTION_QUERY_PARTY_A=
MPESA_LIVE_TRANSACTION_ORIGINATOR_CONVERSATION_ID = ""

Make STK Push/ Request payment from user

from mpesa.customer_to_business import CustomerToBusiness

def make_skt_push():
    amount = 1
    phone = "254712345678"
    bill_reference = "12345"
    customer_to_business=CustomerToBusiness()
    repsonse=customer_to_business.stk_push(amount,phone,bill_reference)
    response_code = repsonse.get("ResponseCode","")
    if response_code == "0":
        #Request was  successful, stk sent to customer
        customerMessage = response.get("CustomerMessage","")
        print(customerMessage)
    else:
        #Request was not successful, stk sent to customer
        errorMessage = response.get("errorMessage","")
        print(errorMessage)

Decord STK Push callback

from mpesa.customer_to_business import CustomerToBusiness

def stk_callback(request):
    data = request.data # Data from MPESA API response sent to callback url
    customer_to_business=CustomerToBusiness()
    feedback=customer_to_business.decode_stk_callback(data)
    if feedback:
        #{'merchant_request_id': '29115-34620561-1', 'checkout_request_id': 'ws_CO_191220191020363925', 'result_code': 0, 'result_description': 'The service request is processed successfully.', 'amount': 1.0, 'mpesa_transaction_code': 'NLJ7RT61SV', 'transaction_date': 20191219102115, 'phone_number': 254708374149}
        merchant_request_id = feedback.get("merchant_request_id","")
	    checkout_request_id= feedback.get("checkout_request_id","")
	    result_code= feedback.get("result_code","")
	    result_description= feedback.get("result_description","")
		amount= feedback.get("amount","")
		mpesa_transaction_code = feedback.get("mpesa_transaction_code","")
		transaction_date = feedback.get("transaction_date","")
		phone_number =feedback.get("phone_number","")

        #Other logice.g saving to db
    else:
        print("Invalid data")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

koros_python_payments-1.0.4.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

koros_python_payments-1.0.4-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file koros_python_payments-1.0.4.tar.gz.

File metadata

  • Download URL: koros_python_payments-1.0.4.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for koros_python_payments-1.0.4.tar.gz
Algorithm Hash digest
SHA256 49a56257e7c87ee7bc986c45fc33df860c026684b99a8a000adab708639db431
MD5 f5769de84a0731c46ef722d96ac3f4b8
BLAKE2b-256 78963496eeb408194758408057f8a255b91f0bc9bf60d6e491d3440a7f589c6b

See more details on using hashes here.

File details

Details for the file koros_python_payments-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for koros_python_payments-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c8d0d6b4b350a928d019058a0bf9e7451b7b4523771e1d6f12e18d89784bc455
MD5 e29a8b6f2bfa00c6c088c1407f52fc96
BLAKE2b-256 dc22712e352815140db68f6cce9d57eee2cf2d014745b6912ffac336b80252b7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page