diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6d319b371..8be40cf41 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,11 +12,11 @@ jobs: matrix: include: - drupal: '^8.9' - civicrm: '~5.32' + civicrm: '~5.33' - drupal: '^8.9' - civicrm: '5.33.x-dev' + civicrm: '5.34.x-dev' - drupal: '^9.0' - civicrm: '5.33.x-dev' + civicrm: '5.34.x-dev' name: Drupal ${{ matrix.drupal }} | CiviCRM ${{ matrix.civicrm }} services: mysql: diff --git a/src/Utils.php b/src/Utils.php index 6c4fa5661..a439065f3 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -638,7 +638,13 @@ function wf_civicrm_api($entity, $operation, $params) { $params += array( 'check_permissions' => FALSE, ); - $result = civicrm_api3($entity, $operation, $params); + if ($operation == 'transact') { + $utils = \Drupal::service('webform_civicrm.utils'); + $result = $utils->wf_civicrm_api3_contribution_transact($params); + } + else { + $result = civicrm_api3($entity, $operation, $params); + } // I guess we want silent errors for getoptions b/c we check it for failure separately if (!empty($result['is_error']) && $operation != 'getoptions') { $bt = debug_backtrace(); @@ -662,6 +668,58 @@ function wf_civicrm_api($entity, $operation, $params) { return $result; } + /** + * Adjust Metadata for Transact action. + * + * The metadata is used for setting defaults, documentation & validation. + * + * @param array $params + * Array of parameters determined by getfields. + */ + function _wf_civicrm_api3_contribution_transact_spec(&$params) { + $fields = civicrm_api3('Contribution', 'getfields', ['action' => 'create']); + $params = array_merge($params, $fields['values']); + $params['receive_date']['api.default'] = 'now'; + } + + /** + * Process a transaction and record it against the contact. + * + * @deprecated + * + * @param array $params + * Input parameters. + * + * @return array + * contribution of created or updated record (or a civicrm error) + */ + function wf_civicrm_api3_contribution_transact($params) { + // CRM_Core_Error::deprecatedFunctionWarning('The contibution.transact api is unsupported & known to have issues. Please see the section at the bottom of https://docs.civicrm.org/dev/en/latest/financial/OrderAPI/ for getting off it'); + // Set some params specific to payment processing + // @todo - fix this function - none of the results checked by civicrm_error would ever be an array with + // 'is_error' set + // also trxn_id is not saved. + // but since there is no test it's not desirable to jump in & make the obvious changes. + $params['payment_processor_mode'] = empty($params['is_test']) ? 'live' : 'test'; + $params['amount'] = $params['total_amount']; + if (!isset($params['net_amount'])) { + $params['net_amount'] = $params['amount']; + } + if (!isset($params['invoiceID']) && isset($params['invoice_id'])) { + $params['invoiceID'] = $params['invoice_id']; + } + + // Some payment processors expect a unique invoice_id - generate one if not supplied + $params['invoice_id'] = \CRM_Utils_Array::value('invoice_id', $params, md5(uniqid(rand(), TRUE))); + + $paymentProcessor = \CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']); + $paymentProcessor['object']->doPayment($params); + + $params['payment_instrument_id'] = $paymentProcessor['object']->getPaymentInstrumentID(); + + return civicrm_api3('Contribution', 'create', $params); + } + /** * Get the values from an api call * diff --git a/tests/src/FunctionalJavascript/CiviCrmTestBase.php b/tests/src/FunctionalJavascript/CiviCrmTestBase.php index 847342c91..73f19d095 100644 --- a/tests/src/FunctionalJavascript/CiviCrmTestBase.php +++ b/tests/src/FunctionalJavascript/CiviCrmTestBase.php @@ -8,7 +8,7 @@ // Requires patching for civicrm-core. // @see https://github.com/civicrm/civicrm-core/pull/18843 // @see https://lab.civicrm.org/dev/core/-/issues/2140 -// @todo move into civicrm-drupal-8 package. +// @todo move into civicrm-drupal-8 package - DONE abstract class CiviCrmTestBase extends WebDriverTestBase { protected $defaultTheme = 'classy'; diff --git a/tests/src/FunctionalJavascript/ContributionPageTest.php b/tests/src/FunctionalJavascript/ContributionPageTest.php index 264a53da8..aceb92abc 100644 --- a/tests/src/FunctionalJavascript/ContributionPageTest.php +++ b/tests/src/FunctionalJavascript/ContributionPageTest.php @@ -36,8 +36,36 @@ private function createPaymentProcessor() { return current($result['values']); } + private function createiATSPaymentProcessor() { + // Download installs and enables! + $result = civicrm_api3('Extension', 'download', [ + 'key' => "com.iatspayments.civicrm", + ]); + $params = [ + 'domain_id' => 1, + 'name' => 'iATS Credit Card - TE4188', + 'payment_processor_type_id' => 'iATS Payments Credit Card', + 'financial_account_id' => 12, + 'is_test' => FALSE, + 'is_active' => 1, + 'user_name' => 'TE4188', + 'password' => 'abcde01', + 'url_site' => 'https://www.iatspayments.com/NetGate/ProcessLinkv2.asmx?WSDL', + 'url_recur' => 'https://www.iatspayments.com/NetGate/ProcessLinkv2.asmx?WSDL', + 'class_name' => 'Payment_iATSService', + 'is_recur' => 1, + 'sequential' => 1, + 'payment_type' => 1, + 'payment_instrument_id' => 'Credit Card', + ]; + $utils = \Drupal::service('webform_civicrm.utils'); + $result = $utils->wf_civicrm_api('payment_processor', 'create', $params); + $this->assertEquals(0, $result['is_error']); + $this->assertEquals(1, $result['count']); + return current($result['values']); + } + private function setupSalesTax(int $financialTypeId, $accountParams = []) { - // https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/CiviTest/CiviUnitTestCase.php#L3104 $params = array_merge([ 'name' => 'Sales tax account ' . substr(sha1(rand()), 0, 4), 'financial_account_type_id' => key(\CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' ")), @@ -66,7 +94,8 @@ private function setupSalesTax(int $financialTypeId, $accountParams = []) { } public function testSubmitContribution() { - $payment_processor = $this->createPaymentProcessor(); + // ToDo: call createiATSPaymentProcessor() + $payment_processor = $this->createiATSPaymentProcessor(); $financialAccount = $this->setupSalesTax(2, $accountParams = []); @@ -151,6 +180,7 @@ public function testSubmitContribution() { $this->getSession()->getPage()->fillField('Postal Code', '53177'); $this->getSession()->getPage()->pressButton('Submit'); + $this->htmlOutput(); $this->assertPageNoErrorMessages(); $this->assertSession()->pageTextContains('New submission added to CiviCRM Webform Test.');