原文地址
http://www.magentocommerce.com/wiki/5_-_modules_and_development/payment/create-payment-method-module
介紹
每個支付接口方法可以被做成一個獨立的Module,一些方法可以被合並到同一個Module ,因為有可能他們共享相同的功能。
讓我們創建一個Module,它有一個支付方法將完成:
1. 接受信用卡信息
2.驗證訂單是否被提交
3.保存Transaction ID 到訂單支付記錄
我們的Moudle命名為NewModule
文件夾的創建確保:app/code/local
是include_path
如果你使用可配置緩存,不要忘記重置它,在修改了任何Config.xml文件,用刪除var/cache下面的文件來達到目的
一個不錯的建議:禁用Cashe, 在開發階段
Moudle 聲明
創建app/etc/modules/CompanyName_NewModule.xml
<config> <modules> <!-- declare CompanyName_NewModule module --> <CompanyName_NewModule> <!-- this is an active module --> <active>true</active> <!-- this module will be located in app/code/local code pool --> <codePool>local</codePool> <!-- specify dependencies for correct module loading order --> <depends> <Mage_Payment /> </depends> </CompanyName_NewModule> </modules> </config>
現在應用程序就知道這個模塊了。我們讓Magento瞭解模塊的詳細信息
Module 的配置
創建:app/code/local/CompanyName/NewModule/etc/config.xml
:
<?xml version="1.0"?> <config> <modules> <CompanyName_NewModule> <!-- declare module's version information for database updates --> <version>0.1.0</version> </CompanyName_NewModule> </modules> <global> <!-- IMPORTANT: if you use your own namespace (i.e. CompanyName) you also have to declare blocks group for new module. See topic: http://www.magentocommerce.com/boards/viewthread/22416/#t102732 --> <blocks> <newmodule> <class>CompanyName_NewModule_Block</class> </newmodule> </blocks> <!-- declare model group for new module --> <models> <!-- model group alias to be used in Mage::getModel('newmodule/...') --> <newmodule> <!-- base class name for the model group --> <class>CompanyName_NewModule_Model</class> </newmodule> </models> <!-- declare resource setup for new module --> <resources> <!-- resource identifier --> <newmodule_setup> <!-- specify that this resource is a setup resource and used for upgrades --> <setup> <!-- which module to look for install/upgrade files in --> <module>CompanyName_NewModule</module> </setup> <!-- specify database connection for this resource --> <connection> <!-- do not create new connection, use predefined core setup connection --> <use>core_setup</use> </connection> </newmodule_setup> <newmodule_write> <connection> <use>core_write</use> </connection> </newmodule_write> <newmodule_read> <connection> <use>core_read</use> </connection> </newmodule_read> </resources> </global> <!-- declare default configuration values for this module --> <default> <!-- 'payment' configuration section (tab) --> <payment> <!-- 'newmodule' configuration group (fieldset) --> <newmodule> <!-- by default this payment method is inactive --> <active>0</active> <!-- model to handle logic for this payment method --> <model>newmodule/paymentMethod</model> <!-- order status for new orders paid by this payment method --> <order_status>pending</order_status> <!-- default title for payment checkout page and order view page --> <title>Credit Card (Authorize.net)</title> <cctypes>AE,VI,MC,DI</cctypes> <payment_action>authorize</payment_action> <allowspecific>0</allowspecific> </newmodule> </payment> </default> </config>
適配器model
備註:PaymentMethod是隨意的,根據你自己意願來,你需要定義這個方法名字在config.xml裏面。比如上面的config→default→payment→newmodule→model
創建app/code/local/CompanyName/NewModule/Model/PaymentMethod.php
:
<?php /** * Our test CC module adapter */ class CompanyName_NewModule_Model_PaymentMethod extends Mage_Payment_Model_Method_Cc { /** * unique internal payment method identifier * * @var string [a-z0-9_] */ protected $_code = 'newmodule'; /** * Here are examples of flags that will determine functionality availability * of this module to be used by frontend and backend. * * @see all flags and their defaults in Mage_Payment_Model_Method_Abstract * * It is possible to have a custom dynamic logic by overloading * public function can* for each flag respectively */ /** * Is this payment method a gateway (online auth/charge) ? */ protected $_isGateway = true; /** * Can authorize online? */ protected $_canAuthorize = true; /** * Can capture funds online? */ protected $_canCapture = true; /** * Can capture partial amounts online? */ protected $_canCapturePartial = false; /** * Can refund online? */ protected $_canRefund = false; /** * Can void transactions online? */ protected $_canVoid = true; /** * 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 = true; /** * Is this payment method suitable for multi-shipping checkout? */ protected $_canUseForMultishipping = true; /** * Can save credit card information for future processing? */ protected $_canSaveCc = false; /** * Here you will need to implement authorize, capture and void public methods * * @see examples of transaction specific public methods such as * authorize, capture and void in Mage_Paygate_Model_Authorizenet */ } ?>
這是將會做所有與你的支付接口交互的類,當某人創建一個訂單,驗證與捕獲方法會調用你這個類的方法,根據你的配置信息來。如果在你的管理界面你已經選擇了Authorize Only, 那麼驗證方法會被調用,如果你選擇了Authorize and Capture,容易混淆的只有capture方法被調用,如果你需要說明一個錯誤。在這些方法內,你可以使用
Mage::throwException("My error/debug message here");
這講導致Magento顯示一個JS的彈出窗口,終止訂單進度
現在我們有一個model讓我們給管理員一個渠道去配置它,也可以讓結算流程知道這個方法
聲明配置選項到後臺配置項:
這文件將定義可配置選項在Magento後臺的System->Configration
創建app/code/local/CompanyName/NewModule/etc/system.xml
:
<?xml version="1.0"?> <config> <sections> <!-- payment tab --> <payment> <groups> <!-- newmodule fieldset --> <newmodule translate="label" module="paygate"> <!-- will have title 'New Module' --> <label>New Module</label> <!-- position between other payment methods --> <sort_order>670</sort_order> <!-- do not show this configuration options in store scope --> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> <fields> <!-- is this payment method active for the website? --> <active translate="label"> <!-- label for the field --> <label>Enabled</label> <!-- input type for configuration value --> <frontend_type>select</frontend_type> <!-- model to take the option values from --> <source_model>adminhtml/system_config_source_yesno</source_model> <!-- field position --> <sort_order>1</sort_order> <!-- do not show this field in store scope --> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</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>0</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>0</show_in_store> </title> </fields> </newmodule> </groups> </payment> </sections> </config>
如果你跑去Admin / System / Configuration / Payment Methods,你會看到“New Module” 組,啟用它,嘗試去結帳,在支付方法頁面你會看到“New Module” 支付方法,並且有個信用卡表單
數據庫更新
創建 app/code/local/CompanyName/NewModule/sql/newmodule_setup/mysql4-install-0.1.0.php
:
<?php // here are the table creation for this module e.g.: $this->startSetup(); $this->run("HERE YOUR SQL"); $this->endSetup();
更新你的config.xml文件里的模塊版本號
<modules> <CompanyName_NewModule> <version>0.2.0</version> </CompanyName_NewModule> </modules>
然後創建文件:app/code/local/CompanyName/NewModule/sql/newmodule_setup/mysql4-upgrade-0.1.0-0.2.0.php
:
<?php // here are the table updates for this module e.g.: $this->startSetup(); $this->run("HERE YOUR UPDATE SQL"); $this->endSetup();