5

I have a plugin that i want to intercept aroundSavePaymentInformationAndPlaceOrder, but it is giving me an error. Here is how it is set up:

Vendor/Module/etc/di.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
          <arguments>
              <argument name="columns">
                  <item name="veratad_action" xsi:type="string">sales_order.veratad_action</item>
              </argument>
          </arguments>
      </virtualType>
      <type name="Magento\Checkout\Model\GuestPaymentInformationManagement">
              <plugin name="veratad_ageverification_before_order" type="Veratad\AgeVerification\Plugin\Ageverification" sortOrder="10" disabled="false"/>
        </type>
    </config>

Vendor/Module/Plugin/Ageverification.php

    namespace Veratad\AgeVerification\Plugin;

    use Magento\Checkout\Model\PaymentInformationManagement as CheckoutPaymentInformationManagement;

    class Ageverification
    {

      public function aroundSavePaymentInformationAndPlaceOrder(CheckoutGuestPaymentInformationManagement $subject){

        return ($subject);

   }

}

I am getting the following error:

    PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Veratad\AgeVerification\Plugin\Ageverification::aroundSavePaymentInformationAndPlaceOrder() must be an instance of Veratad\AgeVerification\Plugin\CheckoutGuestPaymentInformationManagement, instance of Magento\Checkout\Model\GuestPaymentInformationManagement\Interceptor given, called in /home/z3hjb5ti7gx1/public_html/magento/vendor/magento/framework/Interception/Interceptor.php on line 135 and defined in /home/z3hjb5ti7gx1/public_html/magento/app/code/Veratad/AgeVerification/Plugin/Ageverification.php:10
    Stack trace:
    #0 /home/z3hjb5ti7gx1/public_html/magento/vendor/magento/framework/Interception/Interceptor.php(135): Veratad\AgeVerification\Plugin\Ageverification->aroundSavePaymentInformationAndPlaceOrder(Object(Magento\Checkout\Model\GuestPaymentInformationManagement\Interceptor), Object(Closure), 'f2c487019bcfd1b...', 'tcanfarotta2@v....', Object(Magento\Quote\Model\Quote\Payment), Object(Magento\Quote\Model\Quote\Address))
    #1 /home/z3hjb5ti7gx1/public_html/magento/ve in /home/z3hjb5ti7gx1/public_html/magento/app/code/Veratad/AgeVerification/Plugin/Ageverification.php on line 10

Can someone help me understand what I am doing wrong?

New Plugin Code:

      namespace Veratad\AgeVerification\Plugin;

      use Magento\Framework\Exception\CouldNotSaveException;

      class Ageverification {

          public function aroundSavePaymentInformationAndPlaceOrder(
          \Magento\Checkout\Model\PaymentInformationManagement $subject,
          callable $proceed,
          \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
          \Magento\Quote\Api\Data\AddressInterface $billingAddress = null) {
              $action = "FAIL";
              if($action == "FAIL"){
                      throw new CouldNotSaveException(__('You have not been age verified.'));
                      return false;
              }else{
                  $proceed($paymentMethod,$billingAddress);
          }
      }
    }
0

1 Answer 1

1

You plugin class is wrong, The aroundSavePaymentInformationAndPlaceOrder() should have 4 parameters.

  1. $subject which object of Magento\Checkout\Model\GuestPaymentInformationManagement.
  2. second callback $proceed.
  3. 3rd parameter is $paymentMethod which instance of \Magento\Quote\Api\Data\PaymentInterface.

    4 .4th parameter is $billingAddress which instance of Magento\Quote\Api\Data\AddressInterface .

Please check how to use around plugin https://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html

<?php
namespace Veratad\AgeVerification\Plugin;

use Magento\Checkout\Model\PaymentInformationManagement as CheckoutPaymentInformationManagement;

class Ageverification {

    public function aroundSavePaymentInformationAndPlaceOrder(
    CheckoutGuestPaymentInformationManagement $subject, callable $proceed,
    \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
    \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
    ) {

        $proceed($paymentMethod,$billingAddress);
    }

}
7
  • How can i stop this method from proceeding?
    – Thomas C
    Commented Sep 19, 2018 at 14:56
  • Sorry donot understand . you want to stop the execution of rest of codes of this method?
    – Amit Bera
    Commented Sep 19, 2018 at 14:58
  • Yes, conditionally. I have to check a third party API and depending on response I want the payment and place order to save or i want to stop it from saving.
    – Thomas C
    Commented Sep 19, 2018 at 14:59
  • then remove $proceed($paymentMethod,$billingAddress); from around method and return what you want to return and also throw an excpetion from this around that will show at checkout page
    – Amit Bera
    Commented Sep 19, 2018 at 14:59
  • I am getting a new error:
    – Thomas C
    Commented Sep 19, 2018 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.