Home » ASP.Net

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

19. November 2009 by Joel Delpay 0 Comments

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.mysite.com/default.aspx";
  30:    const string cancelreturn_URL ="http://www.mysite.com/fail.aspx";
  31:    string cmd = "_xclick";
  32:    string business ="PVDNNK57K5A6MS";
  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:  

 

Comments are closed