Reputation: 21
I am beginner to integrate razorpay payment gateway on our angular and asp.net core website. Getting 500 error while posting the data to gateway url. Please check my code and pour your answers. Ia m searching it for nearly 2 days.
public async Task<IActionResult> CreateOrder([FromBody] TicketSales Sales)
{
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
string razorkey = "key";
string secret = "secret";
RazorpayClient client = new RazorpayClient(razorkey, secret);
Dictionary<string, object> options = new Dictionary<string, object>();
options.Add("amount", Sales.subTotal.Replace(".", "")); // amount in the smallest currency unit
options.Add("receipt", "Receipt_567");
options.Add("currency", "INR");
options.Add("payment_capture", "0");
Order order = client.Order.Create(options);
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("https://api.razorpay.com/v1/checkout/embedded");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("key",razorkey),
new KeyValuePair<string, string>("Amount", Sales.subTotal),
new KeyValuePair<string, string>("currency", "INR"),
new KeyValuePair<string, string>("name",Sales.bName),
new KeyValuePair<string, string>("description","Test Transaction"),
new KeyValuePair<string, string>("imag", ""),
new KeyValuePair<string, string>("order_id",Convert.ToString(order["id"])),
new KeyValuePair<string, string>("callback_url","localhost:4200//signin"),
});
var result = await httpClient.PostAsync("https://api.razorpay.com/v1/checkout/embedded", content);
if (result.IsSuccessStatusCode)
{
}
}
return Json(new { orderId = order["id"].ToString(),result });
}
Upvotes: 0
Views: 6370
Reputation: 3
For JavaScript client, you should consider the following flow while using asp.net core, I have used it with the React.js client but you can find some similarities and make it work for the Angular.
This is the official documentation link for javascript client integration with your backend server,
This is my React.js client app handler which will be called on button click,
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
const handleRazorpayPayment = () => {
const data = {}; // here anything extra can be passed while creating an order
const response = await axios.post(`api/payment/initialize`, data);
const order_id = response.data.id;
const options = {
key: `razorpay_key`,
amount: 200,
name: 'Your javascript client app',
description: 'Pro Membership',
image: '/your_logo.png',
order_id: order_id,
handler: (response) => {
axios.post(`api/payment/confirm`, response)
.then(response=>alert(response.data))
.catch((err)=>console.log(err))
},
prefill: {
name: "TESTUSER",
email: "[email protected]",
},
theme: {
color: '#F37254'
}
};
const rzp1 = new window.Razorpay(options);
rzp1.open();
};
This is the PaymentController.cs which will create an Order using Razorpay client library,
[Route("api/[controller]")]
[ApiController]
public class PaymentController : ControllerBase
{
private RazorpayClient _razorpayClient;
public PaymentController()
{
_razorpayClient = new RazorpayClient("key", "secret");
}
[HttpPost]
[Route("initialize")]
public async Task<IActionResult> InitializePayment()
{
var options = new Dictionary<string, object>
{
{ "amount", 200 },
{ "currency", "INR" },
{ "receipt", "recipt_1" },
// auto capture payments rather than manual capture
// razor pay recommended option
{ "payment_capture", true }
};
var order = _razorpayClient.Order.Create(options);
var orderId = order["id"].ToString();
var orderJson = order.Attributes.ToString();
return Ok(orderJson);
}
public class ConfirmPaymentPayload
{
public string razorpay_payment_id { get; }
public string razorpay_order_id { get; }
public string razorpay_signature { get; }
}
[HttpPost]
[Route("confirm")]
public async Task<IActionResult> ConfirmPayment(ConfirmPaymentPayload confirmPayment)
{
var attributes = new Dictionary<string, string>
{
{ "razorpay_payment_id", confirmPayment.razorpay_payment_id },
{ "razorpay_order_id", confirmPayment.razorpay_order_id },
{ "razorpay_signature", confirmPayment.razorpay_signature }
};
try
{
Utils.verifyPaymentSignature(attributes);
// OR
var isValid = Utils.ValidatePaymentSignature(attributes);
if (isValid)
{
var order = _razorpayClient.Order.Fetch(confirmPayment.razorpay_order_id);
var payment = _razorpayClient.Payment.Fetch(confirmPayment.razorpay_payment_id);
if (payment["status"] == "captured")
{
return Ok("Payment Successful");
}
}
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError);
}
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
Upvotes: 0
Reputation: 341
check razorpay .net reference here
you have to post the error then someone may give you the solution!
Upvotes: 0