Artem's blog

Mainly .NET (C#, ASP.NET) and my projects

The technical blog is shutdown

CliZ Ware Blog was first published in Nov 2010, although CliZ Ware was actually launched in June 2009. This blog was my primary blog that I used to publish articles with focus on technology news and tips. The blog underwent great changed, for example, the theme was changed several times and also the quality of each article continuously improved, as did my English. I was 14 years when I wrote my first thread and my last thread was written at the age of 17.

Now, I have three other blogs to maintain, that is Mathos Project, Serial Key Manager, and this blog. As you can see, having four blogs can be both time consuming and difficult, but that is not the reason why I decided today to shutdown the blog. The reason is because I thought it would take very much time to improve the layout and the focus of the blog. As I progressed, I learned to keep things in order, and so every blog I maintain at this point is very focused on a specific topic. This blog you are reading right now, however, has a primary emphasis on technology and things I feel to write about. Both the blog I shutdown today and this blog have a common denominator – technology and tips. So, I decided to merge all posts from the old blog to my new blog.

If you came to this page from an external link, I would recommend you to search through this website, because all posts that were in my old blog are copied to this blog.

Please feel free to tell me what you think about it here.

A simple way to integrate PayPal into ASP.NET MVC 4

This week I was working on PayPal integration in Serial Key Manager(http://serialkeymanager.com/). Some time I spent trying to understand how this can be accomplished using a server language, ASP.NET, since I have not been able to find a clear tutorial that would be easy to follow, So, in this guide I am going to outline how the entire process works and provide you with working examples!

1. Setting up the development tools

The first thing you need is to create a PayPal developer account so that you can access this page https://developer.paypal.com/webapps/developer/applications/myapps. When you press on Sandbox accounts to the left of the page, you will have the option to create two types of accounts: Personal or Business

  • Personal account will be used when you buy your product
  • Business account is the account that will receive the payment from the Personal account.

Depending on how much your product will cost, you will have to insert an appropriate amount of money into the Personal account. I think you can have as much as you like since it is only for testing purposes.

2. Creating the button

Now, you will have to create two Views in a Controller, one where you have the button and one where you have the validation mechanism. Let’s start with the button! Put the code below into any View. My View name is IPN in the User controller.

<!-- When you are done with all testing and want to change to the real PayPal, use following instead-->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<!-- end of Real PayPal example-->

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
	<fieldset>
		<input class="full-width" type="hidden" name="business" value="<!--enter the Business account email here-->">
		<input type="hidden" name="cmd" value="_xclick">
		<input type="hidden" name="item_name" value="The unlimited music download subscription">
		<input type="hidden" name="amount" value="9">
		<input type="hidden" name="no_shipping" value="1">
		<input type=hidden name=RETURNURL
			   value="http://example.com/User/IPN">
		<input type="hidden" name="return" value="http://example.com/User/IPN">
		<input type="hidden" name="notify_url" value="http://example.com/User/IPN">

		<button type="submit">Order now!</button>
	</fieldset>
</form>

3. Processing the information returned by PayPal

The example code is based on this code. Please take a look at it to see how it is done with several variables.

Now you need to create a new View in the controller, called IPN. Insert the following code:

public ActionResult IPN()
{

    var order = new Order(); // this is something I have defined in order to save the order in the database

    // Receive IPN request from PayPal and parse all the variables returned
    var formVals = new Dictionary<string, string>();
    formVals.Add("cmd", "_notify-synch"); //notify-synch_notify-validate
    formVals.Add("at", "this is a long token found in Buyers account"); // this has to be adjusted
    formVals.Add("tx", Request["tx"]);

    // if you want to use the PayPal sandbox change this from false to true
    string response = GetPayPalResponse(formVals, false);

    if (response.Contains("SUCCESS"))
    {
        string transactionID = GetPDTValue(response, "txn_id"); // txn_id //d
        string sAmountPaid = GetPDTValue(response,"mc_gross"); // d
        string deviceID = GetPDTValue(response, "custom"); // d
        string payerEmail = GetPDTValue(response,"payer_email"); // d
        string Item = GetPDTValue(response,"item_name");

        //validate the order
        Decimal amountPaid = 0;
        Decimal.TryParse(sAmountPaid, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out amountPaid);

        if (amountPaid == 9 )  // you might want to have a bigger than or equal to sign here!
        {
            if (orders.Count(d => d.PayPalOrderRef == transactionID) < 1)
            {
				//if the transactionID is not found in the database, add it
				//then, add the additional features to the user account
            }
            else
            {
				//if we are here, the user must have already used the transaction ID for an account
				//you might want to show the details of the order, but do not upgrade it!
            }
            // take the information returned and store this into a subscription table
            // this is where you would update your database with the details of the tran

            //return View();

        }
        else
        {
            // let fail - this is the IPN so there is no viewer
            // you may want to log something here
            order.Comments = "User did not pay the right ammount.";

			// since the user did not pay the right amount, we still want to log that for future reference.

            _db.Orders.Add(order); // order is your new Order
            _db.SaveChanges();
        }

    }
    else
    {
        //error
    }
    return View();
}

string GetPayPalResponse(Dictionary<string, string> formVals, bool useSandbox)
{

    string paypalUrl = useSandbox ? "https://www.sandbox.paypal.com/cgi-bin/webscr"
        : "https://www.paypal.com/cgi-bin/webscr";

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(paypalUrl);

    // Set values for the request back
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";

    byte[] param = Request.BinaryRead(Request.ContentLength);
    string strRequest = Encoding.ASCII.GetString(param);

    StringBuilder sb = new StringBuilder();
    sb.Append(strRequest);

    foreach (string key in formVals.Keys)
    {
        sb.AppendFormat("&{0}={1}", key, formVals[key]);
    }
    strRequest += sb.ToString();
    req.ContentLength = strRequest.Length;

    //for proxy
    //WebProxy proxy = new WebProxy(new Uri("http://urlort#");
    //req.Proxy = proxy;
    //Send the request to PayPal and get the response
    string response = "";
    using (StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
    {

        streamOut.Write(strRequest);
        streamOut.Close();
        using (StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()))
        {
            response = streamIn.ReadToEnd();
        }
    }

    return response;
}
string GetPDTValue(string pdt, string key)
{

    string[] keys = pdt.Split('\n');
    string thisVal = "";
    string thisKey = "";
    foreach (string s in keys)
    {
        string[] bits = s.Split('=');
        if (bits.Length > 1)
        {
            thisVal = bits[1];
            thisKey = bits[0];
            if (thisKey.Equals(key, StringComparison.InvariantCultureIgnoreCase))
                break;
        }
    }
    return thisVal;

}

PayPal provides you with many variables, so I suggest that you take a look at how the response looks like. When you’ve found a value that you would like to save into the database, use the GetPDTValue function to retrieve that value.

Please note down the Activation token found in the Buyers account in Profile>My Selling Tools>Website preferences> Update. Then, enable auto return and payment data transfer. There, you will also find the Activation token which you should add to:

formVals.Add("at", "this is a long token found in Buyers account");

Serial Key Manager finished in 2-3 days! :)

From SKM’s blog:

Today, Serial Key Manager received some final revisions, and in max 2-3 days, the application will be complete. Here is an overview of what’s done so far:

As you might already know, the main idea behind this application is to make generation of serial keys more efficient, and avoid any duplicates, which might be the case in Software Protector. Since many users nowadays prefer to use their browser, the application is hosted online, which makes it possible to access/create/validate your keys anywhere, anytime.

Both SKGL API and Software Protector are open-source application, and hopefully, this website application will give everyone who likes SKGL/Software Protector an opportunity to make a donation and get something in return, in this case, a Premium user in Serial Key Manager.

You might wonder why there are so few posts about Serial Key Manger at this stage. That’s simply because all time and effort is put into making this web application a quality product.

We look forward to any feedback about Serial Key Manger! 🙂

A new twitter account

From now on, I am going to use twitter as a source of updates! https://twitter.com/artemlos

Evaluate string expressions

Learn more about how to change a lambda expression on runtime: http://mathosproject.com/updates/convert-a-string-expression-into-a-lambda-expression/

Integral approximation

Constructed an integral approximation application: http://mathosproject.com/updates/integral-approximation-tool/

A new video about HTML

Want to learn HTML from the beginning? See the video below!

Complex numbers: absolute value and conjugates

An interesting fact can be noted down that is similar for absolute values and conjugates:

$$ |z| = a^2+b^2 $$

$$ z times z^* = a^2+b^2$$

This means that:

$$|z| =z times z^* $$.

Using simple algebra, we can rearrange these, and later be able to find an absolute value, given complex number and its conjugate, for example.

Computer programming at Katedralskolan, Uppsala

Hi,

You might have seen the poster about Computer programming on the IB board (and other places). Let me introduce you what it is going to be about!

The idea of this project is to create a group of students interested in programming, mathematics, computer science, etc, and allow them to discuss the current technologies, attend lectures about different programming languages (including cool stuff in math), and investigate related topics (and present them in the group).

There is a big benefit for current IB12 to join this group, as it will count as Creativity (to attend the meetings) and Service (when you talk in front of others about something that you like).

I will encourage everyone to practice on presenting different topics in front of others in a group, as it is an important skill in many fields!

I hope this short summary has given you some introduction, however, if you would get any question, please always ask me (contact here)!

Hope to see you in the Villa on Thursday (19/9) 5:00 pm! 🙂

Best wishes,

Artem Los (IB11)

The Soviet punched card

In The spirit of modernism in the computer world, I wrote about a card that was used before to store information (mainly programs). Below, the actual card:

DSC00437-1

Depending on which number is removed in a column, different letters can be obtained. These letters are also printed at the top, so that it is easier to read the information. These cards were later combined in a pile, in order to produce a program. The corner is removed in order to be able to identify if it is misplaced easily.