How to use monnifyease

For installation instructions, refer to: How to Install monnifyease.

Hint

Before using the MonnifyEase library, you’ll need a secret key, an api key, a base url and a merchant code generated from your Monnify account. Set the following as environment variables named MONNIFY_SECRET_KEY, MONNIFY_API_KEY, MONNIFY_BASE_URL and MONNIFY_MERCHANT_CODE.

The MonnifyEase library supports synchronous operations. To use them, import and instantiate the appropriate wrappers as shown below:

  • Synchronous support:

from monnifyease import Monnify, Currency

client = Monnify()

To perform a transaction synchronously, use the transaction API wrapper. The example below demonstrates the use of the initialize_transaction method in the transaction API wrapper:

create_transaction = client.transactions.initialize_transaction(
    amount=1000,
    customer_name="John Doe
    customer_email="johndoe@gmail.com",
    payment_reference="test123",
    payment_description="Testing payment",
    currency=Currency.NGN.value,
    merchant_contract_code=MONNIFY_MERCHANT_CODE,
    # other optional parameters
)

print(f"Created Transaction: {create_transaction}")

The response from the server will be as follows:

MonnifyResponse(
    status_code=200,
    requestSuccessful=True,
    responseMessage="success",
    responseCode=0
    responseBody={
        "transactionReference": "MNFY|20190915200044|000090",
        "paymentReference": "test123",
        "merchantName": "7PVGX8MEk85tgeEpVDtD",
        "enabledPaymentMethod": ["ACCOUNT_TRANSFER", "CARD"],
        "checkoutUrl": "https://sandbox.sdk.monnify.com/checkout/MNFY|20190915200044|000090",
    }
)

To redirect the user to Monnify checkout page to make payments, your application should call the url method from your response instance and provide a 301 status code parameter (optional).

See Example

This is a django example of how to redirect users to Monnify checkout page to proceed with payment.

session = client.transactions.initialize_transaction(
    amount=1000,
    customer_name="John Doe
    customer_email="johndoe@gmail.com",
    payment_reference="test123",
    payment_description="Testing payment",
    currency=Currency.NGN.value,
    merchant_contract_code=MONNIFY_MERCHANT_CODE,
    redirect_url=success_url  # this is the redirect url to the success page when payment is successful.
    # other optional parameters
)

return redirect(session.url, code=301)

Also the webbrowser module is another approach to redirect users to Monnify checkout page. Which ever is your choice is best.

import webbrowser

webbrowser.open(session.url)