Create a payment using .NET
Check out the API reference for Payments API v3
Refer to the API reference for further information on creating a payment
Once you have acquired a token to interact with the API, you can make a payment. A payment's journey starts with a single HTTP call to our APIs.
Before you create a payment resource, make sure that you have:
- Set up your public key on Console.
- Set up your webhook URI on Console.
- Taken the necessary steps in your code base to sign the HTTP requests you will send to the API.
- The ability to retrieve a token with the scope
payments
.
.NET is one of the languages/frameworks that can be used to create a payment on your backend application. It is highly recommended over a custom integration with C# as using our libraries will drastically reduce the time required to make your first payment.
For installation and general information about our .NET SDKs, visit our Github.
Before you begin
-
Generate a signing key pair used to sign API requests. Learn how to get signing key pairs.
-
Go to Console > Payments API, and upload your public key.
-
Get the Key Id.
Configure settings
-
Add your Client ID, Secret and Signing Key ID to
appsettings.json
or any other supported configuration provider.{ "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, you can create a class that implements IConfigureOptions<TrueLayerOptions>
if you have more complex configuration requirements.
Create a payment resource
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
{
StatementReference = "Your ref"
},
beneficiary: new Beneficiary.ExternalAccount(
"TrueLayer",
"truelayer-dotnet",
new SchemeIdentifier.SortCodeAccountNumber("567890", "12345678")
),
user: new PaymentUser.NewUser("Jane Doe", email: "[email protected]", phone: "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 PaymentToken to the TrueLayer Web or Mobile SDK
// or, redirect to the TrueLayer Hosted Payment Page
string hostedPaymentPageUrl = apiResponse.Data.Match(
authRequired => _client.Payments.CreateHostedPaymentPageLink(
authRequired.Id,
authRequired.PaymentToken,
new Uri("https://redirect.yourdomain.com")
)
);
return Redirect(hostedPaymentPageUrl);
}
}
Updated 27 days ago