Friday, February 14, 2014

Cartthrob bug fix

I posted the following at StackExchange:

I have a client who wants customers' credit cards authorized but not charged until they have manually verified that the product is available and ready to be shipped.
We're using Cartthrob and Authorize.net and we have a CIM account.
I have followed Cartthrob's instructions on this page:http://cartthrob.com/docs/tutorials/authorize_later_with_vaults/index.html
That page includes this sample template code:
{exp:cartthrob:checkout_form
    force_vault="yes"
    force_processing="yes"
}
 {gateway_fields}
 <input type="submit" value="Checkout">
{/exp:cartthrob:checkout_form}

*An admin interface might then look like this:*

<table>
    <thead>
        <tr>
            <th>Order</th>
            <th>Date</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
     {exp:channel:entries channel="orders" status="Processing"}
      <tr>
        <td>{title}</td>
        <td>{entry_date format="%F %j, %Y"}</td>
        <td>
            {exp:cartthrob:checkout_form
                vault_id="{order_vault_id}"
                order_id="{entry_id}"
            }
            <input type="submit" value="Process Order">
            {/exp:cartthrob:checkout_form}
        </td>
      </tr>
    {/exp:channel:entries}
   </tbody>
 </table>
With a few tweaks (no tables, I use Stash DRY templating, and so on), I used that code. The orders are being entered with a "processing" status just fine. The sample admin interface code produces the list of entries that were marked "processing" just fine as well but clicking on the "Process Order" button for any of the entries returns this error message:
Error Number: 1062 Duplicate entry '499' for key 'PRIMARY' INSERT INTO exp_channel_titles(entry_idsite_idchannel_idauthor_idforum_topic_idip_addresstitle,statusversioning_enabledentry_dateedit_dateyearmonthday) VALUES ('499', '1', '22', '1', 0, '127.0.0.1', 0, 'processing', 'y', 1391499771, '20140204024352', '2014', '02', '04') Filename: third_party/cartthrob/models/cartthrob_entries_model.php Line Number: 261
I'm pretty sure this is MySQL telling me that I'm trying to insert a duplicate entry id in the table that keeps the list of ordered products.
Reading Cartthrob's suggested template code, I can see why I'm getting this error message. The checkout_form tag creates an entry in the orders channel as well as sending the capture request off to Authorize.net. Since that entry already exists, I'm getting the "duplicate entry for primary key" error message from MySQL. What I need to have happen is an update to the particular order in the orders channel that also sends off the "capture" request to Authorize.net.
***********

And got a response from Cartthrob that worked:
Open the cartthrob > libraries > Cartthrob_payments.php file.
Around line 1806, you'll find something that looks like this:
                unset($order_entry);
        }
        else
        {
            $this->add_error(lang('you_do_not_have_sufficient_permissions_to_update_this_order'));
            return FALSE;
        }
Change it to this:
                unset($order_entry);
            $order_id = $update_order_id; // add this
        }
        else
        {
            $this->add_error(lang('you_do_not_have_sufficient_permissions_to_update_this_order'));
            return FALSE;
        }
I think that order_id variable got wiped out last time we did some juggling to add updating for subscriptions, some of that area of code underwent some big changes.

No comments:

Post a Comment