From b7f554fe648948495bc9968dd3cb3616b29b4145 Mon Sep 17 00:00:00 2001 From: Edsel Date: Fri, 15 Jan 2016 12:51:24 +0530 Subject: [PATCH] CRM-16259 CIVI-3 Added payment API test ---------------------------------------- * CRM-16259: Create Payment API https://issues.civicrm.org/jira/browse/CRM-16259 --- tests/phpunit/api/v3/PaymentTest.php | 314 +++++++++++++++++++++++++++ 1 file changed, 314 insertions(+) create mode 100644 tests/phpunit/api/v3/PaymentTest.php diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php new file mode 100644 index 0000000000..5f610f1ec3 --- /dev/null +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -0,0 +1,314 @@ +_apiversion = 3; + $this->_individualId = $this->individualCreate(); + $this->_params = array( + 'contact_id' => $this->_individualId, + 'receive_date' => '20120511', + 'total_amount' => 100.00, + 'financial_type_id' => $this->_financialTypeId, + 'non_deductible_amount' => 10.00, + 'fee_amount' => 5.00, + 'net_amount' => 95.00, + 'source' => 'SSF', + 'contribution_status_id' => 1, + ); + $this->_processorParams = array( + 'domain_id' => 1, + 'name' => 'Dummy', + 'payment_processor_type_id' => 10, + 'financial_account_id' => 12, + 'is_active' => 1, + 'user_name' => '', + 'url_site' => 'http://dummy.com', + 'url_recur' => 'http://dummy.com', + 'billing_mode' => 1, + ); + } + + /** + * Clean up after each test. + */ + public function tearDown() { + $this->quickCleanUpFinancialEntities(); + $this->quickCleanup(array('civicrm_uf_match')); + } + + /** + * Test Get. + */ + public function testGetPayment() { + $p = array( + 'contact_id' => $this->_individualId, + 'receive_date' => '2010-01-20', + 'total_amount' => 100.00, + 'financial_type_id' => $this->_financialTypeId, + 'trxn_id' => 23456, + 'contribution_status_id' => 1, + ); + $contribution = $this->callAPISuccess('contribution', 'create', $p); + + $params = array( + 'contribution_id' => $contribution['id'], + ); + + $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__); + + $this->assertEquals(1, $payment['count']); + $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 100.00); + $this->assertEquals($payment['values'][$payment['id']]['trxn_id'], 23456); + $this->assertEquals($payment['values'][$payment['id']]['trxn_date'], '2010-01-20 00:00:00'); + $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1); + $this->assertEquals($payment['values'][$payment['id']]['contribution_id'], $contribution['id']); + $this->callAPISuccess('Contribution', 'Delete', array( + 'id' => $contribution['id'], + )); + } + + public function testCreatePaymentNoLineItems() { + + $params = array( + 'contact_id' => $this->_individualId, + 'receive_date' => '20120511', + 'total_amount' => 200.00, + 'financial_type_id' => $this->_financialTypeId, + 'payment_instrument_id' => 1, + 'contribution_status_id' => 8, + ); + + $contribution = $this->callAPISuccess('contribution', 'create', $params); //Create partially paid contribution + + //Create partial payment + $params = array( + 'contribution_id' => $contribution['id'], + 'total_amount' => 150, + ); + $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__); + + $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7); + $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6); + $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 150); + $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1); + $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1); + + // Check entity financial trxn created properly + $params = array( + 'entity_id' => $contribution['id'], + 'entity_table' => 'civicrm_contribution', + 'financial_trxn_id' => $payment['id'], + ); + + $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); + + $this->assertEquals($eft['values'][$eft['id']]['amount'], 150); + + // Now create payment to complete total amount of contribution + $params = array( + 'contribution_id' => $contribution['id'], + 'total_amount' => 50, + ); + $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__); + + $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7); + $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6); + $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 50); + $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1); + $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1); + + // Check contribution for completed status + $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'])); + + $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); + $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00); + + $this->callAPISuccess('Contribution', 'Delete', array( + 'id' => $contribution['id'], + )); + } + + public function testCreatePaymentLineItems() { + + // Create priceset & price fields + $this->createPriceSet(); + + $params = array( + 'contact_id' => $this->_individualId, + 'receive_date' => '20120511', + 'total_amount' => 200.00, + 'financial_type_id' => $this->_financialTypeId, + 'payment_instrument_id' => 1, + 'contribution_status_id' => 8, + ); + + $contribution = $this->callAPISuccess('contribution', 'create', $params); //Create partially paid contribution + + //Create partial payment + $params = array( + 'contribution_id' => $contribution['id'], + 'total_amount' => 150, + ); + $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__); + + $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7); + $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6); + $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 150); + $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1); + $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1); + + // Check entity financial trxn created properly + $params = array( + 'entity_id' => $contribution['id'], + 'entity_table' => 'civicrm_contribution', + 'financial_trxn_id' => $payment['id'], + ); + + $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); + + $this->assertEquals($eft['values'][$eft['id']]['amount'], 150); + + // Now create payment to complete total amount of contribution + $params = array( + 'contribution_id' => $contribution['id'], + 'total_amount' => 50, + ); + $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__); + + $this->assertEquals($payment['values'][$payment['id']]['from_financial_account_id'], 7); + $this->assertEquals($payment['values'][$payment['id']]['to_financial_account_id'], 6); + $this->assertEquals($payment['values'][$payment['id']]['total_amount'], 50); + $this->assertEquals($payment['values'][$payment['id']]['status_id'], 1); + $this->assertEquals($payment['values'][$payment['id']]['is_payment'], 1); + + // Check contribution for completed status + $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'])); + + $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); + $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00); + + $this->callAPISuccess('Contribution', 'Delete', array( + 'id' => $contribution['id'], + )); + } + + public function cleanUpAfterPriceSets() { + $this->quickCleanUpFinancialEntities(); + $this->contactDelete($this->_ids['contact']); + } + + + /** + * Create price set. + * + * + * @param $entity + * @param array $params + */ + public function createPriceSet() { + $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array( + 'title' => "Test Contribution Page", + 'financial_type_id' => 1, + 'currency' => 'NZD', + 'is_pay_later' => 1, + 'is_monetary' => TRUE, + 'is_email_receipt' => FALSE, + )); + $priceSet = $this->callAPISuccess('price_set', 'create', array( + 'is_quick_config' => 0, + 'extends' => 'CiviContribute', + 'financial_type_id' => 1, + 'title' => 'My Test Price Set', + )); + $priceSetID = $priceSet['id']; + CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID); + + $priceField = $this->callAPISuccess('price_field', 'create', array( + 'price_set_id' => $priceSetID, + 'label' => 'Goat Breed', + 'html_type' => 'Radio', + )); + $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array( + 'price_set_id' => $priceSetID, + 'price_field_id' => $priceField['id'], + 'label' => 'Long Haired Goat', + 'amount' => 50, + 'financial_type_id' => 'Donation', + ) + ); + $this->_priceIds['price_field_value'] = array($priceFieldValue['id']); + $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array( + 'price_set_id' => $priceSetID, + 'price_field_id' => $priceField['id'], + 'label' => 'Shoe-eating Goat', + 'amount' => 150, + 'financial_type_id' => 'Donation', + ) + ); + $this->_priceIds['price_field_value'][] = $priceFieldValue['id']; + $this->_priceIds['price_set'] = $priceSetID; + $this->_priceIds['price_field'] = $priceField['id']; + } + +} -- 2.25.1