Skip to content

.NET / C#

The Sanwo NuGet package provides ASP.NET Tag Helpers that render Sanwo checkout buttons and custom amount forms. Configure via dependency injection, use in any Razor view.

Terminal window
dotnet add package Sanwo

Register Sanwo in Program.cs:

builder.Services.AddSanwo(options =>
{
options.Provider = "paystack";
options.PublicKey = "pk_test_xxxxx";
options.Currency = "NGN";
});

In _ViewImports.cshtml:

@addTagHelper *, Sanwo

In your layout (_Layout.cshtml), before </body>:

<sanwo-scripts />
<sanwo-checkout
amount="500000"
email="customer@example.com"
button-text="Pay ₦5,000" />
Attribute Type Required Default Description
amount int Yes Amount in minor units (kobo, cents)
email string Yes Customer email
button-text string No “Pay Now” Button label
button-class string No “sanwo-button” CSS class
provider string No Config value Provider name
public-key string No Config value Public key
currency string No Config value Currency code
description string No Payment description
reference string No Auto-generated Transaction reference
first-name string No Customer first name
last-name string No Customer last name
phone string No Customer phone
callback string No JS callback function name
template-url string No Custom provider template URL

Basic:

<sanwo-checkout amount="500000" email="user@example.com" />

With model data:

<sanwo-checkout
amount="@Model.TotalInKobo"
email="@User.Identity.Name"
first-name="@Model.FirstName"
button-text="Complete Purchase"
description="Order #@Model.OrderId" />

Override provider:

<sanwo-checkout
amount="500000"
email="user@example.com"
provider="flutterwave"
public-key="FLWPUBK_TEST-xxxxx" />

Let customers enter their own amount:

<sanwo-custom-amount
email="donor@example.com"
button-text="Donate"
placeholder="How much would you like to give?"
min-amount="500"
max-amount="1000000" />
Attribute Type Required Default Description
button-text string No “Pay Now” Button label
placeholder string No “Enter amount” Input placeholder
email string No If omitted, email input is shown
min-amount int No Minimum amount (major units)
max-amount int No Maximum amount (major units)
provider string No Config value Provider name
currency string No Config value Currency code
callback string No JS callback function name

JavaScript callback:

<script>
function onPaid(result) {
if (result.status === 'successful') {
fetch('/api/payments/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ reference: result.reference })
});
}
}
</script>
<sanwo-checkout
amount="500000"
email="user@example.com"
callback="onPaid" />

DOM events:

<script>
document.addEventListener('sanwo:complete', function(e) {
if (e.detail.status === 'successful') {
window.location.href = '/thank-you?ref=' + e.detail.reference;
}
});
</script>

Use any payment gateway — not just the built-in five — by providing your own provider template. See the custom providers guide for how to write templates.

Host your template HTML file and pass its URL:

<sanwo-checkout
amount="500000"
email="customer@example.com"
provider="custom"
public-key="pk_test_xxxxx"
template-url="https://example.com/my-provider-template.html"
button-text="Pay with My Gateway" />

For full control over provider options, use Sanwo.create() with a custom template:

<sanwo-scripts />
<button id="custom-pay">Pay with My Gateway</button>
<script>
var sanwo = Sanwo.create({
provider: 'custom',
publicKey: 'pk_test_xxxxx',
templateUrl: 'https://example.com/my-provider-template.html',
providerId: 'my-gateway',
});
document.getElementById('custom-pay').addEventListener('click', async function() {
var result = await sanwo.checkout({
amount: 500000,
currency: 'NGN',
customer: { email: 'customer@example.com' },
});
if (result.status === 'successful') {
window.location.href = '/thank-you?ref=' + result.reference;
}
});
</script>

Sanwohq/dotnet on GitHub.