Search code examples
androidxamarinxamarin.formspaytm

How to call PayTM Android SDK via Xamarin?


I am trying to integrate PayTM in a Xamarin app using example for android given in this link. I have downloaded the appinvokesdk-1.2.aar from the PayTM maven repository and have generated the necessary native biding using the Xamarin Android Binding Library. I have created an interface called IPayTMService which is present in my Xamarin.Forms common project. The code is as follows

public interface IPayTMService
{
    void MakePayment(string orderId,string txnToken, string amount);
    string Message { get; set; }
}

And the code is implemented in the Xamarin.Android project as

public class AndroidPayTMService : IPayTMService
{
    private Activity _activity;

    public AndroidPayTMService(Activity activity)
    {
        _activity = activity;
    }

    public string Message { get; set; }

    public void MakePayment(string orderId, string txnToken, string amount)
    {
        var mid = "<my mid>";
        var callbackurl = $"https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID={orderId}";
        var paytmOrder = new PaytmOrder(orderId, mid, txnToken, amount, callbackurl);

        var txnManager = new TransactionManager(paytmOrder, new PaytmPaymentTransactionCallback(this));
        txnManager.StartTransaction(_activity, 2);
    }
}

In my MainActivity.cs in the Xamarin.Android app. I have written the following code to initiate the paytm service

var app = new App();
app.PayTMService = new AndroidPayTMService(this);

LoadApplication(app);

And from my Xamarin.Forms common code I am calling the service as follows

async void PayTM_Clicked(object sender, EventArgs e)
    {
        var orderId = Guid.NewGuid().ToString();
        var custId = Guid.NewGuid().ToString();

        var token = await payTM.GetToken(orderId, custId, amount.Text);
        var app = (App)Application.Current;
        app.PayTMService.MakePayment(orderId, token, amount.Text);
    }

When I am running this app I am getting error in this line of my androidpaytmservice

txnManager.StartTransaction(_activity, 2);

as shown in the picture bellow

screenshot of the error

My question is two fold

  1. Am I doing the integration right?
  2. Can you please help me with what am I missing here?

Solution

  • Migrate the project to AndroidX . If you are using latest Visual Studio and Xamarin.Forms >4.5.X then its done internally. Besides install AndroidX nugets.

    https://devblogs.microsoft.com/xamarin/androidx-for-xamarin/

    https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/android/androidx-migration