HOW TO USE PAYPAL IN MVC

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  Mvc 
 792  View(s)
Ratings:
Rate this:
 I want to use paypal in mvc how will do this. please help 
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to use PayPal in MVC

    _Layout.cshtml

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>@ViewBag.Title - My ASP.NET MVC Application</title>
            <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
            <meta name="viewport" content="width=device-width" />
            @Styles.Render("~/Content/css")
            @Scripts.Render("~/bundles/modernizr")
        </head>
        <body>
            <header>
                <div class="content-wrapper">
                    <div class="float-left">
                        <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
                    </div>
                    <div class="float-right">
                       
                        <nav>
                            <ul id="menu">
                                <li>@Html.ActionLink("Home", "Index", "Home")</li>
                                <li>@Html.ActionLink("About", "About", "Home")</li>
                                <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
            <div id="body">
                @RenderSection("featured", required: false)
                <section class="content-wrapper main-content clear-fix">
                    @RenderBody()
                </section>
            </div>
            <footer>
                <div class="content-wrapper">
                    <div class="float-left">
                        <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
                    </div>
                </div>
            </footer>

            @Scripts.Render("~/bundles/jquery")
            @RenderSection("scripts", required: false)
        </body>
    </html>

    Index.cshtml

     

    @{
        ViewBag.Title = "Home Page";
    }
    @using (Html.BeginForm("ValidateCommand", "PayPal"))
    {
        <div>
            <table >
                <tr>
                    <td>
                        product Name:
                    </td>
                    <td>
                        <input type="text" name="product" value="Xolo Mobile" readonly />
                    </td>
                </tr>
                <tr>
                    <td>
                       Total Price:
                    </td>
                    <td>
                        $<input type="text" name="totalPrice" value="14800" readonly />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <input type="submit" name="btnConfirm" value="Check Out with Paypal" />
                    </td>
                </tr>
            </table>
        </div>
    }

     

     

     

     

     

    ValidateCommand.cshtml

    @model PaypalMVC.Models.PayPalModel

    <body>
        <form id="hiddenform" action=@Model.actionURL>
        @Html.HiddenFor(model => model.cmd)
        @Html.HiddenFor(model => model.business)
        @Html.HiddenFor(model => model.no_shipping)
        @Html.HiddenFor(model => model.@return)
        @Html.HiddenFor(model => model.cancel_return)
        @Html.HiddenFor(model => model.notify_url)
        @Html.HiddenFor(model => model.currency_code)
        @Html.HiddenFor(model => model.item_name)
        @Html.HiddenFor(model => model.amount)  
        </form>

        <p style="text-align: center">
            <h3>
                Connecting to Paypal , please wait ...

            </h3>
        </p>
    </body>
     @Scripts.Render("~/bundles/jquery")

    <script type="text/javascript" language="javascript">
        $(this.document).ready(function () {
            var form = $("form");
            form.submit();
        });  
    </script>

    PayPalModel

     

    using System.Configuration;
    namespace PaypalMVC.Models
    {
        public class PayPalModel
        {
            public string cmd { get; set; }
            public string business { get; set; }
            public string no_shipping { get; set; }
            public string @return { get; set; }
            public string cancel_return { get; set; }
            public string notify_url { get; set; }
            public string currency_code { get; set; }
            public string item_name { get; set; }
            public string amount { get; set; }
            public string actionURL { get; set; }
           
            public PayPalModel(bool useSandbox)
            {
                this.cmd = "_xclick";
                this.business = ConfigurationManager.AppSettings["business"];
                this.cancel_return = ConfigurationManager.AppSettings["cancel_return"];
                this.@return = ConfigurationManager.AppSettings["return"];
                if (useSandbox)
                {
                    this.actionURL = ConfigurationManager.AppSettings["test_url"];
                }
                else
                {
                    this.actionURL = ConfigurationManager.AppSettings["Prod_url"];
                }
                // We can add parameters here, for example OrderId, CustomerId, etc....
                this.notify_url = ConfigurationManager.AppSettings["notify_url"];
                // We can add parameters here, for example OrderId, CustomerId, etc....
                this.currency_code = ConfigurationManager.AppSettings["currency_code"];
            }
        }
    }

     

    Web.config

     <appSettings>
       
        <add key="business" value="leyegora-facilitator@yahoo.fr"/
        <add key="IsSandbox" value="false" />
        <add key="currency_code" value="USD" />
        <add key="return" value="http://localhost/PayPal/RedirectFromPaypal" />
        <add key="cancel_return" value="http://localhost/PayPal/CancelFromPaypal" />
        <add key="notify_url" value="http://localhost/PayPal/NotifyFromPaypal" />
       
       <add key="test_url" value="https://www.sandbox.paypal.com/cgi-bin/webscr" />
       <add key="Prod_url" value="https://www.sandbox.paypal.com/cgi-bin/webscr" />
      
      </appSettings>

     

     

      Modified On Nov-19-2015 10:33:22 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!