In Previous tutorial we have learnt that how to create services for Android application in Fortumo.
See Step by Step Fortumo Implementation in Android (Part 1 Setting up Fortumo).
Now in this tutorial we will going to implement Fortumo in Android application.
Let's begin.
1. Create a new project in android studio give a name whatever you like. `
2. Copy fortumo-in-app-android-sdk.jar under libs folder
3 Paste the below code in build.gradle(Module: app)
See Step by Step Fortumo Implementation in Android (Part 1 Setting up Fortumo).
Now in this tutorial we will going to implement Fortumo in Android application.
Let's begin.
1. Create a new project in android studio give a name whatever you like. `
2. Copy fortumo-in-app-android-sdk.jar under libs folder
3 Paste the below code in build.gradle(Module: app)
dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
testCompile 'junit:junit:4.12'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile files('libs/fortumo-in-app-android-sdk.jar')
}
4.activity_main.xml code
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.freelance.fortumoexample.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" /> </RelativeLayout>
5.Create a new file product_template.xml and paste the below code
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginRight="16dp"> <android.support.v7.widget.AppCompatTextView android:id="@+id/text_view_song_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_alignParentLeft="true" android:text="Song Name" /> <android.support.v7.widget.AppCompatButton android:id="@+id/button_buy" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:background="#f44336" android:textColor="#FFFFFF" android:layout_centerInParent="true" android:layout_height="wrap_content" android:text="Buy" /> </RelativeLayout> </android.support.v7.widget.CardView>
6.Create an Interface for listener IPaymentClickListener.java and paste the below code
public interface IPaymentClickListener { void onPaymentClickListener(String songUniqueID, String songName); }
7.Create a SongAdapter.java class and paste the below code
import android.support.v7.widget.AppCompatButton; import android.support.v7.widget.AppCompatTextView; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.List; public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongViewHolder> { private List<Song> listSong; private IPaymentClickListener iPaymentClickListener; public SongAdapter(List<Song> listSong ,IPaymentClickListener iPaymentClickListener) { this.listSong = listSong; this.iPaymentClickListener=iPaymentClickListener; } @Override public SongViewHolder onCreateViewHolder(ViewGroup parent , int viewType) { CardView view= (CardView) LayoutInflater.from(parent.getContext()) .inflate(R.layout.product_template,parent,false); return new SongViewHolder(view); } @Override public void onBindViewHolder(SongViewHolder holder, int position) { Song song=listSong.get(position); holder.text_view_song_name.setText(song.getSongName()); holder.button_buy.setTag(song); holder.button_buy.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { View v=view; Song info= (Song) view.getTag(); iPaymentClickListener.onPaymentClickListener(info.getSongUniqueID() ,info.getSongName()); } }); } @Override public int getItemCount() { return listSong.size(); } public class SongViewHolder extends RecyclerView.ViewHolder { public AppCompatTextView text_view_song_name; public AppCompatButton button_buy; public SongViewHolder(CardView itemView) { super(itemView); text_view_song_name= (AppCompatTextView) itemView. findViewById(R.id.text_view_song_name); button_buy= (AppCompatButton) itemView.findViewById(R.id.button_buy); } } }
8. Create a Song.java Class and past the below code
public class Song { private String songName; private String songUniqueID; public Song(String songUniqueID, String songName) { this.songUniqueID = songUniqueID; this.songName = songName; } public String getSongUniqueID() { return songUniqueID; } public void setSongUniqueID( String songUniqueID) { this.songUniqueID = songUniqueID; } public String getSongName() { return songName; } public void setSongName(String songName) { this.songName = songName; } }
9. Create a PaymentStatusReceiver.java class and paste the below code.
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class PaymentStatusReceiver extends BroadcastReceiver { private static final String TAG = PaymentStatusReceiver .class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); Log.d(TAG, "- billing_status: " + extras.getInt("billing_status")); Log.d(TAG, "- credit_amount: " + extras.getString("credit_amount")); Log.d(TAG, "- credit_name: " + extras.getString("credit_name")); Log.d(TAG, "- message_id: " + extras.getString("message_id")); Log.d(TAG, "- payment_code: " + extras.getString("payment_code")); Log.d(TAG, "- price_amount: " + extras.getString("price_amount")); Log.d(TAG, "- price_currency: " + extras.getString("price_currency")); Log.d(TAG, "- product_name: " + extras.getString("product_name")); Log.d(TAG, "- service_id: " + extras.getString("service_id")); Log.d(TAG, "- user_id: " + extras.getString("user_id")); } }
10 MainActivity.java code
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.freelance.fortumoexample.Manifest.permission; import java.util.ArrayList; import java.util.List; import mp.MpUtils; import mp.PaymentRequest; import mp.PaymentResponse; public class MainActivity extends AppCompatActivity implements IPaymentClickListener { private static final String SERVICE_ID = "XXXXXXXXXX"; //this will be your key private static final String APP_SECRET = "XXXXXXXXXXX";//this will be your key private List<Song> listSong = new ArrayList<>(); private RecyclerView recyclerView; private SongAdapter songAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recycler_view); songAdapter = new SongAdapter(listSong, this); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setAdapter(songAdapter); getSongList(); songAdapter.notifyDataSetChanged(); } private void getSongList() { listSong.add(new Song("Song1001", "Shape Of You")); listSong.add(new Song("Song1002", "Humble")); listSong.add(new Song("Song1003", "That's What I Like")); listSong.add(new Song("Song1004", "iSpy")); listSong.add(new Song("Song1005", "Something Just Like This")); listSong.add(new Song("Song1006", "Body Like A Back Road")); listSong.add(new Song("Song1007", "I Feel It Coming")); listSong.add(new Song("Song1008", "Tunnel Vision")); listSong.add(new Song("Song1009", "Paris")); listSong.add(new Song("Song10010", "Rockabye")); listSong.add(new Song("Song10011", "Mask Off")); listSong.add(new Song("Song10012", "Bad And Boujee")); listSong.add(new Song("Song10013", "It Ain't Me")); listSong.add(new Song("Song10013", "Stay")); } @Override public void onPaymentClickListener(String songUniqueID, String songName) { PaymentRequest.PaymentRequestBuilder builder = new PaymentRequest.PaymentRequestBuilder(); builder.setService(SERVICE_ID, APP_SECRET); builder.setDisplayString(songName); builder.setProductName(songUniqueID); // non-consumable // purchases are restored using this value builder.setType(MpUtils.PRODUCT_TYPE_NON_CONSUMABLE);// // non-consumable items can be later restored builder.setIcon(R.mipmap.ic_launcher); PaymentRequest pr = builder.build(); makePayment(pr); MpUtils.enablePaymentBroadcast(this, permission .PAYMENT_BROADCAST_PERMISSION); } private static final int REQUEST_CODE = 1234; // Can be anything protected final void makePayment(PaymentRequest payment) { startActivityForResult(payment.toIntent(this), REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode , Intent data) { if (requestCode == REQUEST_CODE) { if (data == null) { return; } // OK if (resultCode == RESULT_OK) { PaymentResponse response = new PaymentResponse(data); switch (response.getBillingStatus()) { case MpUtils.MESSAGE_STATUS_BILLED: // ... break; case MpUtils.MESSAGE_STATUS_FAILED: // ... break; case MpUtils.MESSAGE_STATUS_PENDING: // ... break; } // Cancel } else { // .. } } else { super.onActivityResult(requestCode, resultCode, data); } } }
10 AndroidManifest.xml code
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.freelance.fortumoexample"> <!-- Permissions --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission. ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission. READ_PHONE_STATE" /> <uses-permission android:name="android.permission. RECEIVE_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" /> <!-- Define your own permission to protect payment broadcast --> <permission android:name="com.freelance.fortumoexample. PAYMENT_BROADCAST_PERMISSION" android:label="Read payment status" android:protectionLevel="signature" /> <!-- "signature" permission granted automatically by system, without notifying user. --> <uses-permission android:name="com.freelance.fortumoexample. PAYMENT_BROADCAST_PERMISSION" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <!-- Declare these objects, this is part of Fortumo's Android SDK, and should not be called directly --> <receiver android:name="mp.MpSMSReceiver"> <intent-filter> <action android:name="android.provider. Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> <service android:name="mp.MpService" /> <service android:name="mp.StatusUpdateService" /> <activity android:name="mp.MpActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="orientation|keyboardHidden|screenSize" /> <!-- Implement you own BroadcastReceiver to track payment status, should be protected by "signature" permission --> <receiver android:name=".PaymentStatusReceiver" android:permission="com.freelance.fortumoexample. PAYMENT_BROADCAST_PERMISSION"> <intent-filter> <action android:name="mp.info.PAYMENT_STATUS_CHANGED" /> </intent-filter> </receiver> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

i have using this code and error shows on ur country is currently unsupported. so what can do. pls help me
ReplyDeleteI think your operator does not support Fortumo service, Please check your operator availability on their official website
ReplyDeleteI have error this place MpUtils.enablePaymentBroadcast(this, permission
ReplyDelete.PAYMENT_BROADCAST_PERMISSION);
code source please
ReplyDelete