Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# Create Swiss QR-Bill API

using System;
using System.Linq;
using System.Web;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Collections.Generic;
using Newtonsoft.Json;

static void Main(string[] args)
{
	//service and docs
    https://qr.livingtech.ch
	
    // Configuration
    Dictionary myRequestConfiguration = new Dictionary();
    myRequestConfiguration.Add("Account", "CH4431999123000889012");
    myRequestConfiguration.Add("CreditorName", "Muster AG");
    myRequestConfiguration.Add("CreditorAddress1", "Hauptstrasse 1");
    myRequestConfiguration.Add("CreditorAddress2", "8000 Zürich");
    myRequestConfiguration.Add("CreditorCountryCode", "CH");
    myRequestConfiguration.Add("DebtorName", "LivingTech GmbH");
    myRequestConfiguration.Add("DebtorAddress1", "Dörflistrasse 10");
    myRequestConfiguration.Add("DebtorAddress2", "8057 Zürich");
    myRequestConfiguration.Add("DebtorCountryCode", "CH");
    myRequestConfiguration.Add("Amount", "1.50");
    myRequestConfiguration.Add("ReferenceNr", "21000000000313947143000901");
    myRequestConfiguration.Add("UnstructuredMessage", "Mitteilung zur Rechnung");
    myRequestConfiguration.Add("Currency", "CHF");
    myRequestConfiguration.Add("IsQrOnly", "false");
    myRequestConfiguration.Add("Format", "PDF");
    myRequestConfiguration.Add("Language", "DE");

    // Call function to create invoice
    byte[] myByteResult = generateQrInvoice(myRequestConfiguration);

    // Work with binary data
    if(myByteResult != null)
    {
        // ...
    }
}

protected byte[] generateQrInvoice(Dictionary myRequestConfiguration)
{
    // Main configuration
    string myEndpointUrl = "http://qrbillservice.livingtech.ch";
    string myEndpointPath = "/api/qrinvoice/create/";
    string myApiKey = "mySecretApiKey";

    // GET parameters
    string myGetParams = $"?{string.Join("&", myRequestConfiguration.Select(myDict => $"{HttpUtility.UrlEncode(myDict.Key)}={HttpUtility.UrlEncode(myDict.Value)}"))}";

    // HttpClient
    HttpClient myHttpClient = new HttpClient();
    myHttpClient.BaseAddress = new Uri(myEndpointUrl);
    myHttpClient.DefaultRequestHeaders.Add("APIKEY", myApiKey);
    myHttpClient.DefaultRequestHeaders.Accept.Clear();
    myHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    try
    {
        // Perform request
        HttpResponseMessage myResponse = myHttpClient.GetAsync(myEndpointPath + myGetParams).Result;

        // Check status
        if (myResponse.IsSuccessStatusCode)
        {
            // Read and parse JSON
            string myJsonBody = myResponse.Content.ReadAsStringAsync().Result;
            var myJsonObject = Newtonsoft.Json.Linq.JObject.Parse(myJsonBody);

            // Check if error
            if (myJsonObject["isSuccessed"].ToString().ToLower() == "true")
            {
                if (myJsonObject["base64Image"] != null && !String.IsNullOrEmpty(myJsonObject["base64Image"].ToString()))
                {
                    // Convert base64 string to byte array
                    byte[] myByteArray = Convert.FromBase64String(myJsonObject["base64Image"].ToString());

                    // E.g. save file
                    using (FileStream myFileStream = System.IO.File.Create(HttpContext.Current.Server.MapPath("~/App_Data/" + Guid.NewGuid().ToString() + ".pdf")))
                    {
                        myFileStream.Write(myByteArray, 0, myByteArray.Length);
                    }

                    // Return data
                    return myByteArray;
                }
                else
                {
                    throw new Exception("no data provided");
                }
            }
            else
            {
                throw new Exception(myJsonObject["Message"].ToString());
            }
        }
        else
        {
            throw new Exception("status code " + myResponse.StatusCode);
        }
    }
    catch (Exception ex)
    {
        // Handle exception
        Console.WriteLine("Error: " + ex.Message);
        return null;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: Razor switch statement 
Csharp :: c# getdecimal null 
Csharp :: unity prefab button not working 
Csharp :: GetNetworkTime 
Csharp :: c# blazor update state 
Csharp :: how to export xml in linq c# 
Csharp :: NetConnectionDispatch 
Csharp :: startup c# class winform 
Csharp :: calculated field gridview asp.net 
Csharp :: c# enum variable set to nonthing 
Csharp :: .Net Entity Framework Reseed SQL Server Table ID Column 
Csharp :: unity customize hierarchy window 
Csharp :: CS0176 
Csharp :: c# get Full Exception message if InnerException is not NULL 
Csharp :: Filter list contents with predicate (Lambda) 
Csharp :: move position smoth unity 
Csharp :: .net check connection 
Csharp :: c# picturebox zoom 
Csharp :: satisfactory controller support 
Csharp :: c# custom comment tags 
Csharp :: c# check if list is empty 
Csharp :: auto paly a video control in mvc c# 
Csharp :: unity roam, chase, attack states 
Csharp :: how to modigy login page asp.net core 
Csharp :: create entity c# d365 
Csharp :: convert bool to uint in solidity 
Csharp :: params keycord as var name c# 
Csharp :: how to access a dictionary in c# 
Csharp :: entity framework dynamic search 
Csharp :: displaying list in gameobject Unity 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =