Payments with the .NET library
Learn to create a payment with our .NET library.
.NET is one of the languages/frameworks that can be used to create a payment on your backend application. We recommend this over a custom integration with C#, as using our libraries drastically reduces the time before you can make your first payment.
A payment's journey starts with a single HTTP call to our APIs. Check our API reference for more information on how to create a payment.
Before you start
Before you integrate payments with the .NET library, you must:
- Set up Console:
- Upload your public key to Console.
- Specify a webhook URI on Console.
- Configure request signing for the HTTP requests you will send to the API.
- Ensure that you can generate a token with the scope
payments
.
Check the latest version of our SDKs
To access and learn about the latest version of our .NET SDKs, visit our GitHub.
Configure settings
Add your client_id
, client_secret
and public key
value to appsettings.json
or any other supported configuration provider.
```json
{
"TrueLayer": {
"ClientId": "your id",
"ClientSecret": "your secret",
"UseSandbox": true,
"Payments": {
"SigningKey": {
"KeyId": "85eeb2da-702c-4f4b-bf9a-e98af5fd47c3"
}
}
}
}
```
Initialise TrueLayer.NET
Register the TrueLayer client in Startup.cs
or Program.cs
(.NET 6.0):
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddTrueLayer(configuration, options =>
{
if (options.Payments?.SigningKey != null)
{
// For demo purposes only. Private key should be stored securely
options.Payments.SigningKey.PrivateKey = File.ReadAllText("ec512-private-key.pem");
}
});
}
Alternatively, if you have more complex configuration requirements, you can create a class that implements IConfigureOptions<TrueLayerOptions>
.
Create a payment
Inject ITrueLayerClient
into your classes:
public class MyService
{
private readonly ITrueLayerClient _client;
public MyService(ITrueLayerClient client)
{
_client = client;
}
public async Task<ActionResult> MakePayment()
{
var paymentRequest = new CreatePaymentRequest(
amountInMinor: amount.ToMinorCurrencyUnit(2),
currency: Currencies.GBP,
paymentMethod: new PaymentMethod.BankTransfer(
new Provider.UserSelected
{
Filter = new ProviderFilter
{
ProviderIds = new[] { "mock-payments-gb-redirect" }
}
},
new Beneficiary.ExternalAccount(
"TrueLayer",
"truelayer-dotnet",
new AccountIdentifier.SortCodeAccountNumber("567890", "12345678")
)
),
user: new PaymentUserRequest("Jane Doe", "[email protected]", "0123456789")
);
var apiResponse = await _client.Payments.CreatePayment(
paymentRequest,
idempotencyKey: Guid.NewGuid().ToString()
);
if (!apiResponse.IsSuccessful)
{
return HandleFailure(
apiResponse.StatusCode,
// Includes details of any errors
apiResponse.Problem
)
}
// Pass the ResourceToken to the TrueLayer Web or Mobile SDK
// or, redirect to the TrueLayer Hosted Payment Page
string hostedPaymentPageUrl = _client.Payments.CreateHostedPaymentPageLink(
apiResponse.Data!.Id,
apiResponse.Data!.ResourceToken,
new Uri("https://redirect.yourdomain.com"));
return Redirect(hostedPaymentPageUrl);
}
}
Updated 17 days ago