Bimal Pandey
Bimal Pandey

Reputation: 141

How to verify paypal payment on server

I'm using the javascript sdk to accept paypal payment. I have the following code

<PayPalScriptProvider
      options={{
        "client-id":
          "MY_CLIENT_ID",
      }}
    >
      <PayPalButtons
        createOrder={(data, actions) => {
          return actions.order.create({
            purchase_units: [
              {
                description: "Just pay up man.",
                amount: {
                  value: "1.65",
                },
              },
            ],
          });
        }}
        onApprove={async (data, actions) => {
          const order = await actions.order.capture();
          //Send the "order" to the server
        }}
        onError={(err)=>console.log(err)}
      />
    </PayPalScriptProvider>

So when we receive the order, can't we ping the paypal's server from our server that tells us that the payment is done or isn't false so I can save the info on my database? Or is this it?

Upvotes: 0

Views: 1233

Answers (2)

Carsten
Carsten

Reputation: 190

So when we receive the order, can't we ping the paypal's server from our server that tells us that the payment is done (...)

PayPal recommends using the JS SDK on the client-side to provide the right UX for the PayPal button.

However, in principle, as Jakob Wolf said already, you're free to do server-side only, client-side only or a mixture.

Infact, mixture, i.e. Button and UX client-side / payment calls server-side would be best security-wise.

<PayPalScriptProvider
  options={{
    "client-id":
      "MY_CLIENT_ID",
  }}
>
  <PayPalButtons
    createOrder={async (data, actions) => {
      try {
        const response = await fetch("my-server/create-order", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Accept: "application/json"
          }
        });
    
        const order = response ? await response.json() : null;
    
        return order.id;
      } catch (error) {
        throw new Error(error);
      }
    }}
    onApprove={async (data, actions) => {
        const { orderID } = data;
        const body = JSON.stringify({
          orderID
        });
        let response;

        try {
          response = await fetch("my-server/capture-order", {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
              Accept: "application/json"
            },
            body
          });
        } catch (error) {
          throw new Error(error);
        }
        const parsedBody = response ? await response.json() : {};

        if (parsedBody) {
          alert(`Transaction funds captured from ${parsedBody.payer.email_address}`);
        } else {
          alert("Error");
        }    
      }}
    onError={(err) => console.log(err)}
  />
</PayPalScriptProvider>

Implementation server-side for the Order V2 calls is documented here: https://developer.paypal.com/docs/api/orders/v2/

Upvotes: 1

Jakob Wolf
Jakob Wolf

Reputation: 36

You can either implement the payment in the frontend or the backend. Either way as soon you capture the payment (like you did in the frontend) and no error is thrown the payment is done.

If you want you could also ping the server to double check the payments intent and status: https://developer.paypal.com/docs/api/orders/v2/#orders_confirm

Upvotes: 0

Related Questions