Tag

PayTM gateway Integration

Browsing

If you build mobile applications for the Android platform then you are no doubt already familiar with Android Studio, a popular tool and the official Integrated Development Environment (IDE) for Android app development.

Equally, you’ve likely heard of Github and Git. Git is a free and open-source, distributed version control system that lets you manage and track changes in files used in programming. GitHub, is one of many cloud-based hosting services that allows you to manage your Git repositories. Github enables collaborative project development, as multiple programmers can work on project code together. Developers can track and control code changes made by others and, if necessary, return to a state of code before any given change was made, making it an extremely helpful tool.

Android Studio has a user interface that makes it incredibly easy to work with Git without any additional software. In this article, we will show how to clone a repository and work with it using Android Studio and basic Git commands.

Make sure you have a Github account and Android Studio installed and then read on to discover the best way to work with these tools.

Glossary

Before we get started, here are some useful terms worth understanding:

Branch – Branches allow you to work on new features, fix bugs, or develop new ideas in an isolated space within your repository. Branches are needed to isolate development work, so that other branches in the repository are not affected. The existence of branches means that developers can work together on a project and not interfere with each other’s work.

Commit – A commit is like a snapshot (copy) of your repository at specific times. Commits include metadata in addition to the contents and message, like the author, timestamp, and more. The commit history allows you to track changes and if a mistake was made, you can easily find and return to the commit where it happened.

Repository – A repository is a central location where data for your project is stored and managed. A repository contains all of your project’s files and tracks and saves a history of all changes made to files in a project.

The local repository exists on the local machine and can be seen only by the local user.

The remote repository is stored in the cloud, and its local copies are located on the computers of developers. When a developer makes changes to the local version, it can be synchronized with the remote one.

Step 1: Cloning a repository

First you need to open Android Studio.

  1. Press the More Actions command (3-dot icon) on the top row.
  2. Select from the drop-down list “Get from Version Control.”
cloning a repository with Android Studio

Then you need to copy the repository URL that you wish to use from Github.

Go to GitHub and select the project you want to clone and copy the repository URL. For example GitHub – QuickBlox Android Samples. QuickBlox code samples for Android provide an efficient way to add chat or video calling functionality to your application, saving you time from having to code from scratch.

cloning a repository in Android Studio
  1. Press the “Code” button.
  2. Press the Copy URL icon button.

Now return to Android Studio to clone the repository.

cloning a repository with Android Studio
  1. Insert the URL of the repository that you want to clone.
  2. Select the directory where the repository will be cloned.
  3. Press the “Clone” button.

Step 2. Create A New Branch

Now you need to create a new branch where you will work on the code. This is a copy of your workspace in which you can fully work and make changes without affecting the code in the branch from which the copy was made. This allows you to isolate the workflow and not interfere with other developers working on this project.

Create a new branch using Android Studio
  1. Press “Git.”
  2. Select from the drop-down list “Branches.”

This window contains a list of all local and remote branches.

Create a new branch using Android Studio

Press “New Branch

create new branch using Android Studio
  1. Enter the name of the new branch.
  2. Press the “Create” button.

After that, Android Studio will automatically switch to the new branch.

Step 3. Switch branches

To switch between branches you need to go to the window with the list of branches.

Create a new branch using Android Studio
  1. Press the “Git” button.
  2. Select from the drop-down list “Branches.”
switch branches using Android Studio
  1. Select a branch from the list and click on it.
  2. Select “Checkout” from the drop-down list.

This allows you to change the workspace to the branch of your choice.

Step 4. The Commit Command

This is the command to write indexed changes to a Git repository. It saves (writes) states of the branch on the Git project timeline. The “Commit” command should be used after changing something in the project. For example, we have added a new file with the name “Example.java”, and after that, we can save changes to the local repository branch with the commit command.

use commit command in Android Studio
  1. Press the Commit command (green tick icon) on the toolbar.
  2. Provide a brief summary of the changes that were added to this commit.
  3. Press the “Commit” button.

Step 5. The Push Command

The “Push” command will send the local repository branch to the remote repository. It should be used after changes have been committed to a local repository branch. The “push” command can send one or more commits.

Storing data in a remote repository allows you to access content from any computer and other developers who are working on the project can download and apply the actual code to their local repository and stay in sync with the team.

use Push command in Android Studio
  1. Press the Push command (green arrow icon) on the toolbar.
  2. Press the “Push” button.

Step 6. The Pull Command

The “Pull” command is used to retrieve and download content from a remote repository branch and update the local repository branch with that content. This command can be used, for example, if another developer has made changes to the branch code and pushed the changes to a remote repository. For you to have the actual code locally, you need to synchronize your local repository branch with the remote repository branch. After the “Pull” command, the local repository branch will be the same as on the remote.

use the Pull command in Android Studio
  1. Press the Update Project command (blue arrow icon) on the toolbar.
  2. Select “Merge incoming changes into the current branch.”
  3. Press the “OK” button.

Step 7. Using the Log

You can see the history of commits by pressing on the Git in the lower left-hand corner. Here you can find when and by whom the commit was made and what changes were made.

using the log in Android Studio

Conclusion

Android Studio has native integration with Git and GitHub to allow most actions via the Android Studio UI. This makes it much easier to track and manage your Git work, which saves development time. We hope that this article will help you learn how to use basic Git commands with Android Studio so that you can clone a repository and work easily with Git. Happy coding!

I’ll provide you with a sample code and explanation for creating a profile screen with a RecyclerView using Android DataBinding. Here’s a step-by-step guide:

Step 1: Set up your project
Create a new Android project in Android Studio. Make sure you have the necessary dependencies in your build.gradle file:

android {
    // ...
    dataBinding {
        enabled = true
    }
}

Step 2: Create the layout files
Create the following layout files in your project:

activity_profile.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="viewModel"
            type="com.example.yourapp.ProfileViewModel" />
    </data>

    <!-- Your profile screen layout here -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:items="@{viewModel.blogItems}"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:itemLayout="@layout/item_blog" />
    </LinearLayout>
</layout>

item_blog.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="blog"
            type="com.example.yourapp.BlogItem" />
    </data>

    <!-- Your blog item layout here -->
</layout>

Step 3: Create the ViewModel and BlogItem classes
Create the following classes in your project:

ProfileViewModel.kt:

class ProfileViewModel : ViewModel() {
    val blogItems: LiveData<List<BlogItem>> = MutableLiveData(
        listOf(
            BlogItem("Title 1", "Content 1"),
            BlogItem("Title 2", "Content 2"),
            BlogItem("Title 3", "Content 3")
        )
    )
}

class BlogItem(val title: String, val content: String)

Step 4: Create the Adapter and ViewHolder
Create the following classes in your project:

BlogAdapter.kt:

class BlogAdapter(private val blogItems: List<BlogItem>) :
    RecyclerView.Adapter<BlogAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding: ItemBlogBinding = DataBindingUtil.inflate(
            inflater, R.layout.item_blog, parent, false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(blogItems[position])
    }

    override fun getItemCount(): Int = blogItems.size

    class ViewHolder(private val binding: ItemBlogBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(blogItem: BlogItem) {
            binding.blog = blogItem
            binding.executePendingBindings()
        }
    }
}

Step 5: Connect everything in your Activity or Fragment
Update your ProfileActivity or ProfileFragment to include the following code:

ProfileActivity.kt:

class ProfileActivity : AppCompatActivity() {

    private lateinit var binding: ActivityProfileBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_profile)

        val viewModel = ViewModelProvider(this).get(ProfileViewModel::class.java)
        binding.viewModel = viewModel

        val adapter = BlogAdapter(viewModel.blogItems.value ?: emptyList())
        binding.recyclerView.adapter = adapter
    }
}

That’s it! You have now created a profile screen with a RecyclerView using Android DataBinding. Make sure to replace com.example.yourapp it with the appropriate package name in the code.

Remember to customize the layout files and the BlogItem class according to your requirements. You can also update the blog items dynamically by modifying the blogItems LiveData in the ViewModel.

Hope this helps you get started with your profile screen implementation!

Step 1: Sign up for a Paytm merchant account
To integrate the PayTM payment gateway, you need to sign up for a PayTM merchant account. Visit the PayTM Developer Portal (https://developer.paytm.com/) and follow the registration process to create your merchant account.

Step 2: Set up PayTM SDK in your project

  1. Open your Android project in Android Studio.
  2. Add the following dependency to your app-level build. gradle file:
implementation 'com.paytm:pgplussdk:1.4.3'
  1. Sync your project to download the PayTM SDK.

Step 3: Obtain merchant credentials
After signing up for a merchant account, you will receive the necessary credentials from PayTM. These include the merchant ID and merchant key, which you’ll need for integration.

Step 4: Create a PayTM activity
Create a new activity in your Android project to handle the PayTM payment process. For example, create a PaytmActivity.java file.

Step 5: Implement the PayTM payment process
In the PaytmActivity.java file, implement the following steps:

  1. Initialize the PayTM service in the onCreate() method:
PaytmPGService service = PaytmPGService.getStagingService(); // or PaytmPGService.getProductionService() for production environment
  1. Set up the payment parameters and listener:
HashMap<String, String> paramMap = new HashMap<>();
paramMap.put("MID", "YOUR_MERCHANT_ID");
paramMap.put("ORDER_ID", "UNIQUE_ORDER_ID");
paramMap.put("CUST_ID", "CUSTOMER_ID");
paramMap.put("CHANNEL_ID", "WAP");
paramMap.put("TXN_AMOUNT", "AMOUNT");
paramMap.put("WEBSITE", "WEBSTAGING"); // for staging environment, use "DEFAULT" for production
paramMap.put("INDUSTRY_TYPE_ID", "Retail");
paramMap.put("CALLBACK_URL", "CALLBACK_URL"); // the URL to which PayTM will send the payment response

PaytmOrder order = new PaytmOrder(paramMap);

service.initialize(order, null);

Replace the placeholders with the appropriate values from your merchant account.

  1. Start the payment transaction:
service.startPaymentTransaction(this, true, true, new PaytmPaymentTransactionCallback() {
    @Override
    public void onTransactionResponse(Bundle inResponse) {
        // Handle the payment response
    }

    @Override
    public void networkNotAvailable() {
        // Handle network error
    }

    @Override
    public void clientAuthenticationFailed(String inErrorMessage) {
        // Handle authentication error
    }

    @Override
    public void someUIErrorOccurred(String inErrorMessage) {
        // Handle UI error
    }

    @Override
    public void onErrorLoadingWebPage(int iniErrorCode, String inErrorMessage, String inFailingUrl) {
        // Handle web page loading error
    }

    @Override
    public void onBackPressedCancelTransaction() {
        // Handle transaction cancellation
    }

    @Override
    public void onTransactionCancel(String inErrorMessage, Bundle inResponse) {
        // Handle transaction cancellation
    }
});
  1. Handle the payment response in the onTransactionResponse() method:
@Override
public void onTransactionResponse(Bundle inResponse) {
    String status = inResponse.getString("STATUS");
    String message = inResponse.getString("RESPMSG");

    if (status != null && status.equals("TXN_SUCCESS")) {
        // Payment was successful
    } else {
        // Payment failed
    }
}

Step 6: Connect the PayTM activity to your app
Finally, you need to connect the PayTM activity to your e-commerce app. For example, you can add a button to the shopping cart screen and launch the PayTM activity when the user selects the payment option.

That’s it! You have now integrated the PayTM payment gateway into your Android e-commerce app. Remember to replace the placeholder values with your actual merchant credentials and customize the code according to your app’s requirements.

Please note that this guide provides a basic integration example. For a complete and detailed implementation, including error handling and additional features, you may need to refer to the official PayTM documentation or consult their support resources.