Help me choose
카테고리 없음

Getting started with C# (struggles)

by hajinny 2020. 12. 19.

Here are the notes that I took while I was learning how to set up ascx file in C#. Ultimately, I wanted to make a web part (sort of like an iframe, but is able to execute a code). This was being researched without me even opening localhost server for Kentico.

 

Ascx file is an ASP.NET file that can act as a web part (component) in a website that runs on ASP.NET

 

The notes to follow are wrong, but I think it's interesting to see how beginner in C# can get stuck on some stuffs. Here we go:

 

<Importing libraries in C# ascx>

Some C# knowledge is necessary to use Kentico libraries.

To reproduce some basic results, follow these steps. Go to CMS folder (in my case, it is C:\inetpub\wwwroot\CMS). Create a file under Web>C#>Web User Control. This generates an ascx extension file, with a line that says <%@ Control Language=”C#” …

I was very stuck on importing a class from a dll file (dll file is a file similar to jar file in java, that contains a set of precompiled cs files, such that their classes are available for use).

Here’s some conditions to importing class in an ascx file

 

One: Make sure the Control Language is in C#

Two: Add the following lines:

 

<%@ Page Language="C#" %>
<%@ Import Namespace="CMS.Ecommerce.Web.UI"%>

//List down html file 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Net.Mail.SmtpClient client = new SmtpClient();
        CMS.Ecommerce.Web.UI.CMSEcommercePage;


    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

</body>
</html>

Voila! You will see that, within the method block of the script block, you can start typing CMS.Ecommerce.Web.UI and see a list of autocompletion popping up.

All in all, it looks like the following. Ignore the error messages, it’s just that I haven’t declared & assigned the variable.

 

The most difficult part of this was not knowing how to have dll file visible by the ascx file, and the fact that when you are importing it, it doesn’t autocomplete the dll file.

The followings are very noteworthy:

1. Make sure your root folder (solution folder) is the first namespace (package) of the dll file you are trying to import. In my case, I was trying to import CMS.Ecommerce.Web.UI, hence the root folder had to be CMS (I think that’s how it works?)

2. Ah actually, one thing I noticed is that the dll file is in CMS > bin, like below:

So I guess the build path by default has bin folder. (or it might already been preconfigured through build script by Kentico, not sure about the details)

All the default libraries like System.Web seem to be already in the build path, but it’s probably worth mentioning them as <%@ Import Namespace = “System.Web”%>

*Edit: above is wrong. See below.

 

 

 

Now into the meaty stuff:

First of all, there’s a full fleshed example of creating a custom payment gateway on Kentico website. The code is also available in the description (didn’t find it for a few hours)

Developing Custom Payment Gateway (kentico.com)

I’m referring to this website:

Oh and WHAT THE I found a gold mine: Creating a custom payment gateway | Kentico 11 Documentation (xperience.io). Here you have an example section if you scroll down in ‘Creating a custom payment gateway’ section. I just didn’t see that until now (well it’s only been a few hours but whatever). Oh my bad I quoted kentico 11. Find kentico 12 here: Creating a custom payment gateway | Kentico 12 Service Pack Documentation (xperience.io)

ASP.NET TextBox - what are the new TextMode options in ASP.NET 4.5 (C#) - Bing video

 

I said above that I was wrong. I was wrong about the build and stuff.

 

Some terms before we get started:

Solution is a collection of projects, each of which can reference each other

Class library is also project

Class Library project can be made by Right clicking on the solution > Add > New Project > change language to C# > Class Library

 

Here we start.

Firstly, open the project CMSApp.csproj (note that you are not simply opening a folder, but you are opening the csproj file. This is important because it already specifies the references to the libraries; whereas above, I had to import it manually).

This should open the following:

It says 2 of 2 projects because I am screenshotting after I’ve done some things (that I’ll explain now), but it should contain only CMSApp in the root folder (Solution ‘CMSApp’) at the moment. 

Now right-click on the solution > Add > New Project > change language to C# > Class Library – this is a type of project. I gave it a name Windcave.Payments.

You should be able to see Windcae.Payments added below the Solution ‘CMSApp’. (note that you won’t see System.Net.Http.Formatting.Extension)

As per instruction of Kentico, we have to add dependency through Nuget. You MUST save the solution first, because otherwise Nuget Manager won’t even open. I saved the solution file in the default installation folder (CMS):

So make sure the solution is saved (note solution is NOT project – it’s not enough to just save the project)

Now, right click on the newly created Class Library project (‘Windcave.Payments’), then press ‘Manage NuGet Packages…’. 

Simply put System.Net.Http.Formatting.Extension and install it.

From this point, it would be relatively easy to follow the instructions on Creating a custom payment gateway | Kentico 12 Service Pack Documentation (xperience.io). Documentations CMSPaymentGatewayForm.PaymentGatewayCustomData Property (kentico.com) can help when you want to parse what each line of code means without writing the actual code.

 

 

Trouble shooting 1: If you don’t see ‘properties’ folder (that contains AssemblyInfo.cs) in the Class Library project, check if you created .NET Framework Class Library or .NET Standard’s. They are different (.NET standard aims to run on more devices, hence lacking in new features – well I’m not really sure anyways).

Create .NET Framework Class Library 

I configured my project’s target framework to be 4.6.1 as it’s the framework that the CMSApp project currently runs on (the one given by Kentico).

Then you will find this Properties folder within the Windcave.Payment Class Library project. You can find AssemblyInfo.cs under Properties folder.

 

 

Note 1: you can find the dll files (class packages) in Lib folder:

Note 2: To save a Solution (collection of projects), press the solution:

Then press control – s (for windows)

 

 

Now it’s time to think of how payment system is integrated.

In the simplest steps (assuming we only want HPP solution through session):

User submits shopping cart and their details

Those details should have type, amount, currency (in the least) – how do you get the shopping cart’s required fund? – not that hard!: 

Do an HTTP POST request to  https://dev.windcave.com/api/v1/sessions/

Like so:

{
    "type": "purchase",
    "amount": "10.00",
    "currency": "NZD",
}

The returned json file can be parsed, and the link that refers to hpp can be returned as a redirect to the user.

Windcave part (IPN?) calls a finish method (PaymentDirectPaymentReply)

 

 

Question: 

ProcessPayment method in CustomGatewayProvider class is presumably the class that does the POST request – but the method body doesn’t seem to have any sort of ‘POST request’

Besides, we have to have a JSON formatted request, whereas the method body simply concatenates the URL parameters?

Also, how would REST API send the result back by calling ProcessDirectPayReply? (customProvider object). Does a helper class make infinite GET request to see if the payment has been successful, and then call ProcessDirectPayReply?