Populatelog.php 2.96 KB
Newer Older
bgm's avatar
bgm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
<?php

/**
 * Populate the CiviCRM civicrm_system_log with Stripe events.
 *
 * This api will take all stripe events known to Stripe that are of the type
 * invoice.payment_succeeded and add them * to the civicrm_system_log table.
 * It will not add an event that has already been added, so it can be run
 * multiple times. Once added, they can be replayed using the Stripe.Ipn
 * api call.
 */

/**
 * Stripe.Populatelog API specification
 *
 * @param array $spec description of fields supported by this API call
 * @return void
 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards
 */
function _civicrm_api3_stripe_Populatelog_spec(&$spec) {
  $spec['ppid']['title'] = ts("The id of the payment processor."); 
}

/**
 * Stripe.Populatelog API
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_stripe_Populatelog($params) {
  $ppid = NULL;
  if (array_key_exists('ppid', $params)) {
    $ppid = $params['ppid'];
  }
  else {
    // By default, select the live stripe processor (we expect there to be
    // only one).
    $query_params = array('class_name' => 'Payment_Stripe', 'is_test' => 0, 'return' => 'id');
    try {
      $ppid = civicrm_api3('PaymentProcessor', 'getvalue', $params);
    }
    catch (CiviCRM_API3_Exception $e) {
      throw new API_Exception("Expected one live Stripe payment processor, but found none or more than one. Please specify ppid=.", 2234);
    }
  }
  
  $params = array('limit' => 100, 'type' => 'invoice.payment_succeeded');
  if ($ppid) { 
    $params['ppid'] = $ppid;
  }
 
  $items = array();
  $last_item = NULL;
  $more = TRUE;
  while(1) {
    if ($last_item) {
      $params['starting_after'] = $last_item->id;
    }
    $objects = civicrm_api3('Stripe', 'Listevents', $params);
    
    if (count($objects['values']['data']) == 0) {
      // No more!
      break;
    }
    $items = array_merge($items, $objects['values']['data']);
    $last_item = end($objects['values']['data']);
  }
  $results = array();
  foreach($items as $item) {
    $id = $item->id;
    // Insert into System Log if it doesn't exist.
    $like_event_id = '%event_id=' . addslashes($id);
    $sql = "SELECT id FROM civicrm_system_log WHERE message LIKE '$like_event_id'";
    $dao= CRM_Core_DAO::executeQuery($sql);
    if ($dao->N == 0) {
      $message = "payment_notification processor_id=${ppid} event_id=${id}";
      $contact_id = civicrm_api3_stripe_cid_for_trxn($item->data->object->charge);
      if ($contact_id) {
        $item['contact_id'] = $contact_id;
      }
      $log = new CRM_Utils_SystemLogger();
      $log->alert($message, $item);
      $results[] = $id;
    }
  }
  return civicrm_api3_create_success($results);

}

function civcrm_api3_stripe_cid_for_trxn($trxn) {
  $params = array('trxn_id' => $trxn, 'return' => 'contact_id');
  $result = civicrm_api3('Contribution', 'getvalue', $params);
  return $result;
}