Search code examples
javascriptwebupiadyen

Adyen UPI client integration get status


I am somehow trying to get the status (error,success) after paying through the UPI app. I already end up in the onAdditionalDetails() function but here I somehow don't have the possibility to query the status. Is there maybe something needed to get this information in the state object?

async initAdyen_newurl() {
    let config = null;
    config = {
        ...this.config.adyenConfig,
        onPaymentCompleted: (result, component) => {
            console.info("onPaymentCompleted");
            console.info(result, component);
        },
        onError: (error, component) => {
            console.error("onError");
            console.error(error.name, error.message, error.stack, component);
        },
        onAdditionalDetails: (state, component) => {
            const actionUrl = "hardcoded for the moment"
            const obj = {
                paymentMethodType: component.props.paymentMethodType,
                url: actionUrl,
                method: "post",
                type: "redirect",
                paymentData: component.props.paymentData
            }
            component.props.createFromAction(obj, {}).mount("#id");
        },
    };
    AdyenCheckout(config)
        .then((checkout) => {
            // init stuff
        })
        .catch((error) => {
            console.error(`url failure ${error.message}`);
        });
},

I can also redirect to the next page using createFromAction(), but this just happens in both Success and Error. However, this should only happen in Success. I hope that was somehow understandable. Many Thanks

edited: i am using version 5.23.0


Solution

  • The flow involves an additional step (3DS) so the onAdditionalDetails handler is invoked. From there you can add an extra call to /payments/details to fetch the payment status.

    The response includes the resultCode to inform the shopper of the payment status.

    Here is an example:

    ...
      onPaymentCompleted: (result, component) => {
        handleServerResponse(result, component);
      },
      onAdditionalDetails: async (response, _component) => {
        // call server
        const paymentDetailsResponse = await callServer("/api/paymentDetails", response);
      // obtain payment status
      const result = paymentDetailsResponse.resultCode
      },
      onError: (error, component) => {
        console.error(error.name, error.message, error.stack, component);
      }
    
    // Calls your server endpoints
    async function callServer(url, data) {
      const res = await fetch(url, {
        method: "POST",
        body: data ? JSON.stringify(data) : "",
        headers: {
          "Content-Type": "application/json",
        },
      });
    
    

    In the backend perform the paymentsDetails call to obtain the Payment status from the Adyen platform:

    // Check payment result
    app.post("/api/paymentDetails", async (req, res) => {
      try {
        const response = await checkout.paymentsDetails({
          details: req.body.data.details,
          paymentData: req.body.data.paymentData,
        });
        res.json(response); 
      } catch (err) {
        console.error(`Error: ${err.message}, error code: ${err.errorCode}`);
        res.status(err.statusCode).json(err.message);
      }
    });
    

    See Confirm an additional action on your server