Commit 5ff8912d authored by Sascha Grossenbacher's avatar Sascha Grossenbacher Committed by Ursin Cola
Browse files

Issue #3252965 by Berdir: Allow to control if tokens are always, optionally or never requested

parent 3a0d60f8
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -11,3 +11,9 @@ commerce_payment.commerce_payment_gateway.plugin.wallee_redirect_checkout:
    secret:
      type: string
      label: 'API Secret'
    payment_configuration:
      type: string
      label: 'Enabled payment method'
    payment_reusable:
      type: string
      label: 'Reusable payment method'
+3 −4
Original line number Diff line number Diff line
@@ -40,14 +40,14 @@ class PaymentInformation extends BasePaymentInformation {
      $payment_gateway = \Drupal::entityTypeManager()->getStorage('commerce_payment_gateway')->load($payment_method);
      if ($payment_gateway instanceof \Drupal\commerce_payment\Entity\PaymentGateway) {
        $plugin_configuration = $payment_gateway->getPluginConfiguration();
        if (isset($plugin_configuration['payment_reusable']) AND $plugin_configuration['payment_reusable']) {
        if ($plugin_configuration['payment_reusable'] == 'ask') {
          // Check if user has set a value on last checkout.
          $tempstore = \Drupal::service('tempstore.private');
          $store = $tempstore->get('commerce_wallee');

          $pane_form['save_payment'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Save payment information for resuable'),
            '#title' => $this->t('Save payment information'),
            '#description' => $this->t('Only for Visa, Master and Paypal'),
            '#default_value' => $store->get('save_payment', FALSE),
          ];
@@ -73,7 +73,6 @@ class PaymentInformation extends BasePaymentInformation {
      $tempstore = \Drupal::service('tempstore.private');
      $store = $tempstore->get('commerce_wallee');
      $store->set('save_payment', $save_payment);
      $store->set('uid', \Drupal::currentUser()->id());
    }
  }
}
+22 −4
Original line number Diff line number Diff line
@@ -123,6 +123,19 @@ class RedirectCheckout extends OffsitePaymentGatewayBase implements RedirectChec
    );
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'space_id' => '',
      'user_id' => '',
      'secret' => '',
      'payment_configuration' => 'all',
      'payment_reusable' => 'ask',
    ];
  }

  /**
   * {@inheritdoc}
   */
@@ -186,10 +199,15 @@ class RedirectCheckout extends OffsitePaymentGatewayBase implements RedirectChec

      // Allow user to save token.
      $form['payment_reusable'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Allow user to use this for reusable'),
        '#description' => $this->t('Wallee allow right now only Visa, Master and Paypal'),
        '#type' => 'select',
        '#title' => $this->t('Request token and create reusable payment method'),
        '#description' => $this->t('Allows to reuse the payment information for additional payments. Wallee allows right now only Visa, Master and Paypal.'),
        '#default_value' => $this->configuration['payment_reusable'],
        '#options' => [
          'always' => $this->t('Always, payment methods are created for logged in users only'),
          'ask' => $this->t('Ask the user'),
          'never' => $this->t('Never'),
        ]
      ];

      // Webhook configuration.
+27 −13
Original line number Diff line number Diff line
@@ -164,19 +164,11 @@ class PaymentSdk implements PaymentSdkInterface {
      $transactionPayload->setAllowedPaymentMethodConfigurations($pluginConfiguration['payment_configuration']);
    }

    // Get user settings.
    if (\Drupal::currentUser()->id() != 0) {
      // Get user temp store.
      $tempstore = \Drupal::service('tempstore.private');
      $store = $tempstore->get('commerce_wallee');
      $save_payment = $store->get('save_payment', 0);
      $uid = $store->get('uid', 0);
      if ($save_payment == TRUE) {
    if ($this->shouldRequestToken($pluginConfiguration)) {
      // Force token generation on wallee.
      $transactionPayload->setTokenizationMode(\Wallee\Sdk\Model\TokenizationMode::FORCE_CREATION_WITH_ONE_CLICK_PAYMENT);
      $transactionPayload->setCustomerEmailAddress($transactionPayload->getBillingAddress()->getEmailAddress());
        $transactionPayload->setCustomerId($uid);
      }
      $transactionPayload->setCustomerId(\Drupal::currentUser()->id());
    }

    // Allow alter transaction over hook.
@@ -694,4 +686,26 @@ class PaymentSdk implements PaymentSdkInterface {

    return $card_type;
  }

  /**
   * Returns whether a token should be rquested.
   *
   * @param array $plugin_configuration
   *   The payment gateway plugin configuration.
   *
   * @return bool
   *   TRUE if a token should be requested.
   */
  protected function shouldRequestToken(array $plugin_configuration) {
    if ($plugin_configuration['payment_reusable'] == 'always') {
      return TRUE;
    }

    if ($plugin_configuration['payment_reusable'] == 'ask' && \Drupal::currentUser()->id() != 0) {
      // Get user temp store.
      $tempstore = \Drupal::service('tempstore.private');
      $store = $tempstore->get('commerce_wallee');
      return $store->get('save_payment', FALSE);
    }
  }
}