Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pfpayments/csharp-sdk
The C# SDK allows an easy integration of PostFinance Checkout into C# and .net applications.
https://github.com/pfpayments/csharp-sdk
Last synced: about 2 months ago
JSON representation
The C# SDK allows an easy integration of PostFinance Checkout into C# and .net applications.
- Host: GitHub
- URL: https://github.com/pfpayments/csharp-sdk
- Owner: pfpayments
- License: apache-2.0
- Created: 2020-03-31T07:36:19.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-05-14T22:18:25.000Z (8 months ago)
- Last Synced: 2024-11-14T05:14:55.537Z (about 2 months ago)
- Language: C#
- Size: 1.08 MB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PostFinance Checkout C# Library
The PostFinance Checkout C# library wraps around the PostFinance Checkout API. This library facilitates your interaction with various services such as transactions, accounts, and subscriptions.
## Documentation
[PostFinance Checkout Web Service API](https://checkout.postfinance.ch/doc/api/web-service)
## Frameworks supported
- .NET 6.0 or later
- Windows Phone 7.1 (Mango)
## Dependencies
- [RestSharp](https://www.nuget.org/packages/RestSharp) - 110.2.0
- [Json.NET](https://www.nuget.org/packages/Newtonsoft.Json/) - 13.0.3
- [JsonSubTypes](https://www.nuget.org/packages/JsonSubTypes/) - 2.0.1
- [BouncyCastle.Cryptography](https://www.nuget.org/packages/BouncyCastle.Cryptography/) - 2.0.0The DLLs included in the package may not be the latest version. We recommend using [NuGet](https://docs.nuget.org/consume/installing-nuget) to obtain the latest version of the packages:
```
Install-Package RestSharp
Install-Package Newtonsoft.Json
Install-Package JsonSubTypes
Install-Package BouncyCastle.Cryptography
```
## Installation
```
# Package Manager
Install-Package PostFinanceCheckout -Version 7.0.4
# .NET CLI
dotnet add package PostFinanceCheckout --version 7.0.4
# Paket CLI
paket add PostFinanceCheckout --version 7.0.4
# PackageReference```
Then include the DLL (under the `bin` folder) in the C# project, and use the namespaces:
```csharp
using PostFinanceCheckout.Service;
using PostFinanceCheckout.Client;
using PostFinanceCheckout.Model;
```
## Getting Started
Instantiate a configuration like so:
```csharp
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:
```csharp
new Configuration(this.applicationUserID, this.authenticationKey, new RestClientOptions()
{
Proxy = new WebProxy("http://example.com")
});
```Timeout is customizable separately via the ```Configuration.TimeOut``` property.
```csharp
using System;
using System.Configuration;
using System.Collections.Generic;using NUnit.Framework;
using PostFinanceCheckout.Model;
using PostFinanceCheckout.Service;
using PostFinanceCheckout.Client;namespace PostFinanceCheckout.Test
{
///
/// Class for testing TransactionService.
///
[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;///
/// Setup before each unit test.
///
[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 = "[email protected]";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() { 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() { 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);
}}
///
/// Test transaction creation.
///
[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"));
}
}
}
```
### Integrating Webhook Payload Signing Mechanism into webhook callback handlerThe HTTP request which is sent for a state change of an entity now includes an additional field `state`, which provides information about the update of the monitored entity's state. This enhancement is a result of the implementation of our webhook encryption mechanism.
Payload field `state` provides direct information about the state update of the entity, making additional API calls to retrieve the entity state redundant.
#### ⚠️ Warning: Generic Pseudocode
> **The provided pseudocode is intentionally generic and serves to illustrate the process of enhancing your API to leverage webhook payload signing. It is not a complete implementation.**
>
> Please ensure that you adapt and extend this code to meet the specific needs of your application, including appropriate security measures and error handling.
For a detailed webhook payload signing mechanism understanding we highly recommend referring to our comprehensive
[Webhook Payload Signing Documentation](https://checkout.postfinance.ch/doc/webhooks#_webhook_payload_signing_mechanism).```csharp
...
[HttpPost("callback")]
public IActionResult HandleWebhook([FromBody] string requestPayload)
{
var signature = Request.Headers["x-signature"];if (string.IsNullOrEmpty(signature))
{
// Make additional API call to retrieve the entity state
// ...
}
else
{
if (webhookEncryptionService().isContentValid(signature, requestPayload))
{
// Parse requestPayload to extract 'state' value
// Process entity's state change
// ...
}
}// Process the received webhook data
// ...
}
...
```## License
Please see the [license file](https://github.com/pfpayments/csharp-sdk/blob/master/LICENSE) for more information.