Customer.php 4.02 KB
Newer Older
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
<?php

class CRM_Stripe_Customer {

  /**
   * Find an existing Stripe customer in the CiviCRM database
   *
   * @param $params
   *
   * @return null|string
   * @throws \CRM_Core_Exception
   */
  public static function find($params) {
    $requiredParams = ['is_live', 'processor_id'];
    foreach ($requiredParams as $required) {
      if (empty($required)) {
        throw new CRM_Core_Exception('Stripe Customer (find): Missing required parameter: ' . $required);
      }
    }
    if (empty($params['email']) && empty($params['contact_id'])) {
      throw new CRM_Core_Exception('Stripe Customer (find): One of email or contact_id is required');
    }
    $queryParams = [
      1 => [$params['contact_id'], 'String'],
      2 => [$params['is_live'], 'Boolean'],
      3 => [$params['processor_id'], 'Positive'],
    ];

    return CRM_Core_DAO::singleValueQuery("SELECT id
      FROM civicrm_stripe_customers
      WHERE contact_id = %1 AND is_live = %2 AND processor_id = %3", $queryParams);
  }

  /**
   * Add a new Stripe customer to the CiviCRM database
   *
   * @param $params
   *
   * @throws \CRM_Core_Exception
   */
  public static function add($params) {
    $requiredParams = ['contact_id', 'customer_id', 'is_live', 'processor_id'];
    foreach ($requiredParams as $required) {
      if (empty($required)) {
        throw new CRM_Core_Exception('Stripe Customer (add): Missing required parameter: ' . $required);
      }
    }

    $queryParams = [
      1 => [$params['contact_id'], 'String'],
      2 => [$params['customer_id'], 'String'],
      3 => [$params['is_live'], 'Boolean'],
      4 => [$params['processor_id'], 'Integer'],
    ];
    CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers
mattwire's avatar
mattwire committed
56
          (contact_id, id, is_live, processor_id) VALUES (%1, %2, %3, %4)", $queryParams);
57 58 59 60 61 62 63 64 65 66 67 68
  }

  public static function create($params, $paymentProcessor) {
    $requiredParams = ['contact_id', 'card_token', 'is_live', 'processor_id'];
    // $optionalParams = ['email'];
    foreach ($requiredParams as $required) {
      if (empty($required)) {
        throw new CRM_Core_Exception('Stripe Customer (create): Missing required parameter: ' . $required);
      }
    }

    $contactDisplayName = civicrm_api3('Contact', 'getvalue', [
69
      'return' => 'display_name',
70 71 72 73 74 75 76 77 78 79
      'id' => $params['contact_id'],
    ]);

    $sc_create_params = [
      'description' => $contactDisplayName . ' (CiviCRM)',
      'card' => $params['card_token'],
      'email' => CRM_Utils_Array::value('email', $params),
      'metadata' => ['civicrm_contact_id' => $params['contact_id']],
    ];

mattwire's avatar
mattwire committed
80
    $stripeCustomer = $paymentProcessor->stripeCatchErrors('create_customer', $sc_create_params, $params);
81 82

    // Store the relationship between CiviCRM's email address for the Contact & Stripe's Customer ID.
mattwire's avatar
mattwire committed
83 84 85
    if (isset($stripeCustomer)) {
      if ($paymentProcessor->isErrorReturn($stripeCustomer)) {
        return $stripeCustomer;
86 87 88 89
      }

      $params = [
        'contact_id' => $params['contact_id'],
mattwire's avatar
mattwire committed
90
        'customer_id' => $stripeCustomer->id,
91 92 93 94 95 96 97 98
        'is_live' => $params['is_live'],
        'processor_id' => $params['processor_id'],
      ];
      self::add($params);
    }
    else {
      Throw new CRM_Core_Exception(ts('There was an error saving new customer within Stripe.'));
    }
mattwire's avatar
mattwire committed
99
    return $stripeCustomer;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  }
  /**
   * Delete a Stripe customer from the CiviCRM database
   *
   * @param $params
   *
   * @throws \CRM_Core_Exception
   */
  public static function delete($params) {
    $requiredParams = ['contact_id', 'is_live', 'processor_id'];
    foreach ($requiredParams as $required) {
      if (empty($required)) {
        throw new CRM_Core_Exception('Stripe Customer (delete): Missing required parameter: ' . $required);
      }
    }

    $queryParams = [
      1 => [$params['contact_id'], 'String'],
      2 => [$params['is_live'], 'Boolean'],
      3 => [$params['processor_id'], 'Integer'],
    ];
    CRM_Core_DAO::executeQuery("DELETE FROM civicrm_stripe_customers
            WHERE contact_id = %1 AND is_live = %2 AND processor_id = %3", $queryParams);
  }

}