Rest API: Handling exception on create order

gobfrey
New Community Member

I've been integrating Hosted Fields into our platform.  I'm able to successfully create an order, and then capture payment following hosted field form submission.  I'm now working on and testing edge-cases.  Consider the two scenarios where an exception is thrown during an attempt to create an order:

  1. Catch the exception in the callback (see code below).  The SDK then goes through the futile motions of attempting to capture the order (which naturally fails).  Unnecessary API calls to the paypal API are made, which fail, and an error is displayed on the payment form page.  A subsequent form submission may work fine.
  2. Don't catch the exception.  SDK execution halts (no attempt to capture the payment is made); the higher .catch displays an error on the payment form page, but a subsequent submission of the form raises the exception "Hosted Fields payment is already in progress."

Option (2) seems to be the more correct way to handle this, but is there a way to 'reset' the payment form following an exception, so that a second submission does not contain state that was presumably saved before the createOrder callback was executed? 

 

 

    function onCreateOrder(data, actions, args) {
        return $.ajax({
            url: "create_paypal_order",
            data: {
                customer_id: args.customer_id,
            },
            cache: false,
            dataType: "json",
        }).then((json) => {
            return json.id;
        });
    }

...
...
...

        if (paypal.HostedFields.isEligible()) {
            let orderId;
            paypal.HostedFields.render({
                createOrder: (data, actions) => {
                    return onCreateOrder(data, actions, {
                        customer_id: args.customer_id,
                    }).then((createdOrderId) => {
                        orderId = createdOrderId;
                        return orderId;
                    }).catch((error) => {
                        //prevent the paypal sdk from stopping execution this avoids the
                        //"Hosted Fields payment is already in progress." exception on subsequent form submissions
                        console.error(error);
                        return null;
                    });
                },
...
...
...

 

Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.