A C# library for integrating Payload.
Download the latest version from GitHub.
Include the Payload folder in your Visual Studio project.
dotnet add package payload-api
Once you've included the Payload C# library in your project,
include the Payload
namespace to get started.
All Payload objects and methods are accessible using the pl
static class.
To authenticate with the Payload API, you'll need a live or test API key. API keys are accessible from within the Payload dashboard.
using Payload;
var pl = new Payload.Session("secret_key_3bW9JMZtPVDOfFNzwRdfE");
Interfacing with the Payload API is done primarily through Payload Objects. Below is an example of
creating a customer using the pl.Customer
object.
// Create a Customer
var customer = await pl.Customer.CreateAsync(new
{
email = "matt.perez@example.com",
name = "Matt Perez"
});
// Create a Payment
var payment = await pl.Payment.CreateAsync(new
{
amount = 100.0,
payment_method = new pl.Card(new
{
card_number = "4242 4242 4242 4242"
})
});
Object attributes are accessible through both dot and bracket notation.
// Dynamic
Console.WriteLine(customer.name);
// Compile-time checking
Console.WriteLine(customer["email"]);
Console.WriteLine(customer.Data.email);
Updating an object is a simple call to the UpdateAsync
object method.
// Updating a customer's email
await customer.UpdateAsync(new { email = "matt.perez@newwork.com" });
Objects can be selected using any of their attributes.
// Select a customer by email
var customers = await pl.Customer
.FilterBy(new { email = "matt.perez@example.com" })
.OneAsync();
Use the pl.Attr
attribute helper interface to write powerful
queries with a little extra syntax sugar.
var payments = await pl.Payment
.FilterBy(
pl.Attr.amount.gt(100),
pl.Attr.amount.lt(200),
pl.Attr.description.contains("Test"),
pl.Attr.created_at.gt(new DateTime(2019,2,1))
)
.AllAsync();
Tests are contained within the PayloadTests/ directory. To run tests enter the command in terminal
API_KEY=your_test_secret_key dotnet test
To get further information on Payload's C# library and API capabilities, visit the unabridged Payload Documentation.