Tuesday, 15 October 2019

razorpay payment gateway integration in asp.net c#

step 1 : login with razorpay account and get api key and secret.

it has given in setting menu .

step 2 :  download that code from given link

 https://github.com/razorpay/razorpay-dotnet-testapp


step 3: u can add dll from NuGet packages

step 4 : modify that code

on payment.aspx



           <form action="paymentresponse.aspx" method="post">
<script
    src="https://checkout.razorpay.com/v1/checkout.js"
    data-key="rzp_live_7LdgbZgdiDb1yh"
    data-amount="<%=amt%>"
    data-name="Razorpay"
    data-description="Fund"
    data-order_id="<%=orderId%>"
    data-image="https://razorpay.com/favicon.png"
    data-prefill.name="<%=name%>"
    data-prefill.email="<%=email%>"
    data-prefill.contact="<%=mobileno%>"
    data-theme.color="#F37254"
></script>
<input type="hidden" value="Hidden Element" name="hidden">

  <asp:Label ID="lbl1" runat="server"></asp:Label>

 </form>


on payment.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Razorpay.Api;
using System.Net;

public partial class payment : System.Web.UI.Page
{
    public string orderId;
    public string amt;
    public string name;
    public string email;
    public string mobileno;
    protected void Page_Load(object sender, EventArgs e)
    {
   
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;


        string key = "rzp_live_xxxxxxxxxxx";
        string secret = "6f84xxxxxxxxxxx";

        amt = "5000";  // its 500 bec it multiple by 100 by default so to make rs 500 u have to write 5000
        name = "text";
        email = "text@gmail.com";
        mobileno = "912345678";

        Dictionary<string, object> input = new Dictionary<string, object>();
        input.Add("amount", amt); // this amount should be same as transaction amount
        input.Add("currency", "INR");
        input.Add("receipt", "12121");
        input.Add("payment_capture", 1);

        RazorpayClient client = new RazorpayClient(key, secret);
     
        Razorpay.Api.Order order = client.Order.Create(input);
        string orderId = order["id"].ToString();

        lbl1.Text = orderId;
     
    }
}



step 5 : 

nothing will come on design page on charge.aspx 

on Charge.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Razorpay.Api;
using System.Net;

public partial class paymentresponse : System.Web.UI.Page
{
    MyDataClassesDataContext db = new MyDataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        string paymentId = Request.Form["razorpay_payment_id"];

        string key = "rzp_live_xxxxxxxxxxx";
        string secret = "6f84xxxxxxxxxxxxxxxxxxxxxxx";

   
         RazorpayClient client2 = new RazorpayClient(key, secret);
   
        Payment payment2 = client2.Payment.Fetch(paymentId);

        Dictionary<string, object> options = new Dictionary<string, object>();
        options.Add("amount", "100");

        Payment paymentCaptured = payment2.Capture(options);

        string amt = paymentCaptured.Attributes["amount"];

   
       // now u have amt


     
 

    }

}