Makai Studio
Imi ‘Ike - Seeker Of Knowledge
Imi-?ike

ASP.Net and Paypal buy now button: Offer discount at checkout in code behind

November 19, 2009 08:17 by jdelpay

This is a very simple way to offer a discount at chckout for customers. It is more secure than the javascript way of doing it because the code is “hidding”. Following my previous tutorial called: “ASP.Net: adding a Paypal Buy Now in code behind.

Solution
On your default.aspx page add a button from the toolbox. Then under the button add a textbox with an id of couponCodeTXT.
Double click the button you just added to open the default.aspx.cs page (code Behind)
Add and customize the code below.

 

   1: using System;
   2: using System.Collections;
   3: using System.Configuration;
   4: using System.Data;
   5: using System.Linq;
   6: using System.Web;
   7: using System.Web.Security;
   8: using System.Web.UI;
   9: using System.Web.UI.HtmlControls;
  10: using System.Web.UI.WebControls;
  11: using System.Web.UI.WebControls.WebParts;
  12: using System.Xml.Linq;
  13:  
  14: public partial class cc_Default : System.Web.UI.Page
  15:  
  16: {
  17:  
  18: protected void Page_Load(object sender, EventArgs e)
  19:  
  20:     {         
  21:  
  22:    }   
  23:  
  24:  protected void Button1_Click(object sender, EventArgs e) 
  25:  
  26:    {    
  27:  
  28:    const string Server_URL = "https://www.paypal.com/cgi-bin/webscr?";
  29:    const string return_URL = "http://www.macarteverte.com/default.aspx";
  30:    const string cancelreturn_URL ="http://www.macarteverte.com/fail.aspx";
  31:    string cmd = "_xclick";
  32:    string business ="PCZNNK7K5A6MS";
  33:  
  34:    string item_name = "Loterie pour la carte verte";
  35:    double baseamt = 49.00;
  36:    int add = 1;        
  37:  
  38: //Simple way to add 10% discount coupon code
  39:  
  40:        string coupon = "mcv1756"; 
  41:        string couponCode = couponCodeTXT.Text; 
  42:        double amount;
  43:  
  44:        if (couponCode == coupon)
  45:  
  46:         {   
  47:  
  48:          amount = 49.00 - (49.00 * 10.00) / 100.00;
  49:  
  50:     } 
  51:  
  52:        else
  53:  
  54:        {
  55:  
  56:     amount = 49.00;
  57:  
  58:     } 
  59:  
  60:        //End coupon
  61:  
  62:        double shipping = 0.00 ;
  63:        double handling = 0.00;
  64:        int no_shipping = 1;
  65:        int no_note= 1;
  66:        string currency_code = "EUR";
  67:        string lc = "FR";
  68:        string bn = "PP-BuyNowBF";
  69:        string basedes = "ILCV 49.00" ;
  70:        string custom = User.Identity.Name;
  71:        string redirect="";
  72:        redirect+= Server_URL;
  73:        redirect += "cmd=" + cmd;
  74:        redirect += "&business=" + business;
  75:        redirect += "&item_name=" + item_name; 
  76:        redirect += "&baseamt=" + baseamt;
  77:        redirect += "&add=" + add; 
  78:        redirect += "&amount=" + amount;
  79:        redirect += "&shipping=" + shipping;
  80:        redirect += "&handling=" + handling;
  81:        redirect += "&no_shipping=" + no_shipping;
  82:        redirect += "&no_note=" + no_note;
  83:        redirect += "&currency_code=" + currency_code;
  84:        redirect += "&lc=" + lc;
  85:        redirect += "&bn=" + bn;
  86:        redirect += "&basedes=" + basedes;
  87:        redirect += "&return=" + return_URL;
  88:        redirect += "&cancel_return" + cancelreturn_URL;
  89:  
  90:        //Redirect to the payment page 
  91:  
  92:        Response.Redirect(redirect); 
  93:    }
  94: }
  95:  
  96:  

Visit:Makai Studio

AddThis Social Bookmark Button

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

ASP. Net : Adding a Paypal Buy Now in code behind

August 31, 2009 03:58 by jdelpay

Problem:

You can't have HTML form tags inside aspx form tags. The problem is that ASP.NET pages are forms themselves, surrounded by a single <form Runat="Server"> tag that automatically posts back to itself. Therefore, you cannot embed an HTML form inside this server form.
Shown below is typical code for a PayPal "BuyNow" button.

 

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="dradams@dradamsweb.com">
<input type="hidden" name="item_name" value="ASP.NET 2.0 Tutorial">
<input type="hidden" name="item_number" value="WDS03">
<input type="hidden" name="amount" value="52.00">
<input type="hidden" name="return" 
  value="http://www.dradamsweb.com/default.aspx">
<input type="hidden" name="cancel_return" 
  value="http://www.dradamsweb.com/default.aspx">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" border="0" name="submit"
  src="https://www.paypal.com/en_US/i/btn/x-click-but01.gif" 
  alt="Make payments with PayPal - it's fast, free and secure!">
</form>


Solution

On your default.aspx page add a button from the toolbox
Double click the button you just added to open the default.aspx.cs page (code Behind)
Add and customize the code below

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; 


public partial class cc_Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
const string Server_URL = "https://www.paypal.com/cgi-bin/webscr?";
const string return_URL = "http://www.PageWhenOk/default.aspx";
const string cancelreturn_URL = "http://www.PageWhenCancel.com/cc.fail.aspx"; 


string cmd = "_xclick";
string business ="your business email";
string item_name = "Name of the item";
double baseamt = 49.00;//amount
int add = 1;
double amount = 49.00 ;
double shipping = 0.00 ;
double handling = 0.00;
int no_shipping = 1;
int no_note= 1;
string currency_code = "EUR"; //Replace with country Currency code
string lc = "FR";
string bn = "PP-BuyNowBF";
string basedes = "ILCV 49.00" ;

string redirect="";
redirect+= Server_URL;
redirect += "cmd=" + cmd;
redirect += "&business=" + business;
redirect += "&item_name=" + item_name;
redirect += "&baseamt=" + baseamt;
redirect += "&add=" + add;
redirect += "&amount=" + amount;
redirect += "&shipping=" + shipping;
redirect += "&handling=" + handling;
redirect += "&no_shipping=" + no_shipping;
redirect += "&no_note=" + no_note;
redirect += "&currency_code=" + currency_code;
redirect += "&lc=" + lc;
redirect += "&bn=" + bn;
redirect += "&basedes=" + basedes;
redirect += "&return=" + return_URL;
redirect += "&cancel_return" + cancelreturn_URL;
//Redirect to the payment page
Response.Redirect(redirect);

}
}



Voila!


Visit:Makai Studio

AddThis Social Bookmark Button

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Using Arrays - C# Tutorial and Sample - Part 1

February 8, 2008 19:20 by jdelpay

An Array is an unordered sequence of elements. All the elements in an array have the same type, followed by a pair of square brackets, followed by the variable name.
   

int[] pinNumbers; //personal identification numbers


note: C and C++ programmers should note that the size of the array is not part of thedeclaration.


Arrays are reference type
The size of an array does not have to be a constant, it can be calculated at run time.

int size = int.Parse(Console.ReadLine());
    int[] pinNumbers = new int[size];


Its possible to create arrays of size 0. Useful in situations where the size is determined dynamically. Array with size 0 is not a null array.


Initializing Array Variable
i.e Initializing pinNumbers to an array of 4 int with the following values: 9,3,7,2

 

int[] pinNumber = new int[4] {9,3,7,2};

// the number in the curly bracket must match the size of the array


the value within the curly brace do not have to be constants.
   

Random r = new Random();

int[] pinNumber = new int[4]{ r.next() %10, r.next() %10,

r.next() %10, r.next() %10};


note: The system.Random class is a pseudo -random number generator. The Next method returns a nonnegative random number.


When initializing an array, you can omit the 'new' expression and the size of the array:

int[] pinNumbers {9,7,3,2};

   
Accessing Individual Array Elements

  To access an individual array element you must provide an index indicating which element you require.

int myNumber; 
 
    myNumber =  pinnumbers[3] ; 
 

you can also change the contents of an array by assigning a value to an indexed element:

myNumber = 167; 
 
pinnumbers[2] = myNumber; 
 

the initial element in an array is indexed 0 (zero) and not 1.

All array elements access is bounds-checked. when using an integer index that is less than 0 or greater than or equal to the lengh of the array, the compiler throws an indexOutOfRangeException

try 
 
    { 
 
        int[] pinnumbers ={9,3,7,2}; 
 
         Console.WritLine(pinNumber[4]); //error, the 4th element is at index 3 
 
   } 
 
   catch(indexOutOfRangeException ex) 
 
   { 
 
      ... 
 
   }

Visit:Makai Studio

AddThis Social Bookmark Button

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.Net | C#
Actions: E-mail | Permalink | Comments (14) | Comment RSSRSS comment feed