Answers.Body: <p>It's throwing an exception so if you don't want that to kill your script you need to enclose the api call in a <code>try {} catch {}</code>block.  See <a href="https://www.php.net/manual/en/language.exceptions.php" rel="nofollow noreferrer">php documentation on exceptions</a>  Eg:</p>
  <pre><code>try {
    $contribution_results = civicrm_api4( ...... );
  }
  catch (Exception $e) {
    print &quot;Error: &quot; . $e-&gt;getMessage();
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-20T00:30:51.013Z
Answers.Text: Ahhh ... that's why they pay you the big bucks! ;)
Answers.UserDisplayName: jsherk
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-19T20:51:46.920Z
Answers.LastActivityDate: 2022-05-19T20:51:46.920Z
Answers.UserDisplayName: Aidan
Body: <p>If I use the API Explorer V4, and I try to create a new contribution with a Transaction ID that already exists, then the response I get back is as follows:</p>
  <pre><code>status: 500, statusText: Internal Server Error, xhrStatus: complete
  {
    &quot;error_code&quot;: 0,
    &quot;error_message&quot;: &quot;Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: 100&quot;
  }
  </code></pre>
  <p>But when running an external script (CiviCRM v5.48.1 with WordPress v5.9.3), if I try to add a contribution and the Transaction ID already exists, it causes script to crash and returns the following php fatal error in the log:</p>
  <pre><code>PHP Fatal error:  Uncaught CRM_Core_Exception: [0: Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: 99
    thrown in /home/me/public_html/wp-content/plugins/civicrm/civicrm/CRM/Contribute/BAO/Contribution.php on line 103
  </code></pre>
  <p>My expectation was that I should still get a response/result from my call that contains the error message without crashing the script.</p>
  <p>This is my test code to reproduce the error:</p>
  <pre><code>&lt;?php
  
  require_once '/home/me/public_html/wp-content/plugins/civicrm/civicrm/civicrm.config.php';
  require_once '/home/me/public_html/wp-content/plugins/civicrm/civicrm/CRM/Core/Config.php';
  $civi_config = CRM_Core_Config::singleton();
  
  for ($i=1; $i&lt;=3; $i++) {
      
      $transaction_id = &quot;xyz123&quot;; // needs to be unique but use same id to test for this bug
      $total_amount = 2*$i;
      $source = 'Test '.$i;
      $contribution_results = civicrm_api4('Contribution', 'create', [
        'checkPermissions'=&gt;FALSE,
        'values' =&gt; [
          'contact_id' =&gt; '262', 
          'financial_type_id' =&gt; '1', 
          'payment_instrument_id' =&gt; '1', 
          'total_amount' =&gt; $total_amount, 
          'trxn_id' =&gt; $transaction_id, 
          'currency' =&gt; 'USD', 
          'source' =&gt; $source,
        ],
      ]);
      
      echo &quot;&lt;pre&gt;&quot;;
      print_r($contribution_results);
      echo &quot;&lt;/pre&gt;&quot;;
  }
  
  ?&gt;
  </code></pre>
  
Tags: <wordpress><civicontribute><api>
Title: PHP Fatal Error trying to create contribution with duplicate Transaction ID causes script to crash (using api4 on WordPress)
ThreadText: # PHP Fatal Error trying to create contribution with duplicate Transaction ID causes script to crash (using api4 on WordPress)
  
  (Asked by: username_0 on 2022-05-19)
  
  If I use the API Explorer V4, and I try to create a new contribution with a Transaction ID that already exists, then the response I get back is as follows:
  
  ```
  status: 500, statusText: Internal Server Error, xhrStatus: complete
  {
    "error_code": 0,
    "error_message": "Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: 100"
  }
  
  ```
  
  But when running an external script (CiviCRM v5.48.1 with WordPress v5.9.3), if I try to add a contribution and the Transaction ID already exists, it causes script to crash and returns the following php fatal error in the log:
  
  ```
  PHP Fatal error:  Uncaught CRM_Core_Exception: [0: Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: 99
    thrown in /home/me/public_html/wp-content/plugins/civicrm/civicrm/CRM/Contribute/BAO/Contribution.php on line 103
  
  ```
  
  My expectation was that I should still get a response/result from my call that contains the error message without crashing the script.
  
  This is my test code to reproduce the error:
  
  ```
  <?php
  
  require_once '/home/me/public_html/wp-content/plugins/civicrm/civicrm/civicrm.config.php';
  require_once '/home/me/public_html/wp-content/plugins/civicrm/civicrm/CRM/Core/Config.php';
  $civi_config = CRM_Core_Config::singleton();
  
  for ($i=1; $i<=3; $i++) {
      
      $transaction_id = "xyz123"; // needs to be unique but use same id to test for this bug
      $total_amount = 2*$i;
      $source = 'Test '.$i;
      $contribution_results = civicrm_api4('Contribution', 'create', [
        'checkPermissions'=>FALSE,
        'values' => [
          'contact_id' => '262', 
          'financial_type_id' => '1', 
          'payment_instrument_id' => '1', 
          'total_amount' => $total_amount, 
          'trxn_id' => $transaction_id, 
          'currency' => 'USD', 
          'source' => $source,
        ],
      ]);
      
      echo "<pre>";
      print_r($contribution_results);
      echo "</pre>";
  }
  
  ?>
  
  ```
  
  **Tags:** wordpress, civicontribute, api
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-05-19. Score: 1 | Accepted answer
  
  It's throwing an exception so if you don't want that to kill your script you need to enclose the api call in a `try {} catch {}`block. See [php documentation on exceptions](https://www.php.net/manual/en/language.exceptions.php) Eg:
  
  ```
  try {
    $contribution_results = civicrm_api4( ...... );
  }
  catch (Exception $e) {
    print "Error: " . $e->getMessage();
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Ahhh ... that's why they pay you the big bucks! ;)  
  
  ---
  
  