Example #1
0
        //Express Checkout
        public static PayPalRedirect SetExpressCheckout(PayPalOrder order)
        {
            NameValueCollection values = new NameValueCollection();

            values["METHOD"] = "SetExpressCheckout";

            //Seller PayPal API Info
            values["RETURNURL"] = PayPalSettings.ReturnUrl;
            values["CANCELURL"] = PayPalSettings.CancelUrl;
            values["CURRENCYCODE"] = "USD";
            values["BUTTONSOURCE"] = "PP-ECWizard";
            values["USER"] = PayPalSettings.Username;
            values["PWD"] = PayPalSettings.Password;
            values["SIGNATURE"] = PayPalSettings.Signature;
            values["SUBJECT"] = "";
            values["VERSION"] = PayPalSettings.Version;

            //Order Info
            values["PAYMENTREQUEST_0_AMT"] = order.amount;
            values["PAYMENTREQUEST_0_ITEMAMT"] = order.amount;
            values["PAYMENTREQUEST_0_CURRENCYCODE"] = "USD";
            values["PAYMENTREQUEST_0_SHIPPINGAMT"] = "0.00";
            values["REQCONFIRMSHIPPING"] = "0";
            values["NOSHIPPING"] = "1";

            //Optional Order details
            //values["L_PAYMENTREQUEST_0_NAMEian"] = "buy this";
            //values["L_PAYMENTREQUEST_0_DESCian"] = "purchasing this";
            //values["L_PAYMENTREQUEST_0_AMTian"] = order.amount;
            //values["L_PAYMENTREQUEST_0_QTYian"] = "1";

            //submit info for payment
            values = Submit(values);

            string ack = values["ACK"].ToLower(); //Status of Payment after submission

            //Redirect customer to new page based on whether payment successful or not
            if (ack == "success" || ack == "successwithwarning")
            {
                return new PayPalRedirect
                {
                    //For paypal express checkout
                    Token = values["TOKEN"],
                    Url = String.Format("https://{0}/webscr?cmd=_express-checkout&token={1}",
                       PayPalSettings.CgiDomain, values["TOKEN"])

                };
            }
            else
            {
                //throw new Exception(values["L_LONGMESSAGE0"]);

                return new PayPalRedirect
                {
                    //Payment Failed :(
                    //Direct user to error page
                    Url = String.Format("http://localhost:51072/Paid/Failure?ErrMsg={0}", values["L_LONGMESSAGE0"])
                };
            }
        }
Example #2
0
        public ActionResult Index(FormCollection billingForm)
        {
            //create paypal payment object
            PayPalOrder order = new PayPalOrder();

            //collect billing info form data
            order.amount = billingForm["amount"];
            order.firstname = billingForm["firstname"];
            order.lastname = billingForm["lastname"];
            order.email = billingForm["email"];
            order.street1 = billingForm["street1"];
            order.street2 = billingForm["street2"];
            order.city = billingForm["city"];
            order.state = billingForm["state"];
            order.zip = billingForm["zip"];
            order.country = billingForm["country"];

            //collect CC card info
            order.ccType = billingForm["cardType"];
            order.accountNum = billingForm["accountNum"];
            order.cvv = billingForm["cvv"];
            order.expDate = billingForm["expMonth"] + billingForm["expYear"];

            //Get ip address
            string ipAddress;
            //Check for proxy first
            ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            //if no proxy
            if (ipAddress == null)
            {
                ipAddress = Request.ServerVariables["REMOTE_ADDR"];
            }

            //System.Diagnostics.Debug.WriteLine(ipAddress);
            ipAddress = "192.168.0.1"; //comment out if not running locally
            order.ip = ipAddress;

            PayPalRedirect redirect = new PayPalRedirect();

            //send order to paypal
            if (Request.Form["submit"] == "Pay With Credit Card") //want direct payment api
            {
                //check for complete form (billing information and such)
                foreach (string key in billingForm.AllKeys)
                {
                    HashSet<string> optionalFields = new HashSet<string> { "street2" };
                    if (!optionalFields.Contains(key) && billingForm[key] == "")
                    {
                        string errorMsg =
                            String.Format("Entered {0} not valid. Please return to previous page and fix errors.", key);
                        string redirectURL = String.Format("http://localhost:51072/Paid/Failure?ErrMsg={0}", errorMsg);
                        return new RedirectResult(redirectURL);
                    }
                }
                //form is complete , move on with direct payment
                redirect = PaypalPayment.DoDirectPayment(order);
            }

            else //want express checkout api
            {
                //check if there is an amount. If not, then nothing to pay!
                if (billingForm["amount"] == "" || Convert.ToDouble(billingForm["amount"]) <= 0 )
                {
                    string errorMsg = "This transaction cannot be processed. The amount to be charged is zero.";
                    string redirectURL = String.Format("http://localhost:51072/Paid/Failure?ErrMsg={0}", errorMsg);
                    return new RedirectResult(redirectURL);
                }

                //amount is good, move on with order
                redirect = PaypalPayment.SetExpressCheckout(order);
            }

            //get response and redirect user
            Session["token"] = redirect.Token;

            return new RedirectResult(redirect.Url);
        }
Example #3
0
        public static PayPalRedirect DoDirectPayment(PayPalOrder order)
        {
            NameValueCollection values = new NameValueCollection();

            values["METHOD"] = "DoDirectPayment";

            //Seller PayPal API Info
            values["RETURNURL"] = PayPalSettings.ReturnUrl;
            values["CANCELURL"] = PayPalSettings.CancelUrl;
            values["CURRENCYCODE"] = "USD";
            values["BUTTONSOURCE"] = "PP-ECWizard";
            values["USER"] = PayPalSettings.Username;
            values["PWD"] = PayPalSettings.Password;
            values["SIGNATURE"] = PayPalSettings.Signature;
            values["SUBJECT"] = "";
            values["VERSION"] = PayPalSettings.Version;

            //Order Info
            values["AMT"] = order.amount;

            //Customer Info
            values["FIRSTNAME"] = order.firstname;
            values["LASTNAME"] = order.lastname;
            values["STREET"] = order.street1;
            values["STREET2"] = order.street2;
            values["CITY"] = order.city;
            values["STATE"] = order.state;
            values["ZIP"] = order.zip;
            values["COUNTRYCODE"] = order.country;
            values["EMAIL"] = order.email;
            values["IPADDRESS"] = order.ip;

            //Shipping Info
            /*
            values["SHIPTONAME"] = order.firstname + order.lastname;
            values["SHIPTOSTREET"] = order.street;
            values["SHIPTOCITY"] = order.city;
            values["SHIPTOSTATE"] = order.state;
            values["SHIPTOZIP"] = order.zip;
            values["SHIPTOCOUNTRY"] = order.country;
             */

            //Customer CC Info
            values["CREDITCARDTYPE"] = order.ccType;
            values["ACCT"] = order.accountNum;
            values["EXPDATE"] = order.expDate;
            values["CVV2"] = order.cvv;

            //submit info for payment
            values = Submit(values);

            string ack = values["ACK"].ToLower(); //Status of Payment after submission

            //Redirect customer to new page based on whether payment successful or not
            if (ack == "success" || ack == "successwithwarning")
            {
                return new PayPalRedirect
                {
                    /* //For paypal express checkout
                    Token = values["TOKEN"],
                    Url = String.Format("https://{0}/cgi-bin/webscr?cmd=_express-checkout&token={1}",
                       PayPalSettings.CgiDomain, values["TOKEN"])
                     */

                    //Payment sucessful!
                    //Direct user to successful transaction page with transaction info

                    Url = String.Format("http://localhost:51072/Paid/Success?TransID={0}&amt={1}",
                                 values["TRANSACTIONID"],values["AMT"])

                };
            }
            else
            {
                //throw new Exception(values["L_LONGMESSAGE0"]);

                return new PayPalRedirect
                {
                    //Payment Failed :(
                    //Direct user to error page
                    Url = String.Format("http://localhost:51072/Paid/Failure?ErrMsg={0}", values["L_LONGMESSAGE0"])

                };
            }
        }