/** * {@inheritdoc} */ public function view(OrderInterface $order, array $form, FormStateInterface $form_state) { $contents['#attached']['library'][] = 'uc_payment/uc_payment.styles'; if ($this->configuration['show_preview']) { $contents['line_items'] = array('#theme' => 'uc_payment_totals', '#order' => $order, '#weight' => -20); } // Ensure that the form builder uses #default_value to determine which // button should be selected after an ajax submission. This is // necessary because the previously selected value may have become // unavailable, which would result in an invalid selection. $input = $form_state->getUserInput(); unset($input['panes']['payment']['payment_method']); $form_state->setUserInput($input); $options = array(); $methods = PaymentMethod::loadMultiple(); uasort($methods, 'Drupal\\uc_payment\\Entity\\PaymentMethod::sort'); foreach ($methods as $method) { // $set = rules_config_load('uc_payment_method_' . $method['id']); // if ($set && !$set->execute($order)) { // continue; // } if ($method->status()) { $options[$method->id()] = $method->getDisplayLabel(); } } \Drupal::moduleHandler()->alter('uc_payment_method_checkout', $options, $order); if (!$options) { $contents['#description'] = $this->t('Checkout cannot be completed without any payment methods enabled. Please contact an administrator to resolve the issue.'); $options[''] = $this->t('No payment methods available'); } elseif (count($options) > 1) { $contents['#description'] = $this->t('Select a payment method from the following options.'); } if (!$order->getPaymentMethodId() || !isset($options[$order->getPaymentMethodId()])) { $order->setPaymentMethodId(key($options)); } $contents['payment_method'] = array('#type' => 'radios', '#title' => $this->t('Payment method'), '#title_display' => 'invisible', '#options' => $options, '#default_value' => $order->getPaymentMethodId(), '#disabled' => count($options) == 1, '#required' => TRUE, '#ajax' => array('callback' => array($this, 'ajaxRender'), 'wrapper' => 'payment-details', 'progress' => array('type' => 'throbber'))); // If there are no payment methods available, this will be ''. if ($order->getPaymentMethodId()) { $plugin = $this->paymentMethodManager->createFromOrder($order); $definition = $plugin->getPluginDefinition(); $contents['details'] = array('#prefix' => '<div id="payment-details" class="clearfix ' . Html::cleanCssIdentifier('payment-details-' . $definition['id']) . '">', '#markup' => $this->t('Continue with checkout to complete payment.'), '#suffix' => '</div>'); try { $details = $plugin->cartDetails($order, $form, $form_state); if ($details) { unset($contents['details']['#markup']); $contents['details'] += $details; } } catch (PluginException $e) { } } return $contents; }
/** * {@inheritdoc} */ public function buildForm(OrderInterface $order, array $form, FormStateInterface $form_state) { $options = array(); $methods = PaymentMethod::loadMultiple(); uasort($methods, 'Drupal\\uc_payment\\Entity\\PaymentMethod::sort'); foreach ($methods as $method) { $options[$method->id()] = $method->label(); } $form['payment_method'] = array('#type' => 'select', '#title' => $this->t('Payment method'), '#default_value' => $order->getPaymentMethodId(), '#options' => $options, '#ajax' => array('callback' => array($this, 'ajaxCallback'), 'progress' => array('type' => 'throbber'), 'wrapper' => 'payment-details')); $form['payment_details'] = array('#tree' => TRUE, '#prefix' => '<div id="payment-details">', '#suffix' => '</div>'); $method = $form_state->getValue('payment_method') ?: $order->getPaymentMethodId(); if ($method && ($details = PaymentMethod::load($method)->getPlugin()->orderEditDetails($order))) { if (is_array($details)) { $form['payment_details'] += $details; } else { $form['payment_details']['#markup'] = $details; } } return $form; }
/** * Returns an instance of the payment method plugin for a specific order. * * @param \Drupal\uc_order\OrderInterface $order * The order from which the plugin should be instantiated. * * @return \Drupal\uc_payment\PaymentMethodPluginInterface * A fully configured plugin instance. */ public function createFromOrder(OrderInterface $order) { return PaymentMethod::load($order->getPaymentMethodId())->getPlugin(); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $uc_order = NULL) { $this->order = $uc_order; $form['#attached']['library'][] = 'uc_payment/uc_payment.styles'; $total = $this->order->getTotal(); $payments = uc_payment_load_payments($this->order->id()); $form['order_total'] = array('#type' => 'item', '#title' => $this->t('Order total'), '#theme' => 'uc_price', '#price' => $total); $form['payments'] = array('#type' => 'table', '#tree' => TRUE, '#header' => array($this->t('Received'), $this->t('User'), $this->t('Method'), $this->t('Amount'), $this->t('Balance'), $this->t('Comment'), $this->t('Action')), '#weight' => 10); $account = $this->currentUser(); if ($payments !== FALSE) { foreach ($payments as $payment) { $form['payments'][$payment->receipt_id]['received'] = array('#markup' => \Drupal::service('date.formatter')->format($payment->received, 'short')); $form['payments'][$payment->receipt_id]['user'] = array('#theme' => 'uc_uid', '#uid' => $payment->uid); $form['payments'][$payment->receipt_id]['method'] = array('#markup' => $payment->method == '' ? $this->t('Unknown') : $payment->method); $form['payments'][$payment->receipt_id]['amount'] = array('#theme' => 'uc_price', '#price' => $payment->amount); $total -= $payment->amount; $form['payments'][$payment->receipt_id]['balance'] = array('#theme' => 'uc_price', '#price' => $total); $form['payments'][$payment->receipt_id]['comment'] = array('#markup' => $payment->comment == '' ? '-' : $payment->comment); if ($account->hasPermission('delete payments')) { $form['payments'][$payment->receipt_id]['action'] = array('#type' => 'operations', '#links' => array('delete' => array('title' => $this->t('Delete'), 'url' => Url::fromRoute('uc_payments.delete', ['uc_order' => $this->order->id(), 'payment' => $payment->receipt_id])))); } else { $form['payments'][$payment->receipt_id]['action'] = array('#markup' => ''); } } } $form['balance'] = array('#type' => 'item', '#title' => $this->t('Current balance'), '#theme' => 'uc_price', '#price' => $total); if ($account->hasPermission('manual payments')) { $form['payments']['new']['received'] = array('#type' => 'date', '#default_value' => array('month' => \Drupal::service('date.formatter')->format(REQUEST_TIME, 'custom', 'n'), 'day' => \Drupal::service('date.formatter')->format(REQUEST_TIME, 'custom', 'j'), 'year' => \Drupal::service('date.formatter')->format(REQUEST_TIME, 'custom', 'Y'))); $form['payments']['new']['user'] = array('#markup' => '-'); $options = array(); foreach (PaymentMethod::loadMultiple() as $method) { $options[$method->id()] = $method->label(); } $form['payments']['new']['method'] = array('#type' => 'select', '#title' => $this->t('Method'), '#title_display' => 'invisible', '#options' => $options, '#default_value' => $this->order->getPaymentMethodId()); $form['payments']['new']['amount'] = array('#type' => 'textfield', '#title' => $this->t('Amount'), '#title_display' => 'invisible', '#size' => 6); $form['payments']['new']['balance'] = array('#markup' => '-'); $form['payments']['new']['comment'] = array('#type' => 'textfield', '#title' => $this->t('Comment'), '#title_display' => 'invisible', '#size' => 32, '#maxlength' => 256); $form['payments']['new']['action'] = array('#type' => 'actions'); $form['payments']['new']['action']['action'] = array('#type' => 'submit', '#value' => $this->t('Enter')); } return $form; }
public function getPaymentPluginId($uc_payment_methodId) { return PaymentMethod::load($uc_payment_methodId)->getPlugin()->getPluginId(); }