Saturday 12 June 2021

Stripe Integration with C# ASP .NET MVC Visual Studio 2013

 Stripe payment gateway integration in ASP.NET C# MVC using Visual Studio 2013



Stripe payment gateway is simple but documentation for integration is very complicated. Current integration documentation is written for .Net core and if your project is made with visual studio 2013, then it would be very complicated to integrate because no documentation is available for the version of VS 2013. So I have made this post which will help you to strip integration with ASP.Net MVC using C#.

Step 1 :

Install Stripe package 

$ nuget install Stripe.net

If you have any issue to install package then read Nuget Package Installer

Step 2

Stripe work with .Net Framwork 4.6.1. Visual Studio by default support .Net Framework 4.5.1. So you will have to update Framework from https://dotnet.microsoft.com/download/dotnet-framework/net461

Step 3 : 

Create your checkout page 


Pay button is must be [checkout-button]

<button type="button" id="checkout-button" class="btn credit-btn btn-2 m-2">Pay</button>


<script src="https://js.stripe.com/v3/"></script>

<script type="text/javascript">

    // Create an instance of the Stripe object with your publishable API key

    var stripe = Stripe("pk_live_51IzgtVSJWatL4ibRmyDgC1D");

    var checkoutButton = document.getElementById("checkout-button");

 

    checkoutButton.addEventListener("click", function ()

    {      

        fetch("/StripeCheckout/CreateCheckoutSession", {

            method: "POST",

        })

          .then(function (response) {

              return response.json();

          })

          .then(function (session) {

              return stripe.redirectToCheckout({ sessionId: session.id });

          })

          .then(function (result) {              

              if (result.error) {

                  alert(result.error.message);

              }

          })

          .catch(function (error) {

              console.error("Error:", error);

          });

    });

</script>

Step 4 :


StripeCheckout Controller code
 
Namespace 

using Stripe;

using Stripe.Checkout;


namespace Tutor.Controllers

{

  

    public class StripeCheckoutController : Controller

    {

        public StripeCheckoutController()

        {

            StripeConfiguration.ApiKey = "sk_test_51IzgtVSJWatL4ibRYwclksghdlQJ2NeuHcgMQ1fi41qycyQ

";        }

        //

        // GET: /StripeCheckout/

        public ActionResult Index()

        {

            return View();

        }

        [HttpPost]

        public ActionResult CreateCheckoutSession()

        {

            string TotalAmount = "";

            SelectRepository ob = new SelectRepository();

            LectureModel data = new LectureModel();

            data = ob.GetLectureDetails(Session["ID"].ToString()); //implement your logic here for db

            TotalAmount = Session["Amt"].ToString(); //implement your logic for total amount

             var options = new SessionCreateOptions

            {

                PaymentMethodTypes = new List<string>

                {

                  "card",

                },

                LineItems = new List<SessionLineItemOptions>

                {

                  new SessionLineItemOptions

                  {

                    PriceData = new SessionLineItemPriceDataOptions

                    {

                      UnitAmount =Convert.ToInt64(TotalAmount+"00"),

                      Currency = "usd",

                      ProductData = new SessionLineItemPriceDataProductDataOptions

                      {

                        Name = data.Title,

                        Images=new List<string>

                        {

                            "https://example.com/logo-b.jpg"

                        },

                      },

                    },

               

                     Quantity = 1,

                    Description="Mentor Advise",

                  },

                },

                Mode = "payment",

                SuccessUrl = "https://example.com/success",

                CancelUrl = "https://exmaple.com/cancel",              

            };

             var service = new SessionService();

            Session session = service.Create(options);

            return Json(new { id = session.Id });

        }

      }

}

Next Topic

No comments:

Post a Comment