Welcome to our Magento blog!
Nice to see you again in this series of Magento tutorial. Today I will write about Payment Methods – the last stage of checkout process. As we know, once customer details are filled in and the chosen shipping method, the payment method forms will be shown for customer to select. After a customer selects a payment method, the checkout will progress through the payment method’s function. It can be the request via an API, authorization or offline payment. My tutorial today will introduce how to add a custom payment method for Magento?
1. Declare custom payment method by configuration
To add your custom payment method, you need to declare with Magento’s system. Thus you can add the code below to your module configuration (config.xml).
<config> ... <default> <payment> <[custom_payment]> <active>1</active> <model>[custom_module]/method_[payment_model]</model> <order_status>pending</order_status> <title>Custom Payment Method</title> <allowspecific>0</allowspecific> ... </[custom_payment]> </payment> </default> </config>
In this configuration, you see the tag model, this is the configuration of the payment adaptor model.
2. Write adaptor model for custom payment method
Firstly, you have to write your custom model corresponding to the configuration above. This model needs to be extended from class Mage_Payment_Model_Method_Abstract. This model class is similar to the following:
class [Custom_Module]_Model_Method_[Payment_Model] extends Mage_Payment_Model_Method_Abstract { /** form block and info block for custom payment method */ protected $_formBlockType = '[custom_module/form_block]'; protected $_infoBlockType = '[custom_module/info_block]'; /** Payment method features */ protected $_isGateway = true; protected $_canOrder = false; protected $_canAuthorize = false; protected $_canCapture = true; protected $_canCapturePartial = false; protected $_canRefund = true; ... public function authorize(Varien_Object $payment, $amount) { // authorize payment } public function capture(Varien_Object $payment, $amount) { // capture payment } public function order(Varien_Object $payment) { // void payment } public function refund(Varien_Object $payment, $amount) { // refund payment } ... }
Above are some basic methods that the system calls when the order is processed. Thus they are key functions of a payment method. If payment method uses a gateway, some function can send request to the gateway to collect information for processing order.
3. Store payment’s information
Payment model is the abstract of an instance payment method and it works with database. Payment model can be Mage_Sales_Model_Order_Payment or Mage_Sales_Model_Quote_Payment depending on checkout stage. And the database is stored corresponding with tables sales_flat_order_payment and sales_flat_quote_payment. However, Quote_Payment doesn’t have any method to process the order. You can view more details in the Magento’s source code and database.
Topic 8 is about to come to an end. For other topics, visit our Magento blog!
5 Comments
Hello David Thanks for sharing knowledge… it will really help me to prepare for certified exam… 1 more thing can you provide sample question which is already ask in exami… i have 2 year experience in magento and i am preparing for exam… can i pass it with preparation of 2 month?
Thanks And keep writting….
Hello Saillesh,
Thanks for your comment. Following our blog frequently and I think you’ll pass the exam. Good luck!
Hi David, thanks for revealing each of the topic mentioned by magentocommerce. I have just passed the “Magento Certified Developer” exam.
Hi Ankit,
I am so glad to hear that. Congrats!!!
Good job guys keep the good work, is still few people to share advanced magento experience… And tips for certification is a awesome plus…. Thanks David and the others from magestore…