View source
<?php
namespace Drupal\uc_payment_pack\Plugin\Ubercart\PaymentMethod;
use Drupal\Component\Utility\Html;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormStateInterface;
use Drupal\uc_order\OrderInterface;
use Drupal\uc_payment\PaymentMethodPluginBase;
class CashOnDelivery extends PaymentMethodPluginBase {
public function defaultConfiguration() {
return [
'policy' => 'Full payment is expected upon delivery or prior to pick-up.',
'max_order' => 0,
'delivery_date' => FALSE,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['policy'] = [
'#type' => 'textarea',
'#title' => $this
->t('Policy message'),
'#default_value' => $this->configuration['policy'],
'#description' => $this
->t('Help message shown at checkout.'),
];
$form['max_order'] = [
'#type' => 'uc_price',
'#title' => $this
->t('Maximum order total eligible for COD'),
'#default_value' => $this->configuration['max_order'],
'#description' => $this
->t('Set to 0 for no maximum order limit.'),
];
$form['delivery_date'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Let customers enter a desired delivery date.'),
'#default_value' => $this->configuration['delivery_date'],
];
return $form;
}
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');
}
public function cartDetails(OrderInterface $order, array $form, FormStateInterface $form_state) {
$build['#attached']['library'][] = 'uc_payment_pack/cod.styles';
$build['policy'] = [
'#prefix' => '<p>',
'#markup' => Html::escape($this->configuration['policy']),
'#suffix' => '</p>',
];
if (($max = $this->configuration['max_order']) > 0 && is_numeric($max)) {
$build['eligibility'] = [
'#prefix' => '<p>',
'#markup' => $this
->t('Orders totalling more than @amount are <b>not eligible</b> for COD.', [
'@amount' => uc_currency_format($max),
]),
'#suffix' => '</p>',
];
}
if ($this->configuration['delivery_date']) {
$build += $this
->deliveryDateForm($order);
}
return $build;
}
public function cartProcess(OrderInterface $order, array $form, FormStateInterface $form_state) {
if ($this->configuration['delivery_date']) {
$order->payment_details = $form_state
->getValue([
'panes',
'payment',
'details',
]);
if (isset($order->payment_details['delivery_date'])) {
$order->payment_details['delivery_date'] = $order->payment_details['delivery_date']
->getTimestamp();
}
}
return TRUE;
}
public function cartReview(OrderInterface $order) {
$review = [];
if ($this->configuration['delivery_date'] && isset($order->payment_details['delivery_date'])) {
$date = \Drupal::service('date.formatter')
->format($order->payment_details['delivery_date'], 'uc_store');
$review[] = [
'title' => $this
->t('Delivery date'),
'data' => $date,
];
}
return $review;
}
public function orderView(OrderInterface $order) {
$build = [];
if ($this->configuration['delivery_date'] && isset($order->payment_details['delivery_date'])) {
$build['#markup'] = $this
->t('Desired delivery date:') . '<br />' . \Drupal::service('date.formatter')
->format($order->payment_details['delivery_date'], 'uc_store');
}
return $build;
}
public function orderEditDetails(OrderInterface $order) {
$build = [];
if ($this->configuration['delivery_date']) {
$build = $this
->deliveryDateForm($order);
}
return $build;
}
public function orderEditProcess(OrderInterface $order, array $form, FormStateInterface $form_state) {
if ($this->configuration['delivery_date']) {
$payment_details = $form_state
->getValue('payment_details');
if (isset($payment_details)) {
$payment_details['delivery_date'] = $payment_details['delivery_date']
->getTimestamp();
return $payment_details;
}
}
return FALSE;
}
public function orderLoad(OrderInterface $order) {
$result = $this->database
->query('SELECT * FROM {uc_payment_cod} WHERE order_id = :id', [
':id' => $order
->id(),
]);
if ($row = $result
->fetchObject()) {
$order->payment_details['delivery_date'] = $row->delivery_date;
}
}
public function orderSave(OrderInterface $order) {
if (isset($order->payment_details['delivery_date'])) {
$this->database
->merge('uc_payment_cod')
->key([
'order_id' => $order
->id(),
])
->fields([
'delivery_date' => $order->payment_details['delivery_date'],
])
->execute();
}
}
public function orderSubmit(OrderInterface $order) {
$max = $this->configuration['max_order'];
if ($max > 0 && $order
->getTotal() > $max) {
return $this
->t('Your final order total exceeds the maximum for COD payment. Please go back and select a different method of payment.');
}
}
public function orderDelete(OrderInterface $order) {
$this->database
->delete('uc_payment_cod')
->condition('order_id', $order
->id())
->execute();
}
protected function deliveryDateForm(OrderInterface $order) {
$delivery_date = empty($order->payment_details['delivery_date']) ? DrupalDateTime::createFromTimestamp(\Drupal::time()
->getRequestTime()) : DrupalDateTime::createFromTimestamp($order->payment_details['delivery_date']);
$form['delivery_date'] = [
'#type' => 'datetime',
'#title' => $this
->t('Enter a desired delivery date:'),
'#date_date_element' => 'date',
'#date_time_element' => 'none',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#default_value' => $delivery_date,
];
return $form;
}
}