Git Product home page Git Product logo

lyra-systempay-blazor's Introduction

lyra-systempay-blazor

Embedded payment form Basic exemple with C# and blazor web app

Watch the video

Summary

This solution is based on the payment solution of Lyra Documentation : https://paiement.systempay.fr/doc/fr-FR/rest/V4.0/javascript/spa/

Create a new Blasor-Server project.

1) Key of your store (or demo key)

This source code uses test keys. You can find them here : (https://paiement.systempay.fr/doc/fr-FR/rest/V4.0/api/get_my_keys.html#je-nai-pas-de-compte-actif)

2) Add in _Host.cshtml

Open the /Pages/_Host.cshtml page and add this at the bottom of the body of the page:

<!-- systemPay section  -->
<script type="text/javascript"
    src="https://api.systempay.fr/static/js/krypton-client/V4.0/stable/kr-payment-form.min.js" 
    kr-public-key="73239078:testpublickey_Zr3fXIKKx0mLY9YNBQEan42ano2QsdrLuyb2W54QWmUJQ"
    kr-post-url-success="https://my.site.com">
</script>

<!-- theme and plugins. should be loaded in the HEAD section -->
<link rel="stylesheet" href="https://api.systempay.fr/static/js/krypton-client/V4.0/ext/classic-reset.css">
<script src="https://api.systempay.fr/static/js/krypton-client/V4.0/ext/classic.js"></script>

<script src="~/js/SystemPay.js"></script>

3) Add in appsettings.json

You must convert your test password or prod password to base64. So I converted this 73239078:testpassword_SbEbeOueaMDyg8Rtei1bSaiB5lms9V0ZDjzldGXGAnIwH :

 "SystemPay": {
    "API": "https://api.systempay.fr/api-payment/V4/Charge/CreatePayment",
    "Authorization": "NzMyMzkwNzg6dGVzdHBhc3N3b3JkX1NiRWJlT3VlYU1EeWc4UnRlaTFiU2FpQjVsbXM5VjBaRGp6bGRHWEdBbkl3SA=="
  }

4) Add a model of configuration in your projet:

/configurations/SystemPay.cs :

namespace Blazor_SystemPay.Configurations
{
    public class SystemPay
    {
        public string API { get; set; }
        public string Authorization { get; set; }
    }
}

5) Add a Service and its Interface

/Services/SystemPayService.cs

using System;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.Extensions.Options;
using Blazor_SystemPay.Configurations;

namespace Blazor_SystemPay.Services
{
    public class SystemPayService : ISystemPayService
    {
        
        private HttpClient _client;
        private IOptions<SystemPay> _systemPayConfig;

        public SystemPayService(
                            HttpClient client,
                            IOptions<SystemPay> systemPayConfig)
        {
            _client = client;
            _systemPayConfig = systemPayConfig;
        }

        public async Task<string> GetFormToken(string JSON_Order)
        {
            _client.DefaultRequestHeaders.Add("Accept", "application/json");
            _client.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Basic", _systemPayConfig.Value.Authorization);

            var data = new StringContent(JSON_Order, Encoding.UTF8, "application/json");
            var response = await _client.PostAsync(_systemPayConfig.Value.API, data);

            var res = await response.Content.ReadAsStringAsync();

            var json = System.Text.Json.JsonDocument.Parse(res);
            return json.RootElement.GetProperty("answer").GetProperty("formToken").ToString();
        }
    }
}

and /Services/ISystemPayService.cs

using System;
using System.Threading.Tasks;

namespace Blazor_SystemPay.Services
{
    public interface ISystemPayService
    {
        Task<string> GetFormToken(string JSON_Order);
    }
}

6) Add in Startup.cs :

In ConfigurationServices, add :

services.AddSingleton<HttpClient>();
services.AddScoped<ISystemPayService, SystemPayService>();
services.Configure<SystemPay>(Configuration.GetSection("SystemPay"));

7) Add this javascript file

Add SystemPay.js in www/root/js/

$(document).ready(function () {
    KR.onError(function (event) {
        var code = event.errorCode;
        var message = event.detailedErrorMessage;
        var myMessage = code + ": " + message;

        document.getElementById("customerror").innerText = myMessage;
    });
});

function displayPaymentForm(formToken) {
    // Show the payment form
    document.getElementById('paymentForm').style.display = 'block';

    // Set form token
    KR.setFormToken(formToken);

    // Add listener for submit event
    KR.onSubmit(onPaid);
}


function onPaid(event) {
    if (event.clientAnswer.orderStatus === "PAID") {
        // Remove the payment form
        KR.removeForms();

        document.getElementById('paymentForm').style.display = 'none';

        // Show success message
        document.getElementById("paymentSuccessful").style.display = "block";

    } else {
        // Show error message to the user
        alert("Payment failed !");
    }
}

8) In your razor payment page

<!--Hidden payment form -->
<div id="paymentForm" class="kr-embedded" style="display:none">

    <!-- payment form fields -->
    <div class="kr-pan"></div>
    <div class="kr-expiry"></div>
    <div class="kr-security-code"></div>

    <!-- payment form submit button -->
    <button class="kr-payment-button"></button>

    <!-- error zone -->
    <div class="kr-form-error"></div>
</div>
<div id="kr-answer"></div>
<div id="customerror"></div>

9) and this code

protected override async Task OnAfterRenderAsync(bool firstRender)
  {
      if (firstRender)
      {
          string jsonRequest = @"{
                      ""orderId"": 23720,
                      ""amount"": 20000,
                      ""currency"": ""EUR"",
                      ""customer"": {
                          ""email"": ""[email protected]"",
                          ""billingDetails"":
                          {
                              ""firstName"": ""François"",
                              ""lastName"": ""TestLyra"",
                              ""phoneNumber"" : ""11223344556677""
                          }
                      }
                  }";

          string formToken = await systemPayService.GetFormToken(jsonRequest);
          await jsRuntime.InvokeVoidAsync("displayPaymentForm", formToken);
      }
  }

lyra-systempay-blazor's People

Contributors

titanix avatar tossnet avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.