Magento – Payment not saving in quote when creating order programmatically

magento-1.7orderspaymentprogramming

I have a current script that uses a mishmash of pure Magento methods and the API. I'm trying to convert it all to straight Magento methods, but I'm having an issue to get the payment method to stay. Some of the processing is done in one script, which then passes the quote ID to a class.

The first part:

$paymentMethod =    array(
    'method' => 'verisign',
    'cc_cid' => $_REQUEST['cc_verify'],
    'cc_owner' => $_REQUEST['cc_name'],
    'cc_number' => $_REQUEST['cc_number'],
    'cc_type' => $_REQUEST['cc_type'],
    'cc_exp_year' => $_REQUEST['expire_year'],
    'cc_exp_month' => $_REQUEST['expire_month']
);

try {
    $payment = $quote->getPayment();
    $payment->importData($paymentMethod);
    $quote->getBillingAddress()->setPaymentMethod($payment->getMethod());
    $quote->getShippingAddress()->setPaymentMethod($payment->getMethod());
    $quote->setPayment($payment);
    $quote->collectTotals()->save();

    $err = $OTOrder->completeOrder($shopping_cart_id,$paymentMethod);

..... }

and the relevant part of the OTOrder->completeOrder function:

$quote = Mage::getModel('sales/quote')->load($shopping_cart_id); // shopping_cart_id is the quote ID
// setting the shipping methods and other order-specific settings here

$payment=$quote->getPayment();
Mage::log('1');
$convertQuote = Mage::getSingleton('sales/convert_quote');
Mage::log('2');
$order = $convertQuote->addressToOrder($quote->getShippingAddress());
Mage::log('3');
$order->setPayment($convertQuote->paymentToOrderPayment($payment));
Mage::log('4');

The script stops at addressToOrder with the error Uncaught exception 'Mage_Core_Exception' with message 'The requested Payment Method is not available.' Am I perhaps doing something in the wrong order somewhere, or missing a crucial setp?

Best Answer

Instead of re-loading the quote inside of OTOrder, I just created the quote as a variable of OTOrder, and just referenced it wherever I needed it. This also allowed me to bring more of the checkout methods directly into the class.