Introduce Stripe for .NET MAUI
Steps to integrate Stripe SDK in your .NET MAUI app.

Search for a command to run...
Steps to integrate Stripe SDK in your .NET MAUI app.

In this series, I will introduce my own libraries for .NET MAUI which are built on top of my binding libraries and/or the libraries from the community to help you get started quickly.
It's a long story and a huge effort to make this come true. It's the effort of more than 1 year of working, trying, stopping and resuming. A/ The story and challenges The story I am the original author of Xamarin binding libraries for Mapbox SDKs as...
In the previous post of Introduction of dotnet-binding-util, we already learned what it is about. I will sometimes call it the tool for short. In the post, I will go thorough the core part of the tool, artifact and NuGet metadata, the way in which a ...

You might have created a .NET binding library by yourself by following the guide Binding a Java library from Microsoft docs. In many cases, it’s quite simple and straight forward for small libraries. However, there few drawbacks: 1/ You normally emb...

Collecting bugs and improvements in production from the end users

Today, I successfully upgraded my Chick and Paddy .NET MAUI sample to .NET8. It was a long wait because I failed to upgrade to .NET8 preview releases. Things went well except the entry border which was supposed to be none in .NET6/7 as you might find...
Stripe is a very well known payment service to let your end users pay easily and beautifully for your product/service(s) within your mobile apps and websites.
This article will guide you steps to integrate Stripe SDK into a newly created .NET MAUI app step by step.
.NET 8
Java SDK 17
Visual Studio on Windows or Visual Studio for Mac, alternatively
JetBrains Rider alternatively
Visual Studio Code with .NET MAUI extension
.NET workloads for .NET MAUI
Download the get-started source code
dotnet-maui-stripe-get-started.sln
Shared.dev.cs file by making a copy of Shared.dev.cs.sample|--src
|-- QuickStart.Dotnet.StripeHost
|-- Shared.cs
|-- Shared.dev.cs <------ YOUR_FILE_HERE
partial class ClientHelper
{
public const string API_KEY = "YOUR_API_KEY";
public const string DEFAULT_PUBLISHABLE_KEY = "YOUR_DEFAULT_PUBLISHABLE_KEY";
}
HINTS: You can copy these secrets quickly from the official quick start by Stripe.
cd your-path-to-source-code/src/QuickStart.Dotnet.StripeHost
dotnet run -c debug
ngrok http http://localhost:4242
NOTE: ngrok is a tool that allows us to expose a development endpoint publicly via the network with ease.
Shared.dev.cs with the generated URLpartial class ClientHelper
{
public const string BACKEND_URL= "https://generated-subdomain.ngrok-free.app";
}

NOTE: Follow step by step with all default options
Stripe.MAUI package
Stripe.Maui package and install
NOTE:
Before going next
Close the solution
Remove bin/obj folders of the .NET MAUI project
Reopen the solution
IMPORTANT!!! If you faced any issues of project loading failed, please repeat these steps :D
MauiProgram.cs
NOTE: We will add these files as links to share code across projects.
MauiAppBuilderbuilder
.UseStripe(
QuickStart.Dotnet.Stripe.ClientHelper.DEFAULT_PUBLISHABLE_KEY
);
IPaymentSheet into MainPage.xaml.cspublic partial class MainPage : ContentPage
{
private readonly IPaymentSheet paymentSheet;
public MainPage(IPaymentSheet paymentSheet)
{
InitializeComponent();
this.paymentSheet = paymentSheet;
}
}
NOTE: To get the DI to work, we need to register MainPage as a scoped dependency through MauiAppBuilder.
// In MauiProgram.cs
builder.Services.AddScoped<MainPage>();
5.a/ Override MainPage.xaml file with the below content
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GetStartedWithStripe.MainPage">
<StackLayout
Padding="30,24"
Spacing="25">
<Button
x:Name="PayNowButton"
Text="Pay now"
SemanticProperties.Hint="Pay with Stripe"
Clicked="OnPayNow"
IsEnabled="False"
VerticalOptions="EndAndExpand"
HorizontalOptions="Fill" />
</StackLayout>
</ContentPage>
private string paymentIntentClientSecret;
protected override async void OnAppearing()
{
base.OnAppearing();
// The helper method comes from the downloaded source code
await QuickStart.Dotnet.Stripe.ClientHelper.FetchPaymentIntent()
.ContinueWith(t =>
{
var (ok, msg) = t.Result;
if (!ok)
{
MainThread.BeginInvokeOnMainThread(() => DisplayAlert("API Error", msg, "OK"));
return;
}
paymentIntentClientSecret = msg;
MainThread.BeginInvokeOnMainThread(() => PayNowButton.IsEnabled = true);
})
.ConfigureAwait(false);
}
NOTE:
By default,
Pay nowbutton is disabledIt will only be enabled if we can fetch the client secret successfully from the API
OnPayNow handler private async void OnPayNow(object sender, EventArgs e)
{
var configuration = new Configuration("TuyenTV.dev Co.Ltd.");
var (result, msg) = await paymentSheet.PresentWithPaymentIntentAsync(paymentIntentClientSecret, configuration)
.ContinueWith(t =>
{
if (t.IsCompletedSuccessfully) return (t.Result, null);
return (PaymentSheetResult.Canceled, "Cannot pay for a reason...");
});
switch(result)
{
case PaymentSheetResult.Completed:
await DisplayAlert("Payment Result", "Payment completed successfully!", "OK");
break;
case PaymentSheetResult.Canceled:
if (!string.IsNullOrWhiteSpace(msg))
{
await DisplayAlert("Payment Result", "Payment completed failed!", "Try again");
}
break;
}
Debug.WriteLine("Payment complete.");
}
Till now, you're ready to check out the result on both Android and iOS.
Android: You can try either on a emulator or on a real device by plugging it in directly to your PC
iOS:
If you have an Apple paid developer account, you can plug your iPhone directly to your PC and try out
Otherwise, you will need a MacOS device to run the simulator

I just guided you on how to integrate Stripe SDK into a newly created .NET MAUI project. You can follow these steps to add it to your project(s) easily.
Documents from Stripe are also very helpful, please always take a look to understand a feature you desire to add to your app.
If you want to check out the source code of the binding libraries, you can check it here. If you want to check out the source code of the .NET MAUI library (along with a sample app), you can check it here.
Happy Coding!!!