CRM-16188, added order api for cancel action with test
authorPradeep Nayak <pradpnayak@gmail.com>
Wed, 27 Jan 2016 21:12:59 +0000 (02:42 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Tue, 16 Feb 2016 17:04:56 +0000 (22:34 +0530)
----------------------------------------
* CRM-16188: Create an order API
  https://issues.civicrm.org/jira/browse/CRM-16188

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

index 36b093d2ac09686dd84d0ee501efcb35bfce8c9a..126748de39e109672c33b827c90066967dc2d6f5 100644 (file)
@@ -148,6 +148,40 @@ function civicrm_api3_order_delete($params) {
   return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
 }
 
+/**
+ * Cancel a Order.
+ *
+ * @param array $params
+ *   Input parameters.
+ *
+ * @return array
+ */
+function civicrm_api3_order_cancel($params) {
+  $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
+  $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses);
+  $result = civicrm_api3('Contribution', 'create', $params);
+  if (!CRM_Utils_Array::value('is_error', $result)) {
+    CRM_Contribute_BAO_Contribution::transitionComponents($params);
+  }
+  return civicrm_api3_create_success(CRM_Utils_Array::value('values', $result), $params, 'Order', 'cancel');
+}
+
+/**
+ * Adjust Metadata for Cancel action.
+ *
+ * The metadata is used for setting defaults, documentation & validation.
+ *
+ * @param array $params
+ *   Array of parameters determined by getfields.
+ */
+function _civicrm_api3_order_cancel_spec(&$params) {
+  $params['contribution_id'] = array(
+    'api.required' => 1 ,
+    'title' => 'Contribution ID',
+    'type' => CRM_Utils_Type::T_INT,
+  );
+}
+
 /**
  * Adjust Metadata for Create action.
  *
index 74b7f77f9c65270365c58c99159386178ca088fe..8934970f0d9e71df67264ed751d0a8057b2035a0 100644 (file)
@@ -457,4 +457,28 @@ class api_v3_OrderTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test cancel order api
+   */
+  public function testCancelOrder() {
+    $contribution = $this->addOrder(FALSE, 100);
+    $params = array(
+      'contribution_id' => $contribution['id'],
+    );
+    $this->callAPISuccess('order', 'cancel', $params);
+    $order = $this->callAPISuccess('Order', 'get', $params);
+    $expectedResult = array(
+      $contribution['id'] => array(
+        'total_amount' => 100,
+        'contribution_id' => $contribution['id'],
+        'contribution_status' => 'Cancelled',
+        'net_amount' => 100,
+      ),
+    );
+    $this->checkPaymentResult($order, $expectedResult);
+    $this->callAPISuccess('Contribution', 'Delete', array(
+      'id' => $contribution['id'],
+    ));
+  }
+
 }