Skip to main content

Handling API responses when integrating with PayPal APIs

Properly handling API responses ensures that your application can gracefully manage successful payments, declines, errors, and edge cases. This improves user experience, reduces failed transactions, and helps maintain PCI DSS compliance.

This page provides a comprehensive guide for software developers on how to handle API responses when integrating with PayPal APIs. It covers best practices, tools, integration details, and includes code samples for both back-end and front-end environments.

When integrating with PayPal, keep the following principles in mind:

  • Always check and handle API responses and errors to ensure a secure and reliable PayPal integration.
  • Use PayPal’s official SDKs and follow documented response handling patterns for the server-side and client-side environments.
  • Implement robust error handling, logging, and user feedback for all payment flows, and use PayPal’s sandbox tools for testing error scenarios.

Requirements and guidelines

  • Always check the HTTP status code and response body for every API call.
  • Implement error handling for all possible error codes and messages.
  • Log errors and unexpected responses for troubleshooting.
  • Never expose sensitive error details to end users.
  • Use PayPal’s sandbox and negative testing tools to simulate errors.
  • Provide clear user feedback for payment declines or failures.
  • Follow PayPal’s API Response Guidelines.

Tools, integrations, and features

NamePurposeIntegration location
Rest API response handlingReceive structured responses for payment, order, and account operationsBack-end (server-side) and front-end (client-side)
Error handling and codesIdentify and respond to errors, declines, and warningsBack-end and front-end
Negative testing toolsSimulate error conditions and declined transactions in the sandboxBack-end and front-end (sandbox only)
Card decline and funding failure handlingHandle card declines and funding failures gracefullyFront-end and back-end

Rest API response handling

PayPal REST APIs provide structured JSON responses for payment, order, and account operations. These responses include status codes, result objects, and error details. You should always check the HTTP status and parse the response body to ensure your integration can process successful transactions and handle errors appropriately. For more information about REST API response handling, see the Rest API responses documentation.

Error handling and codes

PayPal offers detailed error codes and messages to help you identify and respond to errors, declines, and warnings. By checking the response code and displaying user-friendly messages, your application can guide users through issues and improve the payment experience.

These PayPal documentation pages provide more information about error handling and codes:

Negative testing tools

To ensure your integration handles failures gracefully, PayPal provides negative testing tools that let you simulate error conditions and declined transactions in the sandbox environment. Use special request headers and error conditions to test your application's error handling logic.

These PayPal documentation pages provide more information about negative testing tools:

Card decline and funding failure handling

Handling card declines and funding failures is crucial for a smooth user experience. Your integration should detect these failures and provide clear feedback and retry options to users, ensuring they understand what went wrong and how to proceed.

These PayPal documentation pages provide more information about card declines and handling funding failures:

Integration

This section includes practical integration details and code samples for handling PayPal API responses and errors on both the back end (server-side) and front end (client-side).

Back-end integration

The back end (server-side) handles sensitive operations such as creating payments, verifying transactions, and managing API credentials. The server is responsible for communicating directly with PayPal’s APIs, processing responses, handling errors, and ensuring that you never expose sensitive data, like access tokens and client secrets, to a client.

The following code samples demonstrate how to securely obtain access tokens from PayPal and handle API responses in various server-side programming languages. Each example shows proper error handling and response processing patterns that you should implement in your back-end services.

curl -X POST https://api-m.sandbox.paypal.com/v1/payments/payment \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ACCESS_TOKEN" \
-d '{...}'

Front-end integration

The front end (client-side) interacts with users, initiates payment flows, and displays feedback based on API responses. Proper handling of responses and errors on the client side ensures that you inform users about payment status, users can retry failed transactions, and users receive clear guidance if something goes wrong.

The following code samples demonstrate how to properly handle API responses and errors in various front-end frameworks and approaches, helping you implement user-friendly payment flows that gracefully manage both successful transactions and potential failures.

<script>
fetch('https://api-m.sandbox.paypal.com/v1/payments/payment', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ACCESS_TOKEN'
},
body: JSON.stringify({...})
})
.then(res => res.json().then(data => ({ status: res.status, body: data })))
.then(({ status, body }) => {
if (status === 201) {
alert('Payment created!');
} else {
alert('Error: ' + status);
}
});
</script>

References