From 67031f14624d06ab1af547fcad51b85c95f0e411 Mon Sep 17 00:00:00 2001 From: tavi toporjinschi Date: Thu, 9 Mar 2017 12:05:19 +0000 Subject: [PATCH 1/3] Issue #2858179 by bojanz, vasike: Allow gateways to show text at checkout (in PaymentInformation) --- .../config/schema/commerce_payment.schema.yml | 4 ++++ .../CheckoutPane/PaymentInformation.php | 9 +++++++++ .../PaymentGateway/PaymentGatewayBase.php | 20 +++++++++++++++++++ .../PaymentGatewayInterface.php | 8 ++++++++ .../src/Functional/PaymentGatewayTest.php | 12 +++++++++++ 5 files changed, 53 insertions(+) diff --git a/modules/payment/config/schema/commerce_payment.schema.yml b/modules/payment/config/schema/commerce_payment.schema.yml index 24881a5b24..31e25eb1ee 100644 --- a/modules/payment/config/schema/commerce_payment.schema.yml +++ b/modules/payment/config/schema/commerce_payment.schema.yml @@ -26,6 +26,10 @@ commerce_payment_gateway_configuration: mode: type: string label: 'Mode' + details: + type: text_format + label: 'Details' + translatable: true payment_method_types: type: sequence label: 'Payment method types' diff --git a/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php index 260d88ae47..7d54fdfd33 100644 --- a/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php +++ b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php @@ -153,6 +153,15 @@ public function buildPaneForm(array $pane_form, FormStateInterface $form_state, $selected_option = $pane_form['payment_method'][$default_option]; $payment_gateway = $payment_gateways[$selected_option['#payment_gateway']]; + + // Add details markup if available. + if (empty($selected_option['#payment_method']) && $details = $payment_gateway->getPlugin()->getDetails()) { + $pane_form['details'] = [ + '#type' => 'item', + '#markup' => check_markup($details['value'], $details['format']), + ]; + } + if ($payment_gateway->getPlugin() instanceof SupportsStoredPaymentMethodsInterface) { if (!empty($selected_option['#payment_method_type'])) { /** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */ diff --git a/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php b/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php index 7ee6d3f15d..84ec89b438 100644 --- a/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php +++ b/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php @@ -149,6 +149,13 @@ public function getSupportedModes() { return $this->pluginDefinition['modes']; } + /** + * {@inheritdoc} + */ + public function getDetails() { + return ($this->configuration['details']['value']) ? $this->configuration['details'] : NULL; + } + /** * {@inheritdoc} */ @@ -217,6 +224,10 @@ public function defaultConfiguration() { return [ 'mode' => $modes ? reset($modes) : '', + 'details' => [ + 'value' => '', + 'format' => 'plain_text', + ], 'payment_method_types' => [], ]; } @@ -238,6 +249,14 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#required' => TRUE, '#access' => !empty($modes), ]; + $form['details'] = [ + '#type' => 'text_format', + '#title' => $this->t('Additional details'), + '#description' => $this->t('Additional details about payment to be shown to the customer on checkout.'), + '#default_value' => $this->configuration['details']['value'], + '#format' => $this->configuration['details']['format'], + '#rows' => 2, + ]; if (count($payment_method_types) > 1) { $form['payment_method_types'] = [ '#type' => 'checkboxes', @@ -271,6 +290,7 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s $values['payment_method_types'] = array_filter($values['payment_method_types']); $this->configuration['mode'] = $values['mode']; + $this->configuration['details'] = $values['details']; $this->configuration['payment_method_types'] = array_keys($values['payment_method_types']); } } diff --git a/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayInterface.php b/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayInterface.php index d8087d0428..19aee2a9c4 100644 --- a/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayInterface.php +++ b/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayInterface.php @@ -51,6 +51,14 @@ public function getMode(); */ public function getSupportedModes(); + /** + * Gets the details of the payment gateway or NULL if not defined.. + * + * @return array|null + * The details definition or NULL. + */ + public function getDetails(); + /** * Gets the payment type used by the payment gateway. * diff --git a/modules/payment/tests/src/Functional/PaymentGatewayTest.php b/modules/payment/tests/src/Functional/PaymentGatewayTest.php index a7303918af..ce7f38711c 100644 --- a/modules/payment/tests/src/Functional/PaymentGatewayTest.php +++ b/modules/payment/tests/src/Functional/PaymentGatewayTest.php @@ -36,11 +36,16 @@ public function testPaymentGatewayCreation() { $this->getSession()->getPage()->clickLink('Add payment gateway'); $this->assertSession()->addressEquals('admin/commerce/config/payment-gateways/add'); + $details = [ + 'value' => 'Test details', + 'format' => 'plain_text', + ]; $values = [ 'label' => 'Example', 'plugin' => 'example_offsite_redirect', 'configuration[redirect_method]' => 'post', 'configuration[mode]' => 'test', + 'configuration[details][value]' => $details['value'], 'status' => '1', // Setting the 'id' can fail if focus switches to another field. // This is a bug in the machine name JS that can be reproduced manually. @@ -58,6 +63,7 @@ public function testPaymentGatewayCreation() { $this->assertEquals(TRUE, $payment_gateway->status()); $payment_gateway_plugin = $payment_gateway->getPlugin(); $this->assertEquals('test', $payment_gateway_plugin->getMode()); + $this->assertEquals($details, $payment_gateway_plugin->getDetails()); $configuration = $payment_gateway_plugin->getConfiguration(); $this->assertEquals('post', $configuration['redirect_method']); } @@ -75,9 +81,14 @@ public function testPaymentGatewayEditing() { $payment_gateway = $this->createEntity('commerce_payment_gateway', $values); $this->drupalGet('admin/commerce/config/payment-gateways/manage/' . $payment_gateway->id()); + $details = [ + 'value' => 'Test details', + 'format' => 'plain_text', + ]; $values += [ 'configuration[redirect_method]' => 'get', 'configuration[mode]' => 'live', + 'configuration[details][value]' => $details['value'], ]; $this->submitForm($values, 'Save'); @@ -89,6 +100,7 @@ public function testPaymentGatewayEditing() { $this->assertEquals(TRUE, $payment_gateway->status()); $payment_gateway_plugin = $payment_gateway->getPlugin(); $this->assertEquals('live', $payment_gateway_plugin->getMode()); + $this->assertEquals($details, $payment_gateway_plugin->getDetails()); $configuration = $payment_gateway_plugin->getConfiguration(); $this->assertEquals('get', $configuration['redirect_method']); } From 86a5b9c232b5a6b714b30a53261d34d4f5c5e8bf Mon Sep 17 00:00:00 2001 From: tavi toporjinschi Date: Mon, 10 Apr 2017 06:39:27 +0000 Subject: [PATCH 2/3] Cleanup - travis error. --- .../Plugin/Commerce/PaymentGateway/PaymentGatewayInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayInterface.php b/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayInterface.php index fdc19c8246..40763ba5ed 100644 --- a/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayInterface.php +++ b/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayInterface.php @@ -59,7 +59,7 @@ public function getSupportedModes(); */ public function getDetails(); - /* + /** * Gets the JS library ID. * * This is usually an external library defined in the module's From a54b1362a620f6b5968fc3af72f37d241c3bb06d Mon Sep 17 00:00:00 2001 From: tavi toporjinschi Date: Thu, 3 Aug 2017 10:30:02 +0000 Subject: [PATCH 3/3] Fix travis error - remove extra empty line. --- .../src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php b/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php index 48d672037e..39e43706b6 100644 --- a/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php +++ b/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php @@ -307,7 +307,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta ]; } - if (count($payment_method_types) > 1) { $form['payment_method_types'] = [ '#type' => 'checkboxes',