Xamarin Android PayPal binding library

I have publish a Nuget package for bind PayPal SDK on Xamarin Android project.
The package is available HERE and the source is on BitBucket.
For complete documentation of the SDK reference to PayPal SDK on Github.

Tutorial

Before you can use PayPal on your mobile application you need a PayPal App Client Id. To obtain a Client Id create an app on PayPal Developer portal.
After that you can create a new Xamarin Android Project.
enter image description here

Note:
At the time of this writing the required minimum target level is 16.

Add Nuget package "Xam.PayPal.Droid".

To use the package you need to set the Java Heap Size to 1G. enter image description here

In you Manifest.xml add:

<service android:name="com.paypal.android.sdk.payments.PayPalService" android:exported="false" />
<activity android:name="com.paypal.android.sdk.payments.PaymentActivity" />

Your manifest must be something like:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="net.markjackmilian.tutorial.xam_paypal_droid_tutorial">
    <uses-sdk android:minSdkVersion="16" />
    <application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name">
        <service android:name="com.paypal.android.sdk.payments.PayPalService" android:exported="false" />
        <activity android:name="com.paypal.android.sdk.payments.PaymentActivity" />
    </application>
</manifest> 

You are ready to go!

Example of activity

    [Activity(Label = "Xam.PayPal.Droid.Tutorial", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
  private PayPalConfiguration config = new PayPalConfiguration()
      .Environment(PayPalConfiguration.EnvironmentSandbox)
      .ClientId("YOUR CLIENT ID HERE");

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    // Get our button from the layout resource,
    // and attach an event to it
    Button button = FindViewById<Button>(Resource.Id.myButton);
    button.Click += this.ButtonOnClick;

    // start paypal service
    var intent = new Intent(this, typeof(PayPalService));
    intent.PutExtra(PayPalService.ExtraPaypalConfiguration, config);
    this.StartService(intent);
}

private void ButtonOnClick(object sender, EventArgs eventArgs)
{
    var payment = new PayPalPayment(new BigDecimal("2.45"), "USD", "the item",
        PayPalPayment.PaymentIntentSale);

    var intent = new Intent(this, typeof(PaymentActivity));
    intent.PutExtra(PayPalService.ExtraPaypalConfiguration, config);
    intent.PutExtra(PaymentActivity.ExtraPayment, payment);

    this.StartActivityForResult(intent, 0);
}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    if (resultCode == Result.Ok)
    {
        var confirm = data.GetParcelableExtra(PaymentActivity.ExtraResultConfirmation);
        if (confirm != null)
        {
            try
            {
                Log.Info("Xam.PayPal.Droid.Tutorial", confirm.ToString());

                // TODO: send 'confirm' to your server for verification.
                // see https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
                // for more details.

            }
            catch (JSONException e)
            {
                Log.Error("Xam.PayPal.Droid.Tutorial", "something wen really wrong here: ", e);
            }
        }
    }
    else if (resultCode == Result.Canceled)
    {
        Log.Info("Xam.PayPal.Droid.Tutorial", "Canceled.");
    }
    else if ((int)resultCode == PaymentActivity.ResultExtrasInvalid)
    {
        Log.Info("Xam.PayPal.Droid.Tutorial", "Invalid Payment or PayPalConfiguration.");
    }
}


protected override void OnDestroy()
{
    this.StopService(new Intent(this, typeof(PayPalService)));
    base.OnDestroy();
}

}

That's easy or not?

Have Fun!

Mark Jack Milian

21-December-2016 @ 21:38

Creativity is contagious. Pass it on. -Einstein

share post :