Skip to content

Commit

Permalink
Merge pull request #434 from colemanw/update_civicrm_versions
Browse files Browse the repository at this point in the history
Update civicrm versions and copy over Transaction API into webform civicrm code base
  • Loading branch information
mattwire authored Jan 17, 2021
2 parents 51d0263 + 55255e5 commit b9e2dc7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
60 changes: 59 additions & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,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();
Expand All @@ -668,6 +674,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
*
Expand Down
2 changes: 1 addition & 1 deletion tests/src/FunctionalJavascript/CiviCrmTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
34 changes: 32 additions & 2 deletions tests/src/FunctionalJavascript/ContributionPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ")),
Expand Down Expand Up @@ -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 = []);

Expand Down Expand Up @@ -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.');

Expand Down

0 comments on commit b9e2dc7

Please sign in to comment.