Implement Moncashify SDK in your python projects

Last update: Jan. 18, 2024, 11:27 a.m.
Tags :
Reading Duration : 0:02:00
Views : 426
Available in : English

In this tutorial, I will teach you how to implement the moncashify sdk to accept payments from your customers in Haiti using the Moncash API. This tutorial can be used in any python project.

This package is compatible with python 2 or 3, so you don't really need to worry about which version you are using. In my case, I am using Python 3.7.

Moncashify can be installed with pip - go to your terminal or cmd and type the command below:

pip install moncashify

Once you have installed it, you can open your project to start using the sdk.

I have a customer who bought in my store, a shoe that costs 1500 HTG. This shoe has a unique ID in my database which is SH023. The goal is to charge the customer, 1,500 HTG from his Moncash account.

Set up your Moncash dashboard

To configure your Moncash dashboard, you must have an account. Click on this link to create your own account. Once in the dashboard, go to the General info section to add your business info

enter image description here

After saving all of your business information, you will need a client_id and a client_secret. To have these credentials, Go to General info again, and *click View next to the business account you have just created. All the credentials you need, will be below in a section called REST API.

Client Id: f443363451e40d926edd8ff3eb59ca7
Client Secret: fqCEN-jbcK9K_iterjkkjgdUusd1i64cfFg4-LOnqPeS6PO6tDP2KmXwsYjBAt6C

Note that your client_id and client_secret will not be the same as mine. The client_secret should not be shared with anyone, this is the secret key to keep private. Make sure you have these credentials somewhere, as you will need them.

Initialize the process

To start the process, just import the awesome package built for you.

[class=language-python]

import moncashify

If you previously had the moncashify installed in your project, make sure you install the version 0.3.0 with pip install moncashify==0.3.0.

Note that I will use the name secret_key instead of client_secret.

[class=language-python]

client_id = "f443363451e40d926edd8ff3eb59ca7"
secret_key = "fqCEN-jbcK9K_iterjkkjgdUusd1i64cfFg4-LOnqPeS6PO6tDP2KmXwsYjBAt6C"

This is information that I have in my database.

[class=language-python]

order_id = 'SH023'
amount = 1500 # HTG

The order_id is up to you, and it must be unique, representing your order.

To initialize the connection to the API, do this:

[class=language-python]

moncash = moncashify.API(client_id, secret_key, True)

The first two arguments are the MonCash credentials, and the third one is for debugging, when I set it to True, it means I am testing the gateway..

When you are in development, MonCash will not charge you for the transaction if you use some Test Numbers. Note that the credentials will be different when you go live, and different URL will be used to make the payment.

No worries, moncashify handles everything for you, you just need to have the credentials, set debug to True for development, and to False when your project goes live.

Make a payment

The variable moncash is the instance to use to make the payment.

[class=language-python]

payment = moncash.payment(order_id, amount)
print(payment)
# Payment object - Order ID: SH023, Amount: 1500

And there you are, you've made your first payment. Use the redirect_url attribute of the instance, to get the redirect URL that the client will use to pay.

[class=language-python]

url = payment.redirect_url
print(url)
# https://sandbox.moncashbutton.digicelgroup.com/Moncash-middleware/Payment/Redirect?token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjQ1NzgiLCJyZWYiOiJTSDAyMyIsImNydCI6MTU5MTgwNTA0MzQ2NCwiZXh0IjoxNTkxODA1NjQzNDY0LCJhcGkiOnRydWV9.t82yPlU7lk8qEDbVIu_py1dNW89LXsjwkxzcOUm7hvHFbaat3qauiPt30_dBEjhnGt_G-7YuwwdlKhhmWMRFh6HbaRWrDtHwsWripq2If27AOlFsa5TOY8AygEIu79u-QzvOv2u_7rR6VNAejhflPMc5UuK4NJuzcsFR4Z5vy0hI_ZSCrSlz1clZMEqtUtfzshQT4tIQWIG8Vco2DTNMZ9lQmyFqQ2BkXIp19WTS61ot0BAdDxatE7ApF8L4o3KA76wMizIOdNyGA6XH4xMqa6VCU-JoQcH2K56BOUFU9oNLBQwAjh46e5AWy2TCsIE52pYsg4pnFq5HFiIalUGdww

This URL is valid for only 5 minutes, this is the URL that your customer will use to pay you. Actually, the API sends more information, and to get all of it, just call payment.get_response().

Play the customer role

Open a browser, visit the link and fill out the form with your test account. If you don't have a test number yet, go to the MonCash dashboard and click Add Test Account to create it. By filling this form, you are acting as a customer.

With this test number, you can proceed to the payment test by visiting the link in redirect_url in your browser. Once you're done with everything, the MonCash API will redirect you to the link provided in Alert Url when you were filling the business form in General Info

Get the transaction/order details

You can also get the transaction details with the order_id

[class=language-python]

transaction = moncash.transaction_details(order_id=order_id)
# or 
transaction = moncash.transaction_details_by_order_id(order_id)

I built this SDK to help you as a pythonist benefiting from the MonCash API. I hope this tutorial will help you implement the SDK very well. If you want to know more about all the features, go to the official documentation.

If you want to contribute to this project, visit the Github.