【问题标题】:Razorpay payment gateway integration asp.net coreRazorpay 支付网关集成 asp.net core
【发布时间】:2020-10-23 19:16:08
【问题描述】:

我是在我们的 Angular 和 asp.net 核心网站上集成 razorpay 支付网关的初学者。将数据发布到网关 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 });
        }

【问题讨论】:

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


    【解决方案1】:

    检查razorpay .net 参考here

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

    【讨论】:

      【解决方案2】:

      对于 JavaScript 客户端,您在使用 asp.net core 时应考虑以下流程, 我已经将它与 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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-09-26
        • 2017-02-28
        • 2020-04-16
        • 2020-11-25
        • 2018-09-26
        • 2017-05-18
        • 2011-06-28
        • 2015-08-03
        • 2011-05-05
        相关资源
        最近更新 更多