Give Pay Documentation
JavaScript SDK

Error Handling

Handling validation and network errors in the GivePay SDK

Error Handling

The SDK includes built-in validation and error handling. Common errors include:

Validation Errors

The SDK will throw errors if required fields are missing or invalid:

// Missing currency
Error: Currency is required

// Empty line items
Error: At least one line item is required

// Missing payer
Error: Payer information is required

// Invalid email
Error: Invalid payer email

// Missing return URL
Error: Return URL is required

// Invalid quantity
Error: Line item 0: Quantity must be greater than 0

// Invalid amount
Error: Line item 0: Amount must be greater than 0

Handling Errors

<script>
  try {
    givepay.renderButton({
      currency: "USD",
      line_items: [
        {
          name: "Product",
          quantity: 1,
          amount: 5000,
        },
      ],
      payer: {
        email: "customer@example.com",
      },
      return_url: "https://yoursite.com/success",
    });
  } catch (error) {
    console.error("Payment initialization failed:", error.message);
    // Show error message to user
    alert(
      "Unable to initialize payment. Please check your information and try again."
    );
  }
</script>

Network Errors

If the payment session creation fails (network issues, API errors), the SDK will:

  1. Display an error in the browser console
  2. Show an alert to the user
  3. Re-enable the payment button so the user can retry