diff --git a/payment/uc_credit/config/install/uc_credit.settings.yml b/payment/uc_credit/config/install/uc_credit.settings.yml index 2761b77..fc844d1 100644 --- a/payment/uc_credit/config/install/uc_credit.settings.yml +++ b/payment/uc_credit/config/install/uc_credit.settings.yml @@ -15,5 +15,3 @@ visa: true mastercard: true discover: true amex: true -#uc_pg_' . $id . '_enabled', TRUE), -#uc_pg_' . $id . '_cc_txn_type', UC_CREDIT_AUTH_CAPTURE), diff --git a/payment/uc_credit/src/Plugin/Ubercart/PaymentMethod/CreditCard.php b/payment/uc_credit/src/CreditCardPaymentMethodBase.php similarity index 55% rename from payment/uc_credit/src/Plugin/Ubercart/PaymentMethod/CreditCard.php rename to payment/uc_credit/src/CreditCardPaymentMethodBase.php index a797963..958c6c7 100644 --- a/payment/uc_credit/src/Plugin/Ubercart/PaymentMethod/CreditCard.php +++ b/payment/uc_credit/src/CreditCardPaymentMethodBase.php @@ -2,31 +2,38 @@ /** * @file - * Contains \Drupal\uc_credit\Plugin\Ubercart\PaymentMethod\CreditCard. + * Contains \Drupal\uc_credit\CreditCardPaymentMethodBase. */ -namespace Drupal\uc_credit\Plugin\Ubercart\PaymentMethod; +namespace Drupal\uc_credit; +use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; use Drupal\uc_order\OrderInterface; use Drupal\uc_payment\PaymentMethodPluginBase; use Drupal\uc_store\Encryption; /** - * Defines the check payment method. - * - * @PaymentMethod( - * id = "credit", - * name = @Translation("Credit card"), - * title = @Translation("Credit card"), - * checkout = TRUE, - * no_gateway = FALSE, - * settings_form = "Drupal\uc_credit\Form\CreditSettingsForm", - * weight = 2, - * ) + * Defines a base credit card payment method plugin implementation. */ -class CreditCard extends PaymentMethodPluginBase { +abstract class CreditCardPaymentMethodBase extends PaymentMethodPluginBase { + + /** + * Returns the set of fields which are used by this payment method. + * + * @return array + * An array with keys 'cvv', 'owner', 'start', 'issue', 'bank' and 'type'. + */ + protected function getEnabledFields() { + return [ + 'cvv' => TRUE, + 'owner' => FALSE, + 'start' => FALSE, + 'issue' => FALSE, + 'bank' => FALSE, + 'type' => FALSE, + ]; + } /** * {@inheritdoc} @@ -40,25 +47,27 @@ class CreditCard extends PaymentMethodPluginBase { * {@inheritdoc} */ public function cartReview(OrderInterface $order) { - $credit_config = \Drupal::config('uc_credit.settings'); - if ($credit_config->get('uc_credit_type_enabled')) { + $fields = $this->getEnabledFields(); + + if (!empty($fields['type'])) { $review[] = array('title' => t('Card type'), 'data' => SafeMarkup::checkPlain($order->payment_details['cc_type'])); } - if ($credit_config->get('uc_credit_owner_enabled')) { + if (!empty($fields['owner'])) { $review[] = array('title' => t('Card owner'), 'data' => SafeMarkup::checkPlain($order->payment_details['cc_owner'])); } $review[] = array('title' => t('Card number'), 'data' => uc_credit_display_number($order->payment_details['cc_number'])); - if ($credit_config->get('uc_credit_start_enabled')) { + if (!empty($fields['start'])) { $start = $order->payment_details['cc_start_month'] . '/' . $order->payment_details['cc_start_year']; $review[] = array('title' => t('Start date'), 'data' => strlen($start) > 1 ? $start : ''); } $review[] = array('title' => t('Expiration'), 'data' => $order->payment_details['cc_exp_month'] . '/' . $order->payment_details['cc_exp_year']); - if ($credit_config->get('uc_credit_issue_enabled')) { + if (!empty($fields['issue'])) { $review[] = array('title' => t('Issue number'), 'data' => $order->payment_details['cc_issue']); } - if ($credit_config->get('uc_credit_bank_enabled')) { + if (!empty($fields['bank'])) { $review[] = array('title' => t('Issuing bank'), 'data' => SafeMarkup::checkPlain($order->payment_details['cc_bank'])); } + return $review; } @@ -66,67 +75,67 @@ class CreditCard extends PaymentMethodPluginBase { * {@inheritdoc} */ public function orderView(OrderInterface $order) { - $build = array(); - - // Add the hidden span for the CC details if possible. - $account = \Drupal::currentUser(); - if ($account->hasPermission('view cc details')) { - $rows = array(); - - if (!empty($order->payment_details['cc_type'])) { - $rows[] = t('Card type') . ': ' . SafeMarkup::checkPlain($order->payment_details['cc_type']); - } - - if (!empty($order->payment_details['cc_owner'])) { - $rows[] = t('Card owner') . ': ' . SafeMarkup::checkPlain($order->payment_details['cc_owner']); - } - - if (!empty($order->payment_details['cc_number'])) { - $rows[] = t('Card number') . ': ' . uc_credit_display_number($order->payment_details['cc_number']); - } - - if (!empty($order->payment_details['cc_start_month']) && !empty($order->payment_details['cc_start_year'])) { - $rows[] = t('Start date') . ': ' . $order->payment_details['cc_start_month'] . '/' . $order->payment_details['cc_start_year']; - } - - if (!empty($order->payment_details['cc_exp_month']) && !empty($order->payment_details['cc_exp_year'])) { - $rows[] = t('Expiration') . ': ' . $order->payment_details['cc_exp_month'] . '/' . $order->payment_details['cc_exp_year']; - } - - if (!empty($order->payment_details['cc_issue'])) { - $rows[] = t('Issue number') . ': ' . SafeMarkup::checkPlain($order->payment_details['cc_issue']); - } - - if (!empty($order->payment_details['cc_bank'])) { - $rows[] = t('Issuing bank') . ': ' . SafeMarkup::checkPlain($order->payment_details['cc_bank']); - } - - $build['cc_info'] = array( - '#prefix' => '' . t('Show card details') . '
', - '#markup' => implode('
', $rows), - '#suffix' => '
', - ); - - // Add the form to process the card if applicable. - if ($account->hasPermission('process credit cards')) { - $build['terminal'] = \Drupal::formBuilder()->getForm('uc_credit_order_view_form', $order->id()); - } + $build = array(); + + // Add the hidden span for the CC details if possible. + $account = \Drupal::currentUser(); + if ($account->hasPermission('view cc details')) { + $rows = array(); + + if (!empty($order->payment_details['cc_type'])) { + $rows[] = t('Card type') . ': ' . SafeMarkup::checkPlain($order->payment_details['cc_type']); + } + + if (!empty($order->payment_details['cc_owner'])) { + $rows[] = t('Card owner') . ': ' . SafeMarkup::checkPlain($order->payment_details['cc_owner']); + } + + if (!empty($order->payment_details['cc_number'])) { + $rows[] = t('Card number') . ': ' . uc_credit_display_number($order->payment_details['cc_number']); + } + + if (!empty($order->payment_details['cc_start_month']) && !empty($order->payment_details['cc_start_year'])) { + $rows[] = t('Start date') . ': ' . $order->payment_details['cc_start_month'] . '/' . $order->payment_details['cc_start_year']; + } + + if (!empty($order->payment_details['cc_exp_month']) && !empty($order->payment_details['cc_exp_year'])) { + $rows[] = t('Expiration') . ': ' . $order->payment_details['cc_exp_month'] . '/' . $order->payment_details['cc_exp_year']; + } + + if (!empty($order->payment_details['cc_issue'])) { + $rows[] = t('Issue number') . ': ' . SafeMarkup::checkPlain($order->payment_details['cc_issue']); } - return $build; + if (!empty($order->payment_details['cc_bank'])) { + $rows[] = t('Issuing bank') . ': ' . SafeMarkup::checkPlain($order->payment_details['cc_bank']); + } + + $build['cc_info'] = array( + '#prefix' => '' . t('Show card details') . '
', + '#markup' => implode('
', $rows), + '#suffix' => '
', + ); + + // Add the form to process the card if applicable. + if ($account->hasPermission('process credit cards')) { + $build['terminal'] = \Drupal::formBuilder()->getForm('uc_credit_order_view_form', $order->id()); + } + } + + return $build; } /** * {@inheritdoc} */ public function customerView(OrderInterface $order) { - $build = array(); + $build = array(); - if (!empty($order->payment_details['cc_number'])) { - $build['#markup'] = t('Card number') . ':
' . uc_credit_display_number($order->payment_details['cc_number']); - } + if (!empty($order->payment_details['cc_number'])) { + $build['#markup'] = t('Card number') . ':
' . uc_credit_display_number($order->payment_details['cc_number']); + } - return $build; + return $build; } @@ -141,10 +150,12 @@ class CreditCard extends PaymentMethodPluginBase { * {@inheritdoc} */ public function cartProcess(OrderInterface $order, array $form, FormStateInterface $form_state) { - $credit_config = \Drupal::config('uc_credit.settings'); if (!$form_state->hasValue(['panes', 'payment', 'details', 'cc_number'])) { return; } + + $fields = $this->getEnabledFields(); + // Fetch the CC details from the $_POST directly. $cc_data = $form_state->getValue(['panes', 'payment', 'details']); @@ -187,20 +198,19 @@ class CreditCard extends PaymentMethodPluginBase { $return = TRUE; // Make sure an owner value was entered. - if ($credit_config->get('uc_credit_owner_enabled') && empty($cc_data['cc_owner'])) { + if (!empty($fields['owner']) && empty($cc_data['cc_owner'])) { $form_state->setErrorByName('panes][payment][details][cc_owner', t('Enter the owner name as it appears on the card.')); $return = FALSE; } - // Validate the CC number if that's turned on/check for non-digits. - if (($credit_config->get('uc_credit_validate_numbers') && !_uc_credit_valid_card_number($cc_data['cc_number'])) - || !ctype_digit($cc_data['cc_number'])) { + // Validate the credit card number. + if (!_uc_credit_valid_card_number($cc_data['cc_number'])) { $form_state->setErrorByName('panes][payment][details][cc_number', t('You have entered an invalid credit card number.')); $return = FALSE; } // Validate the start date (if entered). - if ($credit_config->get('uc_credit_start_enabled') && !_uc_credit_valid_card_start($cc_data['cc_start_month'], $cc_data['cc_start_year'])) { + if (!empty($fields['start']) && !_uc_credit_valid_card_start($cc_data['cc_start_month'], $cc_data['cc_start_year'])) { $form_state->setErrorByName('panes][payment][details][cc_start_month', t('The start date you entered is invalid.')); $form_state->setErrorByName('panes][payment][details][cc_start_year'); $return = FALSE; @@ -215,19 +225,19 @@ class CreditCard extends PaymentMethodPluginBase { // Validate the issue number (if entered). With issue numbers, '01' is // different from '1', but is_numeric() is still appropriate. - if ($credit_config->get('uc_credit_issue_enabled') && !_uc_credit_valid_card_issue($cc_data['cc_issue'])) { + if (!empty($fields['issue']) && !_uc_credit_valid_card_issue($cc_data['cc_issue'])) { $form_state->setErrorByName('panes][payment][details][cc_issue', t('The issue number you entered is invalid.')); $return = FALSE; } // Validate the CVV number if enabled. - if ($credit_config->get('uc_credit_cvv_enabled') && !_uc_credit_valid_cvv($cc_data['cc_cvv'])) { + if (!empty($fields['cvv']) && !_uc_credit_valid_cvv($cc_data['cc_cvv'])) { $form_state->setErrorByName('panes][payment][details][cc_cvv', t('You have entered an invalid CVV number.')); $return = FALSE; } // Validate the bank name if enabled. - if ($credit_config->get('uc_credit_bank_enabled') && empty($cc_data['cc_bank'])) { + if (!empty($fields['bank']) && empty($cc_data['cc_bank'])) { $form_state->setErrorByName('panes][payment][details][cc_bank', t('You must enter the issuing bank for that card.')); $return = FALSE; } @@ -256,4 +266,49 @@ class CreditCard extends PaymentMethodPluginBase { return $return; } + /** + * {@inheritdoc} + */ + public function orderLoad(OrderInterface $order) { + // Load the CC details from the credit cache if available. + $order->payment_details = uc_credit_cache('load'); + + // Otherwise load any details that might be stored in the data array. + if (empty($order->payment_details) && isset($order->data->cc_data)) { + $order->payment_details = uc_credit_cache('save', $order->data->cc_data); + } + } + + /** + * {@inheritdoc} + */ + public function orderSave(OrderInterface $order) { + _uc_credit_save_cc_data_to_order($order->payment_details, $order->id()); + } + + /** + * {@inheritdoc} + */ + public function orderSubmit(OrderInterface $order) { + // Clear out that session variable denoting this as a CC paid order. + \Drupal::service('session')->remove('cc_pay'); + + // Process CC transactions when an order is submitted after review. + $credit_config = \Drupal::config('uc_credit.settings'); + $gateway_id = uc_credit_default_gateway(); + $data = array( + 'txn_type' => $credit_config->get('uc_pg_' . $gateway_id . '_cc_txn_type'), + ); + + // Attempt to process the CC payment. + $order->payment_details = uc_credit_cache('load'); + $pass = uc_payment_process_payment('credit', $order->id(), $order->getTotal(), $data, TRUE, NULL, FALSE); + + // If the payment failed, store the data back in the session and + // halt the checkout process. + if (!$pass) { + return array(array('pass' => FALSE, 'message' => t('We were unable to process your credit card payment. Please verify your details and try again.'))); + } + } + } diff --git a/payment/uc_credit/src/Form/CreditSettingsForm.php b/payment/uc_credit/src/Form/CreditSettingsForm.php index a8acc31..2b16260 100644 --- a/payment/uc_credit/src/Form/CreditSettingsForm.php +++ b/payment/uc_credit/src/Form/CreditSettingsForm.php @@ -7,14 +7,14 @@ namespace Drupal\uc_credit\Form; -use Drupal\Core\Form\FormBase; +use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Component\Utility\SafeMarkup; /** * Credit card settings form. */ -class CreditSettingsForm extends FormBase { +class CreditSettingsForm extends ConfigFormBase { /** * {@inheritdoc} @@ -27,23 +27,9 @@ class CreditSettingsForm extends FormBase { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - $credit_config = $this->config('uc_credit.settings'); - $account = $this->currentUser(); - if (!$account->hasPermission('administer credit cards')) { - $form['notice'] = array( - '#markup' => '
' . $this->t('You must have access to administer credit cards to adjust these settings.') . '
', - ); - return $form; - } + $form = parent::buildForm($form, $form_state); - $gateways = _uc_payment_gateway_list('credit'); - if (!count($gateways)) { - $form['notice'] = array( - '#markup' => '
' . $this->t('Please enable a credit card gateway module for your chosen payment provider.') . '
', - ); -// @todo - This is commented out to test this form without a gateway -// return $form; - } + $credit_config = $this->config('uc_credit.settings'); $form['uc_credit'] = array( '#type' => 'vertical_tabs', @@ -59,16 +45,6 @@ class CreditSettingsForm extends FormBase { '#title' => $this->t('Basic settings'), '#group' => 'uc_credit', ); - $options = array(); - foreach ($gateways as $id => $gateway) { - $options[$id] = $gateway['title']; - } - $form['cc_basic']['uc_payment_credit_gateway'] = array( - '#type' => 'radios', - '#title' => $this->t('Default gateway'), - '#options' => $options, - '#default_value' => uc_credit_default_gateway(), - ); $form['cc_basic']['uc_credit_validate_numbers'] = array( '#type' => 'checkbox', '#title' => $this->t('Validate credit card numbers at checkout.'), @@ -170,45 +146,6 @@ class CreditSettingsForm extends FormBase { UC_CREDIT_REFERENCE_SET => $this->t('Set a reference only'), ); - foreach ($gateways as $id => $gateway) { - $form['gateways'][$id] = array( - '#type' => 'details', - '#title' => SafeMarkup::checkPlain($gateway['title']), - '#group' => 'uc_credit', - '#weight' => 5, - ); - $form['gateways'][$id]['uc_pg_' . $id . '_enabled'] = array( - '#type' => 'checkbox', - '#title' => $this->t('Enable this payment gateway for use.'), - '#default_value' => $credit_config->get('uc_pg_' . $id . '_enabled'), - '#weight' => -10, - ); - - // Get the transaction types associated with this gateway. - $gateway_types = uc_credit_gateway_txn_types($id); - $options = array(); - foreach ($txn_types as $type => $title) { - if (in_array($type, $gateway_types)) { - $options[$type] = $title; - } - } - $form['gateways'][$id]['uc_pg_' . $id . '_cc_txn_type'] = array( - '#type' => 'radios', - '#title' => $this->t('Default credit transaction type'), - '#description' => $this->t('Only available transaction types are listed. The default will be used unless an administrator chooses otherwise through the terminal.'), - '#options' => $options, - '#default_value' => $credit_config->get('uc_pg_' . $id . '_cc_txn_type'), - '#weight' => -5, - ); - - if (isset($gateway['settings']) && function_exists($gateway['settings'])) { - $gateway_settings = $gateway['settings'](array(), $form_state); - if (is_array($gateway_settings)) { - $form['gateways'][$id] += $gateway_settings; - } - } - } - if (empty($_POST) && !uc_credit_encryption_key()) { drupal_set_message($this->t('Credit card security settings must be configured in the security settings tab.'), 'warning'); } @@ -303,6 +240,8 @@ class CreditSettingsForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + // Check to see if we need to create an encryption key file. if ($form_state->getValue('update_cc_encrypt_dir')) { $key_path = $form_state->getValue('uc_credit_encryption_path'); @@ -325,11 +264,7 @@ class CreditSettingsForm extends FormBase { } } - // Need to use configFactory() and getEditable() here, because this form is - // wrapped by PaymentMethodSettingsForm so $this->getEditableConfigNames() - // never gets called - $credit_config = \Drupal::configFactory()->getEditable('uc_credit.settings'); - $credit_config + $this->config('uc_credit.settings') ->set('validate_numbers', $form_state->getValue('uc_credit_validate_numbers')) ->set('encryption_path', $form_state->getValue('uc_credit_encryption_path')) ->set('cvv_enabled', $form_state->getValue('uc_credit_cvv_enabled')) @@ -341,4 +276,14 @@ class CreditSettingsForm extends FormBase { ->set('accepted_types', explode("\r\n", $form_state->getValue('uc_credit_accepted_types'))) ->save(); } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + return [ + 'uc_credit.settings', + ]; + } + } diff --git a/payment/uc_credit/src/Tests/CreditCardTest.php b/payment/uc_credit/src/Tests/CreditCardTest.php index 967520e..0a026e9 100644 --- a/payment/uc_credit/src/Tests/CreditCardTest.php +++ b/payment/uc_credit/src/Tests/CreditCardTest.php @@ -53,6 +53,8 @@ class CreditCardTest extends UbercartTestBase { '4012888818888', ); + protected $paymentMethod; + public static $modules = array('uc_payment', 'uc_credit', 'test_gateway'); public static $adminPermissions = array('administer credit cards', 'process credit cards'); @@ -71,17 +73,6 @@ class CreditCardTest extends UbercartTestBase { * Helper function to configure Credit Card payment method settings. */ protected function configureCreditCard() { - $this->drupalPostForm( - 'admin/store/config/payment', - array('methods[credit][status]' => TRUE), - t('Save configuration') - ); - $this->assertFieldByName( - 'methods[credit][status]', - TRUE, - 'Credit card payment method is enabled' - ); - // Create key directory, make it readable and writeable. // Putting this under sites/default/files because SimpleTest needs to be // able to create the directory - this is NOT where you'd put the key file @@ -114,20 +105,7 @@ class CreditCardTest extends UbercartTestBase { * Helper function to configure Credit Card gateway. */ protected function configureGateway() { - $this->drupalPostForm( - 'admin/store/config/payment/method/credit', - array( - 'uc_payment_credit_gateway' => 'test_gateway', - 'uc_pg_test_gateway_enabled' => TRUE, - ), - t('Save configuration') - ); - - $this->assertFieldByName( - 'uc_pg_test_gateway_enabled', - TRUE, - 'Test gateway is enabled' - ); + $this->paymentMethod = $this->createPaymentMethod('test_gateway'); } /** @@ -259,7 +237,7 @@ class CreditCardTest extends UbercartTestBase { * Tests that expiry date validation functions correctly. */ public function testExpiryDate() { - $order = $this->createOrder(array('payment_method' => 'credit')); + $order = $this->createOrder(array('payment_method' => $this->paymentMethod['id'])); $year = date('Y'); $month = date('n'); diff --git a/payment/uc_credit/tests/src/Plugin/Ubercart/PaymentMethod/TestGateway.php b/payment/uc_credit/tests/src/Plugin/Ubercart/PaymentMethod/TestGateway.php new file mode 100644 index 0000000..be26932 --- /dev/null +++ b/payment/uc_credit/tests/src/Plugin/Ubercart/PaymentMethod/TestGateway.php @@ -0,0 +1,21 @@ +getPaymentMethodId() == 'credit') { - // Clear out that session variable denoting this as a CC paid order. - $session->remove('cc_pay'); - - // Process CC transactions when an order is submitted after review. - $gateway_id = uc_credit_default_gateway(); - $data = array( - 'txn_type' => $credit_config->get('uc_pg_' . $gateway_id . '_cc_txn_type'), - ); - - // Attempt to process the CC payment. - $order->payment_details = uc_credit_cache('load'); - $pass = uc_payment_process_payment('credit', $order->id(), $order->getTotal(), $data, TRUE, NULL, FALSE); - - // If the payment failed, store the data back in the session and - // halt the checkout process. - if (!$pass) { - return array(array('pass' => FALSE, 'message' => t('We were unable to process your credit card payment. Please verify your details and try again.'))); - } - } - break; - } -} - -/** - * Implements hook_uc_order_load(). - */ -function uc_credit_uc_order_load($orders) { - foreach ($orders as $order) { - if ($order->getPaymentMethodId() == 'credit') { - // Load the CC details from the credit cache if available. - $order->payment_details = uc_credit_cache('load'); - - // Otherwise load any details that might be stored in the data array. - if (empty($order->payment_details) && isset($order->data->cc_data)) { - $order->payment_details = uc_credit_cache('save', $order->data->cc_data); - } - } - } -} - -/** - * Implements hook_uc_order_update(). - */ -function uc_credit_uc_order_update(OrderInterface $order) { - if ($order->getPaymentMethodId() == 'credit' && !empty($order->payment_details)) { - _uc_credit_save_cc_data_to_order($order->payment_details, $order->id()); - } -} - -/** * Implements hook_uc_payment_method(). */ function temp_uc_credit_uc_payment_method() { diff --git a/payment/uc_credit/uc_credit.routing.yml b/payment/uc_credit/uc_credit.routing.yml index 595e355..57125e1 100644 --- a/payment/uc_credit/uc_credit.routing.yml +++ b/payment/uc_credit/uc_credit.routing.yml @@ -1,3 +1,10 @@ +uc_credit.settings: + path: '/admin/store/config/payment/method/credit' + defaults: + _form: '\Drupal\uc_credit\Form\CreditSettingsForm' + requirements: + _permission: 'administer credit cards' + uc_credit.terminal: path: '/admin/store/orders/{uc_order}/credit' defaults: diff --git a/payment/uc_payment/config/install/uc_payment.method.free_order.yml b/payment/uc_payment/config/install/uc_payment.method.free_order.yml new file mode 100644 index 0000000..9a4b164 --- /dev/null +++ b/payment/uc_payment/config/install/uc_payment.method.free_order.yml @@ -0,0 +1,9 @@ +langcode: en +status: true +dependencies: { } +id: free_order +label: 'No payment required' +weight: 10 +plugin: free_order +settings: { } +locked: true diff --git a/payment/uc_payment/src/Annotation/PaymentMethod.php b/payment/uc_payment/src/Annotation/PaymentMethod.php deleted file mode 100644 index 6f4c8fc..0000000 --- a/payment/uc_payment/src/Annotation/PaymentMethod.php +++ /dev/null @@ -1,73 +0,0 @@ -entityTypeManager()->getStorage('uc_payment_method')->create(array('plugin' => $plugin_id)); + + return $this->entityFormBuilder()->getForm($entity); + } + + /** + * Performs an operation on the payment method entity. + * + * @param \Drupal\uc_payment\PaymentMethodInterface $uc_payment_method + * The payment method entity. + * @param string $op + * The operation to perform, usually 'enable' or 'disable'. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + * A redirect back to the payment method listing page. + */ + public function performOperation(PaymentMethodInterface $uc_payment_method, $op) { + $uc_payment_method->$op()->save(); + + if ($op == 'enable') { + drupal_set_message($this->t('The %label payment method has been enabled.', array('%label' => $uc_payment_method->label()))); + } + elseif ($op == 'disable') { + drupal_set_message($this->t('The %label payment method has been disabled.', array('%label' => $uc_payment_method->label()))); + } + + $url = $uc_payment_method->toUrl('collection'); + return $this->redirect($url->getRouteName(), $url->getRouteParameters(), $url->getOptions()); + } + +} diff --git a/payment/uc_payment/src/Entity/PaymentMethod.php b/payment/uc_payment/src/Entity/PaymentMethod.php new file mode 100644 index 0000000..4ffaafc --- /dev/null +++ b/payment/uc_payment/src/Entity/PaymentMethod.php @@ -0,0 +1,134 @@ +weight; + } + + /** + * {@inheritdoc} + */ + public function isLocked() { + return $this->locked; + } + + /** + * {@inheritdoc} + */ + public function getPlugin() { + return \Drupal::service('plugin.manager.uc_payment.method')->createInstance($this->plugin, $this->settings); + } + + /** + * Returns the payment method entity for a specific order. + * + * @param \Drupal\uc_order\OrderInterface $order + * The order from which the payment method should be loaded. + * + * @return static|null + * The entity object or NULL if there is no valid payment method. + */ + public static function loadFromOrder(OrderInterface $order) { + if ($method = $order->getPaymentMethodId()) { + return static::load($method); + } + return NULL; + } + +} diff --git a/payment/uc_payment/src/Form/PaymentMethodForm.php b/payment/uc_payment/src/Form/PaymentMethodForm.php new file mode 100644 index 0000000..7d94fd9 --- /dev/null +++ b/payment/uc_payment/src/Form/PaymentMethodForm.php @@ -0,0 +1,105 @@ +plugin = $this->entity->getPlugin(); + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function form(array $form, FormStateInterface $form_state) { + $form = parent::form($form, $form_state); + + $definition = $this->plugin->getPluginDefinition(); + $form['type'] = array( + '#type' => 'item', + '#title' => $this->t('Type'), + '#markup' => $definition['name'], + ); + + $form['label'] = array( + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#maxlength' => 255, + '#default_value' => $this->entity->label(), + '#description' => $this->t('The name shown to the customer when they choose this payment method at checkout.'), + '#required' => TRUE, + ); + $form['id'] = array( + '#type' => 'machine_name', + '#default_value' => $this->entity->id(), + '#machine_name' => array( + 'exists' => '\Drupal\uc_payment\Entity\PaymentMethod::load', + ), + '#disabled' => !$this->entity->isNew(), + ); + + $form['settings'] = $this->plugin->buildConfigurationForm([], $form_state); + $form['settings']['#tree'] = TRUE; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + parent::validateForm($form, $form_state); + + $this->plugin->validateConfigurationForm($form['settings'], $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + + $this->plugin->submitConfigurationForm($form['settings'], $form_state); + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $this->entity->save(); + + drupal_set_message($this->t('Saved the %label payment method.', ['%label' => $this->entity->label()])); + + $form_state->setRedirectUrl($this->entity->toUrl('collection')); + } + +} diff --git a/payment/uc_payment/src/Form/PaymentMethodSettingsForm.php b/payment/uc_payment/src/Form/PaymentMethodSettingsForm.php deleted file mode 100644 index a15e8a3..0000000 --- a/payment/uc_payment/src/Form/PaymentMethodSettingsForm.php +++ /dev/null @@ -1,82 +0,0 @@ -getDefinition($method); - $form['#title'] = $this->t('@method settings', ['@method' => $definition['name']]); - - $this->instance = \Drupal::service('plugin.manager.uc_payment.method')->createInstance($method); - $this->settings = $this->instance->getSettingsForm(); - - $form = $this->settings->buildForm($form, $form_state); - - return parent::buildForm($form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, FormStateInterface $form_state) { - $this->settings->validateForm($form, $form_state); - - parent::validateForm($form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state->cleanValues(); - $this->settings->submitForm($form, $form_state); - - return parent::submitForm($form, $form_state); - } - -} diff --git a/payment/uc_payment/src/Form/PaymentMethodsForm.php b/payment/uc_payment/src/Form/PaymentMethodsForm.php deleted file mode 100644 index 3c93233..0000000 --- a/payment/uc_payment/src/Form/PaymentMethodsForm.php +++ /dev/null @@ -1,154 +0,0 @@ -paymentMethodManager = $payment_method_manager; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('config.factory'), - $container->get('plugin.manager.uc_payment.method') - ); - } - - /** - * {@inheritdoc} - */ - public function getFormID() { - return 'uc_payment_methods_form'; - } - - /** - * {@inheritdoc} - */ - protected function getEditableConfigNames() { - return [ - 'uc_payment.settings', - ]; - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state) { - $form['help'] = array( - '#markup' => t('By default, only the "Free order" payment method is listed here. To see additional payment methods you must install additional modules. The "Payment Method Pack" module that comes with Ubercart provides "Check" and "COD" payment methods. The "Credit Card" module that comes with Ubercart provides a credit card payment method, although you will need an additional module to provide a payment gateway for your credit card. For more information about payment methods and settings please read the Ubercart Documentation.', ['@install' => Url::fromRoute('system.modules_list')->toString(), '@doc' => Url::fromUri('http://www.drupal.org/documentation/modules/ubercart')->toString()]), - ); - - $form['methods'] = array( - '#type' => 'table', - '#header' => array(t('Payment method'), t('List position'), t('Operations')), - '#tabledrag' => array( - array( - 'action' => 'order', - 'relationship' => 'sibling', - 'group' => 'uc-payment-method-weight', - ), - ), - ); - - foreach ($this->paymentMethodManager->getDefinitions() as $id => $method) { - $form['methods'][$id]['#attributes']['class'][] = 'draggable'; - $form['methods'][$id]['status'] = array( - '#type' => 'checkbox', - '#title' => SafeMarkup::checkPlain($method['name']), - '#default_value' => $method['checkout'], - ); - $form['methods'][$id]['weight'] = array( - '#type' => 'weight', - '#title' => t('Weight for @title', array('@title' => $method['name'])), - '#title_display' => 'invisible', - '#default_value' => $method['weight'], - '#attributes' => array( - 'class' => array('uc-payment-method-weight'), - ), - ); - - if (empty($method['no_gateway'])) { - $gateways = _uc_payment_gateway_list($id, TRUE); - $options = array(); - foreach ($gateways as $gateway_id => $gateway) { - $options[$gateway_id] = $gateway['title']; - } - if ($options) { - $form['methods'][$id]['status']['#title'] .= ' (' . t('includes %gateways', array('%gateways' => implode(', ', $options))) . ')'; - } - } - - $links = array(); - - if (!empty($method['settings_form'])) { - $links['settings'] = array( - 'title' => t('Settings'), - 'url' => Url::fromRoute('uc_payment.method_settings', ['method' => $id]), - ); - } - - // $links['conditions'] = array( - // 'title' => t('Conditions'), - // 'url' => Url::fromRoute('admin/store/config/payment/manage/uc_payment_method_', ['method' => $id]), - // ); - - $form['methods'][$id]['settings'] = array( - '#type' => 'operations', - '#links' => $links, - ); - } - - return parent::buildForm($form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - $this->config('uc_payment.settings') - ->set('methods', $form_state->getValue('methods')) - ->save(); - - $this->paymentMethodManager->clearCachedDefinitions(); - - parent::submitForm($form, $form_state); - } - -} diff --git a/payment/uc_payment/src/PaymentMethodInterface.php b/payment/uc_payment/src/PaymentMethodInterface.php new file mode 100644 index 0000000..80d34a0 --- /dev/null +++ b/payment/uc_payment/src/PaymentMethodInterface.php @@ -0,0 +1,33 @@ +paymentMethodManager = $payment_method_manager; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { + return new static( + $entity_type, + $container->get('entity.manager')->getStorage($entity_type->id()), + $container->get('plugin.manager.uc_payment.method') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'uc_payment_methods_form'; + } + + /** + * {@inheritdoc} + */ + public function buildHeader() { + $header['label'] = array( + 'data' => $this->t('Payment method'), + ); + $header['plugin'] = array( + 'data' => $this->t('Type'), + 'class' => array(RESPONSIVE_PRIORITY_LOW), + ); + $header['status'] = array( + 'data' => $this->t('Status'), + ); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + $row['label'] = $entity->label(); + + $definition = $entity->getPlugin()->getPluginDefinition(); + $row['plugin']['#markup'] = $definition['name']; + + $row['status']['#markup'] = $entity->status() ? $this->t('Enabled') : $this->t('Disabled'); + + return $row + parent::buildRow($entity); + } + + /** + * {@inheritdoc} + */ + public function getDefaultOperations(EntityInterface $entity) { + $operations = parent::getDefaultOperations($entity); + + // Locked payment methods may not be deleted. + if (isset($operations['delete']) && $entity->isLocked()) { + unset($operations['delete']); + } + + return $operations; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $options = array_map(function ($definition) { + return $definition['name']; + }, array_filter($this->paymentMethodManager->getDefinitions(), function ($definition) { + return !$definition['no_ui']; + })); + + if ($options) { + uasort($options, 'strnatcasecmp'); + + $form['add'] = array( + '#type' => 'details', + '#title' => 'Add payment method', + '#open' => TRUE, + '#attributes' => array( + 'class' => array('container-inline'), + ), + ); + $form['add']['payment_method_type'] = array( + '#type' => 'select', + '#title' => $this->t('Type'), + '#empty_option' => $this->t('- Choose -'), + '#options' => $options, + ); + $form['add']['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Add payment method'), + '#validate' => array('::validateAddPaymentMethod'), + '#submit' => array('::submitAddPaymentMethod'), + '#limit_validation_errors' => array(array('payment_method_type')), + ); + } + + $form = parent::buildForm($form, $form_state); + $form[$this->entitiesKey]['#empty'] = $this->t('No payment methods have been configured.'); + + $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Save configuration'), + '#button_type' => 'primary', + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + + drupal_set_message($this->t('The configuration options have been saved.')); + } + + /** + * Form validation handler for adding a new payment method. + */ + public function validateAddPaymentMethod(array &$form, FormStateInterface $form_state) { + if ($form_state->isValueEmpty('payment_method_type')) { + $form_state->setErrorByName('payment_method_type', $this->t('You must select the new payment method type.')); + } + } + + /** + * Form submission handler for adding a new payment method. + */ + public function submitAddPaymentMethod(array &$form, FormStateInterface $form_state) { + $form_state->setRedirect( + 'entity.uc_payment_method.add_form', + array('plugin_id' => $form_state->getValue('payment_method_type')) + ); + } + +} diff --git a/payment/uc_payment/src/PaymentMethodPluginBase.php b/payment/uc_payment/src/PaymentMethodPluginBase.php index 84355ca..5fea1ed 100644 --- a/payment/uc_payment/src/PaymentMethodPluginBase.php +++ b/payment/uc_payment/src/PaymentMethodPluginBase.php @@ -19,6 +19,62 @@ abstract class PaymentMethodPluginBase extends PluginBase implements PaymentMeth /** * {@inheritdoc} */ + public function __construct(array $configuration, $plugin_id, $plugin_definition) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->configuration += $this->defaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return array(); + } + + /** + * {@inheritdoc} + */ + public function getConfiguration() { + return $this->configuration; + } + + /** + * {@inheritdoc} + */ + public function setConfiguration(array $configuration) { + $this->configuration = $configuration; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + return array(); + } + + /** + * {@inheritdoc} + */ public function cartDetails(OrderInterface $order, array $form, FormStateInterface $form_state) { return array(); } diff --git a/payment/uc_payment/src/PaymentMethodPluginInterface.php b/payment/uc_payment/src/PaymentMethodPluginInterface.php index 4a01a39..cd0f66c 100644 --- a/payment/uc_payment/src/PaymentMethodPluginInterface.php +++ b/payment/uc_payment/src/PaymentMethodPluginInterface.php @@ -7,14 +7,16 @@ namespace Drupal\uc_payment; +use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\PluginFormInterface; use Drupal\uc_order\OrderInterface; /** * Defines an interface for payment method plugins. */ -interface PaymentMethodPluginInterface extends PluginInspectionInterface { +interface PaymentMethodPluginInterface extends PluginInspectionInterface, PluginFormInterface, ConfigurablePluginInterface { /** * Returns the form or render array to be displayed at checkout. @@ -23,7 +25,7 @@ interface PaymentMethodPluginInterface extends PluginInspectionInterface { * The order which is being processed. * @param array $form * The checkout form array. - * @param array $form_state + * @param \Drupal\Core\Form\FormStateInterface $form_state * The checkout form state array. * * @return array @@ -41,7 +43,7 @@ interface PaymentMethodPluginInterface extends PluginInspectionInterface { * The order which is being processed. * @param array $form * The checkout form array. - * @param array $form_state + * @param \Drupal\Core\Form\FormStateInterface $form_state * The checkout form state array. * * @return bool @@ -94,7 +96,7 @@ interface PaymentMethodPluginInterface extends PluginInspectionInterface { * The order that is being edited. * @param array $form * The form array. - * @param array $form_state + * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state array. * * @return array diff --git a/payment/uc_payment/src/Plugin/PaymentMethodManager.php b/payment/uc_payment/src/Plugin/PaymentMethodManager.php index 9e79724..b4cc052 100644 --- a/payment/uc_payment/src/Plugin/PaymentMethodManager.php +++ b/payment/uc_payment/src/Plugin/PaymentMethodManager.php @@ -1,6 +1,7 @@ TRUE, - 'weight' => 0, - ); - - /** * Constructs a PaymentMethodManager object. * * @param \Traversable $namespaces * An object that implements \Traversable which contains the root paths - * keyed by the corresponding namespace to look for plugin implementations, + * keyed by the corresponding namespace to look for plugin implementations. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend * Cache backend instance to use. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. */ public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { - $this->discovery = new AnnotatedClassDiscovery('Plugin/Ubercart/PaymentMethod', $namespaces, 'Drupal\uc_payment\Annotation\PaymentMethod'); - $this->discovery = new InfoHookDecorator($this->discovery, 'uc_payment_method', 'Drupal\uc_payment\Plugin\Ubercart\PaymentMethod\LegacyPaymentMethod'); - $this->factory = new ContainerFactory($this); - - $this->moduleHandler = $module_handler; + parent::__construct('Plugin/Ubercart/PaymentMethod', $namespaces, $module_handler, PaymentMethodPluginInterface::class, UbercartPaymentMethod::class); $this->alterInfo('uc_payment_method'); $this->setCacheBackend($cache_backend, 'uc_payment_methods'); - - $this->methodConfig = \Drupal::config('uc_payment.settings')->get('methods'); - } - - /** - * {@inheritdoc} - */ - public function getDefinitions() { - $methods = parent::getDefinitions(); - - uasort($methods, 'Drupal\Component\Utility\SortArray::sortByWeightElement'); - - return $methods; - } - - /** - * Overrides \Drupal\Component\Plugin\PluginManagerBase::processDefinition(). - */ - public function processDefinition(&$definition, $plugin_id) { - parent::processDefinition($definition, $plugin_id); - - if (isset($this->methodConfig[$plugin_id]['status'])) { - $definition['checkout'] = $this->methodConfig[$plugin_id]['status']; - } - if (isset($this->methodConfig[$plugin_id]['weight'])) { - $definition['weight'] = $this->methodConfig[$plugin_id]['weight']; - } } /** @@ -90,7 +47,7 @@ class PaymentMethodManager extends DefaultPluginManager { * A fully configured plugin instance. */ public function createFromOrder(OrderInterface $order) { - return $this->createInstance($order->getPaymentMethodId()); + return PaymentMethod::load($order->getPaymentMethodId())->getPlugin(); } /** diff --git a/payment/uc_payment/src/Plugin/Ubercart/CheckoutPane/PaymentMethodPane.php b/payment/uc_payment/src/Plugin/Ubercart/CheckoutPane/PaymentMethodPane.php index a2fbc94..863bd60 100644 --- a/payment/uc_payment/src/Plugin/Ubercart/CheckoutPane/PaymentMethodPane.php +++ b/payment/uc_payment/src/Plugin/Ubercart/CheckoutPane/PaymentMethodPane.php @@ -12,6 +12,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\uc_cart\CheckoutPanePluginBase; use Drupal\uc_order\OrderInterface; +use Drupal\uc_payment\Entity\PaymentMethod; use Drupal\uc_payment\Plugin\PaymentMethodManager; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -83,14 +84,14 @@ class PaymentMethodPane extends CheckoutPanePluginBase implements ContainerFacto $form_state->setUserInput($input); $options = array(); - foreach (uc_payment_method_list() as $id => $method) { + foreach (PaymentMethod::loadMultiple() as $method) { // $set = rules_config_load('uc_payment_method_' . $method['id']); // if ($set && !$set->execute($order)) { // continue; // } - if ($method['checkout'] && !isset($method['express'])) { - $options[$id] = $method['title']; + if ($method->status()) { + $options[$method->id()] = $method->label(); } } @@ -131,14 +132,16 @@ class PaymentMethodPane extends CheckoutPanePluginBase implements ContainerFacto '#suffix' => '', ); - try { - $details = $this->paymentMethodManager->createFromOrder($order)->cartDetails($order, $form, $form_state); - if ($details) { - unset($contents['details']['#markup']); - $contents['details'] += $details; + if ($order->getPaymentMethodId()) { + try { + $details = $this->paymentMethodManager->createFromOrder($order)->cartDetails($order, $form, $form_state); + if ($details) { + unset($contents['details']['#markup']); + $contents['details'] += $details; + } + } + catch (PluginException $e) { } - } - catch (PluginException $e) { } return $contents; diff --git a/payment/uc_payment/src/Plugin/Ubercart/PaymentMethod/FreeOrder.php b/payment/uc_payment/src/Plugin/Ubercart/PaymentMethod/FreeOrder.php index e83a146..57db6cb 100644 --- a/payment/uc_payment/src/Plugin/Ubercart/PaymentMethod/FreeOrder.php +++ b/payment/uc_payment/src/Plugin/Ubercart/PaymentMethod/FreeOrder.php @@ -14,13 +14,10 @@ use Drupal\uc_payment\PaymentMethodPluginBase; /** * Defines a free order payment method. * - * @PaymentMethod( + * @UbercartPaymentMethod( * id = "free_order", * name = @Translation("Free order"), - * title = @Translation("No payment required"), - * checkout = TRUE, - * no_gateway = TRUE, - * weight = 0, + * no_ui = TRUE * ) */ class FreeOrder extends PaymentMethodPluginBase { diff --git a/payment/uc_payment/src/Plugin/Ubercart/PaymentMethod/LegacyPaymentMethod.php b/payment/uc_payment/src/Plugin/Ubercart/PaymentMethod/LegacyPaymentMethod.php deleted file mode 100644 index 4777fba..0000000 --- a/payment/uc_payment/src/Plugin/Ubercart/PaymentMethod/LegacyPaymentMethod.php +++ /dev/null @@ -1,116 +0,0 @@ -pluginDefinition['callback']('cart-details', $order, $form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function cartProcess(OrderInterface $order, array $form, FormStateInterface $form_state) { - return $this->pluginDefinition['callback']('cart-process', $order, $form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function cartReview(OrderInterface $order) { - return $this->pluginDefinition['callback']('cart-review', $order); - } - - /** - * {@inheritdoc} - */ - public function orderDelete(OrderInterface $order) { - return $this->pluginDefinition['callback']('order-delete', $order); - } - - /** - * {@inheritdoc} - */ - public function orderEditDetails(OrderInterface $order) { - return $this->pluginDefinition['callback']('order-details', $order); - } - - /** - * {@inheritdoc} - */ - public function orderEditProcess(OrderInterface $order, array $form, FormStateInterface $form_state) { - return $this->pluginDefinition['callback']('edit-process', $order, $form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function orderLoad(OrderInterface $order) { - return $this->pluginDefinition['callback']('order-load', $order); - } - - /** - * {@inheritdoc} - */ - public function orderSave(OrderInterface $order) { - return $this->pluginDefinition['callback']('order-save', $order); - } - - /** - * {@inheritdoc} - */ - public function orderSubmit(OrderInterface $order) { - return $this->pluginDefinition['callback']('order-submit', $order); - } - - /** - * {@inheritdoc} - */ - public function orderView(OrderInterface $order) { - return $this->pluginDefinition['callback']('order-view', $order); - } - - /** - * {@inheritdoc} - */ - public function customerView(OrderInterface $order) { - return $this->pluginDefinition['callback']('customer-view', $order); - } - - /** - * {@inheritdoc} - */ - public function settingsForm(array $form, FormStateInterface $form_state) { - $null = NULL; - return $this->pluginDefinition['callback']('settings', $null, $form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function submitConfigurationForm(array $form, FormStateInterface $form_state) { - // @todo Refactor when uc_credit is moved to a separate plugin. - if ($this->pluginId == 'credit') { - \Drupal::configFactory()->getEditable('uc_credit.settings') - ->set('encryption_path', $form_state->getValue('encryption_path')) - ->save(); - } - } - -} diff --git a/payment/uc_payment/src/Tests/PaymentPaneTest.php b/payment/uc_payment/src/Tests/PaymentPaneTest.php index c49898d..3098118 100644 --- a/payment/uc_payment/src/Tests/PaymentPaneTest.php +++ b/payment/uc_payment/src/Tests/PaymentPaneTest.php @@ -29,23 +29,22 @@ class PaymentPaneTest extends UbercartTestBase { */ public function testPaymentMethodOptions() { // No payment methods. - $edit = array('methods[check][status]' => FALSE); - $this->drupalPostForm('admin/store/config/payment', $edit, 'Save configuration'); $this->drupalGet('cart/checkout'); $this->assertText('Checkout cannot be completed without any payment methods enabled. Please contact an administrator to resolve the issue.'); // Single payment method. - $edit = array('methods[check][status]' => TRUE); - $this->drupalPostForm('admin/store/config/payment', $edit, 'Save configuration'); + $check = $this->createPaymentMethod('check'); $this->drupalGet('cart/checkout'); $this->assertNoText('Select a payment method from the following options.'); + $this->assertEscaped($check['label']); $this->assertFieldByXPath("//input[@name='panes[payment][payment_method]' and @disabled='disabled']"); // Multiple payment methods. - $edit = array('methods[other][status]' => TRUE); - $this->drupalPostForm('admin/store/config/payment', $edit, 'Save configuration'); + $other = $this->createPaymentMethod('other'); $this->drupalGet('cart/checkout'); $this->assertText('Select a payment method from the following options.'); + $this->assertEscaped($check['label']); + $this->assertEscaped($other['label']); $this->assertNoFieldByXPath("//input[@name='panes[payment][payment_method]' and @disabled='disabled']"); } @@ -73,19 +72,18 @@ class PaymentPaneTest extends UbercartTestBase { */ public function testFreeOrders() { $free_product = $this->createProduct(array('price' => 0)); - $edit = array('methods[check][status]' => TRUE); - $this->drupalPostForm('admin/store/config/payment', $edit, 'Save configuration'); + $check = $this->createPaymentMethod('check'); // Check that paid products cannot be purchased for free. $this->drupalGet('cart/checkout'); - $this->assertText('Check or money order'); + $this->assertEscaped($check['label']); $this->assertNoText('No payment required'); $this->assertNoText('Subtotal: $0.00'); // Check that a mixture of free and paid products cannot be purchased for free. $this->addToCart($free_product); $this->drupalGet('cart/checkout'); - $this->assertText('Check or money order'); + $this->assertEscaped($check['label']); $this->assertNoText('No payment required'); $this->assertNoText('Subtotal: $0.00'); @@ -94,7 +92,7 @@ class PaymentPaneTest extends UbercartTestBase { $this->drupalPostForm('cart', array(), t('Remove')); $this->addToCart($free_product); $this->drupalGet('cart/checkout'); - $this->assertNoText('Check or money order'); + $this->assertNoEscaped($check['label']); $this->assertText('No payment required'); $this->assertText('Continue with checkout to complete your order.'); $this->assertText('Subtotal: $0.00'); diff --git a/payment/uc_payment/uc_payment.info.yml b/payment/uc_payment/uc_payment.info.yml index 3bacad1..eebc003 100644 --- a/payment/uc_payment/uc_payment.info.yml +++ b/payment/uc_payment/uc_payment.info.yml @@ -5,4 +5,4 @@ package: Ubercart - core (optional) core: 8.x dependencies: - uc_order -configure: uc_payment.methods +configure: entity.uc_payment_method.collection diff --git a/payment/uc_payment/uc_payment.links.menu.yml b/payment/uc_payment/uc_payment.links.menu.yml index e14f416..f93cdf0 100644 --- a/payment/uc_payment/uc_payment.links.menu.yml +++ b/payment/uc_payment/uc_payment.links.menu.yml @@ -1,5 +1,5 @@ uc_payment.admin.store.config.payment: title: 'Payment methods' description: 'Choose and configure available payment methods.' - route_name: uc_payment.methods + route_name: entity.uc_payment_method.collection parent: uc_store.admin.store.config diff --git a/payment/uc_payment/uc_payment.module b/payment/uc_payment/uc_payment.module index 5940af4..389ca89 100644 --- a/payment/uc_payment/uc_payment.module +++ b/payment/uc_payment/uc_payment.module @@ -12,10 +12,11 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; -use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Component\Utility\Xss; use Drupal\uc_order\Entity\Order; +use Drupal\uc_payment\Entity\PaymentMethod; use Symfony\Component\HttpFoundation\RedirectResponse; use Drupal\user\Entity\User; use Drupal\uc_order\OrderInterface; @@ -24,6 +25,16 @@ require_once dirname(__FILE__) . '/uc_payment_order_pane.inc'; /** + * Implements hook_help(). + */ +function uc_payment_help($route_name, RouteMatchInterface $route_match) { + switch ($route_name) { + case 'entity.uc_payment_method.collection': + return '

' . t('By default, only the "Free order" payment method is listed here. To see additional payment methods you must install additional modules. The "Payment Method Pack" module that comes with Ubercart provides "Check" and "COD" payment methods. The "Credit Card" module that comes with Ubercart provides a credit card payment method, although you will need an additional module to provide a payment gateway for your credit card. For more information about payment methods and settings please read the Ubercart Documentation.', ['@install' => Url::fromRoute('system.modules_list')->toString(), '@doc' => Url::fromUri('http://www.drupal.org/documentation/modules/ubercart')->toString()]) . '

'; + } +} + +/** * Implements hook_theme(). */ function uc_payment_theme() { @@ -55,11 +66,12 @@ function template_preprocess_uc_payment_totals(&$variables) { * Adds express buttons for enabled payment modules directly to the cart page. */ function uc_payment_form_uc_cart_view_form_alter(&$form, FormStateInterface $form_state) { - foreach (uc_payment_method_list() as $id => $method) { - if ($method['checkout'] && isset($method['express']) && $express = $method['express'](array(), $form_state)) { - $form['actions']['checkout'][$id] = $express; - } - } +// @todo Add express payment methods +// foreach (uc_payment_method_list() as $id => $method) { +// if ($method['checkout'] && isset($method['express']) && $express = $method['express'](array(), $form_state)) { +// $form['actions']['checkout'][$id] = $express; +// } +// } } /** @@ -260,12 +272,8 @@ function uc_payment_enter($order_id, $method, $amount, $uid = 0, $data = NULL, $ $order = uc_order_load($order_id, TRUE); $account = User::load($uid); - try { - $method_name = \Drupal::service('plugin.manager.uc_payment.method')->createInstance($method)->cartReviewTitle(); - } - catch (PluginException $e) { - $method_name = t('Other'); - } + $method_entity = PaymentMethod::load($method); + $method_name = $method_entity ? $method_entity->getPlugin()->cartReviewTitle() : t('Other'); if (is_array($data)) { $data = serialize($data); diff --git a/payment/uc_payment/uc_payment.routing.yml b/payment/uc_payment/uc_payment.routing.yml index 22260c1..9c28cab 100644 --- a/payment/uc_payment/uc_payment.routing.yml +++ b/payment/uc_payment/uc_payment.routing.yml @@ -1,15 +1,48 @@ -uc_payment.methods: +entity.uc_payment_method.collection: path: '/admin/store/config/payment' defaults: - _form: '\Drupal\uc_payment\Form\PaymentMethodsForm' + _entity_list: 'uc_payment_method' _title: 'Payment methods' requirements: _permission: 'administer store' -uc_payment.method_settings: - path: '/admin/store/config/payment/method/{method}' +entity.uc_payment_method.add_form: + path: '/admin/store/config/payment/add/{plugin_id}' defaults: - _form: '\Drupal\uc_payment\Form\PaymentMethodSettingsForm' + _controller: '\Drupal\uc_payment\Controller\PaymentMethodController::addForm' + _title: 'Add payment method' + requirements: + _permission: 'administer store' + +entity.uc_payment_method.edit_form: + path: '/admin/store/config/payment/{uc_payment_method}' + defaults: + _entity_form: 'uc_payment_method' + _title: 'Edit payment method' + requirements: + _permission: 'administer store' + +entity.uc_payment_method.enable: + path: '/admin/store/config/payment/{uc_payment_method}/enable' + defaults: + _controller: '\Drupal\uc_payment\Controller\PaymentMethodController::performOperation' + op: 'enable' + requirements: + _permission: 'administer store' + +entity.uc_payment_method.disable: + path: '/admin/store/config/payment/{uc_payment_method}/disable' + defaults: + _controller: '\Drupal\uc_payment\Controller\PaymentMethodController::performOperation' + op: 'disable' + requirements: + _permission: 'administer store' + +entity.uc_payment_method.delete_form: + path: '/admin/store/config/payment/{uc_payment_method}/delete' + defaults: + _entity_form: 'uc_payment_method.delete' + _title: 'Delete' requirements: _permission: 'administer store' diff --git a/payment/uc_payment/uc_payment.tokens.inc b/payment/uc_payment/uc_payment.tokens.inc index 0f7971e..cd761a7 100644 --- a/payment/uc_payment/uc_payment.tokens.inc +++ b/payment/uc_payment/uc_payment.tokens.inc @@ -5,8 +5,8 @@ * Token hooks for the uc_payment module. */ -use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Core\Render\BubbleableMetadata; +use Drupal\uc_payment\Entity\PaymentMethod; /** * Implements hook_token_info(). @@ -37,12 +37,8 @@ function uc_payment_tokens($type, $tokens, array $data, array $options, Bubbleab if (isset($tokens['payment-method'])) { $original = $tokens['payment-method']; - try { - $values[$original] = \Drupal::service('plugin.manager.uc_payment.method')->createFromOrder($order)->cartReviewTitle(); - } - catch (PluginException $e) { - $values[$original] = t('Other'); - } + $method = PaymentMethod::loadFromOrder($order); + $values[$original] = $method ? $method->getPlugin()->cartReviewTitle() : t('Other'); } if (isset($tokens['payment-balance'])) { diff --git a/payment/uc_payment/uc_payment_order_pane.inc b/payment/uc_payment/uc_payment_order_pane.inc index 52d3d41..da97dbf 100644 --- a/payment/uc_payment/uc_payment_order_pane.inc +++ b/payment/uc_payment/uc_payment_order_pane.inc @@ -16,6 +16,7 @@ use Drupal\Core\Ajax\PrependCommand; use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; +use Drupal\uc_payment\Entity\PaymentMethod; /** * Handles the Payment order pane. @@ -61,11 +62,16 @@ function uc_order_pane_payment($op, $order, &$form = NULL, FormStateInterface $f return $build; case 'edit-form': + $options = array(); + foreach (PaymentMethod::loadMultiple() as $method) { + $options[$method->id()] = $method->label(); + } + $form['payment']['payment_method'] = array( '#type' => 'select', '#title' => t('Payment method'), '#default_value' => $order->getPaymentMethodId(), - '#options' => \Drupal::service('plugin.manager.uc_payment.method')->listOptions(), + '#options' => $options, '#ajax' => array( 'callback' => 'uc_payment_order_pane_ajax_callback', 'progress' => array('type' => 'throbber'), @@ -80,7 +86,7 @@ function uc_order_pane_payment($op, $order, &$form = NULL, FormStateInterface $f ); $method = $form_state->getValue('payment_method') ?: $order->getPaymentMethodId(); - if ($method && $details = \Drupal::service('plugin.manager.uc_payment.method')->createInstance($method)->orderEditDetails($order)) { + if ($method && $details = PaymentMethod::load($method)->getPlugin()->orderEditDetails($order)) { if (is_array($details)) { $form['payment']['payment_details'] += $details; } diff --git a/payment/uc_payment_pack/config/install/uc_payment_pack.check.settings.yml b/payment/uc_payment_pack/config/install/uc_payment_pack.check.settings.yml deleted file mode 100644 index f707d17..0000000 --- a/payment/uc_payment_pack/config/install/uc_payment_pack.check.settings.yml +++ /dev/null @@ -1,10 +0,0 @@ -policy: 'Personal and business checks will be held for up to 10 business days to ensure payment clears before an order is shipped.' -mailing_address: - name: '' - company: '' - street1: '' - street2: '' - city: '' - zone: '' - postal_code: '' - country: '' diff --git a/payment/uc_payment_pack/config/install/uc_payment_pack.cod.settings.yml b/payment/uc_payment_pack/config/install/uc_payment_pack.cod.settings.yml deleted file mode 100644 index 830d950..0000000 --- a/payment/uc_payment_pack/config/install/uc_payment_pack.cod.settings.yml +++ /dev/null @@ -1,3 +0,0 @@ -policy: 'Full payment is expected upon delivery or prior to pick-up.' -max_order: 0 -delivery_date: FALSE diff --git a/payment/uc_payment_pack/src/Form/CashOnDeliverySettingsForm.php b/payment/uc_payment_pack/src/Form/CashOnDeliverySettingsForm.php deleted file mode 100644 index 4508094..0000000 --- a/payment/uc_payment_pack/src/Form/CashOnDeliverySettingsForm.php +++ /dev/null @@ -1,63 +0,0 @@ -config('uc_payment_pack.cod.settings'); - - $form['uc_cod_policy'] = array( - '#type' => 'textarea', - '#title' => t('Policy message'), - '#default_value' => $cod_config->get('policy'), - '#description' => t('Help message shown at checkout.'), - ); - $form['uc_cod_max_order'] = array( - '#type' => 'textfield', - '#title' => t('Maximum order total eligible for COD'), - '#default_value' => $cod_config->get('max_order'), - '#description' => t('Set to 0 for no maximum order limit.'), - ); - $form['uc_cod_delivery_date'] = array( - '#type' => 'checkbox', - '#title' => t('Let customers enter a desired delivery date.'), - '#default_value' => $cod_config->get('delivery_date'), - ); - return $form; - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - $cod_config = $this->configFactory()->getEditable('uc_payment_pack.cod.settings'); - $cod_config - ->set('policy', $form_state->getValue('uc_cod_policy')) - ->set('max_order', $form_state->getValue('uc_cod_max_order')) - ->set('delivery_date', $form_state->getValue('uc_cod_delivery_date')) - ->save(); - } -} - diff --git a/payment/uc_payment_pack/src/Form/CheckSettingsForm.php b/payment/uc_payment_pack/src/Form/CheckSettingsForm.php deleted file mode 100644 index b635c29..0000000 --- a/payment/uc_payment_pack/src/Form/CheckSettingsForm.php +++ /dev/null @@ -1,83 +0,0 @@ -config('uc_payment_pack.check.settings'); - - $form['check_address_info'] = array( - '#markup' => '
' . t('Set the mailing address to display to customers who choose this payment method during checkout.') . '
', - ); - $form['uc_check_mailing_name'] = array( - '#type' => 'textfield', - '#title' => t('Contact'), - '#description' => t('Direct checks to a person or department.'), - '#default_value' => $check_config->get('mailing_address.name'), - ); - $default_country = $check_config->get('mailing_address.country') ? $check_config->get('mailing_address.country') : $this->config('uc_store.settings')->get('address.country'); - $form['uc_check_address'] = array( - '#type' => 'uc_address', - '#default_value' => array( - 'uc_check_mailing_company' => $check_config->get('mailing_address.company'), - 'uc_check_mailing_street1' => $check_config->get('mailing_address.street1'), - 'uc_check_mailing_street2' => $check_config->get('mailing_address.street2'), - 'uc_check_mailing_city' => $check_config->get('mailing_address.city'), - 'uc_check_mailing_zone' => $check_config->get('mailing_address.zone'), - 'uc_check_mailing_country' => $form_state->hasValue('uc_check_mailing_country') ? $form_state->getValue('uc_check_mailing_country') : $default_country, - 'uc_check_mailing_postal_code' => $check_config->get('mailing_address.postal_code'), - ), - '#required' => FALSE, - '#key_prefix' => 'uc_check_mailing', - ); - $form['uc_check_policy'] = array( - '#type' => 'textarea', - '#title' => t('Check payment policy', [], ['context' => 'cheque']), - '#description' => t('Instructions for customers on the checkout page.'), - '#default_value' => $check_config->get('policy'), - '#rows' => 3, - ); - - return $form; - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - $check_config = $this->configFactory()->getEditable('uc_payment_pack.check.settings'); - $check_config - ->set('mailing_address.name', $form_state->getValue('uc_check_mailing_name')) - ->set('mailing_address.company', $form_state->getValue('uc_check_mailing_company')) - ->set('mailing_address.street1', $form_state->getValue('uc_check_mailing_street1')) - ->set('mailing_address.street2', $form_state->getValue('uc_check_mailing_street2')) - ->set('mailing_address.city', $form_state->getValue('uc_check_mailing_city')) - ->set('mailing_address.zone', $form_state->getValue('uc_check_mailing_zone')) - ->set('mailing_address.country', $form_state->getValue('uc_check_mailing_country')) - ->set('mailing_address.postal_code', $form_state->getValue('uc_check_mailing_postal_code')) - ->set('policy', $form_state->getValue('uc_check_policy')) - ->save(); - } -} diff --git a/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/CashOnDelivery.php b/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/CashOnDelivery.php index 568acd9..113a05b 100644 --- a/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/CashOnDelivery.php +++ b/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/CashOnDelivery.php @@ -14,14 +14,9 @@ use Drupal\uc_payment\PaymentMethodPluginBase; /** * Defines the cash on delivery payment method. * - * @PaymentMethod( + * @UbercartPaymentMethod( * id = "cod", * name = @Translation("Cash on delivery"), - * title = @Translation("Cash on delivery"), - * checkout = FALSE, - * no_gateway = TRUE, - * settings_form = "Drupal\uc_payment_pack\Form\CashOnDeliverySettingsForm", - * weight = 1, * ) */ class CashOnDelivery extends PaymentMethodPluginBase { @@ -29,20 +24,62 @@ class CashOnDelivery extends PaymentMethodPluginBase { /** * {@inheritdoc} */ - public function cartDetails(OrderInterface $order, array $form, FormStateInterface $form_state) { - $cod_config = \Drupal::config('uc_payment_pack.cod.settings'); + public function defaultConfiguration() { + return [ + 'policy' => 'Full payment is expected upon delivery or prior to pick-up.', + 'max_order' => 0, + 'delivery_date' => FALSE, + ]; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['policy'] = array( + '#type' => 'textarea', + '#title' => t('Policy message'), + '#default_value' => $this->configuration['policy'], + '#description' => t('Help message shown at checkout.'), + ); + $form['max_order'] = array( + '#type' => 'textfield', + '#title' => t('Maximum order total eligible for COD'), + '#default_value' => $this->configuration['max_order'], + '#description' => t('Set to 0 for no maximum order limit.'), + ); + $form['delivery_date'] = array( + '#type' => 'checkbox', + '#title' => t('Let customers enter a desired delivery date.'), + '#default_value' => $this->configuration['delivery_date'], + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['policy'] = $form_state->getValue('policy'); + $this->configuration['max_order'] = $form_state->getValue('max_order'); + $this->configuration['delivery_date'] = $form_state->getValue('delivery_date'); + } + /** + * {@inheritdoc} + */ + public function cartDetails(OrderInterface $order, array $form, FormStateInterface $form_state) { $build['policy'] = array( - '#markup' => '

' . $cod_config->get('policy') . '

' + '#markup' => '

' . $this->configuration['policy'] . '

' ); - if (($max = $cod_config->get('max_order')) > 0 && is_numeric($max)) { + if (($max = $this->configuration['max_order']) > 0 && is_numeric($max)) { $build['eligibility'] = array( '#markup' => '

' . t('Orders totalling more than @amount are not eligible for COD.', ['@amount' => uc_currency_format($max)]) . '

' ); } - if ($cod_config->get('delivery_date')) { + if ($this->configuration['delivery_date']) { $build += $this->deliveryDateForm($order); } @@ -53,9 +90,7 @@ class CashOnDelivery extends PaymentMethodPluginBase { * {@inheritdoc} */ public function cartProcess(OrderInterface $order, array $form, FormStateInterface $form_state) { - $cod_config = \Drupal::config('uc_payment_pack.cod.settings'); - - if ($cod_config->get('delivery_date')) { + if ($this->configuration['delivery_date']) { $order->payment_details = $form_state->getValue(['panes', 'payment', 'details']); } @@ -66,11 +101,9 @@ class CashOnDelivery extends PaymentMethodPluginBase { * {@inheritdoc} */ public function cartReview(OrderInterface $order) { - $cod_config = \Drupal::config('uc_payment_pack.cod.settings'); - $review = array(); - if ($cod_config->get('delivery_date')) { + if ($this->configuration['delivery_date']) { $date = uc_date_format( $order->payment_details['delivery_month'], $order->payment_details['delivery_day'], @@ -86,11 +119,9 @@ class CashOnDelivery extends PaymentMethodPluginBase { * {@inheritdoc} */ public function orderView(OrderInterface $order) { - $cod_config = \Drupal::config('uc_payment_pack.cod.settings'); - $build = array(); - if ($cod_config->get('delivery_date') && + if ($this->configuration['delivery_date'] && isset($order->payment_details['delivery_month']) && isset($order->payment_details['delivery_day']) && isset($order->payment_details['delivery_year'])) { @@ -109,11 +140,9 @@ class CashOnDelivery extends PaymentMethodPluginBase { * {@inheritdoc} */ public function orderEditDetails(OrderInterface $order) { - $cod_config = \Drupal::config('uc_payment_pack.cod.settings'); - $build = array(); - if ($cod_config->get('delivery_date')) { + if ($this->configuration['delivery_date']) { $build = $this->deliveryDateForm($order); } @@ -156,8 +185,7 @@ class CashOnDelivery extends PaymentMethodPluginBase { * {@inheritdoc} */ public function orderSubmit(OrderInterface $order) { - $cod_config = \Drupal::config('uc_payment_pack.cod.settings'); - $max = $cod_config->get('max_order'); + $max = $this->configuration['max_order']; if ($max > 0 && $order->getTotal() > $max) { $result[] = array( diff --git a/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/Check.php b/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/Check.php index ea801d6..9b9a5c5 100644 --- a/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/Check.php +++ b/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/Check.php @@ -16,54 +16,83 @@ use Drupal\uc_store\Address; /** * Defines the check payment method. * - * @PaymentMethod( + * @UbercartPaymentMethod( * id = "check", * name = @Translation("Check", context = "cheque"), - * title = @Translation("Check or money order"), - * checkout = TRUE, - * no_gateway = TRUE, - * settings_form = "Drupal\uc_payment_pack\Form\CheckSettingsForm", - * weight = 1, * ) */ class Check extends PaymentMethodPluginBase { + public function defaultConfiguration() { + $config = \Drupal::config('uc_store.settings'); + return [ + 'policy' => $this->t('Personal and business checks will be held for up to 10 business days to ensure payment clears before an order is shipped.'), + 'name' => '', + 'address' => $config->get('address') + ['company' => $config->get('name')], + ]; + } + /** * {@inheritdoc} */ - public function cartDetails(OrderInterface $order, array $form, FormStateInterface $form_state) { - $check_config = \Drupal::config('uc_payment_pack.check.settings'); + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['policy'] = array( + '#type' => 'textarea', + '#title' => t('Check payment policy', [], ['context' => 'cheque']), + '#description' => t('Instructions for customers on the checkout page.'), + '#default_value' => $this->configuration['policy'], + '#rows' => 3, + ); + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Contact'), + '#description' => t('Direct checks to a person or department.'), + '#default_value' => $this->configuration['name'], + ); + $form['address'] = array( + '#type' => 'uc_address', + '#tree' => TRUE, + '#default_value' => $this->configuration['address'], + '#required' => FALSE, + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['policy'] = $form_state->getValue('policy'); + $this->configuration['name'] = $form_state->getValue('name'); + $this->configuration['address'] = $form_state->getValue('address'); + } + /** + * {@inheritdoc} + */ + public function cartDetails(OrderInterface $order, array $form, FormStateInterface $form_state) { $build['instructions'] = array( '#markup' => t('Checks should be made out to:') ); - if (!$check_config->get('mailing_address.street1')) { - $build['address'] = array( - '#markup' => uc_store_address(), - '#prefix' => '

', - '#suffix' => '

', - ); - } - else { - $address = new Address(); - $address->first_name = $check_config->get('mailing_address.name'); - $address->company = $check_config->get('mailing_address.company'); - $address->street1 = $check_config->get('mailing_address.street1'); - $address->street2 = $check_config->get('mailing_address.street2'); - $address->city = $check_config->get('mailing_address.city'); - $address->zone = $check_config->get('mailing_address.zone'); - $address->postal_code = $check_config->get('mailing_address.postal_code'); - $address->country = $check_config->get('mailing_address.country'); - $build['address'] = array( - '#markup' => (string) $address, - '#prefix' => '

', - '#suffix' => '

', - ); - } + $address = new Address(); + $address->first_name = $this->configuration['name']; + $address->company = $this->configuration['address']['company']; + $address->street1 = $this->configuration['address']['street1']; + $address->street2 = $this->configuration['address']['street2']; + $address->city = $this->configuration['address']['city']; + $address->zone = $this->configuration['address']['zone']; + $address->postal_code = $this->configuration['address']['postal_code']; + $address->country = $this->configuration['address']['country']; + $build['address'] = array( + '#markup' => (string) $address, + '#prefix' => '

', + '#suffix' => '

', + ); $build['policy'] = array( - '#markup' => '

' . $check_config->get('policy') . '

' + '#markup' => '

' . $this->configuration['policy'] . '

' ); return $build; @@ -73,30 +102,20 @@ class Check extends PaymentMethodPluginBase { * {@inheritdoc} */ public function cartReview(OrderInterface $order) { - $check_config = \Drupal::config('uc_payment_pack.check.settings'); - - if (!$check_config->get('mailing_address.street1')) { - $review[] = array( - 'title' => t('Mail to'), - 'data' => uc_store_address(), - ); - } - else { - $address = new Address(); - $address->first_name = $check_config->get('mailing_address.name'); - $address->company = $check_config->get('mailing_address.company'); - $address->street1 = $check_config->get('mailing_address.street1'); - $address->street2 = $check_config->get('mailing_address.street2'); - $address->city = $check_config->get('mailing_address.city'); - $address->zone = $check_config->get('mailing_address.zone'); - $address->postal_code = $check_config->get('mailing_address.postal_code'); - $address->country = $check_config->get('mailing_address.country'); - - $review[] = array( - 'title' => t('Mail to'), - 'data' => (string) $address, - ); - } + $address = new Address(); + $address->first_name = $this->configuration['name']; + $address->company = $this->configuration['address']['company']; + $address->street1 = $this->configuration['address']['street1']; + $address->street2 = $this->configuration['address']['street2']; + $address->city = $this->configuration['address']['city']; + $address->zone = $this->configuration['address']['zone']; + $address->postal_code = $this->configuration['address']['postal_code']; + $address->country = $this->configuration['address']['country']; + + $review[] = array( + 'title' => t('Mail to'), + 'data' => (string) $address, + ); return $review; } diff --git a/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/Other.php b/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/Other.php index 60793da..ebc1ada 100644 --- a/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/Other.php +++ b/payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/Other.php @@ -13,13 +13,9 @@ use Drupal\uc_payment\PaymentMethodPluginBase; /** * Defines a generic payment method. * - * @PaymentMethod( + * @UbercartPaymentMethod( * id = "other", * name = @Translation("Other"), - * title = @Translation("Other"), - * checkout = FALSE, - * no_gateway = TRUE, - * weight = 10, * ) */ class Other extends PaymentMethodPluginBase { diff --git a/payment/uc_payment_pack/src/Tests/PaymentPackTest.php b/payment/uc_payment_pack/src/Tests/PaymentPackTest.php index e2773f3..cb74d01 100644 --- a/payment/uc_payment_pack/src/Tests/PaymentPackTest.php +++ b/payment/uc_payment_pack/src/Tests/PaymentPackTest.php @@ -39,13 +39,15 @@ class PaymentPackTest extends UbercartTestBase { * Tests for Check payment method. */ public function testCheck() { - $this->drupalGet('admin/store/config/payment'); - $this->assertText('Check', 'Check payment method found.'); - $this->assertFieldByName('methods[check][status]', 1, 'Check payment method is enabled by default.'); + $this->drupalGet('admin/store/config/payment/add/check'); + $this->assertText('Check'); + $this->assertFieldByName('settings[policy]', 'Personal and business checks will be held for up to 10 business days to ensure payment clears before an order is shipped.', 'Default check payment policy found.'); - $this->drupalGet('admin/store/config/payment/method/check'); - $this->assertTitle('Check settings | Drupal'); - $this->assertText(\Drupal::config('uc_payment_pack.check.settings')->get('policy'), 'Default check payment policy found.'); + $edit = [ + 'id' => strtolower($this->randomMachineName()), + 'label' => $this->randomString(), + 'settings[policy]' => $this->randomString(), + ]; // Fill in and save the check address settings. $address = new Address(); @@ -57,40 +59,34 @@ class PaymentPackTest extends UbercartTestBase { $address->postal_code = mt_rand(10000, 99999); $country_id = array_rand(\Drupal::service('country_manager')->getEnabledList()); $address->country = $country_id; - - //$edit = array( - // 'uc_check_mailing_country' => $address->country, - //); - //$this->drupalPostAjaxForm('admin/store/config/payment/method/check', $edit, 'uc_check_mailing_country'); - $edit = array( - 'uc_check_mailing_name' => $address->first_name, - 'uc_check_mailing_company' => $address->company, - 'uc_check_mailing_street1' => $address->street1, - 'uc_check_mailing_street2' => $address->street2, - 'uc_check_mailing_city' => $address->city, - 'uc_check_mailing_postal_code' => $address->postal_code, + $this->drupalPostAjaxForm(NULL, ['settings[address][country]' => $address->country], 'settings[address][country]'); + + $edit += array( + 'settings[name]' => $address->first_name, + 'settings[address][company]' => $address->company, + 'settings[address][street1]' => $address->street1, + 'settings[address][street2]' => $address->street2, + 'settings[address][city]' => $address->city, + 'settings[address][country]' => $address->country, + 'settings[address][postal_code]' => $address->postal_code, ); - // Don't try to set the zone unless the store country has zones! + // Don't try to set the zone unless the country has zones! $zone_list = \Drupal::service('country_manager')->getZoneList($country_id); if (!empty($zone_list)) { $address->zone = array_rand($zone_list); $edit += array( - 'uc_check_mailing_zone' => $address->zone, + 'settings[address][zone]' => $address->zone, ); } - // Fool the Ajax by setting the store default country to our randomly-chosen - // country before we post the form. Otherwise the zone select won't be - // populated correctly. - \Drupal::configFactory()->getEditable('uc_payment_pack.check.settings')->set('mailing_address.country', $country_id)->save(); - $this->drupalPostForm('admin/store/config/payment/method/check', $edit, t('Save configuration')); + $this->drupalPostForm(NULL, $edit, 'Save'); // Test that check settings show up on checkout page. $this->drupalGet('cart/checkout'); - $this->assertFieldByName('panes[payment][payment_method]', 'check', 'Check payment method is selected at checkout.'); + $this->assertFieldByName('panes[payment][payment_method]', $edit['id'], 'Check payment method is selected at checkout.'); $this->assertText('Checks should be made out to:'); $this->assertRaw((string) $address, 'Properly formatted check mailing address found.'); - $this->assertText(\Drupal::config('uc_payment_pack.check.settings')->get('policy'), 'Check payment policy found at checkout.'); + $this->assertEscaped($edit['settings[policy]'], 'Check payment policy found at checkout.'); // Test that check settings show up on review order page. $this->drupalPostForm(NULL, array(), 'Review order'); @@ -101,7 +97,7 @@ class PaymentPackTest extends UbercartTestBase { // Test user order view $order = Order::load(1); - $this->assertEqual($order->getPaymentMethodId(), 'check', 'Order has check payment method.'); + $this->assertEqual($order->getPaymentMethodId(), $edit['id'], 'Order has check payment method.'); $this->drupalGet('user/' . $order->getOwnerId() . '/orders/' . $order->id()); $this->assertText('Method: Check', 'Check payment method displayed.'); @@ -135,25 +131,18 @@ class PaymentPackTest extends UbercartTestBase { * Tests for Cash on Delivery payment method. */ public function testCashOnDelivery() { - $this->drupalGet('admin/store/config/payment'); - $this->assertText('Cash on delivery', 'COD payment method found.'); - $this->assertFieldByName('methods[cod][status]', 0, 'COD payment method is disabled by default.'); - $edit = array( - 'methods[cod][status]' => 1, - 'methods[cod][weight]' => -10, - ); - $this->drupalPostForm(NULL, $edit, 'Save configuration'); + $this->drupalGet('admin/store/config/payment/add/cod'); + $this->assertFieldByName('settings[policy]', 'Full payment is expected upon delivery or prior to pick-up.', 'Default COD policy found.'); - $this->drupalGet('admin/store/config/payment/method/cod'); - $this->assertTitle('Cash on delivery settings | Drupal'); - $this->assertText(\Drupal::config('uc_payment_pack.cod.settings')->get('policy'), 'Default COD policy found.'); - // @todo: Test changing the policy on settings page - // @todo: Test enabling delivery datae on settings page + $cod = $this->createPaymentMethod('cod', [ + 'settings[policy]' => $this->randomString(), + ]); + // @todo: Test enabling delivery date on settings page // Test checkout page $this->drupalGet('cart/checkout'); - $this->assertFieldByName('panes[payment][payment_method]', 'cod', 'COD payment method is selected at checkout.'); - $this->assertText(\Drupal::config('uc_payment_pack.cod.settings')->get('policy'), 'Default COD policy found at checkout.'); + $this->assertFieldByName('panes[payment][payment_method]', $cod['id'], 'COD payment method is selected at checkout.'); + $this->assertEscaped($cod['settings[policy]'], 'COD policy found at checkout.'); // Test review order page $this->drupalPostForm(NULL, array(), 'Review order'); @@ -162,7 +151,7 @@ class PaymentPackTest extends UbercartTestBase { // Test user order view $order = Order::load(1); - $this->assertEqual($order->getPaymentMethodId(), 'cod', 'Order has COD payment method.'); + $this->assertEqual($order->getPaymentMethodId(), $cod['id'], 'Order has COD payment method.'); $this->drupalGet('user/' . $order->getOwnerId() . '/orders/' . $order->id()); $this->assertText('Method: Cash on delivery', 'COD payment method displayed.'); @@ -176,18 +165,11 @@ class PaymentPackTest extends UbercartTestBase { * Tests for Other payment method. */ public function testOther() { - $this->drupalGet('admin/store/config/payment'); - $this->assertText('Other', 'Other payment method found.'); - $this->assertFieldByName('methods[other][status]', 0, 'Other payment method is disabled by default.'); - $edit = array( - 'methods[other][status]' => 1, - 'methods[other][weight]' => -10, - ); - $this->drupalPostForm(NULL, $edit, 'Save configuration'); + $other = $this->createPaymentMethod('other'); // Test checkout page $this->drupalGet('cart/checkout'); - $this->assertFieldByName('panes[payment][payment_method]', 'other', 'Other payment method is selected at checkout.'); + $this->assertFieldByName('panes[payment][payment_method]', $other['id'], 'Other payment method is selected at checkout.'); // Test review order page $this->drupalPostForm(NULL, array(), 'Review order'); @@ -196,7 +178,7 @@ class PaymentPackTest extends UbercartTestBase { // Test user order view $order = Order::load(1); - $this->assertEqual($order->getPaymentMethodId(), 'other', 'Order has other payment method.'); + $this->assertEqual($order->getPaymentMethodId(), $other['id'], 'Order has other payment method.'); $this->drupalGet('user/' . $order->getOwnerId() . '/orders/' . $order->id()); $this->assertText('Method: Other', 'Other payment method displayed.'); @@ -206,7 +188,7 @@ class PaymentPackTest extends UbercartTestBase { $this->assertText('Method: Other', 'Other payment method displayed.'); $this->drupalGet('admin/store/orders/' . $order->id() . '/edit'); - $this->assertFieldByName('payment_method', 'other', 'Other payment method is selected in the order edit form.'); + $this->assertFieldByName('payment_method', $other['id'], 'Other payment method is selected in the order edit form.'); $edit = array( 'payment_details[description]' => $this->randomString(), ); diff --git a/shipping/uc_quote/src/Tests/QuoteTest.php b/shipping/uc_quote/src/Tests/QuoteTest.php index 11bb3b6..5c52f92 100644 --- a/shipping/uc_quote/src/Tests/QuoteTest.php +++ b/shipping/uc_quote/src/Tests/QuoteTest.php @@ -24,6 +24,7 @@ class QuoteTest extends UbercartTestBase { public function setUp() { parent::setUp(); $this->drupalLogin($this->adminUser); + $this->createPaymentMethod('check'); // In order to test zone-based conditions, this particular test class // assumes that US is enabled as default, and CA is also enabled. diff --git a/uc_cart/src/Plugin/Ubercart/CheckoutPane/AddressPaneBase.php b/uc_cart/src/Plugin/Ubercart/CheckoutPane/AddressPaneBase.php index db9343d..5cca548 100644 --- a/uc_cart/src/Plugin/Ubercart/CheckoutPane/AddressPaneBase.php +++ b/uc_cart/src/Plugin/Ubercart/CheckoutPane/AddressPaneBase.php @@ -87,6 +87,7 @@ abstract class AddressPaneBase extends CheckoutPanePluginBase { $contents['address'] = array( '#type' => 'uc_address', '#default_value' => $order->getAddress($pane), + '#parents' => ['panes', $pane], '#prefix' => '
', '#suffix' => '
', ); diff --git a/uc_cart/src/Tests/CartCheckoutTest.php b/uc_cart/src/Tests/CartCheckoutTest.php index 41126da..2dc8498 100644 --- a/uc_cart/src/Tests/CartCheckoutTest.php +++ b/uc_cart/src/Tests/CartCheckoutTest.php @@ -51,6 +51,9 @@ class CartCheckoutTest extends UbercartTestBase { // Create a simple customer user account. $this->customer = $this->drupalCreateUser(); + // Create a payment method. + $this->createPaymentMethod('check'); + // Ensure test mails are logged. \Drupal::configFactory()->getEditable('system.mail') ->set('interface.uc_order', 'test_mail_collector') diff --git a/uc_country/src/Tests/CountryTest.php b/uc_country/src/Tests/CountryTest.php index 2ab5fe4..230f5cb 100644 --- a/uc_country/src/Tests/CountryTest.php +++ b/uc_country/src/Tests/CountryTest.php @@ -55,7 +55,7 @@ class CountryTest extends WebTestBase { // Verify that last random country doesn't show up as available. $this->drupalGet('admin/store/config/store'); $this->assertNoOption( - 'edit-uc-store-country', + 'edit-address-country', $last_country, SafeMarkup::format('%country not listed in uc_address select country field.', ['%country' => $countries[$last_country]]) ); @@ -72,7 +72,7 @@ class CountryTest extends WebTestBase { // Verify that last random country now shows up as available. $this->drupalGet('admin/store/config/store'); $this->assertOption( - 'edit-uc-store-country', + 'edit-address-country', $last_country, SafeMarkup::format('%country is listed in uc_address select country field.', ['%country' => $countries[$last_country]]) ); diff --git a/uc_order/uc_order.order_pane.inc b/uc_order/uc_order.order_pane.inc index 8c9354a..c65aa42 100644 --- a/uc_order/uc_order.order_pane.inc +++ b/uc_order/uc_order.order_pane.inc @@ -56,9 +56,9 @@ function uc_order_pane_ship_to($op, $order, &$form = NULL, FormStateInterface $f return $build; case 'edit-form': - $form['ship_to']['#tree'] = TRUE; $form['ship_to']['address'] = array( '#type' => 'uc_address', + '#parents' => ['ship_to'], '#default_value' => $order->getAddress('delivery'), '#required' => FALSE, ); @@ -99,9 +99,9 @@ function uc_order_pane_bill_to($op, $order, &$form = NULL, FormStateInterface $f return $build; case 'edit-form': - $form['bill_to']['#tree'] = TRUE; $form['bill_to']['address'] = array( '#type' => 'uc_address', + '#parents' => ['bill_to'], '#default_value' => $order->getAddress('billing'), '#required' => FALSE, ); diff --git a/uc_role/src/Tests/RoleCheckoutTest.php b/uc_role/src/Tests/RoleCheckoutTest.php index 4ff0df0..dd4456c 100644 --- a/uc_role/src/Tests/RoleCheckoutTest.php +++ b/uc_role/src/Tests/RoleCheckoutTest.php @@ -36,6 +36,9 @@ class RoleCheckoutTest extends UbercartTestBase { } public function testCheckoutRoleAssignment() { + $this->drupalLogin($this->adminUser); + $method = $this->createPaymentMethod('other'); + // Add role assignment to the test product. $rid = $this->drupalCreateRole(array('access content')); $this->drupalLogin($this->adminUser); @@ -45,10 +48,11 @@ class RoleCheckoutTest extends UbercartTestBase { // Process an anonymous, shippable order. $order = $this->createOrder([ 'uid' => 0, + 'payment_method' => $method['id'], ]); $order->products[1]->data->shippable = 1; $order->save(); - uc_payment_enter($order->id(), 'SimpleTest', $order->getTotal()); + uc_payment_enter($order->id(), $method['id'], $order->getTotal()); // Find the order uid. $uid = db_query('SELECT uid FROM {uc_orders} ORDER BY order_id DESC')->fetchField(); @@ -71,10 +75,11 @@ class RoleCheckoutTest extends UbercartTestBase { $order = $this->createOrder(array( 'uid' => 0, 'primary_email' => $this->customer->getEmail(), + 'payment_method' => $method['id'], )); $order->products[2]->data->shippable = 0; $order->save(); - uc_payment_enter($order->id(), 'SimpleTest', $order->getTotal()); + uc_payment_enter($order->id(), $method['id'], $order->getTotal()); $account = User::load($this->customer->id()); // @todo Re-enable when Rules is available. // $this->assertTrue($account->hasRole($rid), 'Existing user was granted role.'); diff --git a/uc_role/src/Tests/RoleTest.php b/uc_role/src/Tests/RoleTest.php index bb9d49e..3a34ec1 100644 --- a/uc_role/src/Tests/RoleTest.php +++ b/uc_role/src/Tests/RoleTest.php @@ -33,9 +33,10 @@ class RoleTest extends UbercartTestBase { $this->drupalPostForm(NULL, $edit, t('Save feature')); // Check out with the test product. + $method = $this->createPaymentMethod('other'); $this->addToCart($this->product); $order = $this->checkout(); - uc_payment_enter($order->id(), 'other', $order->getTotal()); + uc_payment_enter($order->id(), $method['id'], $order->getTotal()); // Test that the role was granted. // @todo Re-enable when Rules is available. diff --git a/uc_store/src/Element/UcAddress.php b/uc_store/src/Element/UcAddress.php index 6af1b21..8925bf2 100644 --- a/uc_store/src/Element/UcAddress.php +++ b/uc_store/src/Element/UcAddress.php @@ -192,7 +192,6 @@ class UcAddress extends Element\FormElement { $element[$field] = $subelement + array( '#title' => $labels[$base_field], '#default_value' => is_object($value) ? $value->$field : $value[$field], - '#parents' => array_merge(array_slice($element['#parents'], 0, -1), array($field)), '#access' => !$element['#hidden'] && !empty($config[$base_field]['status']), '#required' => $element['#required'] && !empty($config[$base_field]['required']), '#weight' => isset($config[$base_field]['weight']) ? $config[$base_field]['weight'] : 0, diff --git a/uc_store/src/Form/StoreSettingsForm.php b/uc_store/src/Form/StoreSettingsForm.php index 3c0c1a4..f4ee28e 100644 --- a/uc_store/src/Form/StoreSettingsForm.php +++ b/uc_store/src/Form/StoreSettingsForm.php @@ -89,16 +89,8 @@ class StoreSettingsForm extends ConfigFormBase { ); $form['address']['address'] = array( '#type' => 'uc_address', - '#default_value' => array( - 'uc_store_street1' => $config->get('address.street1'), - 'uc_store_street2' => $config->get('address.street2'), - 'uc_store_city' => $config->get('address.city'), - 'uc_store_zone' => $config->get('address.zone'), - 'uc_store_country' => $form_state->hasValue('uc_store_country') ? $form_state->getValue('uc_store_country') : $config->get('address.country'), - 'uc_store_postal_code' => $config->get('address.postal_code'), - ), + '#default_value' => $config->get('address'), '#required' => FALSE, - '#key_prefix' => 'uc_store', ); $form['currency'] = array( @@ -263,12 +255,7 @@ class StoreSettingsForm extends ConfigFormBase { ->set('phone', $form_state->getValue('uc_store_phone')) ->set('fax', $form_state->getValue('uc_store_fax')) ->set('help_page', $form_state->getValue('uc_store_help_page')) - ->set('address.street1', $form_state->getValue('uc_store_street1')) - ->set('address.street2', $form_state->getValue('uc_store_street2')) - ->set('address.city', $form_state->getValue('uc_store_city')) - ->set('address.zone', $form_state->getValue('uc_store_zone')) - ->set('address.country', $form_state->getValue('uc_store_country')) - ->set('address.postal_code', $form_state->getValue('uc_store_postal_code')) + ->set('address', $form_state->getValue('address')) ->set('currency.code', $form_state->getValue('uc_currency_code')) ->set('currency.symbol', $form_state->getValue('uc_currency_sign')) ->set('currency.symbol_after', $form_state->getValue('uc_sign_after_amount')) diff --git a/uc_store/src/Plugin/Discovery/InfoHookDecorator.php b/uc_store/src/Plugin/Discovery/InfoHookDecorator.php deleted file mode 100644 index d423d6c..0000000 --- a/uc_store/src/Plugin/Discovery/InfoHookDecorator.php +++ /dev/null @@ -1,63 +0,0 @@ -decorated = $decorated; - $this->hook = $hook; - $this->legacyClass = $class; - } - - /** - * {@inheritdoc} - */ - public function getDefinitions() { - $definitions = $this->decorated->getDefinitions(); - foreach (\Drupal::moduleHandler()->getImplementations($this->hook) as $module) { - $function = $module . '_' . $this->hook; - $result = $function($definitions); - if (is_array($result)) { - foreach ($result as $plugin_id => $definition) { - $definition += array( - 'id' => $plugin_id, - 'provider' => $module, - 'class' => $this->legacyClass, - ); - $definitions[$definition['id']] = $definition; - } - } - } - return $definitions; - } - -} diff --git a/uc_store/src/Tests/AjaxTest.php b/uc_store/src/Tests/AjaxTest.php index 675d8c6..55ca899 100644 --- a/uc_store/src/Tests/AjaxTest.php +++ b/uc_store/src/Tests/AjaxTest.php @@ -80,11 +80,8 @@ class AjaxTest extends UbercartTestBase { public function testCheckoutAjax() { // Enable two payment methods and set a condition on one. - $edit = array( - 'methods[check][status]' => TRUE, - 'methods[other][status]' => TRUE, - ); - $this->drupalPostForm('admin/store/config/payment', $edit, 'Save configuration'); + $this->createPaymentMethod('check'); + $this->createPaymentMethod('other'); // $this->addPaymentZoneCondition('other', 'KS'); // Specify that the billing zone should update the payment pane. diff --git a/uc_store/src/Tests/StoreTest.php b/uc_store/src/Tests/StoreTest.php index c8dc1e2..880148e 100644 --- a/uc_store/src/Tests/StoreTest.php +++ b/uc_store/src/Tests/StoreTest.php @@ -30,10 +30,10 @@ class StoreTest extends UbercartTestBase { 'uc_store_phone' => $this->randomMachineName(), 'uc_store_fax' => $this->randomMachineName(), 'uc_store_help_page' => $this->randomMachineName(), - 'uc_store_street1' => $this->randomMachineName(), - 'uc_store_street2' => $this->randomMachineName(), - 'uc_store_city' => $this->randomMachineName(), - 'uc_store_postal_code' => $this->randomMachineName(), + 'address[street1]' => $this->randomMachineName(), + 'address[street2]' => $this->randomMachineName(), + 'address[city]' => $this->randomMachineName(), + 'address[postal_code]' => $this->randomMachineName(), 'uc_currency_code' => $this->randomMachineName(3), 'uc_currency_sign' => $this->randomMachineName(1), 'uc_currency_thou' => $this->randomMachineName(1), @@ -46,7 +46,7 @@ class StoreTest extends UbercartTestBase { $zone_list = \Drupal::service('country_manager')->getZoneList($country_id); if (!empty($zone_list)) { $edit += array( - 'uc_store_zone' => array_rand($zone_list), + 'address[zone]' => array_rand($zone_list), ); } diff --git a/uc_store/src/Tests/UbercartTestBase.php b/uc_store/src/Tests/UbercartTestBase.php index d078416..8e26a25 100644 --- a/uc_store/src/Tests/UbercartTestBase.php +++ b/uc_store/src/Tests/UbercartTestBase.php @@ -308,6 +308,28 @@ abstract class UbercartTestBase extends WebTestBase { } /** + * Defines a new payment method. + */ + protected function createPaymentMethod($plugin_id, $values = []) { + $has_user = $this->loggedInUser; + if (!$has_user) { + $this->drupalLogin($this->adminUser); + } + + $values += [ + 'id' => strtolower($this->randomMachineName()), + 'label' => $this->randomString(), + ]; + $this->drupalPostForm('admin/store/config/payment/add/' . $plugin_id, $values, 'Save'); + + if (!$has_user) { + $this->drupalLogout(); + } + + return $values; + } + + /** * Asserts that the most recently sent e-mails do not have the string in it. * * @param $field_name diff --git a/uc_tax/src/Tests/StoredTaxTest.php b/uc_tax/src/Tests/StoredTaxTest.php index fe98420..15e358e 100644 --- a/uc_tax/src/Tests/StoredTaxTest.php +++ b/uc_tax/src/Tests/StoredTaxTest.php @@ -43,8 +43,7 @@ class StoredTaxTest extends UbercartTestBase { $this->drupalLogin($this->adminUser); // Enable a payment method for the payment preview checkout pane. - $edit = array('methods[check][status]' => 1); - $this->drupalPostForm('admin/store/config/payment', $edit, t('Save configuration')); + $this->createPaymentMethod('check'); // Create a 20% inclusive tax rate. $rate = (object) array(