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


     
 

    }

}


 


7 comments:

  1. Web customers locate the Online Payments framework profoundly useful. They can buy various types of products online to encourage their necessities and wants.

    ReplyDelete
  2. If you are looking the latest solution of a payment gateway integration in ASP.NET Web Forms, check out demos:

    - For PayPal https://techtolia.com/PayPal/
    Receive payments from PayPal, PayPal Credit, credit or debit cards, Bancontact, BLIK, eps, giropay, iDEAL, MyBank, Przelewy24, SEPA-Lastschrift, Sofort, Venmo via PayPal.

    - For Stripe https://techtolia.com/Stripe/
    Receive payments from credit or debit cards, Alipay, WeChat Pay, Bancontact, EPS, giropay, iDEAL, Multibanco, Przelewy24, Sofort, Payment Request Button and Secure Remote Commerce via Stripe.

    ReplyDelete
  3. how to change text and apply css . this is by default show pay now i want to customize help me.
    my email sandipmodanwalrbl@gmail.com

    ReplyDelete
  4. I am getting error ,


    An existing connection was forcibly closed by the remote host. How to remove this

    ReplyDelete
  5. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    use this line in the beginning of your method.

    ReplyDelete
  6. If we want cancel the payment then how can redirect to our defined page

    ReplyDelete