Git Product home page Git Product logo

chowder's Introduction

Chowder M-Pesa Checkout Android Library [DEPRECATED]

Chowder is based on the M-Pesa SOAP APIs which are being phased out. Unless you explicitly want to work with those APIs, I wouldn't recommend using Chowder, but instead use the M-Pesa REST APIs.

This library, using the M-Pesa C2B APIs will allow you to prompt a user to make a payment from their M-Pesa account to a PayBill number without having to leave your app.

For successful requests, Safaricom will send a push USSD to the user's mobile device and prompt them to enter their Bonga PIN. Funds will then be transferred from the user's M-Pesa account into your PayBill account after which you can provide the user the goods or services that they have purchased.

Get the sample apk from here.

Find progress on updating Chowder to use new M-Pesa REST APIs here.

Screenshots

The screenshots below show how it works.

Use Cases

You can easily use Chowder in your Android app for the following cases:

  • Having a user pay before accessing your app, or certain features in your app.
  • Having a list of items, such as products, tickets, meals, books, music, images or other media, and having the user reliably pay to access them.
  • In-app purchases in games.
  • Having a user pay to access the premium/ad-free version of your app.
  • Subscribing a user and having them pay again after a set period of time.
  • Any form of payment you need a user to make for you to provide them goods/services via Android.

Usage

Add this to the build.gradle file of your module:

dependencies {
    compile 'com.toe.chowder:chowder:0.8.2'
}

If you are using a version lower than 0.8.2 make sure you switch to this version because it has some important security updates.

The minSdkVersion is 9

Parameters

You're going to need some parameters beforehand in order to successfully receive payments:

  • Pay Bill number: This is the PayBill number to which the user will be making payments. You get this from Safaricom.
  • Passkey: This is a string that you also get from Safarcom after they enable your PayBill account for online checkouts.
  • Amount: This is the amount that you would like to charge the user, or the cost of your product, feature or service.
  • Phone number: This is the Safaricom phone number of the person who is supposed to make the payment. They will have to confirm the payment using their Bonga PIN.
  • Product Id: This is the unique id of the product, feature or service that you are selling.

You must provide all of these parameters or else you will receive an error.

Sample Code

Process payment:

This is how you initialize the Chowder object and process a payment.

Get the test PAYBILL_NUMBER and PASSKEY from the sample project.

    //You can create a single global variable for Chowder like this
    Chowder chowder = new Chowder(YourActivity.this, PAYBILL_NUMBER, PASSKEY, this);

    //You can also add your callback URL using this constructor
    Chowder chowder = new Chowder(YourActivity.this, PAYBILL_NUMBER, callbackUrl ,PASSKEY, this);

    //You can then use processPayment() to process individual payments
    chowder.processPayment(amount, phoneNumber, productId);

Guess what? You're done! All you have to do is wait for the user to make the payment.

Confirm payment:

This is how you confirm whether a user has paid or not, so you can then take the necessary action.

chowder.checkTransactionStatus(PAYBILL_NUMBER, transactionId);
//Use the transaction id provided by the PaymentListener

Interface

You use PaymentListener to know the results of the payment processes. There are three methods:

@Override
public void onPaymentReady(String returnCode, String processDescription, String merchantTransactionId, String transactionId) {
    //The user is now waiting to enter their PIN on the Safaricom push USSD
    //Show the user something cause it might be awkward just sitting there
    //You can use the transaction id provided to confirm payment to make sure you store the ids somewhere if you want the user to be able to check later
}

@Override
public void onPaymentSuccess(String merchantId, String phoneNumber, String amount, String mpesaTransactionDate, String mpesaTransactionId, String transactionStatus, String returnCode, String processDescription, String merchantTransactionId, String encParams, String transactionId) {
    //The payment was successful, and real money has moved from the user to the PayBill account
}

@Override
public void onPaymentFailure(String merchantId, String phoneNumber, String amount, String transactionStatus, String processDescription) {
    //The payment failed. The user most probably cancelled the transaction. They can always try again.
}

Subscriptions

You can subscribe the user for a product or service using Chowder. This means that after a certain period of time the subscription will become invalid and therfore you may charge the user again. Here's how that works:

@Override
public void onPaymentSuccess(String merchantId, String msisdn, String amount, String mpesaTransactionDate, String mpesaTransactionId, String transactionStatus, String returnCode, String processDescription, String merchantTransactionId, String encParams, String transactionId) {
    //When the payment is complete you have the option to subscribe the user
    //This means that after a set period of time you can prompt the user to make another payment if the subscription is invalid
    String productId = "1717171717171";
    chowder.subscribeForProduct(productId, Chowder.SUBSCRIBE_DAILY);

    //After a day, if you check the product's subscription it will be invalid, but before it will be valid
}

After the user subscribes for a product you can periodically check whether the subscription is valid like this:

//This is how you check whether a single product's subscription is valid
String productId = "1717171717171";
boolean isSubscribed = chowder.checkSubscription(productId);

//This is how you retrieve all the product's subscriptions
ArrayList<Subscription> validatedSubscriptions = chowder.checkAllSubscriptions();

//This how you check a subscribed product's data from the list
if (validatedSubscriptions.size() > 0) {
    Subscription subcribedProduct = validatedSubscriptions.get(0);
    String subscribedProductId = subcribedProduct.getProductId();
    boolean isSubscriptionValid = subcribedProduct.isSubscriptionValid();

    //You get the product Id and whether or not it's subscription is valid
}

Here are the subscription periods:

Chowder.SUBSCRIBE_DAILY;
Chowder.SUBSCRIBE_WEEKLY;
Chowder.SUBSCRIBE_MONTHLY;
Chowder.SUBSCRIBE_YEARLY;

You can check whether the subscription is valid every time the user tries to access the product or service.

Disclaimer

The subscriptions are stored locally, therefore if a user clears the app's data or uninstalls it, the subscriptions will be lost. This will mean a user potentially won't get access to a product or service they paid for.

Debugging

  • You can use the Log tag "M-PESA REQUEST" to view any errors or exceptions.
  • If you get errors, look up the response code (which will be toasted and also logged) in the Developers Guide under Reference Faults. Find it here.

Other Platforms

PHP

If you would like to use the M-Pesa API for a PHP project, find a PHP implementation here.

Conclusion

And you are done! Get more code in the sample project.

If you have any feature suggestions or additions that you wish to make, please feel free. Please open issues if you come across anything weird.

chowder's People

Contributors

ianwambai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chowder's Issues

Bonga PIN vs. MPESA PIN

Hi,

When you say "Bonga Pin", do you mean "MPESA PIN"? or specifically the Bonga Points PIN?

Error Handling

Most of the faults return "Something went wrong" this should be more informative

New M-Pesa REST APIs

Update Chowder to use the new Safaricom M-Pesa REST APIs. I'll try finish this by the end of the year, but it's not 100% certain. You can find more documentation here.

Here are benefits of the REST API:

  • It's uses REST and not SOAP. Android doesn't natively support SOAP, and switching to REST, means you don't need to use Chowder anymore if you're comfortable making basic API requests, which is super easy.

  • More APIs. I'm not sure, but I'll probably be able to add a few more features into Chowder. This includes B2B and B2C APIs, which should be really cool. I should also be able to add account balance checking, transaction reversals, Lipa na M-Pesa online payments (via STK push), and queries.

multiple payments for one subscription

Hi thanks for the good work. i am using the Chowder but i have one concern, what if there is this scenario where the customer wants to pay monthly subscription once say for 5 months.

Unable to open dialog for entering Boga PIN

I have used this library and all is working fine except boga pin confirmation dialog.
I am unable to enter boga pin for confirm payment.
Please check the screenshot
screenshot_2017-06-02-15-37-31-686_com toe chowder

After enter pay button, this screen will be open

screenshot_2017-06-02-15-37-39-691_com toe chowder

After click on Ok button, nothig will be happned.

Please suggest me what I can do for further process?

Thanks

Merchant Id and Passkey

The merchant id is the same as the PayBill number and the passkey is a parameter you get when you register for a PayBill account.

javax.net.ssl.SSLPeerUnverifiedException

Chowder, cause I was dumb, contains a piece of code that effectively removes the protection of HTTPS from your connections. Fixing this and updating the dependency on Bintray asap.

Subscription Payments

It would be cool if the developer could select a period of time after which the user is prompted to pay again.

Call back url error

I have compiled a sample app, everything is working fine except the call back url part. Would you assist with a sample call back file?

Payment Success not being called

Hi! Great work you've done with this. I'm thinking of porting it to iOS. My problem is that onPaymentSuccess isn't being called on a successful request. onPaymentReady gets called though. I tried with the sample app and on mine.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.