hook_order, prevent pass thru still submits payment

Posts: 6
Joined: 09/29/2008

As part of a module I've created, I'm restricting the zip code permitted for delivery address to a admin determined list. I implement hook_order to block the order from going thru:

<?php

function mymodule_order($op, &$arg1, $arg2) {
  switch (
$op) {
    case
'submit':
     
$allowed_zips = explode(',',variable_get('mymodule_zip_codes','10001,10002,10003'));
      if (!
in_array($arg1->delivery_postal_code,$allowed_zips)) {
          return array(array(
'pass' => FALSE, 'message' => t('Sorry, but we don\'t deliver to the address you entered. Please !contact for more information.',
            array(
'!contact'=>l('contact us', 'contact')
        ))));
      }
      break;
  }
}
?>

This properly returns me to the checkout screen when submitting the review screen with an invalid address, however the card is still charged! I assume this is because the payment module's hook is still getting called. Is there a built in way to prevent the order from being charged and advanced?

Posts: 5349
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Hmm... yeah, I knew that code would come back to bite me. :-/

I don't know at the moment. The best I can say is do the check in a custom submit handler on the checkout form itself using hook_form_alter() instead of hook_order().

Posts: 6
Joined: 09/29/2008

Oops! form_alter would work tho it's ugly. It seems like it would be simple to implement a "validate" hook that allows modules to cancel an order before it gets to the review state. In fact it would seem quite useful in any number of cases. The checkout process should validate before it goes to review i would think.

Are you committing new features to the 1.5 branch? I'd be glad to submit a patch (since I'd rather not use a hacked core)

Posts: 5349
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

I'm not sure... I kind of don't want to fix what isn't really broken, if you get my drift. In this case, a hook_form_alter() isn't really bad at all and will actually catch the error on the page when they're submitting the order for review. Another thing you could do is create and enable a checkout pane that doesn't display anything but does this extra bit of validation.