Next.js Discord

Discord Forum

Razorpay

Answered
Birman posted this in #help-forum
Open in Discord
Avatar
BirmanOP
IDK

36 Replies

Avatar
BirmanOP
ok so if i want to implement a razorpay payment button and have dynamic values, i have to first create a new order and then provide its id and other details to a script element that has the src as the src of razorpay
<form action="/api/orders/session/verify-payment" method="POST">
  <script
    id="razorpay-script"
src="https://checkout.razorpay.com/v1/checkout.js"
            data-key={razorPayConfig("test").key_id}
            data-amount={razorPayData.amount}
            data-currency="INR"
            data-order_id={razorPayData.id}
            data-buttontext="Pay with Razorpay"
            data-name=""
            data-description="s"
            data-image=""
          />
          <input
            type="hidden"
            name="amount"
            value={razorPayData.amount}
          />
          <input
            type="hidden"
            name="notes"
            value={JSON.stringify(razorPayData.notes)}
          />
        </form>
this scipt element adds an input element below it that has the type "submit"
But as there is 1 more new input element in the client side generated page while it wasn't there in the server side render
nextjs is throwing error saying client and server component mismatch
and shows an error and the button hides
Avatar
Answer
Avatar
Using client-side components for handling payments and related tasks might not be the best approach. It's generally recommended to handle these functionalities on the server side.
Avatar
BirmanOP
owh hes creating a script element using js
okkkk thanks
Avatar
BirmanOP
thank you
Avatar
BirmanOP
btw do you know any way to stop a useState from runnign more than 1 time
Image
Avatar
BirmanOP
i tried by created a generated state
which is false at first
but still 2 are generated
Avatar
can you share how are you using it
I'm guessing something like this
{state && <button>pay with Razorpay</button>}
Avatar
BirmanOP
document.createElement with that i am creating the script element
and then with formElement.appenChild i am adding it to the form element
and with the state i am checking
if(!state) then create and append the element and then set the state to true
otherwise do not
Avatar
It doesn't make any sense to me why'd you create and append child in jsx
Avatar
BirmanOP
bru ;-;
bcz if i directly add the script element
it adds an input button element
which breaks the code
as the client side has +1 input element as compared to server side generated
Avatar
is there in any sandbox or codebase where one can view your code??
Avatar
BirmanOP
its private code :<
Avatar
yeah it should be, but the portion which is causing you trouble could be public (excluding the sensitive creds)
this is what happens when i directly place <script> element inside the component
Avatar
Yeah I've seen those in #discussions
have you given this approach a shot??
Avatar
BirmanOP
yes i did as it said but its resulting in script element generating more than 1 time
Avatar
Ohh no
It should not