$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,
+ );
+}
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'],
+ ));
+ }
+
}