Commit 3d883b71 authored by jyoti's avatar jyoti
Browse files

Issue #2959041 by lucassc, chakkche, arunkumark, mrinalini9, Deepthi kumari,...

Issue #2959041 by lucassc, chakkche, arunkumark, mrinalini9, Deepthi kumari, Libbna, ankitjain28may, krina.addweb, i-trokhanenko, vitorbs, nehajyoti, saphemmy, lucasbaralm: Drupal coding standard issue
parent c59d50e5
Loading
Loading
Loading
Loading
+1 −1
Changes for commerce_razorpay.routing.yml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -3,6 +3,6 @@ commerce_razorpay.capture_payment:
  defaults:
    _controller: '\Drupal\commerce_razorpay\Controller\Controller::capturePayment'
  requirements:
    # Needs to be accessed by an anonymous user.
    _access: 'TRUE'
      #@TODO to be changed later.
      # Needs to be accessed by an anonymous user.
+63 −25
Changes for src/Controller/Controller.php: 63 added lines, 25 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_razorpay\Controller;
use Drupal\commerce_order\Entity\Order;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Razorpay\Api\Api;
use Razorpay\Api\Errors\SignatureVerificationError;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Class Controller
 * Class Controller.
 *
 * Razorpay controller for payment actions.
 *
 * @package Drupal\commerce_razorpay\Controller
 */
class Controller extends ControllerBase {

  use StringTranslationTrait;

  /**
   * The Request Stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Constructing variables for payment.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The Request stack.
   */
  public function __construct(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
  }

  /**
   * Create Method to get services.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   Container variable to get services.
   *
   * @return static
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('request_stack')
    );
  }

  /**
   * Capture Payment function for payments.
   */
  public function capturePayment() {
    $amount = $_GET['amount'];
    $commerce_order_id = $_GET['order_id'];
    $payment_settings = json_decode($_GET['payment_settings']);
    $response = json_decode($_GET['response']);
    $amount = $this->requestStack->getCurrentRequest()->query->get('amount');
    $commerce_order_id = $this->requestStack->getCurrentRequest()->query->get('order_id');
    $payment_settings = json_decode($this->requestStack->getCurrentRequest()->query->get('payment_settings'));
    $response = json_decode($this->requestStack->getCurrentRequest()->query->get('response'));
    $razorpay_signature = $response->razorpay_signature;
    $razorpay_payment_id = $response->razorpay_payment_id;
    $razorpay_order_id = $response->razorpay_order_id;
@@ -28,48 +70,44 @@ class Controller extends ControllerBase{
    $api = new Api($key_id, $key_secret);
    $payment = $api->payment->fetch($razorpay_payment_id);
    if ($payment->status == 'authorized') {
      $payment->capture(array('amount' => $amount));
      $payment->capture(['amount' => $amount]);
    }
    // @TODO Save payment method details in order object.

    // @todo Save payment method details in order object.
    // Validating  Signature.
    $success = true;
    $success = TRUE;
    $error = "Payment Failed";

    if (empty($razorpay_payment_id) === false)
    {
    if (empty($razorpay_payment_id) === FALSE) {
      $api = new Api($key_id, $key_secret);
      try
      {
        $attributes = array(
      try {
        $attributes = [
          'razorpay_order_id' => $razorpay_order_id,
          'razorpay_payment_id' => $razorpay_payment_id,
          'razorpay_signature' => $razorpay_signature
        );
          'razorpay_signature' => $razorpay_signature,
        ];
        $api->utility->verifyPaymentSignature($attributes);
      }
      catch(SignatureVerificationError $e)
      {
        $success = false;
      catch (SignatureVerificationError $e) {
        $success = FALSE;
        $error = 'Razorpay Error : ' . $e->getMessage();

      }
    }

    // If Payment is successfully captured at razorpay end.
    if ($success === true) {
    if ($success === TRUE) {
      $message = "Payment ID: {$razorpay_payment_id}";
      $this->messenger()->addStatus(t($message));
      $this->messenger()->addStatus($this->t(':message', [':message' => $message]));
    }
    else {
      $message = "Your payment failed " . $error;
      $this->messenger()->addError(t($message));
      $this->messenger()->addError($this->t(':message', [':message' => $message]));
    }
    $url = Url::fromRoute('commerce_payment.checkout.return', [
      'commerce_order' => $commerce_order_id,
      'step' => 'payment',
    ], ['absolute' => TRUE])->toString();
    return new RedirectResponse($url);

    return new RedirectResponse($url);
  }

}
+31 −16
Changes for src/Plugin/Commerce/PaymentGateway/RazorpayRedirect.php: 31 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -123,32 +123,46 @@ class RazorpayRedirect extends OffsitePaymentGatewayBase {
    $api = new Api($key_id, $key_secret);
    $payment = $api->order->fetch($order->getData('merchant_order_id'));
    $payment_object = $payment->payments();
    $status = $payment_object['items'][0]->status; // eg : refunded, captured, authorized, failed.
    $refund_status = $payment_object['items'][0]->refund_status; // eg : full, partial
    $amount_refunded = ($payment_object['items'][0]->amount_refunded)/100;
    $service_tax = $payment_object['items'][0]->service_tax;
    $amount = $payment_object['items'][0]->amount;

    // Eg : refunded, captured, authorized, failed.
    $status = $payment_object['items'][0]->status;
    // Eg : full, partial.
    // Commenting unused variables.
    // $refund_status = $payment_object['items'][0]->refund_status;
    // $amount_refunded = ($payment_object['items'][0]->amount_refunded) / 100;
    // $service_tax = $payment_object['items'][0]->service_tax;
    // $amount = $payment_object['items'][0]->amount;
    // Succeessful.
    $message = '';
    $remote_status = '';
    $request_time = $this->time->getRequestTime();
    if ($status == "captured") {
      // Status is success.
      $remote_status = t('Success');
      $message = $this->t('Your payment was successful with Order id : @orderid has been received at : @date', ['@orderid' => $order->id(), '@date' => date("d-m-Y H:i:s", $request_time)]);
      $remote_status = $this->t('Success');
      $message = $this->t('Your payment was successful with Order id : @orderid has been received at : @date',
        [
          '@orderid' => $order->id(),
          '@date' => date("d-m-Y H:i:s", $request_time),
        ]);
      $status = COMMERCE_PAYMENT_STATUS_SUCCESS;
    }
    elseif ($status == "authorized") {
      // Batch process - Pending orders.
      $remote_status = t('Pending');
      $message = $this->t('Your payment with Order id : @orderid is pending at : @date', ['@orderid' => $order->id(), '@date' => date("d-m-Y H:i:s", $request_time)]);
      $remote_status = $this->t('Pending');
      $message = $this->t('Your payment with Order id : @orderid is pending at : @date',
      [
        '@orderid' => $order->id(),
        '@date' => date("d-m-Y H:i:s", $request_time),
      ]);
      $status = COMMERCE_PAYMENT_STATUS_PENDING;
    }
    elseif ($status == "failed") {
      // Failed transaction.
      $remote_status = t('Failure');
      $message = $this->t('Your payment with Order id : @orderid failed at : @date', ['@orderid' => $order->id(), '@date' => date("d-m-Y H:i:s", $request_time)]);
      $remote_status = $this->t('Failure');
      $message = $this->t('Your payment with Order id : @orderid failed at : @date',
      [
        '@orderid' => $order->id(),
        '@date' => date("d-m-Y H:i:s", $request_time),
      ]);
      $status = COMMERCE_PAYMENT_STATUS_FAILURE;
    }

@@ -181,7 +195,6 @@ class RazorpayRedirect extends OffsitePaymentGatewayBase {
    ]), 'error');
  }


  /**
   * {@inheritdoc}
   */
@@ -199,7 +212,9 @@ class RazorpayRedirect extends OffsitePaymentGatewayBase {
    $razorpay_payment_id = $payment->getRemoteId();
    $razorpay_payment = $api->payment->fetch($razorpay_payment_id);

    if (!in_array($payment->getState()->value, ['capture_completed', 'capture_partially_refunded'])) {
    if (!in_array($payment->getState()->value,
      ['capture_completed', 'capture_partially_refunded']
    )) {
      throw new \InvalidArgumentException('Only payments in the "capture_completed" and "capture_partially_refunded" states can be refunded.');
    }

@@ -222,7 +237,8 @@ class RazorpayRedirect extends OffsitePaymentGatewayBase {
      }
      else {
        $payment->state = 'capture_refunded';
        $razorpay_payment->refund(array('amount' => $new_refunded_amount* 100)); // for partial refund
        // For partial refund.
        $razorpay_payment->refund(['amount' => $new_refunded_amount * 100]);
      }

      $payment->setRefundedAmount($new_refunded_amount)->save();
@@ -233,5 +249,4 @@ class RazorpayRedirect extends OffsitePaymentGatewayBase {

  }


}
+13 −10
Changes for src/PluginForm/OffsiteRedirect/RazorpayForm.php: 13 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -6,14 +6,12 @@ use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_payment\PluginForm\PaymentOffsiteForm as BasePaymentOffsiteForm;
use Drupal\Core\Form\FormStateInterface;
use Razorpay\Api\Api;
use Symfony\Component\ExpressionLanguage\Tests\Node\Obj;

/**
 * Provides the Off-site payment form.
 */
class RazorpayForm extends BasePaymentOffsiteForm {


  /**
   * {@inheritdoc}
   */
@@ -27,12 +25,14 @@ class RazorpayForm extends BasePaymentOffsiteForm {
    /** @var \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface $payment_gateway_plugin */
    $payment_gateway_plugin = $payment->getPaymentGateway()->getPlugin();

    $owner = \Drupal::routeMatch()->getParameter('commerce_order')->getCustomer();
    // Commenting unused variable.
    // $owner = \Drupal::routeMatch()->getParameter('commerce_order')
    // ->getCustomer();
    $order_id = \Drupal::routeMatch()->getParameter('commerce_order')->id();
    $order = Order::load($order_id);

    $billing_profile = $order->getBillingProfile();

    // Commenting unused variable.
    // $billing_profile = $order->getBillingProfile();
    $address = $order->getBillingProfile()->address->first();

    $amount = ($payment->getAmount()->getNumber()) * 100;
@@ -44,12 +44,12 @@ class RazorpayForm extends BasePaymentOffsiteForm {
    $payment_capture = FALSE;

    $api = new Api($key_id, $key_secret);
    $razorpay_order = $api->order->create(array(
    $razorpay_order = $api->order->create([
      'amount' => $amount,
      "currency" => $currency,
      "receipt" => $receipt,
      'payment_capture' => $payment_capture
    ));
      'payment_capture' => $payment_capture,
    ]);

    $merchant_order_id = $razorpay_order->id;
    $order->setData('merchant_order_id', $merchant_order_id);
@@ -59,7 +59,7 @@ class RazorpayForm extends BasePaymentOffsiteForm {

    // Attach library.
    $form['#attached']['library'][] = 'commerce_razorpay/commerce_razorpay.payment';
    $form['#attached']['drupalSettings']['commerce_razorpay'] = array(
    $form['#attached']['drupalSettings']['commerce_razorpay'] = [
      'amount' => $amount,
      'key' => $key_id,
      'logo' => $base_url . "/" . drupal_get_path('module', 'commerce_razorpay') . '/logo.jpg',
@@ -69,11 +69,14 @@ class RazorpayForm extends BasePaymentOffsiteForm {
      'name' => $address->getGivenName() . " " . $address->getFamilyName(),
      'address' => $address->getAddressLine1() . " " . $address->getLocality() . " " . $address->getAdministrativeArea(),
      'email' => $order->getEmail(),
    );
    ];

    return $this->buildRedirectForm($form, $form_state);
  }

  /**
   * Empty Build of redirect form.
   */
  protected function buildRedirectForm(array $form, FormStateInterface $form_state) {

    return $form;
+4 −4

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.