CRM-16188, added order api for create action with test
authorPradeep Nayak <pradpnayak@gmail.com>
Wed, 27 Jan 2016 21:36:48 +0000 (03:06 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Wed, 27 Jan 2016 21:36:48 +0000 (03:06 +0530)
----------------------------------------
* CRM-16188: Create an order API
  https://issues.civicrm.org/jira/browse/CRM-16188

api/v3/Order.php
tests/phpunit/CiviTest/Membership.php
tests/phpunit/api/v3/OrderTest.php

index 4ef1b32b250a862ee83d5b8c1bd743ba160e1291..7580405072b9dfe51216d6acd5584c2c9cbbb7c4 100644 (file)
@@ -59,3 +59,97 @@ function civicrm_api3_order_get($params) {
   $params['sequential'] = $isSequential;
   return civicrm_api3_create_success($contributions, $params, 'Order', 'get');
 }
+
+/**
+ * Add or update a Order.
+ *
+ * @param array $params
+ *   Input parameters.
+ *
+ * @throws API_Exception
+ * @return array
+ *   Api result array
+ */
+function civicrm_api3_order_create(&$params) {
+  $contribution = array();
+  $entity = NULL;
+  $entityIds = array();
+  if (CRM_Utils_Array::value('line_items', $params) && is_array($params['line_items'])) {
+    $priceSetID = NULL;
+    CRM_Contribute_BAO_Contribution::checkLineItems($params);
+    foreach ($params['line_items'] as $lineItems) {
+      $entityParams = CRM_Utils_Array::value('params', $lineItems, array());
+      if (!empty($entityParams) && !empty($lineItems['line_item'])) {
+        $item = reset($lineItems['line_item']);
+        $entity = str_replace('civicrm_', '', $item['entity_table']);
+      }
+      if ($entityParams) {
+        if (in_array($entity, array('participant', 'membership'))) {
+          $entityParams['skipLineItem'] = TRUE;
+          $entityResult = civicrm_api3($entity, 'create', $entityParams);
+          $params['contribution_mode'] = $entity;
+          $entityIds[] = $params[$entity . '_id'] = $entityResult['id'];
+          foreach ($lineItems['line_item'] as &$items) {
+            $items['entity_id'] = $entityResult['id'];
+          }
+        }
+        else {
+          // pledge payment
+        }
+      }
+      if (empty($priceSetID)) {
+        $item = reset($lineItems['line_item']);
+        $priceSetID = civicrm_api3('PriceField', 'getvalue', array(
+          'return' => 'price_set_id',
+          'id' => $item['price_field_id'],
+        ));
+        $params['line_item'][$priceSetID] = array();
+      }
+      $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']);
+    }
+  }
+  $contribution = civicrm_api3('Contribution', 'create', $params);
+  // add payments
+  if ($entity && CRM_Utils_Array::value('id', $contribution)) {
+    foreach ($entityIds as $entityId) {
+      $paymentParams = array(
+        'contribution_id' => $contribution['id'],
+        $entity . '_id' => $entityId,
+      );
+      // if entity is pledge then build pledge param
+      if ($entity == 'pledge') {
+        $paymentParams += $entityParams;
+      }
+      $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams);
+    }
+  }
+  return civicrm_api3_create_success(CRM_Utils_Array::value('values', $contribution), $params, 'Order', 'create');
+}
+
+/**
+ * Adjust Metadata for Create action.
+ *
+ * The metadata is used for setting defaults, documentation & validation.
+ *
+ * @param array $params
+ *   Array of parameters determined by getfields.
+ */
+function _civicrm_api3_order_create_spec(&$params) {
+  $params['contact_id'] = array(
+    'name' => 'contact_id',
+    'title' => 'Contact ID',
+    'type' => CRM_Utils_Type::T_INT,
+    'api.required' => TRUE,
+  );
+  $params['total_amount'] = array(
+    'name' => 'total_amount',
+    'title' => 'Total Amount',
+    'api.required' => TRUE,
+  );
+  $params['financial_type_id'] = array(
+    'name' => 'financial_type_id',
+    'title' => 'Financial Type',
+    'type' => CRM_Utils_Type::T_INT,
+    'api.required' => TRUE,
+  );
+}
index cec5c4e4edaf21a82bb8782c9c988765ed866493..5ec005d9ec6170a78563f944b8a5adacd7877ba2 100644 (file)
@@ -31,6 +31,7 @@ class Membership extends PHPUnit_Framework_Testcase {
       'is_active' => 1,
       'contact_check' => 1,
       'relationship_direction' => 'a_b',
+      'member_of_contact_id' => $orgId,
     );
 
     $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);
index 4585cdd8745fae8a9cb698fa9c1ac22aeb453255..e187dfd56242bbc90969ac26a9a85957216df3b2 100644 (file)
@@ -171,4 +171,269 @@ class api_v3_OrderTest extends CiviUnitTestCase {
     return $this->callAPISuccess('Contribution', 'create', $p);
   }
 
+  /**
+   * Test create order api
+   */
+  public function testAddOrder() {
+    $order = $this->addOrder(FALSE, 100);
+    $params = array(
+      'contribution_id' => $order['id'],
+    );
+    $order = $this->callAPISuccess('order', 'get', $params);
+    $expectedResult = array(
+      $order['id'] => array(
+        'total_amount' => 100,
+        'contribution_id' => $order['id'],
+        'contribution_status' => 'Completed',
+        'net_amount' => 100,
+      ),
+    );
+    $lineItems[] = array(
+      'entity_table' => 'civicrm_contribution',
+      'entity_id' => $order['id'],
+      'contribution_id' => $order['id'],
+      'unit_price' => 100,
+      'line_total' => 100,
+      'financial_type_id' => 1,
+    );
+    $this->checkPaymentResult($order, $expectedResult, $lineItems);
+    $this->callAPISuccess('Contribution', 'Delete', array(
+      'id' => $order['id'],
+    ));
+  }
+
+  /**
+   * Test create order api for membership
+   */
+  public function testAddOrderForMembership() {
+    require_once 'CiviTest/Membership.php';
+    $membership = new Membership();
+    $membershipType = $membership->createMembershipType();
+    $membershipType1 = $membership->createMembershipType();
+    $membershipType = $membershipTypes = array($membershipType->id, $membershipType1->id);
+    $p = array(
+      'contact_id' => $this->_individualId,
+      'receive_date' => '2010-01-20',
+      'total_amount' => 200,
+      'financial_type_id' => $this->_financialTypeId,
+      'contribution_status_id' => 1,
+    );
+    $priceFields = $this->createPriceSet();
+    foreach ($priceFields['values'] as $key => $priceField) {
+      $lineItems[$key] = array(
+        'price_field_id' => $priceField['price_field_id'],
+        'price_field_value_id' => $priceField['id'],
+        'label' => $priceField['label'],
+        'field_title' => $priceField['label'],
+        'qty' => 1,
+        'unit_price' => $priceField['amount'],
+        'line_total' => $priceField['amount'],
+        'financial_type_id' => $priceField['financial_type_id'],
+        'entity_table' => 'civicrm_membership',
+        'membership_type_id' => array_pop($membershipType),
+      );
+    }
+    $p['line_items'][] = array(
+      'line_item' => array(array_pop($lineItems)),
+      'params' => array(
+        'contact_id' => $this->_individualId,
+        'membership_type_id' => array_pop($membershipTypes),
+        'join_date' => '2006-01-21',
+        'start_date' => '2006-01-21',
+        'end_date' => '2006-12-21',
+        'source' => 'Payment',
+        'is_override' => 1,
+        'status_id' => 1,
+      ),
+    );
+    $order = $this->callAPISuccess('order', 'create', $p);
+    $params = array(
+      'contribution_id' => $order['id'],
+    );
+    $order = $this->callAPISuccess('order', 'get', $params);
+    $expectedResult = array(
+      $order['id'] => array(
+        'total_amount' => 200,
+        'contribution_id' => $order['id'],
+        'contribution_status' => 'Completed',
+        'net_amount' => 200,
+      ),
+    );
+    $this->checkPaymentResult($order, $expectedResult);
+    $this->callAPISuccessGetCount('MembershipPayment', $params, 1);
+    $this->callAPISuccess('Contribution', 'Delete', array(
+      'id' => $order['id'],
+    ));
+    $p['line_items'][] = array(
+      'line_item' => array(array_pop($lineItems)),
+      'params' => array(
+        'contact_id' => $this->_individualId,
+        'membership_type_id' => array_pop($membershipTypes),
+        'join_date' => '2006-01-21',
+        'start_date' => '2006-01-21',
+        'end_date' => '2006-12-21',
+        'source' => 'Payment',
+        'is_override' => 1,
+        'status_id' => 1,
+      ),
+    );
+    $p['total_amount'] = 300;
+    $order = $this->callAPISuccess('order', 'create', $p);
+    $expectedResult = array(
+      $order['id'] => array(
+        'total_amount' => 300,
+        'contribution_status' => 'Completed',
+        'net_amount' => 300,
+      ),
+    );
+    $paymentMembership = array(
+      'contribution_id' => $order['id'],
+    );
+    $order = $this->callAPISuccess('order', 'get', $paymentMembership);
+    $this->checkPaymentResult($order, $expectedResult);
+    $this->callAPISuccessGetCount('MembershipPayment', $paymentMembership, 2);
+    $this->callAPISuccess('Contribution', 'Delete', array(
+      'id' => $order['id'],
+    ));
+  }
+
+  /**
+   * Test create order api for participant
+   */
+  public function testAddOrderForPariticipant() {
+    require_once 'CiviTest/Event.php';
+    $this->_eventId = Event::create($this->_individualId);
+    $p = array(
+      'contact_id' => $this->_individualId,
+      'receive_date' => '2010-01-20',
+      'total_amount' => 300,
+      'financial_type_id' => $this->_financialTypeId,
+      'contribution_status_id' => 1,
+    );
+    $priceFields = $this->createPriceSet();
+    foreach ($priceFields['values'] as $key => $priceField) {
+      $lineItems[$key] = array(
+        'price_field_id' => $priceField['price_field_id'],
+        'price_field_value_id' => $priceField['id'],
+        'label' => $priceField['label'],
+        'field_title' => $priceField['label'],
+        'qty' => 1,
+        'unit_price' => $priceField['amount'],
+        'line_total' => $priceField['amount'],
+        'financial_type_id' => $priceField['financial_type_id'],
+        'entity_table' => 'civicrm_participant',
+      );
+    }
+    $p['line_items'][] = array(
+      'line_item' => $lineItems,
+      'params' => array(
+        'contact_id' => $this->_individualId,
+        'event_id' => $this->_eventId,
+        'status_id' => 1,
+        'role_id' => 1,
+        'register_date' => '2007-07-21 00:00:00',
+        'source' => 'Online Event Registration: API Testing',
+      ),
+    );
+    $order = $this->callAPISuccess('order', 'create', $p);
+    $params = array(
+      'contribution_id' => $order['id'],
+    );
+    $order = $this->callAPISuccess('order', 'get', $params);
+    $expectedResult = array(
+      $order['id'] => array(
+        'total_amount' => 300,
+        'contribution_id' => $order['id'],
+        'contribution_status' => 'Completed',
+        'net_amount' => 300,
+      ),
+    );
+    $this->checkPaymentResult($order, $expectedResult);
+    $this->callAPISuccessGetCount('ParticipantPayment', $params, 1);
+    $this->callAPISuccess('Contribution', 'Delete', array(
+      'id' => $order['id'],
+    ));
+    $p['line_items'][] = array(
+      'line_item' => $lineItems,
+      'params' => array(
+        'contact_id' => $this->individualCreate(),
+        'event_id' => $this->_eventId,
+        'status_id' => 1,
+        'role_id' => 1,
+        'register_date' => '2007-07-21 00:00:00',
+        'source' => 'Online Event Registration: API Testing',
+      ),
+    );
+    $p['total_amount'] = 600;
+    $order = $this->callAPISuccess('order', 'create', $p);
+    $expectedResult = array(
+      $order['id']=> array(
+        'total_amount' => 600,
+        'contribution_status' => 'Completed',
+        'net_amount' => 600,
+      ),
+    );
+    $paymentParticipant = array(
+      'contribution_id' => $order['id'],
+    );
+    $order = $this->callAPISuccess('order', 'get', $paymentParticipant);
+    $this->checkPaymentResult($order, $expectedResult);
+    $this->callAPISuccessGetCount('ParticipantPayment', $paymentParticipant, 2);
+    $this->callAPISuccess('Contribution', 'Delete', array(
+      'id' => $order['id'],
+    ));
+  }
+
+  /**
+   * Test create order api with line items
+   */
+  public function testAddOrderWithLineItems() {
+    $order = $this->addOrder(TRUE);
+    $params = array(
+      'contribution_id' => $order['id'],
+    );
+    $order = $this->callAPISuccess('order', 'get', $params);
+    $expectedResult = array(
+      $order['id'] => array(
+        'total_amount' => 300,
+        'contribution_id' => $order['id'],
+        'contribution_status' => 'Completed',
+        'net_amount' => 300,
+      ),
+    );
+    $items[] = array(
+      'entity_table' => 'civicrm_contribution',
+      'entity_id' => $order['id'],
+      'contribution_id' => $order['id'],
+      'unit_price' => 100,
+      'line_total' => 100,
+    );
+    $items[] = array(
+      'entity_table' => 'civicrm_contribution',
+      'entity_id' => $order['id'],
+      'contribution_id' => $order['id'],
+      'unit_price' => 200,
+      'line_total' => 200,
+    );
+    $this->checkPaymentResult($order, $expectedResult, $items);
+    $params = array(
+      'entity_table' => 'civicrm_contribution',
+      'entity_id' => $order['id'],
+    );
+    $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
+    $this->assertEquals($eft['values'][$eft['id']]['amount'], 300);
+    $params = array(
+      'entity_table' => 'civicrm_financial_item',
+      'financial_trxn_id' => $eft['values'][$eft['id']]['financial_trxn_id'],
+    );
+    $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
+    $amounts = array(200, 100);
+    foreach ($eft['values'] as $value) {
+      $this->assertEquals($value['amount'], array_pop($amounts));
+    }
+    $this->callAPISuccess('Contribution', 'Delete', array(
+      'id' => $order['id'],
+    ));
+  }
+
 }