From 65f7f9f6ec7c6ec3d3db8fbe54f99316301c2860 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Thu, 28 Jan 2016 02:42:59 +0530 Subject: [PATCH] CRM-16188, added order api for cancel action with test ---------------------------------------- * CRM-16188: Create an order API https://issues.civicrm.org/jira/browse/CRM-16188 --- api/v3/Order.php | 34 ++++++++++++++++++++++++++++++ tests/phpunit/api/v3/OrderTest.php | 24 +++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/api/v3/Order.php b/api/v3/Order.php index 36b093d2ac..126748de39 100644 --- a/api/v3/Order.php +++ b/api/v3/Order.php @@ -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. * diff --git a/tests/phpunit/api/v3/OrderTest.php b/tests/phpunit/api/v3/OrderTest.php index 74b7f77f9c..8934970f0d 100644 --- a/tests/phpunit/api/v3/OrderTest.php +++ b/tests/phpunit/api/v3/OrderTest.php @@ -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'], + )); + } + } -- 2.25.1