You are here

class SquareApi in Commerce Square Connect 7

A wrapper around the Square Connect SDK.

Hierarchy

Expanded class hierarchy of SquareApi

File

includes/SquareApi.php, line 18
Square Connect SDK wrapper.

View source
class SquareApi {
  protected $client;

  /**
   * Creates a new SquareApi object.
   *
   * @param string $access_token
   *   The access token.
   * @param string $mode
   *   The mode: test or live.
   */
  public function __construct($access_token, $mode) {
    libraries_load('square');
    $configuration = new Configuration();
    $configuration
      ->setAccessToken($access_token);
    if ($mode === 'test') {
      $configuration
        ->setHost('https://connect.squareupsandbox.com');
    }
    $this->client = new ApiClient($configuration);
  }

  /**
   * Create a class instance from a payment method instance.
   *
   * @param string $instance_id
   *   The payment method instance ID.
   *
   * @return self
   *   The Square API wrapper.
   */
  public static function createFromInstanceId($instance_id) {
    $square_settings = variable_get('commerce_square_settings', commerce_square_default_settings()) + commerce_square_default_settings();
    $payment_method = commerce_payment_method_instance_load($instance_id);
    $mode = $payment_method['settings']['mode'];
    return new self($square_settings[$mode . '_access_token'], $mode);
  }

  /**
   * Gets the client.
   *
   * @return \SquareConnect\ApiClient
   *   The API client.
   */
  public function getClient() {
    return $this->client;
  }

  /**
   * Get locations.
   *
   * @return \SquareConnect\Model\Location[]
   *   The locations.
   */
  public function getLocations() {
    $location_api = new LocationsApi($this
      ->getClient());
    $location_api
      ->listLocations();
    return $location_api
      ->listLocations()
      ->getLocations();
  }

  /**
   * Voids a transaction.
   *
   * @param object $transaction
   *   The payment transaction.
   *
   * @return \SquareConnect\Model\VoidTransactionResponse
   *   The transaction response.
   */
  public function void($transaction) {
    $payment_method = commerce_payment_method_instance_load($transaction->instance_id);
    $mode = $payment_method['settings']['mode'];
    $location_id = $mode = $payment_method['settings'][$mode . '_location_id'];
    list($transaction_id, $tender_id) = explode('|', $transaction->remote_id);
    $transaction_api = new TransactionsApi($this
      ->getClient());
    return $transaction_api
      ->voidTransaction($location_id, $transaction_id);
  }

  /**
   * Refunds a transaction.
   *
   * @param object $transaction
   *   The payment transaction.
   * @param int $amount
   *   The amount.
   *
   * @return \SquareConnect\Model\CreateRefundResponse
   *   The transaction response.
   */
  public function refund($transaction, $amount) {
    $payment_method = commerce_payment_method_instance_load($transaction->instance_id);
    $mode = $payment_method['settings']['mode'];
    $location_id = $mode = $payment_method['settings'][$mode . '_location_id'];
    list($transaction_id, $tender_id) = explode('|', $transaction->remote_id);
    $transaction_api = new TransactionsApi($this
      ->getClient());
    $refund_request = new CreateRefundRequest(array(
      'idempotency_key' => uniqid(),
      'tender_id' => $tender_id,
      'amount_money' => new Money(array(
        'amount' => (int) $amount,
        'currency' => $transaction->currency_code,
      )),
      'reason' => 'Refunded through store backend',
    ));
    return $transaction_api
      ->createRefund($location_id, $transaction_id, $refund_request);
  }

  /**
   * Captures a transaction.
   *
   * @param object $transaction
   *   The payment transaction.
   *
   * @return \SquareConnect\Model\CaptureTransactionResponse
   *   The transaction response.
   */
  public function capture($transaction) {
    $payment_method = commerce_payment_method_instance_load($transaction->instance_id);
    $mode = $payment_method['settings']['mode'];
    $location_id = $mode = $payment_method['settings'][$mode . '_location_id'];
    list($transaction_id, $tender_id) = explode('|', $transaction->remote_id);
    $transaction_api = new TransactionsApi($this
      ->getClient());
    return $transaction_api
      ->captureTransaction($location_id, $transaction_id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SquareApi::$client protected property
SquareApi::capture public function Captures a transaction.
SquareApi::createFromInstanceId public static function Create a class instance from a payment method instance.
SquareApi::getClient public function Gets the client.
SquareApi::getLocations public function Get locations.
SquareApi::refund public function Refunds a transaction.
SquareApi::void public function Voids a transaction.
SquareApi::__construct public function Creates a new SquareApi object.