In this blog, we are going to send Payment Link with a redirect URL to Customer or any person on his/her Email and/or Mobile number using Razorpay in Asp.Net MVC.
when the Customer opens that payment link and pay, After successful completion of the payment customer will be redirected to a specific URL on which we want. for example, After a successful payment customers will redirect to our website.
this way we can log or create/update the entry of customer payment on our site.
if you don’t know How To Integrate Razorpay In Asp.Net MVC then click here. also if you want to learn in detail about How To Send Payment link To Customer Using Razorpay In Asp.Net MVC then click here.
or Using Razorpay in our .Net Application, I am using its NuGet Package named Razorpay. Razorpay if you don’t have much idea about it then I prefer you to see this first, How To Integrate Razorpay In Asp.Net MVC.
First, we are sending a payment link with redirect URL to a customer.
for that, go to HomeController and add the following code into the Index Method.
public ActionResult Index() { bool IsPaymentSent = false; try { Dictionary<string, object> options = new Dictionary<string, object>(); Dictionary<string, string> customer = new Dictionary<string, string>(); customer.Add("name", "tabish"); customer.Add("email", "tabishzrangrej.vision@gmail.com"); customer.Add("contact", "8866141791"); options.Add("customer", customer); options.Add("type", "link"); options.Add("amount", 5000); options.Add("currency", "INR"); options.Add("description", "demo description"); options.Add("callback_url", "http://localhost:58542/Home/RazorSuccessPayment"); options.Add("callback_method", "get"); Invoice ObjInvoice = new Invoice().Create(options); var InvoiceId = Convert.ToString(ObjInvoice["id"]); if (!string.IsNullOrEmpty(InvoiceId)) { IsPaymentSent = true; } } catch (Exception ex) { } ViewBag.IsPaymentSent = IsPaymentSent; return View(); }
in the above code, we define options Dictionary in which we store all options related to Invoice. for Customer or on which customer we want to send payment link we need to give his/her Name and Email,(and/or) mobile number. for that, I created another Dictionary named of the customer in which I store all the customer details. if you only give either a Mobile number or mail it will send a link according to that. if you provide both then it will send a link on both.
as you see the option type, it will be link
. so Razorpay creates it as a link.
in the Amount you need to pass the smallest unit of the currency, For example, pass 2000
for ₹20. in currency option you need to pass currency, Defaults isINR
.
in callback_url you need to pass redirect URL on which you want to redirect after successful payment.
in callback_method you need to pass get as a method.
after successful payment razor pay will redirect user to callback_url with following parameters.
there are many options you can pass according to your need. I used basic options. you can find it’s all options here.
now goto Index.cshtml, and add the following code into it.
@{ ViewBag.Title = "Home Page"; } <div class="jumbotron"> <h1>RazorPay Demo</h1> @if (ViewBag.IsPaymentSent == true) { <p class="lead"><label class="label-success">Payment Link Sent successfully</label></p> } else { <p class="lead"><label class="label-danger">Payment Link Not Sent</label></p> } </div>
go to HomeController and create RazorSuccessPayment Method with the following code into it.
public ActionResult RazorSuccessPayment(string razorpay_payment_id, string razorpay_invoice_id, string razorpay_invoice_receipt, string razorpay_invoice_status, string razorpay_signature) { bool IsValidRequest = true; bool IsPaymentSuccess = false; try { var payload = razorpay_invoice_id + '|' + razorpay_invoice_receipt + '|' + razorpay_invoice_status + '|' + razorpay_payment_id; Utils.verifyWebhookSignature(payload, razorpay_signature, "Your-API-Secret-Key"); if (!string.IsNullOrEmpty(razorpay_payment_id)) { IsPaymentSuccess = true; } } //this exception will be called when Signature is not verified catch (SignatureVerificationError ex) { IsValidRequest = false; } catch (Exception ex) { } ViewBag.IsValidRequest = IsValidRequest; ViewBag.IsPaymentSuccess = IsPaymentSuccess; return View(); }
After successful completion of the payment customer will be redirected to the RazorSuccessPayment method above, with parameters.
as you can see in the RazorSuccessPayment method, we are verifying that the request is valid or not. for that, we are checking/Verifying Signature.
we verify the razorpay_signature parameter to validate that it is authentic and sent from Razorpay servers so if any someone tries to modify the request or do invalid requests then we can verify it.
Razorpay server is returning razorpay_signature in a parameter. we need to check it against our response.
For verifying the signature, we need to create/Generate a signature using razorpay_invoice_id, razorpay_invoice_receipt, razorpay_invoice_status, and razorpay_payment_id as payload and your key_secret (your API secret) as secret.
here we are connecting it all in the payload variable.
And we are using Utils.verifyWebhookSignature() method for verify it. in Utils.verifyWebhookSignature() method we need to pass payload, expectedSignature (razorpay_signature which we get from Razorpay server), secret (your API secret).
if the signature is Invalid then it will throw Exception and go in SignatureVerificationError Exception block. if the signature is valid then it will execute the next line.
so this way you can verify the valid request, and you can write your business logic after Utils.verifyWebhookSignature() method.
now create RazorSuccessPayment .cshtml and add the following code into it.
@{ ViewBag.Title = "RazorSuccessPayment"; } <div class="jumbotron"> @if (ViewBag.IsValidRequest == false) { <p class="lead"><label class="label-danger">Invalid Request.!</label></p> } else { if (ViewBag.IsPaymentSuccess == true) { <p class="lead"><label class="label-success">Payment done successfully.!</label></p> } } </div>
The following are outputs of the above codes.
Index View Page
Got Link in the mail.
Payment Link View (while doing payment)
RazorSuccessPayment Page (After successful completion)
RazorSuccessPayment Page (After changing parameter manually and do invalid request)
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular