commit 94857d9cf70461bb953dd2cdfc1b329889c4a4d6
Author: Bart Feenstra <bart@mynameisbart.com>
Date:   Mon Dec 9 17:47:19 2013 +0100

    Issue #1818454 by Xano: Convert Payment Reference Field to general queue/reference solution.

diff --git a/payment_reference/lib/Drupal/payment_reference/Queue.php b/payment/lib/Drupal/payment/Queue.php
similarity index 64%
rename from payment_reference/lib/Drupal/payment_reference/Queue.php
rename to payment/lib/Drupal/payment/Queue.php
index 1e742c4..d35ebdc 100644
--- a/payment_reference/lib/Drupal/payment_reference/Queue.php
+++ b/payment/lib/Drupal/payment/Queue.php
@@ -2,10 +2,10 @@
 
 /**
  * @file
- * Contains \Drupal\payment_reference\Queue.
+ * Contains \Drupal\payment\Queue.
  */
 
-namespace Drupal\payment_reference;
+namespace Drupal\payment;
 
 use Drupal\Component\Utility\Random;
 use Drupal\Core\Database\Connection;
@@ -14,7 +14,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
 use Drupal\payment\Plugin\Payment\Status\Manager;
 
 /**
- * The payment reference queue manager.
+ * The payment queue.
  */
 class Queue implements QueueInterface {
 
@@ -55,8 +55,17 @@ class Queue implements QueueInterface {
   protected $randomGenerator;
 
   /**
+   * The unique ID of the queue (instance).
+   *
+   * @var string
+   */
+  protected $queueId;
+
+  /**
    * Constructor.
    *
+   * @param string $queue_id
+   *   The unique ID of the queue (instance).
    * @param \Drupal\Core\Database\Connection $database
    *   A database connection.
    * @param \Drupal\Core\Extension\ModuleHandlerInterface
@@ -64,21 +73,23 @@ class Queue implements QueueInterface {
    * @param \Drupal\payment\Plugin\Payment\Status\Manager
    *   The payment status plugin manager.
    */
-  public function __construct(Connection $database, ModuleHandlerInterface $module_handler, Manager $payment_status_manager) {
+  public function __construct($queue_id, Connection $database, ModuleHandlerInterface $module_handler, Manager $payment_status_manager) {
     $this->database = $database;
     $this->moduleHandler = $module_handler;
     $this->paymentStatusManager = $payment_status_manager;
     $this->randomGenerator = new Random();
+    $this->queueId = $queue_id;
   }
 
   /**
    * {@inheritdoc}
    */
-  function save($field_instance_id, $payment_id) {
-    $this->database->insert('payment_reference')
+  function save($category_id, $payment_id) {
+    $this->database->insert('payment_queue')
       ->fields(array(
-        'field_instance_id' => $field_instance_id,
+        'category_id' => $category_id,
         'payment_id' => $payment_id,
+        'queue_id' => $this->queueId,
       ))
       ->execute();
   }
@@ -86,13 +97,13 @@ class Queue implements QueueInterface {
   /**
    * {@inheritdoc}
    */
-  public function claim($payment_id) {
-    $acquisition_code = $this->tryClaimOnce($payment_id);
+  public function claimPayment($payment_id) {
+    $acquisition_code = $this->tryClaimPaymentOnce($payment_id);
     // If a payment cannot be claimed at the first try, wait until the prevous
     // claim has expired and try to claim the payment one more time.
     if ($acquisition_code === FALSE) {
       sleep(static::CLAIM_EXPIRATION_PERIOD);
-      $acquisition_code = $this->tryClaimOnce($payment_id);
+      $acquisition_code = $this->tryClaimPaymentOnce($payment_id);
     }
 
     return $acquisition_code;
@@ -107,13 +118,14 @@ class Queue implements QueueInterface {
    *   An acquisition code to acquire the payment with on success, or FALSE if
    *   the payment could not be claimed.
    */
-  protected function tryClaimOnce($payment_id) {
+  protected function tryClaimPaymentOnce($payment_id) {
     $acquisition_code = $this->randomGenerator->string(255);
-    $count = $this->database->update('payment_reference', array(
+    $count = $this->database->update('payment_queue', array(
       'return' => Database::RETURN_AFFECTED,
     ))
       ->condition('claimed', time() - self::CLAIM_EXPIRATION_PERIOD, '<')
       ->condition('payment_id', $payment_id)
+      ->condition('queue_id', $this->queueId)
       ->fields(array(
         'acquisition_code' => $acquisition_code,
         'claimed' => time(),
@@ -126,25 +138,27 @@ class Queue implements QueueInterface {
   /**
    * {@inheritdoc}
    */
-  public function acquire($payment_id, $acquisition_code) {
-    return (bool) $this->database->delete('payment_reference', array(
+  public function acquirePayment($payment_id, $acquisition_code) {
+    return (bool) $this->database->delete('payment_queue', array(
       'return' => Database::RETURN_AFFECTED,
     ))
       ->condition('acquisition_code', $acquisition_code)
       ->condition('claimed', time() - self::CLAIM_EXPIRATION_PERIOD, '>=')
       ->condition('payment_id', $payment_id)
+      ->condition('queue_id', $this->queueId)
       ->execute();
   }
 
   /**
    * {@inheritdoc}
    */
-  public function release($payment_id, $acquisition_code) {
-    return (bool) $this->database->update('payment_reference', array(
+  public function releaseClaim($payment_id, $acquisition_code) {
+    return (bool) $this->database->update('payment_queue', array(
       'return' => Database::RETURN_AFFECTED,
     ))
       ->condition('payment_id', $payment_id)
       ->condition('acquisition_code', $acquisition_code)
+      ->condition('queue_id', $this->queueId)
       ->fields(array(
         'claimed' => 0,
       ))
@@ -154,31 +168,18 @@ class Queue implements QueueInterface {
   /**
    * {@inheritdoc}
    */
-  function loadFieldInstanceId($payment_id, $owner_id) {
-    $query = $this->database->select('payment_reference', 'pr');
-    $query->addJoin('INNER', 'payment', 'p', 'p.id = pr.payment_id');
-
-    return $query->fields('pr', array('field_instance_id'))
-      ->condition('pr.payment_id', $payment_id)
-      ->condition('p.owner_id', $owner_id)
-      ->execute()
-      ->fetchField();
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  function loadPaymentIds($field_instance_id, $owner_id) {
-    $query = $this->database->select('payment_reference', 'pr');
+  function loadPaymentIds($category_id, $owner_id) {
+    $query = $this->database->select('payment_queue', 'pr');
     $query->addJoin('INNER', 'payment', 'p', 'p.id = pr.payment_id');
     $query->addJoin('INNER', 'payment_status', 'ps', 'p.last_payment_status_id = ps.id');
     $query->fields('pr', array('payment_id'))
-      ->condition('pr.field_instance_id', $field_instance_id)
+      ->condition('pr.category_id', $category_id)
       ->condition('ps.plugin_id', array_merge($this->paymentStatusManager->getDescendants('payment_success'), array('payment_success')))
-      ->condition('p.owner_id', $owner_id);
+      ->condition('p.owner_id', $owner_id)
+      ->condition('pr.queue_id', $this->queueId);
 
     $payment_ids = $query->execute()->fetchCol();
-    $this->moduleHandler->alter('payment_reference_queue_payment_ids', $field_instance_id, $owner_id, $payment_ids);
+    $this->moduleHandler->alter('payment_queue_payment_ids', $category_id, $owner_id, $payment_ids);
     // @todo Add a Rules event.
 
     return $payment_ids;
@@ -188,26 +189,29 @@ class Queue implements QueueInterface {
    * {@inheritdoc}
    */
   function deleteByPaymentId($id) {
-    $this->database->delete('payment_reference')
+    $this->database->delete('payment_queue')
       ->condition('payment_id', $id)
+      ->condition('queue_id', $this->queueId)
       ->execute();
   }
 
   /**
    * {@inheritdoc}
    */
-  function deleteByFieldId($field_id) {
-    $this->database->delete('payment_reference')
-      ->condition('field_instance_id', $field_id . '.%', 'LIKE')
+  function deleteByCategoryId($category_id) {
+    $this->database->delete('payment_queue')
+      ->condition('category_id', $category_id)
+      ->condition('queue_id', $this->queueId)
       ->execute();
   }
 
   /**
    * {@inheritdoc}
    */
-  function deleteByFieldInstanceId($field_instance_id) {
-    $this->database->delete('payment_reference')
-      ->condition('field_instance_id', $field_instance_id)
+  function deleteByCategoryIdPrefix($category_id_prefix) {
+    $this->database->delete('payment_queue')
+      ->condition('category_id', $category_id_prefix . '%', 'LIKE')
+      ->condition('queue_id', $this->queueId)
       ->execute();
   }
 }
diff --git a/payment/lib/Drupal/payment/QueueInterface.php b/payment/lib/Drupal/payment/QueueInterface.php
new file mode 100644
index 0000000..8064a81
--- /dev/null
+++ b/payment/lib/Drupal/payment/QueueInterface.php
@@ -0,0 +1,91 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\payment\QueueInterface.
+ */
+
+namespace Drupal\payment;
+
+/**
+ * Defines a payment reference queue manager.
+ */
+interface QueueInterface {
+
+  /**
+   * Saves a payment available for referencing.
+   *
+   * @param string $category_id
+   *   The ID of the category the payment falls in.
+   * @param integer $payment_id
+   */
+  public function save($category_id, $payment_id);
+
+  /**
+   * Claims a payment available for referencing through a field instance.
+   *
+   * After a payment has been claimed, it can be definitely acquired with
+   * self::acquire().
+   *
+   * @param integer $payment_id
+   *
+   * @return string|false
+   *   An acquisition code to acquire the payment with on success, or FALSE if
+   *   the payment could not be claimed.
+   */
+  public function claimPayment($payment_id);
+
+  /**
+   * Releases a claimed payment.
+   *
+   * @param integer $payment_id
+   * @param string $acquisition_code
+   *   The code that was received from self::claim().
+   */
+  public function releaseClaim($payment_id, $acquisition_code);
+
+  /**
+   * Acquires a payment and removes if from the queue.
+   *
+   * @param integer $payment_id
+   * @param string $acquisition_code
+   *   The code that was received from self::reserve().
+   *
+   * @return bool
+   *   Whether the acquisition was successful.
+   */
+  public function acquirePayment($payment_id, $acquisition_code);
+
+  /**
+   * Loads the IDs of payments available for referencing through an instance.
+   *
+   * @param string $category_id
+   *   The ID of the field instance to load payment IDs for.
+   * @param integer $owner_id
+   *   The UID of the user for whom the payment should be available.
+   *
+   * @return array
+   */
+  public function loadPaymentIds($category_id, $owner_id);
+
+  /**
+   * Deletes a payment from the queue by payment ID.
+   *
+   * @param integer $payment_id
+   */
+  public function deleteByPaymentId($payment_id);
+
+  /**
+   * Deletes payments from the queue by category ID.
+   *
+   * @param string $category_id
+   */
+  public function deleteByCategoryId($category_id);
+
+  /**
+   * Deletes payments from the queue by category ID prefix.
+   *
+   * @param string $category_id_prefix
+   */
+  public function deleteByCategoryIdPrefix($category_id_prefix);
+}
diff --git a/payment/lib/Drupal/payment/Tests/QueueWebTest.php b/payment/lib/Drupal/payment/Tests/QueueWebTest.php
new file mode 100644
index 0000000..39f8056
--- /dev/null
+++ b/payment/lib/Drupal/payment/Tests/QueueWebTest.php
@@ -0,0 +1,138 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\payment\Tests\QueueWebTest.
+ */
+
+namespace Drupal\payment\Tests;
+
+
+use Drupal\payment\Generate;
+use Drupal\payment\Queue;
+use Drupal\simpletest\WebTestBase;
+
+/**
+ * Tests \Drupal\payment\Queue.
+ */
+class QueueWebTest extends WebTestBase {
+
+  /**
+   * The database connection.
+   *
+   * @var \Drupal\Core\Database\Connection
+   */
+  protected $database;
+
+  /**
+   * The payment method plugin manager.
+   *
+   * @var \Drupal\payment\Plugin\Payment\Method\Manager
+   */
+  protected $paymentMethodManager;
+
+  /**
+   * The payment status plugin manager.
+   *
+   * @var \Drupal\payment\Plugin\Payment\Status\Manager
+   */
+  protected $paymentStatusManager;
+
+  /**
+   * The payment reference queue service under test.
+   *
+   * @var \Drupal\payment\Queue
+   */
+  protected $queue;
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $modules = array('payment', 'payment_test');
+
+  /**
+   * {@inheritdoc}
+   */
+  static function getInfo() {
+    return array(
+      'description' => '',
+      'group' => 'Payment',
+      'name' => '\Drupal\payment\Queue web test',
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function setUp() {
+    parent::setUp();
+    $this->database = \Drupal::database();
+    $this->paymentMethodManager = \Drupal::service('plugin.manager.payment.method');
+    $this->paymentStatusManager = \Drupal::service('plugin.manager.payment.status');
+    $queue_id = $this->randomName();
+    $this->queue = new Queue($queue_id, $this->database, \Drupal::moduleHandler(), $this->paymentStatusManager);
+  }
+
+  /**
+   * Tests queue service.
+   */
+  function testQueue() {
+    $category_id_prefix = $this->randomName();
+    $category_id = $category_id_prefix . $this->randomName();
+    $payment = Generate::createPayment(2);
+    $payment->setStatus($this->paymentStatusManager->createInstance('payment_success'));
+    $payment->save();
+
+    // Tests save().
+    $this->queue->save($category_id, $payment->id());
+    $payment_ids = $this->queue->loadPaymentIds($category_id, $payment->getOwnerId());
+    $this->assertTrue(in_array($payment->id(), $payment_ids));
+
+    // Tests claimPayment().
+    $this->assertTrue(is_string($this->queue->claimPayment($payment->id())));
+    $this->assertFalse($this->queue->claimPayment($payment->id()));
+    $acquisition_code = $this->queue->claimPayment($payment->id());
+    $this->assertTrue(is_string($acquisition_code));
+
+    // Tests releaseClaim().
+    $released = $this->queue->releaseClaim($payment->id(), $acquisition_code);
+    $this->assertTrue($released);
+    $acquisition_code = $this->queue->claimPayment($payment->id());
+    $this->assertTrue(is_string($acquisition_code));
+
+    // Tests acquirePayment().
+    $acquired = $this->queue->acquirePayment($payment->id(), $acquisition_code);
+    $this->assertTrue($acquired);
+    $acquisition_code = $this->queue->claimPayment($payment->id());
+    $this->assertFalse($acquisition_code);
+
+    // Save another payment to the queue, because acquiring the previous one
+    // deleted it.
+    $payment = Generate::createPayment(2);
+    $payment->setStatus($this->paymentStatusManager->createInstance('payment_success'));
+    $payment->save();
+    $this->queue->save($category_id, $payment->id());
+
+    // Tests loadPaymentIds().
+    $loaded_payment_ids = $this->queue->loadPaymentIds($category_id, $payment->getOwnerId());
+    $this->assertEqual($loaded_payment_ids, array($payment->id()));
+    $this->assertTrue(\Drupal::state()->get('payment_test_payment_queue_payment_ids_alter'));
+
+    // Tests deleteByPaymentId().
+    $this->queue->deleteByPaymentId($payment->id());
+    $payment_ids = $this->queue->loadPaymentIds($category_id, $payment->getOwnerId());
+    $this->assertFalse(in_array($payment->id(), $payment_ids));
+
+    // Tests deleteByCategoryIdPrefix().
+    $this->queue->save($category_id, $payment->id());
+    $this->queue->deleteByCategoryIdPrefix($category_id_prefix);
+    $payment_ids = $this->queue->loadPaymentIds($category_id, $payment->getOwnerId());
+    $this->assertFalse(in_array($payment->id(), $payment_ids));
+
+    // Tests deleteByCategoryId().
+    $this->queue->save($category_id, $payment->id());
+    $this->queue->deleteByCategoryId($category_id);
+    $payment_ids = $this->queue->loadPaymentIds($category_id, $payment->getOwnerId());
+    $this->assertFalse(in_array($payment->id(), $payment_ids));
+  }
+}
diff --git a/payment/payment.api.php b/payment/payment.api.php
index 88cb3f2..31094a8 100644
--- a/payment/payment.api.php
+++ b/payment/payment.api.php
@@ -113,3 +113,18 @@ function hook_payment_execute_access(PaymentInterface $payment, PaymentMethodInt
  * @see \Drupal\payment\Plugin\Payment\Method\Base::executePayment()
  */
 function hook_payment_pre_execute(PaymentInterface $payment) {}
+
+/**
+ * Alters the IDs of payments available for referencing through an instance.
+ *
+ * @param string $field_instance_id
+ *   The ID of the field instance to load payment IDs for.
+ * @param integer $owner_id
+ *   The UID of the user for whom the payment should be available.
+ * @param array $payment_ids
+ *   The IDs of the payments that are available.
+ */
+function hook_payment_reference_queue_payment_ids_alter($field_instance_id, $owner_id, array &$payment_ids) {
+  // Add or remove payment IDs. All IDs MUST be stored in the queue before they
+  // can be used.
+}
diff --git a/payment/payment.install b/payment/payment.install
index d705a63..3f5b3b1 100644
--- a/payment/payment.install
+++ b/payment/payment.install
@@ -146,6 +146,49 @@ function payment_schema() {
     ),
     'primary key' => array('id'),
   );
+  $schema['payment_queue'] = array(
+    'fields' => array(
+      'acquisition_code' => array(
+        'default' => '',
+        'length' => 255,
+        'not null' => TRUE,
+        'type' => 'varchar',
+      ),
+      'category_id' => array(
+        'length' => 255,
+        'not null' => TRUE,
+        'type' => 'varchar',
+      ),
+      'claimed' => array(
+        'default' => 0,
+        'not null' => TRUE,
+        'type' => 'int',
+      ),
+      'payment_id' => array(
+        'default' => 0,
+        'not null' => TRUE,
+        'type' => 'int',
+      ),
+      'queue_id' => array(
+        'length' => 255,
+        'not null' => TRUE,
+        'type' => 'varchar',
+      ),
+    ),
+    'primary key' => array('payment_id'),
+    'foreign keys' => array(
+      'payment_id' => array(
+        'table' => 'payment',
+        'columns' => array(
+          'payment_id' => 'id',
+        ),
+      ),
+    ),
+    'indexes' => array(
+      'category_id' => array('category_id'),
+      'queue_id' => array('queue_id'),
+    ),
+  );
   $schema['payment_status'] = array(
     'fields' => array(
       'created' => array(
diff --git a/payment_reference/lib/Drupal/payment_reference/PaymentReference.php b/payment_reference/lib/Drupal/payment_reference/PaymentReference.php
index 0efcd35..97969d9 100644
--- a/payment_reference/lib/Drupal/payment_reference/PaymentReference.php
+++ b/payment_reference/lib/Drupal/payment_reference/PaymentReference.php
@@ -8,14 +8,14 @@
 namespace Drupal\payment_reference;
 
 /**
- * Static service container wrapper for Payment Reference Field.
+ * Provides wrappers for services.
  */
 class PaymentReference {
 
   /**
-   * Returns the payment reference queue service.
+   * Returns the payment reference queue.
    *
-   * @return \Drupal\payment_reference\QueueInterface
+   * @return \Drupal\payment\QueueInterface
    */
   public static function queue() {
     return \Drupal::service('payment_reference.queue');
diff --git a/payment_reference/lib/Drupal/payment_reference/PaymentReferenceUi.php b/payment_reference/lib/Drupal/payment_reference/PaymentReferenceUi.php
index 0022e68..9e9bb81 100644
--- a/payment_reference/lib/Drupal/payment_reference/PaymentReferenceUi.php
+++ b/payment_reference/lib/Drupal/payment_reference/PaymentReferenceUi.php
@@ -14,6 +14,7 @@ use Drupal\Core\Form\FormBuilder;
 use Drupal\field\FieldInstanceInterface;
 use Drupal\payment\Entity\PaymentInterface;
 use Drupal\payment\Plugin\Payment\LineItem\Manager as PaymentLineItemManager;
+use Drupal\payment\QueueInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\Request;
 
@@ -39,7 +40,7 @@ class PaymentReferenceUi extends ControllerBase implements ContainerInjectionInt
   /**
    * The payment reference queue.
    *
-   * @var \Drupal\payment_reference\QueueInterface
+   * @var \Drupal\payment\QueueInterface
    */
   protected $queue;
 
@@ -48,7 +49,7 @@ class PaymentReferenceUi extends ControllerBase implements ContainerInjectionInt
    *
    * @param \Drupal\Core\Form\FormBuilder $form_builder
    * @param \Drupal\payment\Plugin\Payment\LineItem\Manager $payment_line_item_manager
-   * @param \Drupal\payment_reference\QueueInterface $queue
+   * @param \Drupal\payment\QueueInterface $queue
    */
   public function __construct(FormBuilder $form_builder, PaymentLineItemManager $payment_line_item_manager, QueueInterface $queue) {
     $this->formBuilder = $form_builder;
diff --git a/payment_reference/lib/Drupal/payment_reference/Plugin/Field/FieldType/PaymentReference.php b/payment_reference/lib/Drupal/payment_reference/Plugin/Field/FieldType/PaymentReference.php
index 6f31d0c..ad57674 100644
--- a/payment_reference/lib/Drupal/payment_reference/Plugin/Field/FieldType/PaymentReference.php
+++ b/payment_reference/lib/Drupal/payment_reference/Plugin/Field/FieldType/PaymentReference.php
@@ -8,15 +8,11 @@
 namespace Drupal\payment_reference\Plugin\Field\FieldType;
 
 use Drupal\Component\Utility\NestedArray;
-use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\Core\TypedData\DataDefinitionInterface;
 use Drupal\Core\TypedData\TypedDataInterface;
 use Drupal\currency\Entity\Currency;
 use Drupal\entity_reference\ConfigurableEntityReferenceItem;
 use Drupal\payment\Element\PaymentLineItemsInput;
-use Drupal\payment_reference\PaymentReference as PaymentReferenceServiceWrapper;
-use Symfony\Component\DependencyInjection\ContainerInterface;
-use Symfony\Component\Validator\ConstraintViolation;
 
 /**
  * Provides a configurable payment reference field.
@@ -44,7 +40,7 @@ class PaymentReference extends ConfigurableEntityReferenceItem {
   /**
    * The payment queue.
    *
-   * @var \Drupal\payment_reference\QueueInterface
+   * @var \Drupal\payment\QueueInterface
    */
   protected $queue;
 
@@ -133,9 +129,9 @@ class PaymentReference extends ConfigurableEntityReferenceItem {
   public function preSave() {
     $payment_id = $this->get('target_id')->getValue();
     $queue = $this->getPaymentQueue();
-    $acquisition_code = $queue->claim($payment_id);
+    $acquisition_code = $queue->claimPayment($payment_id);
     if ($acquisition_code !== FALSE) {
-      $queue->acquire($payment_id, $acquisition_code);
+      $queue->acquirePayment($payment_id, $acquisition_code);
     }
     else {
       $this->get('target_id')->setValue(0);
@@ -148,6 +144,6 @@ class PaymentReference extends ConfigurableEntityReferenceItem {
    * @todo Inject this once https://drupal.org/node/2053415 is fixed.
    */
   protected function getPaymentQueue() {
-    return PaymentReferenceServiceWrapper::queue();
+    return \Drupal::service('payment_reference.queue');
   }
 }
diff --git a/payment_reference/lib/Drupal/payment_reference/QueueInterface.php b/payment_reference/lib/Drupal/payment_reference/QueueInterface.php
deleted file mode 100644
index 7620450..0000000
--- a/payment_reference/lib/Drupal/payment_reference/QueueInterface.php
+++ /dev/null
@@ -1,107 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains \Drupal\payment_reference\QueueInterface.
- */
-
-namespace Drupal\payment_reference;
-
-/**
- * Defines a payment reference queue manager.
- */
-interface QueueInterface {
-
-  /**
-   * Saves a payment available for referencing through a field instance.
-   *
-   * @param string $field_instance_id
-   * @param integer $payment_id
-   */
-  public function save($field_instance_id, $payment_id);
-
-  /**
-   * Claims a payment available for referencing through a field instance.
-   *
-   * After a payment has been claimed, it can be definitely acquired with
-   * self::acquire().
-   *
-   * @param integer $payment_id
-   *
-   * @return string|false
-   *   An acquisition code to acquire the payment with on success, or FALSE if
-   *   the payment could not be claimed.
-   */
-  public function claim($payment_id);
-
-  /**
-   * Releases a claimed payment.
-   *
-   * @param integer $payment_id
-   * @param string $acquisition_code
-   *   The code that was received from self::claim().
-   */
-  public function release($payment_id, $acquisition_code);
-
-  /**
-   * Acquires a payment and removes if from the queue.
-   *
-   * @param integer $payment_id
-   * @param string $acquisition_code
-   *   The code that was received from self::reserve().
-   *
-   * @return bool
-   *   Whether the acquisition was successful.
-   */
-  public function acquire($payment_id, $acquisition_code);
-
-  /**
-   * Checks if a payment is available for referencing.
-   *
-   * @param integer $payment_id
-   *   The ID of the payment to check.
-   * @param integer $owner_id
-   *   The UID of the user that should be the payment's owner.
-   *
-   * @return string|null
-   *   NULL if the payment is not available. Otherwise it is the ID of the
-   *   field instance the payment is available for.
-   *
-   * @todo Consider removing this, as payments have an entity reference to field
-   *   instances that can be used.
-   */
-  public function loadFieldInstanceId($payment_id, $owner_id);
-
-  /**
-   * Loads the IDs of payments available for referencing through an instance.
-   *
-   * @param string $field_instance_id
-   *   The ID of the field instance to load payment IDs for.
-   * @param integer $owner_id
-   *   The UID of the user for whom the payment should be available.
-   *
-   * @return array
-   */
-  public function loadPaymentIds($field_instance_id, $owner_id);
-
-  /**
-   * Deletes a payment from the queue by payment ID.
-   *
-   * @param integer $id
-   */
-  public function deleteByPaymentId($id);
-
-  /**
-   * Deletes payments from the queue by field ID.
-   *
-   * @param string $field_id
-   */
-  public function deleteByFieldId($field_id);
-
-  /**
-   * Deletes payments from the queue by field instance ID.
-   *
-   * @param string $field_instance_id
-   */
-  public function deleteByFieldInstanceId($field_instance_id);
-}
diff --git a/payment_reference/lib/Drupal/payment_reference/Tests/PaymentReferenceUiUnitTest.php b/payment_reference/lib/Drupal/payment_reference/Tests/PaymentReferenceUiUnitTest.php
index 00f3ad4..37e9826 100644
--- a/payment_reference/lib/Drupal/payment_reference/Tests/PaymentReferenceUiUnitTest.php
+++ b/payment_reference/lib/Drupal/payment_reference/Tests/PaymentReferenceUiUnitTest.php
@@ -53,7 +53,7 @@ class PaymentReferenceUiUnitTest extends UnitTestCase {
   /**
    * The payment reference queue.
    *
-   * @var \Drupal\payment_reference\QueueInterface|\PHPUnit_Framework_MockObject_MockObject
+   * @var \Drupal\payment\QueueInterface|\PHPUnit_Framework_MockObject_MockObject
    */
   protected $queue;
 
@@ -88,7 +88,7 @@ class PaymentReferenceUiUnitTest extends UnitTestCase {
       ->disableOriginalConstructor()
       ->getMock();
 
-    $this->queue = $this->getMockBuilder('\Drupal\payment_reference\QueueInterface')
+    $this->queue = $this->getMockBuilder('\Drupal\payment\QueueInterface')
       ->disableOriginalConstructor()
       ->getMock();
 
diff --git a/payment_reference/lib/Drupal/payment_reference/Tests/PaymentReferenceUnitTest.php b/payment_reference/lib/Drupal/payment_reference/Tests/PaymentReferenceUnitTest.php
index 5ed49a0..e43d017 100644
--- a/payment_reference/lib/Drupal/payment_reference/Tests/PaymentReferenceUnitTest.php
+++ b/payment_reference/lib/Drupal/payment_reference/Tests/PaymentReferenceUnitTest.php
@@ -2,8 +2,7 @@
 
 /**
  * @file
- * Contains
- * \Drupal\payment_reference\Test\PaymentReferenceUnitTest.
+ * Contains \Drupal\payment_reference\Test\PaymentReferenceUnitTest.
  */
 
 namespace Drupal\payment_reference\Test;
@@ -23,19 +22,17 @@ class PaymentReferenceUnitTest extends UnitTestCase {
   public static function getInfo() {
     return array(
       'description' => '',
-      'group' => 'Payment Reference Field',
+      'group' => 'Payment',
       'name' => '\Drupal\payment_reference\PaymentReference unit test',
     );
   }
 
   /**
-   * Tests queue().
+   * Tests lineItemManager().
    */
-  public function testQueue() {
+  public function testLineItemManager() {
     $container = new Container();
-    $queue = $this->getMockBuilder('\Drupal\payment_reference\Queue')
-      ->disableOriginalConstructor()
-      ->getMock();
+    $queue = $this->getMock('\Drupal\payment\QueueInterface');
     $container->set('payment_reference.queue', $queue);
     \Drupal::setContainer($container);
     $this->assertSame($queue, PaymentReference::queue());
diff --git a/payment_reference/lib/Drupal/payment_reference/Tests/Plugin/Field/FieldType/PaymentReferenceUnitTest.php b/payment_reference/lib/Drupal/payment_reference/Tests/Plugin/Field/FieldType/PaymentReferenceUnitTest.php
index d8d65a2..bb16f01 100644
--- a/payment_reference/lib/Drupal/payment_reference/Tests/Plugin/Field/FieldType/PaymentReferenceUnitTest.php
+++ b/payment_reference/lib/Drupal/payment_reference/Tests/Plugin/Field/FieldType/PaymentReferenceUnitTest.php
@@ -25,7 +25,7 @@ class PaymentReferenceUnitTest extends UnitTestCase {
   /**
    * The payment queue.
    *
-   * @var \Drupal\payment_reference\QueueInterface|\PHPUnit_Framework_MockObject_MockObject
+   * @var \Drupal\payment\QueueInterface|\PHPUnit_Framework_MockObject_MockObject
    */
   protected $queue;
   /**
@@ -50,7 +50,7 @@ class PaymentReferenceUnitTest extends UnitTestCase {
    * {@inheritdoc}
    */
   protected function setUp() {
-    $this->queue = $this->getMock('\Drupal\payment_reference\QueueInterface');
+    $this->queue = $this->getMock('\Drupal\payment\QueueInterface');
 
     $this->targetId = $this->getMock('\Drupal\Core\TypedData\TypedDataInterface');
 
@@ -99,11 +99,11 @@ class PaymentReferenceUnitTest extends UnitTestCase {
       ->method('getValue')
       ->will($this->returnValue($payment_id));
     $this->queue->expects($this->once())
-      ->method('claim')
+      ->method('claimPayment')
       ->with($payment_id)
       ->will($this->returnValue($acquisition_code));
     $this->queue->expects($this->once())
-      ->method('acquire')
+      ->method('acquirePayment')
       ->with($payment_id, $acquisition_code);
     $this->fieldType->preSave();
   }
diff --git a/payment_reference/lib/Drupal/payment_reference/Tests/QueueWebTest.php b/payment_reference/lib/Drupal/payment_reference/Tests/QueueWebTest.php
deleted file mode 100644
index 83938df..0000000
--- a/payment_reference/lib/Drupal/payment_reference/Tests/QueueWebTest.php
+++ /dev/null
@@ -1,140 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains \Drupal\payment_reference\Tests\QueueWebTest.
- */
-
-namespace Drupal\payment_reference\Tests;
-
-
-use Drupal\payment\Generate;
-use Drupal\simpletest\WebTestBase;
-
-/**
- * Tests \Drupal\payment_reference\Queue.
- */
-class QueueWebTest extends WebTestBase {
-
-  /**
-   * The database connection.
-   *
-   * @var \Drupal\Core\Database\Connection
-   */
-  protected $database;
-
-  /**
-   * The payment method plugin manager.
-   *
-   * @var \Drupal\payment\Plugin\Payment\Method\Manager
-   */
-  protected $paymentMethodManager;
-
-  /**
-   * The payment status plugin manager.
-   *
-   * @var \Drupal\payment\Plugin\Payment\Status\Manager
-   */
-  protected $paymentStatusManager;
-
-  /**
-   * The payment reference queue service under test.
-   *
-   * @var \Drupal\payment_reference\Queue
-   */
-  protected $queue;
-
-  /**
-   * {@inheritdoc}
-   */
-  public static $modules = array('payment', 'payment_reference', 'payment_reference_test');
-
-  /**
-   * {@inheritdoc}
-   */
-  static function getInfo() {
-    return array(
-      'description' => '',
-      'group' => 'Payment Reference Field',
-      'name' => '\Drupal\payment_reference\Queue web test',
-    );
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  function setUp() {
-    parent::setUp();
-    $this->database = \Drupal::database();
-    $this->paymentMethodManager = \Drupal::service('plugin.manager.payment.method');
-    $this->paymentStatusManager = \Drupal::service('plugin.manager.payment.status');
-    $this->queue= \Drupal::service('payment_reference.queue');
-  }
-
-  /**
-   * Tests queue service.
-   */
-  function testQueue() {
-    $field_id = 'foo.bar';
-    $field_instance_id = $field_id . '.baz';
-    $payment = Generate::createPayment(2);
-    $payment->setStatus($this->paymentStatusManager->createInstance('payment_success'));
-    $payment->save();
-
-    // Tests save().
-    $this->queue->save($field_instance_id, $payment->id());
-    $payment_ids = $this->queue->loadPaymentIds($field_instance_id, $payment->getOwnerId());
-    $this->assertTrue(in_array($payment->id(), $payment_ids));
-
-    // Tests claim().
-    $this->assertTrue(is_string($this->queue->claim($payment->id())));
-    $this->assertFalse($this->queue->claim($payment->id()));
-    $acquisition_code = $this->queue->claim($payment->id());
-    $this->assertTrue(is_string($acquisition_code));
-
-    // Tests release().
-    $released = $this->queue->release($payment->id(), $acquisition_code);
-    $this->assertTrue($released);
-    $acquisition_code = $this->queue->claim($payment->id());
-    $this->assertTrue(is_string($acquisition_code));
-
-    // Tests acquire().
-    $acquired = $this->queue->acquire($payment->id(), $acquisition_code);
-    $this->assertTrue($acquired);
-    $acquisition_code = $this->queue->claim($payment->id());
-    $this->assertFalse($acquisition_code);
-
-    // Save another payment to the queue, because acquiring the previous one
-    // deleted it.
-    $payment = Generate::createPayment(2);
-    $payment->setStatus($this->paymentStatusManager->createInstance('payment_success'));
-    $payment->save();
-    $this->queue->save($field_instance_id, $payment->id());
-
-    // Tests loadFieldInstanceId().
-    $loaded_field_instance_id = $this->queue->loadFieldInstanceId($payment->id(), $payment->getOwnerId());
-    $this->assertEqual($loaded_field_instance_id, $field_instance_id);
-
-    // Tests loadPaymentIds().
-    $loaded_payment_ids = $this->queue->loadPaymentIds($field_instance_id, $payment->getOwnerId());
-    $this->assertEqual($loaded_payment_ids, array($payment->id()));
-    $this->assertTrue(\Drupal::state()->get('payment_reference_test_payment_reference_queue_payment_ids_alter'));
-
-    // Tests deleteByPaymentId().
-    $this->queue->deleteByPaymentId($payment->id());
-    $payment_ids = $this->queue->loadPaymentIds($field_instance_id, $payment->getOwnerId());
-    $this->assertFalse(in_array($payment->id(), $payment_ids));
-
-    // Tests deleteByFieldId().
-    $this->queue->save($field_instance_id, $payment->id());
-    $this->queue->deleteByFieldId($field_id);
-    $payment_ids = $this->queue->loadPaymentIds($field_instance_id, $payment->getOwnerId());
-    $this->assertFalse(in_array($payment->id(), $payment_ids));
-
-    // Tests deleteByFieldInstanceId().
-    $this->queue->save($field_instance_id, $payment->id());
-    $this->queue->deleteByFieldInstanceId($field_instance_id);
-    $payment_ids = $this->queue->loadPaymentIds($field_instance_id, $payment->getOwnerId());
-    $this->assertFalse(in_array($payment->id(), $payment_ids));
-  }
-}
diff --git a/payment_reference/payment_reference.api.php b/payment_reference/payment_reference.api.php
deleted file mode 100644
index d3fcda3..0000000
--- a/payment_reference/payment_reference.api.php
+++ /dev/null
@@ -1,21 +0,0 @@
-<?php
-
-/**
- * @file
- * Hook documentation.
- */
-
-/**
- * Alters the IDs of payments available for referencing through an instance.
- *
- * @param string $field_instance_id
- *   The ID of the field instance to load payment IDs for.
- * @param integer $owner_id
- *   The UID of the user for whom the payment should be available.
- * @param array $payment_ids
- *   The IDs of the payments that are available.
- */
-function hook_payment_reference_queue_payment_ids_alter($field_instance_id, $owner_id, array &$payment_ids) {
-  // Add or remove payment IDs. All IDs MUST be stored in the queue before they
-  // can be used.
-}
diff --git a/payment_reference/payment_reference.install b/payment_reference/payment_reference.install
deleted file mode 100644
index 8e13f61..0000000
--- a/payment_reference/payment_reference.install
+++ /dev/null
@@ -1,51 +0,0 @@
-<?php
-
-/**
- * @file
- * Installation and uninstallation functions.
- */
-
-/**
- * Implements hook_schema().
- */
-function payment_reference_schema() {
-  $schema['payment_reference'] = array(
-    'fields' => array(
-      'field_instance_id' => array(
-        'type' => 'varchar',
-        'length' => 255,
-        'not null' => TRUE,
-      ),
-      'payment_id' => array(
-        'type' => 'int',
-        'not null' => TRUE,
-        'default' => 0,
-      ),
-      'acquisition_code' => array(
-        'default' => '',
-        'length' => 255,
-        'not null' => TRUE,
-        'type' => 'varchar',
-      ),
-      'claimed' => array(
-        'default' => 0,
-        'not null' => TRUE,
-        'type' => 'int',
-      ),
-    ),
-    'primary key' => array('payment_id'),
-    'foreign keys' => array(
-      'payment_id' => array(
-        'table' => 'payment',
-        'columns' => array(
-          'payment_id' => 'id',
-        ),
-      ),
-    ),
-    'indexes' => array(
-      'field_instance_id' => array('field_instance_id'),
-    ),
-  );
-
-  return $schema;
-}
diff --git a/payment_reference/payment_reference.services.yml b/payment_reference/payment_reference.services.yml
index e635ce2..09a4613 100644
--- a/payment_reference/payment_reference.services.yml
+++ b/payment_reference/payment_reference.services.yml
@@ -1,4 +1,4 @@
 services:
   payment_reference.queue:
-    arguments: ['@database', '@module_handler', '@plugin.manager.payment.status']
-    class: Drupal\payment_reference\Queue
+    arguments: ['payment_reference', '@database', '@module_handler', '@plugin.manager.payment.status']
+    class: Drupal\payment\Queue
\ No newline at end of file
diff --git a/payment_reference_test/payment_reference_test.module b/payment_reference_test/payment_reference_test.module
index 756bf03..e69de29 100644
--- a/payment_reference_test/payment_reference_test.module
+++ b/payment_reference_test/payment_reference_test.module
@@ -1,13 +0,0 @@
-<?php
-
-/**
- * @file
- * Hook implementations and shared functions.
- */
-
-/**
- * Implements hook_payment_reference_queue_payment_ids_alter().
- */
-function payment_reference_test_payment_reference_queue_payment_ids_alter($field_instance_id, $owner_id, array &$payment_ids) {
-  \Drupal::state()->set('payment_reference_test_payment_reference_queue_payment_ids_alter', TRUE);
-}
diff --git a/payment_test/payment_test.module b/payment_test/payment_test.module
index 829b934..8e9e459 100644
--- a/payment_test/payment_test.module
+++ b/payment_test/payment_test.module
@@ -39,3 +39,10 @@ function payment_test_payment_status_set(PaymentInterface $payment, PaymentStatu
 function payment_test_payment_pre_execute(Payment $payment) {
   $payment->payment_test_payment_pre_execute = TRUE;
 }
+
+/**
+ * Implements hook_payment_queue_payment_ids_alter().
+ */
+function payment_test_payment_queue_payment_ids_alter($field_instance_id, $owner_id, array &$payment_ids) {
+  \Drupal::state()->set('payment_test_payment_queue_payment_ids_alter', TRUE);
+}
