简体   繁体   English

通过Android App将Mpesa和AirtelMoney等移动货币集成在一起

[英]Intergrating Mobile Money like Mpesa and AirtelMoney with Android App

In Kenya and now spreading to Africa and the rest of the world we have an amazing way of sending and receiving money via cellphones coined mobile money. 在肯尼亚,如今已扩展到非洲和世界其他地区,我们有一种惊人的方式,可以通过手机硬币创造的移动电话来收发钱。 Two leading service providers Safaricom and Airtel have their mobile money platforms namely Mpesa and AirtelMoney respectively. 两家领先的服务提供商Safaricom和Airtel分别拥有其移动支付平台Mpesa和AirtelMoney。

Since Google Merchant services are not available in Kenya and also using them will shy off potential consumers, I have been thinking of using both Mpesa and AirtelMoney to sell my app to users. 由于肯尼亚没有提供Google Merchant服务,而且使用它们还会避开潜在的消费者,因此我一直在考虑同时使用Mpesa和AirtelMoney来向用户出售我的应用程序。 Now both Mobile Money services send confirmation text messages to the sender and receivers whenever a transaction occurs. 现在,无论何时发生交易,两个Mobile Money服务都向发送方和接收方发送确认文本消息。

Now how will i use this services on my app since i have been unsuccessfuly to use available apis which use web platform among other technologies. 现在我将如何在我的应用程序上使用此服务,因为我无法成功使用可使用的API,这些API使用Web平台以及其他技术。 Not all of my users use internet on a daily basis since mine is an app that is used by church members. 并非我的所有用户每天都使用互联网,因为我的是教堂成员使用的应用程序。 But for sure they do use mobile money on a daily basis. 但是可以肯定的是,他们确实每天都在使用移动货币。 I will appreciate any effort towards this. 我将为此付出的一切努力表示赞赏。

I want my app to be able to change from trial to premium when a user pays me via Mpesa because it kind of appears working with sms is easier compared with web apis 我希望我的应用程序能够在用户通过Mpesa向我付款时从试用版更改为高级版,因为与Web api相比,使用sms看起来更容易使用

I had a similar problem to you and decided to use sms to achieve that objective. 我遇到了类似的问题,因此决定使用短信来实现该目标。 First of all I used the permissions for allowing me to do that: 首先,我使用权限允许我执行以下操作:

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver
            android:name="com.example.myapp.IncomingMessage"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS" >
            <intent-filter android:priority="2147483647" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

and in my IncomingMessage activity I added the following code: 在我的IncomingMessage活动中,添加了以下代码:

package com.example.myapp;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

@SuppressLint("ShowToast")
public class IncomingMessage extends BroadcastReceiver
{

  final SmsManager sms = SmsManager.getDefault();

  public void onReceive(Context paramContext, Intent paramIntent)
  {
      SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(paramContext.getApplicationContext()).edit();

      if (!PreferenceManager.getDefaultSharedPreferences(paramContext.getApplicationContext()).getBoolean("js_vsb_is_paid", false))
        {

          Bundle localBundle = paramIntent.getExtras();
            if (localBundle != null) {}
            for (;;)
            {
              int i;
              String sender;
              String message;
              try {
                Object[] arrayOfObject = (Object[])localBundle.get("pdus");
                i = 0;
                if (i >= arrayOfObject.length) {
                  return;
                }
                SmsMessage localSmsMessage = SmsMessage.createFromPdu((byte[])arrayOfObject[i]);
                sender = localSmsMessage.getDisplayOriginatingAddress();
                message = localSmsMessage.getDisplayMessageBody();
                Log.i("SmsReceiver", "senderNum: " + sender + "; message: " + message);

                if (sender.equalsIgnoreCase("MPESA")) {
                    if (message.contains("JACKSON SIRO"))
                    {
                        editor.putBoolean("app_is_paid", true);
                        editor.commit();
                        //Toast.makeText(paramContext, "App Has been Activated!", Toast.LENGTH_LONG).show();

                    }
                 } 
                   else if (sender.equalsIgnoreCase("AirtelMoney")) {
                    if (message.contains("JACKSON SIRO"))  {
                        editor.putBoolean("app_is_paid", true);
                        editor.commit();
                        //Toast.makeText(paramContext, "App Has been Activated!", Toast.LENGTH_LONG).show();
                    }
                  } 

              } catch (Exception localException) {
                Log.e("SmsReceiver", "Exception smsReceiver" + localException);
                return;
              }

     }
    }
  }

I hope this helps. 我希望这有帮助。 The code is simple as such maybe you will modify it to suit your needs 代码很简单,因此您可能会对其进行修改以满足您的需求

Safaricom has released the M-Pesa APIs as RESTful APIs accessible through their developer portal . Safaricom已将M-Pesa API作为RESTful API发布,可通过其开发人员门户进行访问。

The Safaricom github repository , has a sample android application that uses "Lipa na M-Pesa Online" API. Safaricom github 存储库中有一个示例Android应用程序 ,该应用程序使用“ Lipa na M-Pesa Online” API。 This API initiates an M-Pesa transaction on behalf of the user of an app, the user needs only to input their M-Pesa PIN to complete the transaction. 此API代表应用程序用户发起M-Pesa交易,用户只需输入其M-Pesa PIN即可完成交易。

Using a back end language of your choice (Java, php, ETC), the best way to address MPESA integration is to have Mpesa Payments stored in a Database table and then extending your business logic from there. 使用您选择的后端语言(Java,PHP,ETC),解决MPESA集成的最佳方法是将Mpesa Payments存储在数据库表中,然后从那里扩展您的业务逻辑。

You can also require/include your business logic file as soon as there is a Payment conformation in the confirmation file and execute your business logic from there. 一旦确认文件中有“付款”确认,您还可以要求/包括您的业务逻辑文件,并从那里执行您的业务逻辑。 This works for C2B mpesa API. 这适用于C2B mpesa API。 Check this Mpesa C2B integration guide for more info. 查看此Mpesa C2B集成指南以获取更多信息。

On the other hand, the B2C mpesa integration is best done on the back end too. 另一方面,B2C mpesa集成也最好在后端完成。 Just require the B2C payment request file as soon as the business logic is script is done. 一旦完成业务逻辑脚本,就只需要B2C付款请求文件。

Read on Mpesa B2C integration guide here for more details. 在此处阅读Mpesa B2C集成指南 ,以了解更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM