Payment Integrations

Stripe Payment Integration In C#

Introduction

In this article, we will learn how to implement a Stripe payment gateway in a .NET Web application using C#.

Let’s begin.

Go to https://stripe.com/

Click on the START NOW button to do the registration. After successful registration, the below page appears.

Click on Developers > API Keys, and get your Publishable key and Secret key

Now, Install “Stripe.net” SDK in your application

Go to Tools > NuGet Package Manager > Manage NuGet Packages For Solutions.

Click on Browse Tab and search for “Stripe.net”. Click on search NuGet and click to Install button.

After installation of SDK, Set Publishable key and Secret key in web.config.

C# Code Example

Open the HomeController.cs file and add the code in it.

public ActionResult Index()
{
    ViewBag.StripePublishKey = ConfigurationManager.AppSettings["stripePublishableKey"];
     return View();
}
    
[HttpPost]
public ActionResult Charge(string stripeToken, string stripeEmail)
{
   Stripe.StripeConfiguration.SetApiKey("your Publishable key");
    Stripe.StripeConfiguration.ApiKey = "your Secret key";
    
    var myCharge = new Stripe.ChargeCreateOptions();

    // always set these properties
    myCharge.Amount = 500;
    myCharge.Currency = "USD";

    myCharge.ReceiptEmail = stripeEmail;
    myCharge.Description = "Sample Charge";
    myCharge.Source = stripeToken;
    myCharge.Capture = true;

    var chargeService = new Stripe.ChargeService();
    Charge stripeCharge = chargeService.Create(myCharge);

    return View();
}

Open the Index.cshtml file and add the code in it.

<form action="/Home/Charge" method="POST">
    <article>
        <label>Amount: $5.00</label>
    </article>
    <script src="//checkout.stripe.com/v2/checkout.js"
            class="stripe-button"
            data-key="@ViewBag.StripePublishKey"
            data-locale="auto"
            data-description="Sample Charge"
            data-amount="500">
    </script>
</form>

That’s it, you are ready to use the Stripe gateway.

 

Sumit Dangasiya

View Comments

  • I tried same code, but it redirects me to given action URL instead of displaying stripe popup/iframe. any suggestions?

  • Hi,Thank you for the code you have provided. Please help how can i add success return or cancel return link?

  • I couldn't get this to work. I got the popup message and pressed the button to purchase but no record of the purchase in my stripe account.

  • Wonderful goods from you, man. I have understand your stuff previous to and you're just extremely magnificent. I actually like what you've acquired here, really like what you're saying and the way in which you say it. You make it enjoyable and you still take care of to keep it wise. I can not wait to read far more from you. This is really a terrific web site.

Share
Published by
Sumit Dangasiya

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago