From: Pradeep Nayak Date: Wed, 27 Jan 2016 21:12:59 +0000 (+0530) Subject: CRM-16188, added order api for cancel action with test X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=65f7f9f6ec7c6ec3d3db8fbe54f99316301c2860;p=civicrm-core.git CRM-16188, added order api for cancel action with test ---------------------------------------- * CRM-16188: Create an order API https://issues.civicrm.org/jira/browse/CRM-16188 --- 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'], + )); + } + }