VirtueMart Forum

VirtueMart 2 + 3 + 4 => Plugins: Payment, Shipment and others => Topic started by: ndaumalle on July 28, 2016, 00:54:27 AM

Title: VMPayment Plugin: How to validate input data from user?
Post by: ndaumalle on July 28, 2016, 00:54:27 AM
Hello,

We are actually updating our payment plugins for Virtuemart. But we need help.
For some of our payment methods, it is required that the user write a bank code (bic) from a text field in the checkout page.
We would like to validate this bank code, verifying that it is not empty and valid.

We are using this function: plgVmOnSelectCheckPayment

The problem is that even if we return false in this function, the next function plgVmConfirmedOrder is called anyway, instead of showing the checkout page with an error message.

For example:
public function plgVmOnSelectCheckPayment(VirtueMartCart $cart, &$msg) {
       
        if (!$this->selectedThisByMethodId($cart->virtuemart_paymentmethod_id)) {
            return NULL; // Another method was selected, do nothing
        }

        if (!($this->_currentMethod = $this->getVmPluginMethod($cart->virtuemart_paymentmethod_id))) {
            return FALSE;
        }
       
        // load payment method
        $method = $this->getVmPluginMethod($cart->virtuemart_paymentmethod_id);

        $this->_gc_giropay_bic = vRequest::getVar('gc_giropay_bic_' . $cart->virtuemart_paymentmethod_id, '');

        $this->_gc_giropay_bic = isset($this->_gc_giropay_bic) ? (is_string($this->_gc_giropay_bic) ? trim($this->_gc_giropay_bic) : '') : '';

        if ($this->_gc_giropay_bic) {
           //More code to validate the bank code..
        } else {
                $msg = "Bank code empty.";
              return false;

        }
    }

Any help would be very apreciated.
Thanks.
Title: Re: VMPayment Plugin: How to validate input data from user?
Post by: GJC Web Design on July 28, 2016, 09:53:05 AM
study the function in components\com_virtuemart\helpers\cart.php ~ line  860

the return is an array.. is something else returning true before yours?

//Add a hook here for other payment methods, checking the data of the choosed plugin
$msg = '';
$_dispatcher = JDispatcher::getInstance();
$_retValues = $_dispatcher->trigger('plgVmOnSelectCheckPayment', array( $this, &$msg));
$dataValid = true;
foreach ($_retValues as $_retVal) {
if ($_retVal === true ) {
$this->setCartIntoSession();
// Plugin completed succesfull; nothing else to do
break;
} else if ($_retVal === false ) {
if ($redirect) {
$app = JFactory::getApplication();
$app->redirect(JRoute::_('index.php?option=com_virtuemart&view=cart&task=editpayment',$this->useXHTML,$this->useSSL), $msg);
break;
} else {
return;
}
}
}
Title: Re: VMPayment Plugin: How to validate input data from user?
Post by: ndaumalle on July 28, 2016, 17:47:52 PM
Thanks for the reply. We were observing this function and we can see that the value of $_retValues is  Array(