This project is an example of how to start an integration with .Net and Checkout SDK
This project uses Microsoft.AspNetCore.App and net6.0 and NuGetPackages as the package manager for all the requirements that a project needs.
To remain comply with PCI regulations, you need to protect your card numbers, fortunately Checkout have Frames Framework that helps you tokenize the payment card, this wrapper includes method submitCard(). In the below example, we call this when the "Pay Now" button is clicked.
<form id="payment-form" method="POST">
<div class="one-liner">
<div class="card-frame"></div>
<button id="pay-button" disabled>
PAY GBP 24.99
</button>
</div>
<p class="error-message"></p>
<p class="payment-message"></p>
</form>
<script src="https://cdn.checkout.com/js/framesv2.min.js"></script>Then we intercept the event and let Frames tokenize the card
form.addEventListener('submit', function (event) {
event.preventDefault();
Frames.submitCard();
});Make sure to provide your correct Public Key in script.js, which is the key that authorizes to you the access to Checkout API's
Frames.init('pk_sbox_XXX');Once that Frames validates and returns the tokenized token, then you can add another event to handle the request to the backed. If you have any questions regarding the Frames events you can visit the documentation page
On the backed you need to initiate the SDK with the proper Secret Key as follows in appsettings.json:
"Checkout": {
"SecretKey": "sk_sbox_XXX",
"Environment": "Sandbox",
"PlatformType": "Default"
}If you have any questions regarding SDK usage, please refer to SDK landing page
Then you need to build your request, in this case is a payment request with token source and then just call the the SDK function to request a payment
try
{
PaymentRequest request = new()
{
Source = new RequestTokenSource { Token = data.Token },
Amount = 2499,
Currency = Currency.GBP,
ProcessingChannelId = "pc_XXX"
};
var response = await _checkoutApi.PaymentsClient().RequestPayment(request);
return response.Body;
}
catch (CheckoutApiException e)
{
IDictionary<string, object> errorDetails = e.ErrorDetails;
_log.LogError("Error while processing request = {error}", errorDetails["error_codes"]);
return BadRequest();
}
}And that's it! Your payment has been processed.
- Restore the project dependencies
dotnet restore- Build the project
dotnet build- Run the project
dotnet run