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 = 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 logic e.g saving to db
    else:
        print("No 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.5.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

koros_python_payments-1.0.5-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: koros_python_payments-1.0.5.tar.gz
  • Upload date:
  • Size: 5.8 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.5.tar.gz
Algorithm Hash digest
SHA256 e5f03027a7dfc5816ba4c412744c375b4c10ca0193586535b02afa112dc69591
MD5 d41f072ff82b2d4484d70283e1d943fe
BLAKE2b-256 e1d07971dc8294bf79076bd274b21714491efc7df865b43233b39edc0b0776eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for koros_python_payments-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 212c4e330251b0082dfcf5260a9c722f578767894f9a574e2d3aacd654ee0ef9
MD5 4305c2cf5466e58584f3e389113e02ce
BLAKE2b-256 81d72ddd381da55dd4d40dbd4fc9e43371ac9946bc7fbc2124ccb7f414c71e50

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