Razorpay付款网关集成asp.net核心

时间:2020-07-03 05:30:16

标签: asp.net-core-webapi payment-gateway razorpay

我是将razorpay付款网关集成到我们的angular和asp.net核心网站上的初学者。将数据发布到网关url时出现500错误。请检查我的代码,然后输入答案。我搜索了将近2天。

 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 });
        }

2 个答案:

答案 0 :(得分:0)

检查razorpay .net参考here

您必须发布错误,然后有人才能为您提供解决方案!

答案 1 :(得分:0)

对于JavaScript客户端,在使用asp.net内核时应考虑以下流程, 我已经将它与React.js客户端一起使用,但是您可以找到一些相似之处并使它适用于Angular。

这是javascript客户端与后端服务器集成的官方文档链接,

https://razorpay.com/docs/payment-gateway/web-integration/standard/#step-1-create-an-order-from-your-server

这是我的React.js客户端应用处理程序,将在单击按钮时调用,

<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: "testuser@mail.com",
      },
      theme: {
        color: '#F37254'
      }
    };
    const rzp1 = new window.Razorpay(options);
    rzp1.open();
};

这是PaymentController.cs,它将使用Razorpay客户端库创建订单,

[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);
    }
}