.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.
Installation
Section titled “Installation”dotnet add package SanwoConfiguration
Section titled “Configuration”Register Sanwo in Program.cs:
builder.Services.AddSanwo(options =>{ options.Provider = "paystack"; options.PublicKey = "pk_test_xxxxx"; options.Currency = "NGN";});{ "Sanwo": { "Provider": "paystack", "PublicKey": "pk_test_xxxxx", "Currency": "NGN", "Debug": false }}builder.Services.AddSanwo(builder.Configuration.GetSection("Sanwo"));Register Tag Helpers
Section titled “Register Tag Helpers”In _ViewImports.cshtml:
@addTagHelper *, SanwoAdd the script
Section titled “Add the script”In your layout (_Layout.cshtml), before </body>:
<sanwo-scripts />Checkout button
Section titled “Checkout button”<sanwo-checkout amount="500000" email="customer@example.com" button-text="Pay ₦5,000" />Available attributes
Section titled “Available attributes”| 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 |
Examples
Section titled “Examples”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" />Custom amount form
Section titled “Custom amount form”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" />Custom amount attributes
Section titled “Custom amount attributes”| 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 |
Handling results
Section titled “Handling results”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>Custom provider templates
Section titled “Custom provider templates”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.
Via template URL
Section titled “Via template URL”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" />Programmatic approach
Section titled “Programmatic approach”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>Source code
Section titled “Source code”Sanwohq/dotnet on GitHub.