1

I need to create a custom payment method in magento, so that when I create orders programaticalyl via models, I can set the "Title" of the payment to anything I want (for e.g. Cash, Invoice, etc...). A cron job will need to use this custom payment method to import orders.

So, this payment method should NOT be available at checkout, only when used via models from admin context.

So, I have the following in my custom module's system.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <sections>
        <payment>
            <groups>
                <MyCompany translate="label" module="paygate">
                    <label>MyCompany</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>2</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <active translate="label">
                            <label>Enabled</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </active>
                        <order_status translate="label">
                            <label>New order status</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_order_status_processing</source_model>
                            <sort_order>4</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </order_status>
                        <title translate="label">
                            <label>Title</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </title>
                    </fields>
                </MyCompany>
            </groups>
        </payment>
    </sections>
</config>

and this is my Model/Payment.php

<?php

class MyCompany_CustomPayment_Model_Payment
    extends Mage_Payment_Model_Method_Abstract
{
    /**
     * unique internal payment method identifier
     *
     * @var string [a-z0-9_]
     */
    protected $_code = 'custom_payment';

    /**
     * Is this payment method a gateway (online auth/charge) ?
     */
    protected $_isGateway               = false;

    /**
     * Can authorize online?
     */
    protected $_canAuthorize            = false;

    /**
     * Can capture funds online?
     */
    protected $_canCapture              = false;

    /**
     * Can capture partial amounts online?
     */
    protected $_canCapturePartial       = false;

    /**
     * Can refund online?
     */
    protected $_canRefund               = false;

    /**
     * Can void transactions online?
     */
    protected $_canVoid                 = false;

    /**
     * Can use this payment method in administration panel?
     */
    protected $_canUseInternal          = true;

    /**
     * Can show this payment method as an option on checkout payment page?
     */
    protected $_canUseCheckout          = false;

    /**
     * Is this payment method suitable for multi-shipping checkout?
     */
    protected $_canUseForMultishipping  = false;

    /**
     * Can save credit card information for future processing?
     */
    protected $_canSaveCc = false;

    /**
     * Retrieve information from payment configuration
     *
     * @param string $field
     * @param int|string|null|Mage_Core_Model_Store $storeId
     *
     * @return mixed
     */

    /**
     * Retrieve payment method title
     *
     * @return string
     */
    public function getTitle()
    {
        if ($this->getInfoInstance()->getOrigData('additional_data') != '') {
            return $this->getInfoInstance()->getOrigData('additional_data');
        } else {
            return $this->getConfigData('title');
        }
    }
}

When the module is installed, it appears to be working, although I can only see it in Default configuration scope (not in the store view scopes). This is what I did to enable it:

enter image description here

So, I tried to use it like this (only relevant code is shown, other code is snipped):

// Set Payment
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($orderInfo->StoreId)
->setCustomerPaymentId(0)
->setMethod('custom_payment')
->setAdditionalData($orderInfo->PaymentMethodTitle)
->setPo_number(' – ');
$order->setPayment($orderPayment);

When the above code executes, I get the following error/exception:

The requested Payment Method is not available.

Any idea what might be wrong? The order I am trying to create is actually for one of the store views (not the default store).

1 Answer 1

0

Try this:

In the Payment Method Model let the property: protected $_canUseCheckout = true;

Then in the Cron file set some registry flag ie:

Mage::register('my_registry_flag', true); 
// - NOTES: 
// - before register the flag check if exists to avoid errors 
// - It's better to use a constant instead a string

And implement the validate() method in the Payment Method Model:

public function validate()
{
    if(!Mage::registry('my_registry_flag')) {
       return false;
    }
    return parent::validate();
}

I'm assuming that your "order create" script it's working with other payment methods.

6
  • I have tried this and unfortunately it still does not work. Same error.
    – Latheesan
    Jan 22, 2016 at 9:19
  • 1) can you try your payment method in a regular situation (in the checkout) an then if is working apply my "registry-flag" workaround? 2) is your create order code working with other Payment Methods like check money order? Jan 22, 2016 at 11:40
  • Yea it works fine with other payment method. For example checkmo works great. Also these custom payments method don't show in front end checkout screen.
    – Latheesan
    Jan 22, 2016 at 16:44
  • Ok i reccomend to you that copy checkmo configuration to your payment method and try it in a regular checkout then apply the registry flag to deactivate it in the checkout. I'm pretty sure that the problem is in the peyment method model configuration. Jan 22, 2016 at 16:55
  • That's what I ended up doing. I copied checkmo and now it's working fine.
    – Latheesan
    Jan 23, 2016 at 15:55

Your Answer

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

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