Skip to content

Instantly share code, notes, and snippets.

@craftfortress
Created March 2, 2013 11:53
Show Gist options
  • Save craftfortress/5070671 to your computer and use it in GitHub Desktop.
Save craftfortress/5070671 to your computer and use it in GitHub Desktop.
PayPal ASP.Net C#
// create a new order and redirect to a payment page
protected void checkoutButton_Click(object sender, EventArgs e)
{
DataTable dt = ShoppingCartAccess.CreateOrder();
StringBuilder sb = new StringBuilder();
sb.Append("https://www.sandbox.paypal.com/cgi-bin/webscr?");
sb.Append("cmd=_cart");
sb.Append("&upload=1");
sb.Append("&business=sell_1273576034_biz@globalcrossing.com");
int i = 0;
int b = 1;
string price = "10000";
while (dt.Rows.Count > i)
{
if (!string.IsNullOrEmpty(dt.Rows[i][4].ToString()))
{
decimal a = (decimal)dt.Rows[i][4];
decimal c = decimal.Round(a, 2);
price = c.ToString();
}
sb.Append("&item_name_" + b + "=" + dt.Rows[i][2].ToString());
sb.Append("&amount_" + b + "=" + price);
sb.Append("&quantity_" + b + "=" + dt.Rows[i][3].ToString());
i++;
b++;
}
sb.Append("¤cy_code=" + NailShopConfig.PaypalCurrency);
sb.Append("&return=http://localhost:3500/Paypal2.aspx");
sb.Append("&cancel_return=http://localhost:3500/cancel.aspx");
// Get the total amount
decimal amount = ShoppingCartAccess.getTotalAmount();
// Go to PayPal checkout
string destination = sb.ToString();
Response.Redirect(destination);
}
On success it returns to my paypal2.aspx page It always returns invalid and does not pick up any info on request... what have i done?? and what do I need to do to make this work
string stringPost = Request.Form.ToString(); All request objects are not returning values not sure if I have something set up to course this
// assign posted variables to local variables
Txn_id = Request.Form["txn_id"];
Receiver_email = Request.Form["receiver_email"];
Item_name = Request.Form["item_name"];
Item_number = Request.Form["item_number"];
Quantity = Request.Form["quantity"];
Invoice = Request.Form["invoice"];
Custom = Request.Form["custom"];
Payment_status = Request.Form["payment_status"];
Pending_reason = Request.Form["pending_reason"];
if (Payment_status != "Pending")
{
Pending_reason = " ";
}
Payment_date = Request.Form["payment_date"];
Payment_fee = Request.Form["payment_fee"];
Payment_gross = Request.Form["payment_gross"];
Txn_type = Request.Form["txn_type"];
First_name = Request.Form["first_name"];
Last_name = Request.Form["last_name"];
Address_street = Request.Form["address_street"];
Address_city = Request.Form["address_city"];
Address_state = Request.Form["address_state"];
Address_zip = Request.Form["address_zip"];
Address_country = Request.Form["address_country"];
Address_status = Request.Form["address_status"];
Payer_email = Request.Form["payer_email"];
Payer_status = Request.Form["payer_status"];
Payer_id = Request.Form["payer_id"];
Payment_type = Request.Form["payment_type"];
Notify_version = Request.Form["notify_version"];
Verify_sign = Request.Form["verify_sign"];
// post to paypal and await response : use: "https://www.paypal.com/cgi-bin/webscr" for real;
// use: "http://www.eliteweaver.co.uk/testing/ipntest.php" to test;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr?");
httpWebRequest.Method = "POST";
// length plus 21 because &cmd=_notify-validate is 21 chars long
httpWebRequest.ContentLength = stringPost.Length + 21;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
StreamWriter streamWriter = null;
streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());
stringPost = stringPost + "&cmd=_notify-validate"; As request did not pick up this string isnt working
streamWriter.Write(stringPost);
streamWriter.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
{
response = streamReader.ReadToEnd();
streamReader.Close();
}
// Step 1c: Process the response from PayPal.
if (httpWebResponse.StatusCode != HttpStatusCode.OK)
{
// an error has occurred
MailUsTheOrder("Status Error: " + httpWebResponse.StatusCode);
}
else
{
switch (response)
{
// check for new version of paypal if different send notify email
case "VERIFIED":
if (Notify_version != "1.4")
{
//MailMessage mailObj = new MailMessage();
//mailObj.From = "CJC BodyCare";
//mailObj.To = Receiver_email;
//mailObj.Subject = "Paypal Version Change";
//mailObj.Body = "I see a new version of PayPal Notify IPN Service!!! Go check the PayPal site for updates!" + '\n'
//+ "I currently see version: " + Notify_version;
//mailObj.BodyFormat = MailFormat.Html;
//SmtpMail.Send(mailObj);
}
//****************************************************
// still to do
//******************************************************
// check that Txn_id has not been previously processed
// check that Receiver_email is an email address in your PayPal account
// process payment
//******************************************************
// check that Payment_status=Completed
switch (Payment_status)
{
case "Completed": //The payment has been completed and the funds are successfully in your account balance
//**************************
// Perform steps 2-5 above.
// Continue with automation processing if all steps succeeded.
//**************************
if (Receiver_email == "seller_1273570829_biz@globalcrossing.com")
{
switch (Txn_type)
{
case "web_accept": //The payment was sent by your customer via the Web Accept feature.
case "cart": //This payment was sent by your customer via the Shopping Cart feature
MailUsTheOrder("PROCESS ME: The order was completed successfully.");
break;
case "send_money": //This payment was sent by your customer from the PayPal website, using the "Send Money" tab
MailUsTheOrder("PROCESS ME: Somebody sent us money!");
break;
case "subscr_signup": //This IPN is for a subscription sign-up
MailUsTheOrder("PROCESS ME: Subscription signup.");
break;
case "subscr_cancel": //This IPN is for a subscription cancellation
MailUsTheOrder("PROCESS ME: Subscription cancellation.");
break;
case "subscr_failed": //This IPN is for a subscription payment failure
MailUsTheOrder("FAILURE: Subscription failed.");
break;
case "subscr_payment": //This IPN is for a subscription payment
MailUsTheOrder("COOL: We got cash!");
break;
case "subscr_eot": //This IPN is for a subscription's end of term
MailUsTheOrder("WHAT IS THIS? Subscription end of term.");
break;
}
switch (Address_status)
{
case "confirmed": //Customer provided a Confirmed Address
break;
case "unconfirmed": //Customer provided an Unconfirmed Address
break;
}
switch (Payer_status)
{
case "verified": //Customer has a Verified U.S. PayPal account
break;
case "unverified": //Customer has an Unverified U.S. PayPal account
break;
case "intl_verified": //Customer has a Verified International PayPal account
break;
case "intl_unverified": //Customer has an Unverified International PayPal account
break;
}
switch (Payment_type)
{
case "echeck": //This payment was funded with an eCheck
break;
case "instant": //This payment was funded with PayPal balance, credit card, or Instant Transfer
break;
}
}
else
{
MailUsTheOrder("WEIRD: Someone is notifying us that the payments were received by someone else???");
}
break;
case "Pending": //The payment is pending - see the "pending reason" variable below for more information. Note: You will receive another instant payment notification when the payment becomes "completed", "failed", or "denied"
switch (Pending_reason)
{
case "echeck": // The payment is pending because it was made by an eCheck, which has not yet cleared
break;
case "intl": //The payment is pending because you, the merchant, hold an international account and do not have a withdrawal mechanism. You must manually accept or deny this payment from your Account Overview
break;
case "verify": //The payment is pending because you, the merchant, are not yet verified. You must verify your account before you can accept this payment
break;
case "address": //The payment is pending because your customer did not include a confirmed shipping address and you, the merchant, have your Payment Receiving Preferences set such that you want to manually accept or deny each of these payments. To change your preference, go to the "Preferences" section of your "Profile"
break;
case "upgrade": //The payment is pending because it was made via credit card and you, the merchant, must upgrade your account to Business or Premier status in order to receive the funds
break;
case "unilateral": //The payment is pending because it was made to an email address that is not yet registered or confirmed
break;
case "other": //The payment is pending for an "other" reason. For more information, contact customer service
break;
}
MailUsTheOrder("PENDING: Order is waiting to be processed.");
break;
case "Failed": //The payment has failed. This will only happen if the payment was made from your customer's bank account
MailUsTheOrder("FAILED: This only happens if the payment was made from our customer's bank account.");
break;
case "Denied": //You, the merchant, denied the payment. This will only happen if the payment was previously pending due to one of the "pending reasons"
MailUsTheOrder("DENIED: We denied this payment.");
break;
}
// add transaction to database
//IBuySpy.TransactionDB TransactionSystem = new IBuySpy.TransactionDB();
//TransactionSystem.AddTransaction(Txn_id, Receiver_email, Item_name, Item_number, Quantity, Invoice, Custom, Payment_status, Pending_reason, Payment_date, Payment_fee, Payment_gross, Txn_type, First_name, Last_name, Address_street, Address_city, Address_state, Address_zip, Address_country, Address_status, Payer_email, Payer_status, Payer_id, Payment_type, Notify_version, Verify_sign);
break;
case "INVALID":
// Possible fraud. Log for investigation or an error
MailUsTheOrder("INVALID: Possible fraud. Log for investigation or an error");
break;
default:
// error
MailUsTheOrder("Default: error: Response is: " + response);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment