Hi I am developing a xamarin forms application that targets both android and ios. I want to add google pay as my payment option to order items in android.
please help me is there any documentation available.
Hi I am developing a xamarin forms application that targets both android and ios. I want to add google pay as my payment option to order items in android.
please help me is there any documentation available.
welcome to StackOverflow.
Google Pay does not process payments, and as such, it needs to reference your existing processor or gateway to do that. Here is a list of currently supported processors.
As Leo pointed out, you can integrate Google Pay in Xamarin using Xamarin's libs including Google Play Services.
During the integration, make sure to use the right configuration based on the payment processor of your choice. Follow this link to see some examples for different processors.
Hope it helps.
First of all, Google pay must be installed on the device.
Place these three methods in your MainActivity.cs
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 void PayViaGooglePay()
{
if (IsGooglePayInstalled())
{
//Generate random unique number for transaction reference ID
string transId = $"UPI{Guid.NewGuid().ToString().Substring(0, 8)}";
using (var uri = new Android.Net.Uri.Builder()
.Scheme("upi")
.Authority("pay")
.AppendQueryParameter("pa", "yourgooglepayusername@test")//Google pay email
.AppendQueryParameter("pn", "Test Name") //Google pay name
.AppendQueryParameter("pn", "Sending Amount of 1 JOD") // Google pay note
.AppendQueryParameter("mc", "0000") //
.AppendQueryParameter("tr", transId) //transaction reference ID
.AppendQueryParameter("tn", "Pay to Test Name") //Transaction note
.AppendQueryParameter("am", "1") // Amount
.AppendQueryParameter("cu", "JOD") //Currency
.Build())
{
Intent = new Intent(Intent.ActionView);
Intent.SetData(uri);
Intent.SetPackage("com.google.android.apps.nbu.paisa.user");
StartActivityForResult(Intent, gpay_requestCode);
}
}
}
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;
}
Because you can't call any of these methods from your shared project, you need to create a messaging center and call it wherever you wish.
Place the messaging center in your OnCreate method in the same MainActivity.cs
MessagingCenter.Subscribe<string>(this, "PayViaGooglePay", (value) =>
{
PayViaGooglePay();
});
And then you can call it wherever you want, just put this code wherever you need it
MessagingCenter.Send<string>("", "PayViaGooglePay");
I hope this helps :)