Trust Payments C# Library
The Trust Payments C# library wraps around the Trust Payments API. This library facilitates your interaction with various services such as transactions, accounts, and subscriptions.
Documentation
Trust Payments Web Service API
Frameworks supported
- .NET 6.0 or later
- Windows Phone 7.1 (Mango)
Dependencies
- RestSharp - 110.2.0
- Json.NET - 13.0.3
- JsonSubTypes - 2.0.1
The DLLs included in the package may not be the latest version. We recommend using NuGet to obtain the latest version of the packages:
Install-Package RestSharp
Install-Package Newtonsoft.Json
Install-Package JsonSubTypes
Installation
# Package Manager
Install-Package TrustPayments -Version 7.0.0
# .NET CLI
dotnet add package TrustPayments --version 7.0.0
# Paket CLI
paket add TrustPayments --version 7.0.0
# PackageReference
<PackageReference Include="TrustPayments" Version="7.0.0" />
Then include the DLL (under the bin
folder) in the C# project, and use the namespaces:
using TrustPayments.Service;
using TrustPayments.Client;
using TrustPayments.Model;
Getting Started
Instantiate a configuration like so:
new Configuration(this.applicationUserID, this.authenticationKey)
Or if you want to customize the RestSharp client that's being used underneath (e.g set the proxy), use the overloaded constructor:
new Configuration(this.applicationUserID, this.authenticationKey, new RestClientOptions()
{
Proxy = new WebProxy("http://example.com")
});
Timeout is customizable separately via the Configuration.TimeOut
property.
using System;
using System.Configuration;
using System.Collections.Generic;
using NUnit.Framework;
using TrustPayments.Model;
using TrustPayments.Service;
using TrustPayments.Client;
namespace TrustPayments.Test
{
/// <summary>
/// Class for testing TransactionService.
/// </summary>
[TestFixture]
public class TransactionPaymentPageServiceTest
{
private TransactionService transactionService;
private TransactionCreate transactionCreate;
private Configuration configuration;
private long spaceId;
private string applicationUserID;
private string authenticationKey;
private ApiResponse<Transaction> transaction;
/// <summary>
/// Setup before each unit test.
/// </summary>
[SetUp]
public void SetUpTest()
{
this.spaceId = 405;
this.authenticationKey = "FKrO76r5VwJtBrqZawBspljbBNOxp5veKQQkOnZxucQ=";
this.applicationUserID = "512";
this.configuration = new Configuration(this.applicationUserID, this.authenticationKey);
this.transactionService = new TransactionService(configuration);
this.CreateTransaction();
}
private void CreateTransaction() {
AddressCreate billingAddress = new AddressCreate();
billingAddress.Salutation = "Ms";
billingAddress.GivenName = "Good";
billingAddress.FamilyName = "Customer";
billingAddress.Gender = Gender.FEMALE;
billingAddress.Country = "CH";
billingAddress.City = "Winterthur";
billingAddress.PostCode = "8400";
billingAddress.DateOfBirth = new DateTime(1988, 4, 19);
billingAddress.OrganizationName = "Test GmbH";
billingAddress.MobilePhoneNumber = "+41791234567";
billingAddress.EmailAddress = "test@example.com";
LineItemCreate lineItem1 = new LineItemCreate(
name: "Item 1",
uniqueId: "unique-id-item-1",
type: LineItemType.PRODUCT,
quantity: 1,
amountIncludingTax: (decimal) 67.47
);
lineItem1.Sku = "item-1";
lineItem1.ShippingRequired = true;
this.transactionCreate = new TransactionCreate(new List<LineItemCreate>() { lineItem1 });
this.transactionCreate.BillingAddress = billingAddress;
this.transactionCreate.ShippingAddress = billingAddress;
this.transactionCreate.CustomerEmailAddress = billingAddress.EmailAddress;
this.transactionCreate.CustomerId = "cutomer-x";
this.transactionCreate.MerchantReference = Guid.NewGuid().ToString();
this.transactionCreate.InvoiceMerchantReference = "order-1";
this.transactionCreate.SuccessUrl = "http://localhost/success?orderId=1";
this.transactionCreate.FailedUrl = "http://localhost/failed?orderId=1";
this.transactionCreate.ShippingMethod = "Test Shipping";
this.transactionCreate.ChargeRetryEnabled = false;
this.transactionCreate.AllowedPaymentMethodConfigurations = new List<long?>() { 8656L };
this.transactionCreate.Language = "en-US";
this.transactionCreate.Currency = "CHF";
try
{
this.transaction = this.transactionService.CreateWithHttpInfo(
this.spaceId,
this.transactionCreate
);
} catch (ApiException e){
Assert.Fail("Failed to create transaction. Reason: " + e.Message + " Details: " + e.ErrorContent);
}
}
/// <summary>
/// Test transaction creation.
/// </summary>
[Test]
public void TestPaymentPageUrl() {
// If needed configure configure a custom timeout. (Default is 25 seconds)
this.configuration.Timeout = 30;
TransactionPaymentPageService transactionPaymentPageService = new TransactionPaymentPageService(this.configuration);
String paymentPageUrl = null;
try {
paymentPageUrl = transactionPaymentPageService.PaymentPageUrl(this.spaceId, this.transaction.Data.Id);
} catch (ApiException e) {
Console.WriteLine(e.ToString());
}
Console.WriteLine(paymentPageUrl);
Assert.IsTrue(paymentPageUrl.Contains("http"));
}
}
}
License
Please see the license file for more information.