esewa-1.1.2/esewa.module

esewa.module
<?php

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\commerce_payment\Entity\PaymentInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * @file
 * Contains esewa.module.
 */

/**
 * Implements hook_help().
 */
function esewa_help($route_name, RouteMatchInterface $arg) {

  switch ($route_name) {
    case 'help.page.esewa':
    $output = '';
    $output .= '<h3>' . t('About') . '</h3>';
    $output .= '<p>' . t('A custom module for Drupal 10 eSewa payment that extends the functionality of Drupal Commerce and integrates it with the eSewa Payment gateway. It allows you to handle transactions securely using the eSewa payment platform which will redirect users to a pre-built eSewa form.') . '</p>';

    return $output;
  }
}

/**
 * Custom function to change the payment status to "completed".
 *
 * @param \Drupal\commerce_payment\Entity\PaymentInterface $payment
 *   The payment entity to update.
 */
function esewa_change_payment_status_completed(PaymentInterface $payment, OrderInterface $order) {
 $payment->setState('completed');

 $payment->save();

  // Set the order status to "completed".
 $order->set('state', 'completed');

  // Save the order entity.
 $order->save();
}

/**
 * Custom function to change the payment status to "completed".
 *
 * @param \Drupal\commerce_payment\Entity\PaymentInterface $payment
 *   The payment entity to update.
 */
function esewa_change_payment_status_cancelled(OrderInterface $order) {
  // Set the order status to "cancelled".
 $order->set('state', 'cancelled');

  // Save the order entity.
 $order->save();
}


 /**
  * Log messages on the system
  * @param  $log_type type of the error message
  * @param  $message  message body
  * @return
  */
 function log_esewa_messages($log_type, $message)
 {
  switch ($log_type) {
    case 'alert':
    \Drupal::logger('esewa')->alert($message);
    break;

    case 'warning':
    \Drupal::logger('esewa')->warning($message);
    break;

    case 'debug':
    \Drupal::logger('esewa')->debug($message);
    break;

    default:
    \Drupal::logger('esewa')->info($message);
    break;
  }
}

/**
 * Implements hook_form_alter().
 */
function esewa_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'commerce_payment_gateway_edit_form' || $form_id == 'commerce_payment_gateway_add_form') {
    $form['configuration']['form']['merchant_code']['#states'] = [
      'visible' => [
        'input[name="configuration[esewa][mode]"]' => ['value' => 'live']
      ]
    ];
    $form['#validate'][] = 'esewa_form_validation';
  }
}

/**
 * Validate esewa form
 */
function esewa_form_validation(array &$form, FormStateInterface $form_state) {
  $configuration = $form_state->getValue('configuration');
  if(isset($configuration['esewa'])) {
    $esewa = $configuration['esewa'];
    $mode = $esewa['mode'];
    $merchant_code = $esewa['merchant_code'];

    // Check if live mode and the merchant code is empty.
    if ($mode == 'live' && $merchant_code == '') {
      $form_state->setErrorByName('merchant_code', t('The Merchant Code is required when gateway is in live mode.'));
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc