Q. How do I take one-time, variable-amount, variable-reason web site payment using PayPal IPN, ASP.Net and C#?
A. Build a StringBuilder URL request and redirect the Response to PayPal.
This answer is the “get” method, because we’re going to redirect the response to PayPal using a query string.
Payment Form: make_payment.aspx
Create a web form that collects user input for:
Anything else you collect on this form should be saved to the PayPal custom variable as demonstrated below. This data might be the customer’s name, address, email and company name.
protected void btnPay_Click(object sender, EventArgs e)
{ if (!Page.IsValid) { return;}
StringBuilder sbPayPal = new StringBuilder();
StringBuilder custom = new StringBuilder();
custom.Append(Server.UrlEncode(firstname.Text));
custom.Append(" ");
custom.Append(Server.UrlEncode(lastname.Text));
custom.Append(", ");
custom.Append(Server.UrlEncode(this.amount.Text));
custom.Append(",");
custom.Append(Server.UrlEncode(companyname.Text));
custom.Append(",");
custom.Append(Server.UrlEncode(email.Text));
custom.Append(",");
custom.Append(Server.UrlEncode(purpose.Text));
custom.Append(",");
if (sandbox.Checked) { sbPayPal.Append("https://www.sandbox.paypal.com/xclick/business=yoursandbox@emailhere.com");
} else {
// if you use this you'll really be paying me
sbPayPal.Append("https://www.paypal.com/xclick/business=caroline@bogartcomputing.com");}
sbPayPal.Append("&item_name=");
sbPayPal.Append(Server.UrlEncode(purpose.Text));
sbPayPal.Append("&quantity=1");
sbPayPal.Append("&custom=");
sbPayPal.Append(custom.ToString());
sbPayPal.Append("&amount=");
sbPayPal.Append(Server.UrlEncode(amount.Text));
sbPayPal.Append("&invoice=");
sbPayPal.Append(System.Guid.NewGuid().ToString());
sbPayPal.Append("&
nonote=1");
sbPayPal.Append("&no_shipping=1");
sbPayPal.Append("&return=");
sbPayPal.Append("http://bogartcomputing.com/payment_thankyou.aspx");
sbPayPal.Append("&
cancel_return=");
sbPayPal.Append("http://bogartcomputing.com/payment_cancel.aspx");
sbPayPal.Append("notify_url=");
sbPayPal.Append("http://bogartcomputing.com/YOUR_IPN_Form.aspx");
Response.Redirect(sbPayPal.ToString());
Response.Write(Server.UrlDecode(sbPayPal.ToString().Replace("&", " ")));
}
Cancellation Form: payment_cancel.aspx
Create a web form that statically informs the user that the payment was cancelled.
Payment Cancellation Your payment was not processed. Click here to make a payment
Thank You Form: payment_thankyou.aspx
Create a web form that accepts a post (Request.Form) to:
- display payment details
- statically inform the customer that he will receive an email when the payment is confirmed
if (Request.Form.Keys.Count>0)
{
try {
lblPaymentDate.Text=FormItem("payment_date");
lblTransactionType.Text= FormItem("txn_type");
lblLastName.Text= FormItem("last_name");
lblResidenceCountry.Text= FormItem("residence_country");
lblItemName.Text= FormItem("item_name");
lblPaymentGross.Text= FormItem("payment_gross");
lblMcCurrency.Text= FormItem("mc_currency");
lblBusiness.Text= FormItem("business");
lblPaymentType.Text= FormItem("payment_type");
lblVerifySign.Text= FormItem("verify_sign");
lblPayerStatus.Text= FormItem("payer_status");
lblTax.Text= FormItem("tax");
lblPayerEmail.Text= FormItem("payer_email");
lblTransactionID.Text= FormItem("txn_id");
lblQuantity.Text= FormItem("quantity");
lblReceiverEmail.Text=FormItem("receiver_email");
lblFirstName.Text= FormItem("first_name");
lblPayerID.Text= FormItem("payer_id");
lblReceiverId.Text= FormItem("receiver_id");
lblMemo.Text= FormItem("memo");
lblItemNumber.Text= FormItem("item_number");
lblPaymentStatus.Text= FormItem("payment_status");
lblPaymentFee.Text= FormItem("payment_fee");
lblMcFee.Text= FormItem("mc_fee");
lblShipping.Text= FormItem("shipping");
lblMcGross.Text= FormItem("mc_gross");
lblCustom.Text= FormItem("custom");
lblPendingReason.Text=FormItem("pending_reason");
lblAddressName.Text=FormItem("address_name");
lblAddressStreet.Text=FormItem("address_street");
lblAddressCity.Text=FormItem("address_city");
lblAddressStatus.Text=FormItem("address_status");
lblAddressState.Text=FormItem("address_state");
lblAddressZip.Text=FormItem("address_zip");
lblAddressCountryCode.Text=FormItem("address_country_code");
lblAddressCountry.Text=FormItem("address_country");
lblAddressCountry.Text=FormItem("address_country");
lblInvoice.Text=FormItem("invoice");
lblMcCurrency.Text=FormItem("mc_currency");
} catch (System.Exception ex) { Response.Write(ex.Message);
} }
Create an IPN (Instant Payment Notification) Form: YOUR_IPN_form.aspx
PayPal will post to this page when it has processed the transaction. You will check that the transaction is verified (the post to the page really came from PayPal) and not a duplicate (you’ve already processed this txn_id).
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Mail;
public partial class payment_notify : System.Web.UI.Page { TextWriter log = null;
mail m = null;
StringBuilder sb = null;
string txn_id, txn_type, payment_date, last_name, residence_country, item_name, payment_gross;
string item_number, payment_status, payment_fee, mc_fee, shipping, mc_gross, custom, pending_reason, address_name, address_street, address_city, address_state, address_status, address_zip, address_country, address_country_code, invoice, quantity, receiver_email, first_name, payer_id, receiver_id, memo, mc_currency, business, payment_type, verify_sign, payer_status, tax, payer_email;
StringBuilder merchantMsg;
StringBuilder customerMsg;
protected void Page_Load(object sender, EventArgs e) { try { // writes local log file CreateLog();
} catch (System.Exception logex) { Response.Write(logex.Message);
return;
} try { // sends me notice that ipn script has been triggered // TriggerEmail();
} catch (System.Exception teex) { Response.Write(teex.Message);
return;
} try { // checks Request.Form data against call to PayPal // checks txn_id against local database if (IsValidIPN() && !IsDupeTxn()) { // send message to merchant, customer that payment string is valid. // Doesn't mean payment is done, it means transaction has processed, perhaps to completion, to cancellation, to denial. CreateMessages();
SendMessages();
} } catch (System.Exception allex) { Response.Write(allex.Message);
return;
} finally { try { log.Flush();
log.Close();
} catch (System.Exception ioex) { Response.Write(ioex.Message);
} } } private void SendDebugMessage(string subject, string body) { try { m = new mail();
m.Body=body;
m.From="caroline@bogartcomputing.com";
m.FromDisplayName="web site paypal ipn debug msg";
m.IsBodyHtml=true;
m.Subject=subject;
m.To="ctbogart@yahoo.com";
m.send();
} catch {} } private void CreateMessages() { // PayPal has posted to this page. Get the Request.Form items. // Build customer and merchant email messages. log.WriteLine("CreateMessages()");
StringBuilder ctemp=new StringBuilder();
StringBuilder mtemp = new StringBuilder();
StringBuilder sb = new StringBuilder();
// merchant txn details invoice =FormItem("invoice");
item_number = FormItem("item_number");
item_name = FormItem("item_name");
quantity = FormItem("quantity");
memo = FormItem("memo");
custom = FormItem("custom");
sb.Append("Invoice: ");
sb.Append(invoice);
sb.Append("
");
sb.Append("Item Number: ");
sb.Append(item_number);
sb.Append("
");
sb.Append("Quantity: ");
sb.Append(quantity);
sb.Append("
");
sb.Append("Memo from buyer: ");
sb.Append(memo);
sb.Append("
");
ctemp.Append(sb.ToString());
mtemp.Append(sb.ToString());
// paypal txn details payment_date =FormItem("payment_date");
verify_sign = FormItem("verify_sign");
payment_status = FormItem("payment_status");
pending_reason =FormItem("pending_reason");
txn_id = FormItem("txn_id");
txn_type = FormItem("txn_type");
business = FormItem("business");
receiver_email = FormItem("receiver_email");
receiver_id = FormItem("receiver_id");
sb = new StringBuilder();
sb.Append("Payment date: ");
sb.Append(payment_date);
sb.Append("
");
ctemp.Append(sb.ToString());
mtemp.Append(sb.ToString());
mtemp.Append("Verify sign: ");
mtemp.Append(verify_sign);
mtemp.Append("
");
mtemp.Append("Payment status: ");
mtemp.Append(payment_status);
mtemp.Append("
");
mtemp.Append("Pending reason: ");
mtemp.Append(pending_reason);
mtemp.Append("
");
mtemp.Append("Transaction ID: ");
mtemp.Append(txn_id);
mtemp.Append("
");
mtemp.Append("Transaction Type: ");
mtemp.Append(txn_type);
mtemp.Append("
");
mtemp.Append("Business (email): ");
mtemp.Append(business);
mtemp.Append("
");
mtemp.Append("Receiver Email: ");
mtemp.Append(receiver_email);
mtemp.Append("
");
mtemp.Append("Receiver ID: ");
mtemp.Append(receiver_id);
mtemp.Append("
");
// paypal money details mc_currency = FormItem("mc_currency");
payment_gross = FormItem("payment_gross");
mc_gross = FormItem("mc_gross");
payment_type = FormItem("payment_type");
tax = FormItem("tax");
shipping = FormItem("shipping");
payment_fee = FormItem("payment_fee");
mc_fee = FormItem("mc_fee");
sb = new StringBuilder();
sb.Append("Currency: ");
sb.Append(mc_currency);
sb.Append("
");
sb.Append("Tax: ");
sb.Append(tax);
sb.Append("
");
sb.Append("Shipping: ");
sb.Append(shipping);
sb.Append("
");
sb.Append("Amount: ");
sb.Append(mc_gross);
sb.Append("
");
sb.Append("Payment Type: ");
sb.Append(payment_type);
sb.Append("
");
ctemp.Append(sb.ToString());
mtemp.Append(sb.ToString());
mtemp.Append("Fee: ");
mtemp.Append(mc_fee);
sb.Append("
");
// customer details last_name = FormItem("last_name");
first_name = FormItem("first_name");
address_name =FormItem("address_name");
address_street =FormItem("address_street");
address_city =FormItem("address_city");
address_status =FormItem("address_status");
address_state =FormItem("address_state");
address_zip =FormItem("address_zip");
address_country = FormItem("address_country");
address_country_code = FormItem("address_country_code");
residence_country = FormItem("residence_country");
payer_id = FormItem("payer_id");
payer_status = FormItem("payer_status");
payer_email = FormItem("payer_email");
sb = new StringBuilder();
sb.Append("Customer: ");
sb.Append(first_name);
sb.Append(" ");
sb.Append(last_name);
sb.Append("
");
sb.Append("Addressed To: ");
sb.Append(address_name);
sb.Append("
");
sb.Append(address_street);
sb.Append("
");
sb.Append(address_city);
sb.Append(", ");
sb.Append(address_state);
sb.Append(" ");
sb.Append(address_zip);
sb.Append("
");
sb.Append(address_country);
sb.Append(" (");
sb.Append(address_country_code);
sb.Append(")");
sb.Append("
");
ctemp.Append(sb.ToString());
mtemp.Append(sb.ToString());
mtemp.Append("Residence Country: ");
mtemp.Append(residence_country);
mtemp.Append("
");
mtemp.Append("Payer ID: ");
mtemp.Append(payer_id);
mtemp.Append("
");
mtemp.Append("Payer Status: ");
mtemp.Append(payer_status);
mtemp.Append("
");
mtemp.Append("Payer Email: ");
mtemp.Append(payer_email);
mtemp.Append("
");
customerMsg=new StringBuilder();
customerMsg.Append("Dear ");
customerMsg.Append(first_name);
customerMsg.Append(" ");
customerMsg.Append(last_name);
customerMsg.Append(",
");
customerMsg.Append("Thank you for your purchase. Your details are below.
");
customerMsg.Append(ctemp.ToString());
merchantMsg=new StringBuilder();
merchantMsg.Append("To: ");
merchantMsg.Append(business);
merchantMsg.Append("
");
merchantMsg.Append("
");
merchantMsg.Append("Custom pass through data: ");
merchantMsg.Append(custom);
merchantMsg.Append("
");
merchantMsg.Append(mtemp.ToString());
log.WriteLine(customerMsg.ToString());
log.WriteLine(merchantMsg.ToString());
} private void SendMessages() { log.WriteLine("SendMessage");
try
{
m = new mail();
m.Body=customerMsg.ToString();
m.From="caroline@bogartcomputing.com";
m.FromDisplayName="Bogart Computing Payment Received";
m.IsBodyHtml=true;
m.Subject="Bogart Computing PayPal Payment Received";
m.To=payer_email;
log.WriteLine("Sending customer email");
log.WriteLine(m.To.ToString());
m.send();
m.Body=merchantMsg.ToString();
m.To=business;
log.WriteLine("sending merchant email");
log.WriteLine(m.To.ToString());
m.send();
} catch (System.Exception smex)
{
log.WriteLine(smex.Message);
}
}
private bool IsDupeTxn()
{ return false;
}
private void CreateLog()
{
try
{
StringBuilder logname=new StringBuilder();
logname.Append(Request.MapPath("."));
logname.Append("\\log_");
logname.Append(DateTime.Now.Ticks.ToString());
logname.Append(".txt");
string filename = logname.ToString();
log = new StreamWriter(filename);
log.WriteLine(DateTime.Now.ToString());
log.WriteLine("Form");
log.Write(Request.Form.ToString());
} catch (System.Exception logex) { try { SendDebugMessage("IPN create log failed", logex.Message);
} catch { } throw logex;
} } private void TriggerEmail() { log.WriteLine("TriggerEmail()");
try { // send me note that ipn was triggered // inform owner that ipn is triggered mail m = new mail();
m.From="caroline@bogartcomputing.com";
m.FromDisplayName="Bogart Computing web site";
m.To="caroline@bogartcomputing.com";
m.Subject="bogart computing ipn triggered";
m.IsBodyHtml=true;
m.Body=Request.Form.ToString().Replace("&", "
");
log.WriteLine("about to send email cb notification start email");
try { m.send();
} catch (System.Exception ex) { log.WriteLine(ex.Message);
throw ex;
} } catch (System.Exception mex) { throw mex;
} } private bool IsValidIPN() { log.WriteLine("IsValidIPN()");
try { // ask PayPal if this is a valid IPN string value, response;
string form = Request.Form.ToString();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr");
req.Method = "POST";
req.ContentType="application/x-www-form-urlencoded";
value=form + "&cmd=_notify-validate";
req.ContentLength=value.Length;
log.WriteLine("create streamout");
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(value);
streamOut.Close();
log.WriteLine("create streamin");
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
// response from PayPal response = streamIn.ReadToEnd();
streamIn.Close();
log.WriteLine(response);
return (response.ToLower()=="verified");
} catch (System.Exception ex) { log.WriteLine(ex.Message);
throw ex;
} } private string FormItem(string formAddress) { string result;
try { result=Server.HtmlEncode(Request.Form[formAddress]);
} catch { result="N/A";
} if (result==null) { result="N/A";
} return result;
} }
0