I want to do payment using google pay in xamarin.form.
Google pay always throw error: "Payment Failed. this transaction may be risky. for your safety it can't be completed at this time."
Below is my code for android application:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
int gpay_requestCode = 123;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
MessagingCenter.Subscribe<string>(this, "PayViaGooglePay", (value) =>
{
PayViaGooglePay();
});
}
private void PayViaGooglePay()
{
if (IsGooglePayInstalled())
{
string GOOGLE_PAY_PACKAGE_NAME = "com.google.android.apps.nbu.paisa.user";
int GOOGLE_PAY_REQUEST_CODE = 123;
Uri uri =
new Uri.Builder()
.Scheme("upi")
.Authority("pay")
.AppendQueryParameter("pa", "9725258448@okbizaxis")
.AppendQueryParameter("pn", "Gurukrupa")
.AppendQueryParameter("mc", "5812")
.AppendQueryParameter("tr", "123456789")
.AppendQueryParameter("tn", "test")
.AppendQueryParameter("am", "1")
.AppendQueryParameter("cu", "INR")
.Build();
Intent intent = new Intent(Intent.ActionView);
intent.SetData(uri);
intent.SetPackage(GOOGLE_PAY_PACKAGE_NAME);
StartActivityForResult(intent, GOOGLE_PAY_REQUEST_CODE);
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == gpay_requestCode)
{
if (data != null)
{
string response = data.GetStringExtra("response");
string[] resArray = response.Split("&");
string txnId = resArray[0].Split("=")[1].ToString();
string responseCode = resArray[1].Split("=")[1].ToString();
string status = resArray[2].Split("=")[1].ToString();
string txnRef = resArray[3].Split("=")[1].ToString();
if (status == "SUCCESS")
{
Toast.MakeText(this, "Payment Success", ToastLength.Long).Show();
}
else
{
Toast.MakeText(this, "Payment Failed", ToastLength.Long).Show();
}
}
else
{
Toast.MakeText(this, "Payment Failed", ToastLength.Long).Show();
}
}
}
private bool IsGooglePayInstalled()
{
PackageManager pm = this.PackageManager;
bool installed = false;
try
{
pm.GetPackageInfo("com.google.android.apps.nbu.paisa.user", PackageInfoFlags.Activities);
installed = true;
}
catch (PackageManager.NameNotFoundException e)
{
Toast.MakeText(this, "Google Pay Is Not Installed", ToastLength.Long).Show();
}
return installed;
}
This code will open google pay application, but when I do payment it throw error "Payment Failed. this transaction may be risky. for your safety it can't be completed at this time."
Please help me and share me your suggestion.
Thanks in advance.