Rest API: Handling exception on create order
gobfrey
New Community Member
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on
Oct-02-2023
02:45 AM
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:
- 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.
- 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;
});
},
...
...
...
Labels:
- Labels:
-
Payments REST APIs
0 REPLIES 0
![](/skins/images/969EE294DC97B096AB9429A53D934B29/responsive_peak/images/icon_anonymous_message.png)
Haven't Found your Answer?
It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.
Related Content
- PAYER_ACTION_REQUIRED returned for Capture intent when calling v2 checkout orders in Sandbox Environment
- Encrypted subscription button has stopped working in NVP/SOAP APIs
- Cant work with umlaute ( ä,ö,ü) and Invoice API in REST APIs
- createOrder not being directed to OnApprove actions.redirect in PayPal Payments Standard