Click here to Skip to main content
15,881,898 members
Articles / Web Development / ASP.NET
Tip/Trick

ASP.NET and Paypal with Dynamic Ordering Values

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
13 May 2013CPOL2 min read 20K   2.1K   9   2
In this article will show you how to work with ASP.Net and Paypal with Dynamic Ordering Values

Scenarios

Today, in this article I will show you how to integrate Paypal ordering with dynamic values into an Asp.net website using C# and Master page.

You can find many articles on the internet with the same contents posted by someone to guide you how to integrate/implement Paypal into your website. However, almost of those posts only aim to introduce the basic concepts related to paypal.

When starting to research this issue, I also consult many sources on the internet, but I cannot find the contents as my expectations. In addition, I see many people have troubles while working with this issue. Therefore, I have taken the time to write an article to guide all of you How to combine Paypal into Asp.Net website in the best way and easiest to understand.

In this article, instead of aiming to the concepts and theories about Paypal, I will only focus on the development.  

Step 1: Create your Master page.

Step 2: Create a Default page. In this page will display a list of products which you want to sell and integrate Paypal payment.

In this demo, I will provide hardcode data for the products. So, when you use this my demo as your reference then then function to provide dynamic product’s data need to be changed. This is an easy task as I knowJ.

Once your ordering data is ready then you are able to submit your order. Next to step 3 for more detail.

Step 3: When you submit your order in step 2, then the system will redirect you to a page called as PaypalProcess. In this page, we will work with paypal controls and assign the values to them before your order is processing.

Normally, we have controls with name as “cmd, business, item_name, item_number, amount, no_shipping, quantity”. Maybe, it still has other controls, but I think with these items are enough for our purposes.

Step 4: What codes to be needed to have?   

  • Create an entity class namely PaypalEntity  
C#
public class PaypalEntity
{ 
public string Business { get; set; }
public string ItemName { get; set; }
public string ItemNumber { get; set; }
public string Amount { get; set; }
public string NoShipping { get; set; }
public string Quantity { get; set; }
} 
  • Set values to PaypalEntity object in the code behind of Default page, then saving this object to a session variable. 
  • In PaypalProcess page you need to get the session variable and convert it to PaypalEntity object type. After that, you need to assign the session values to Paypal’s controls before the system submitting your order.

Front code:  

ASP.NET
<form id="frmPaypal" method="post" action="https://www.paypal.com/cgi-bin/webscr" runat="server">
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="business" value="<%= this.BusinessValue %>" />
<input type="hidden" name="item_name" value="<%= this.ItemNameValue %>" />
<input type="hidden" name="item_number" value="<%= this.ItemNumberValue %>" />
<input type="hidden" name="amount" value="<%= this.AmountValue %>" />
<input type="hidden" name="no_shipping" value="<%= this.NoShippingValue %>" />
<input type="hidden" name="quantity" value="<%= this.QuantityValue %>" />
</form>  

Behind code

C#
public partial class PaypalProcess : System.Web.UI.Page
{
PaypalEntity pe = new PaypalEntity();        
protected string BusinessValue { get; set; }
protected string ItemNameValue { get; set; }
protected string ItemNumberValue { get; set; }
protected string AmountValue { get; set; }
protected string NoShippingValue { get; set; }
protected string QuantityValue { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
pe = (PaypalEntity)Session["myOrderingEntity"];
this.BusinessValue = pe.Business;
this.ItemNameValue = pe.ItemName;
this.ItemNumberValue = pe.ItemNumber;
this.AmountValue = pe.Amount;
this.NoShippingValue = pe.NoShipping;
this.QuantityValue = pe.Quantity;
}
}
}

Hope that help everyone, who is looking for the help this issue! Smile | :)  

Happy coding.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager
Vietnam Vietnam
Updated LinkedIn: https://vn.linkedin.com/in/chienvh

I am currently working as the position of project manager for a long time. Had to take care a lot of projects at the same times, so I don't have many free times in a day for contributing the articles, tips/tricks on codeproject.
While I was at the previous company sometimes I participated in training courses for new employees, so I have good teaching skills and ability to convey information to others. Meaning in each my post I will try to explain more detail as possible for the junior devs are able to implement/understand what's I have done.

Also, would like to share my responsibilities for current position:

• Coordinate internal resources and third parties/vendors for the flawless execution of projects
• Ensure that all projects are delivered on-time, within scope and within budget
• Assist in the definition of project scope and objectives, involving all relevant stakeholders and ensuring technical feasibility
• Ensure resource availability and allocation
• Develop a detailed project plan to monitor and track progress
• Report and escalate to management as needed
• Perform risk management to minimize project risks
• Establish and maintain relationships with third parties/vendors
• Create and maintain comprehensive project documentation
• Support team members to solve technical issues

Opening and looking forward to finding suitable jobs.

Comments and Discussions

 
QuestionHi, is it possible to put a return payment result page from paypal after payment? Pin
Member 822634021-Mar-14 0:27
Member 822634021-Mar-14 0:27 
QuestionObject reference not set to an instance to an object Pin
Member 1055933430-Jan-14 2:25
Member 1055933430-Jan-14 2:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.