📜  如何在Android中集成Razorpay付款网关?

📅  最后修改于: 2021-05-09 17:10:08             🧑  作者: Mango

如今,许多应用程序都需要在其应用程序内具有一个支付网关,以便用户可以在其应用程序内进行任何交易以购买任何产品或服务。许多应用程序都使用支付网关功能,但是在Android应用程序中,集成此支付网关是一项艰巨的任务。因此,为了使此任务变得简单和轻松, Razorpay提供了一项服务,我们可以非常轻松地将付款解决方案集成到我们的应用程序中,并且还可以管理我们应用程序中的所有付款方式。在本文中,我们将介绍我们的Android应用中的支付网关的实现。

我们将在本文中构建什么?

我们将构建一个简单的Android应用程序,其中将显示一个EditText和一个按钮。在此屏幕中,我们必须添加要支付的金额,然后单击按钮,我们将打开Razorpay付款网关并进行付款。在本文中,我们将添加测试凭据以在Android中实现Razorpay。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解。注意,我们将使用Java语言实现该项目。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:在build.gradle文件中添加Razor pay库的依赖项

导航到Gradle脚本> build.gradle(Module:app)并将以下依赖项添加到“依赖项”部分。

添加此依赖项后,同步您的项目,现在我们将转向XML部分。

步骤3:向Internet添加权限

导航至应用程序> AndroidManifest.xml文件,然后将以下代码添加到其中。

XML


XML


  
    
    
      
    
    


Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.razorpay.Checkout;
import com.razorpay.PaymentResultListener;
  
import org.json.JSONException;
import org.json.JSONObject;
  
public class MainActivity extends AppCompatActivity implements PaymentResultListener {
  
    // variables for our 
    // edit text and button.
    private EditText amountEdt;
    private Button payBtn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing all our variables.
        amountEdt = findViewById(R.id.idEdtAmount);
        payBtn = findViewById(R.id.idBtnPay);
          
        // adding on click listener to our button.
        payBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // on below line we are getting 
                // amount that is entered by user.
                String samount = amountEdt.getText().toString();
                  
                // rounding off the amount.
                int amount = Math.round(Float.parseFloat(samount) * 100);
                  
                // initialize Razorpay account.
                Checkout checkout = new Checkout();
                  
                // set your id as below
                checkout.setKeyID("Enter your key id here");
                  
                // set image
                checkout.setImage(R.drawable.gfgimage);
                  
                // initialize json object
                JSONObject object = new JSONObject();
                try {
                    // to put name
                    object.put("name", "Geeks for Geeks");
                     
                    // put description
                    object.put("description", "Test payment");
                     
                    // to set theme color
                    object.put("theme.color", "");
                      
                    // put the currency
                    object.put("currency", "INR");
                      
                    // put amount
                    object.put("amount", amount);
                      
                    // put mobile number
                    object.put("prefill.contact", "9284064503");
                      
                    // put email
                    object.put("prefill.email", "chaitanyamunje@gmail.com");
                      
                    // open razorpay to checkout activity
                    checkout.open(MainActivity.this, object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
  
    @Override
    public void onPaymentSuccess(String s) {
        // this method is called on payment success.
        Toast.makeText(this, "Payment is successful : " + s, Toast.LENGTH_SHORT).show();
    }
  
    @Override
    public void onPaymentError(int i, String s) {
        // on payment failed.
        Toast.makeText(this, "Payment Failed due to error : " + s, Toast.LENGTH_SHORT).show();
    }
}


步骤4:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML格式



  
    
    
      
    
    

步骤5:生成用于使用Razorpay的API密钥

在Google中浏览Razorpay网站,或者您可以单击此处的链接。单击此链接后,您只需使用电子邮件和密码进行注册,然后添加一些基本信息,例如您的电话号码。

在设置屏幕内,单击创建新密钥选项,将生成密钥。我们将在我们的应用程序中使用密钥ID来测试Razor的付款。密钥ID将以rzp_test开头

步骤6:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.razorpay.Checkout;
import com.razorpay.PaymentResultListener;
  
import org.json.JSONException;
import org.json.JSONObject;
  
public class MainActivity extends AppCompatActivity implements PaymentResultListener {
  
    // variables for our 
    // edit text and button.
    private EditText amountEdt;
    private Button payBtn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing all our variables.
        amountEdt = findViewById(R.id.idEdtAmount);
        payBtn = findViewById(R.id.idBtnPay);
          
        // adding on click listener to our button.
        payBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // on below line we are getting 
                // amount that is entered by user.
                String samount = amountEdt.getText().toString();
                  
                // rounding off the amount.
                int amount = Math.round(Float.parseFloat(samount) * 100);
                  
                // initialize Razorpay account.
                Checkout checkout = new Checkout();
                  
                // set your id as below
                checkout.setKeyID("Enter your key id here");
                  
                // set image
                checkout.setImage(R.drawable.gfgimage);
                  
                // initialize json object
                JSONObject object = new JSONObject();
                try {
                    // to put name
                    object.put("name", "Geeks for Geeks");
                     
                    // put description
                    object.put("description", "Test payment");
                     
                    // to set theme color
                    object.put("theme.color", "");
                      
                    // put the currency
                    object.put("currency", "INR");
                      
                    // put amount
                    object.put("amount", amount);
                      
                    // put mobile number
                    object.put("prefill.contact", "9284064503");
                      
                    // put email
                    object.put("prefill.email", "chaitanyamunje@gmail.com");
                      
                    // open razorpay to checkout activity
                    checkout.open(MainActivity.this, object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
  
    @Override
    public void onPaymentSuccess(String s) {
        // this method is called on payment success.
        Toast.makeText(this, "Payment is successful : " + s, Toast.LENGTH_SHORT).show();
    }
  
    @Override
    public void onPaymentError(int i, String s) {
        // on payment failed.
        Toast.makeText(this, "Payment Failed due to error : " + s, Toast.LENGTH_SHORT).show();
    }
}

现在运行您的应用程序,并查看该应用程序的输出。

输出:

由于我们正在使用测试凭据,因此我们的付款将无法完成。为了使您的付款生效,您必须使您的应用程序在Razorpay控制台中生效并生成一个新密钥。

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!