Step 1: 添加以下代码到您的应用程序的 build.gradle 文件中
implementation 'com.razorpay:checkout:1.5.14'
Step 2: 在您的AndroidManifest.xml文件中添加以下权限。
Step 3: 添加以下代码到您的Activity
import com.razorpay.Checkout; import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.设置按钮的Click事件
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
//2.初始化Razorpay Checkout
Checkout.preload(getApplicationContext());
}
@Override
public void onClick(View v) {
//3.创建订单
try {
JSONObject options = new JSONObject();
options.put("name", "宝贝的商品");
options.put("description", "订单的拼音");
options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
options.put("currency", "INR");
options.put("amount", "500");
JSONObject preFill = new JSONObject();
preFill.put("email", "[email protected]");
preFill.put("contact", "9876543210");
options.put("prefill", preFill);
Checkout.open(this, options);
} catch(Exception e) {
Log.e("PAYMENT", "Error in starting Razorpay Checkout", e);
}
}
//4.处理Razorpay的回调
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//在onActivityResult方法中调用以下方法
if (requestCode == Checkout.RAZORPAY_PAYMENT_REQUEST_CODE) {
RazorpayPaymentResultResult result = data.getParcelableExtra(Checkout.EXTRA_RESULT);
String status = result.getStatus();
if (status.equals("成功")) {
Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show