ロボカッサを例として使用してMagentoの支払い方法を書く

こんにちは、ハブロフスク!



今日、Magentoで支払い方法を書いた経験を共有したいと思います。 例としての支払い方法は偶然ではなく、最も重要なものの1つとして選択されたと同時に、ユーザーのオンラインストアの国に大きく依存しています。 また、標準のMagentoパッケージには主に欧米のユーザーを対象としたメソッドが含まれているため、この記事はCISのユーザーに役立つ場合があります。 最初は、実験としてQiWiサポートを作成したかったのですが、そこでテストアカウントを作成することは許可されなかったため、 ロボカッサを介した支払い方法が選択されました。







まず、新しいモジュールを作成する必要があります。これをより詳細に行う方法については、 http://habrahabr.ru/blogs/php/80108/を参照してください 。 支払い方法に集中しないようにします。



1. XMLファイルの作成





したがって、最初に、対応するxmlファイルをapp / etc / modules /ディレクトリに配置して存在を宣言する必要があります。



Sample_Robokassa.xml:

<?xml version="1.0"?> <config> <modules> <Sample_Robokassa> <active>true</active> <codePool>local</codePool> <depends> <Mage_Sales/> <Mage_Checkout/> </depends> </Sample_Robokassa> </modules> </config>
      
      







これらの行は、Magentoにモジュールを探す場所を指示します。 次に、モジュールの構成ファイルを作成し、前のファイルに従って配置する必要があります。



app / code / local / Sample / Robokassa / etc / config.xml:

 <?xml version="1.0"?> <config> <modules> <Sample_Robokassa> <version>1.0.0.0</version> </Sample_Robokassa> </modules> <global> <models> <robokassa> <class>Sample_Robokassa_Model</class><!--      --> </robokassa> </models> <blocks> <robokassa> <class>Sample_Robokassa_Block</class><!--   --> </robokassa> </blocks> <helpers> <robokassa> <class>Sample_Robokassa_Helper</class><!--  helper   --> </robokassa> </helpers> </global> <frontend> <routers> <robokassa> <use>standard</use> <args> <module>Sample_Robokassa</module> <frontName>robokassa</frontName> </args> </robokassa> </routers> </frontend> </config>
      
      







最後のXMLファイルは、管理パネルに追加される設定ファイルです。



 <?xml version="1.0"?> <config> <sections> <payment> <groups> <robokassa_redirect translate="label"> <label>Robokassa</label> <frontend_type>text</frontend_type> <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> <fields> <active translate="label"> <label>Enabled</label> <frontend_type>select</frontend_type> <source_model>adminhtml/system_config_source_yesno</source_model> <sort_order>10</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> </active> <title translate="label"> <label>Title</label> <frontend_type>text</frontend_type> <sort_order>20</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </title> <login translate="label"> <label>Merchant Login</label> <frontend_type>obscure</frontend_type> <backend_model>adminhtml/system_config_backend_encrypted</backend_model> <sort_order>30</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> </login> <password1 translate="label"> <label>Merchant Password #1</label> <!--       --> <frontend_type>obscure</frontend_type> <backend_model>adminhtml/system_config_backend_encrypted</backend_model> <sort_order>40</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> </password1> <password2 translate="label"> <label>Merchant Password #2</label> <!--       --> <frontend_type>obscure</frontend_type> <backend_model>adminhtml/system_config_backend_encrypted</backend_model> <sort_order>45</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> </password2> <test translate="label"> <label>Test Mode</label> <frontend_type>select</frontend_type> <source_model>adminhtml/system_config_source_yesno</source_model> <sort_order>50</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> </test> <sort_order translate="label"> <label>Sort Order</label> <frontend_type>text</frontend_type> <sort_order>60</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> </sort_order> </fields> </robokassa_redirect> </groups> </payment> </sections> </config>
      
      







これらすべてのファイルを管理パネルに追加すると、システム->設定->支払い方法->ロボカッサのパスに次のようなものが表示されます。

画像



2.モデル





支払い方法モデルを作成するとき、継承する抽象クラスMage_Payment_Model_Method_Abstractの属性セクションに特に注意を払う必要があります。 これらすべてのプロパティのうち、必要なのは3つだけです。

 protected $_canUseForMultishipping = false;//      protected $_canUseInternal = false;//    protected $_isInitializeNeeded = true;//     
      
      





初期化手順では、注文に特別なステータス「pending_payment」が付加されます。これは、ユーザーが支払いシステム側にリダイレクトされたが、支払いがまだ行われていないことを意味します。 この状態では、管理パネルから注文を行うことはできません。



モデルの主なタスクは、robocashデスクへの正しいリクエストをコンパイルし、そこからの回答を検証することです。 これを行うには、コントローラーから呼び出す2つのメソッドを実装します。

 public function getRedirectFormFields() { $result = array(); $session = Mage::getSingleton('checkout/session'); $order = Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId()); if (!$order->getId()) { return $result; } $result['MrchLogin'] = $this->getConfigData('login'); $result['OutSum'] = $order->getBaseGrandTotal(); $result['InvId'] = $order->getIncrementId(); $result['Desc'] = 'Shopping in' . Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_STORE_STORE_NAME); $result['IncCurrLabel'] = 'AlfaBankR';//    $result['SignatureValue'] = md5( $result['MrchLogin'] . ':' . $result['OutSum'] . ':' . $result['InvId'] . ':' . $this->getConfigData('password1') ); return $result; } public function validateRequest($request) { if (!isset($request['OutSum']) || !isset($request['InvId']) || !isset($request['SignatureValue'])) { return false; } $crc = md5($request['OutSum'] . ':' . $request['InvId'] . ':' . $this->getConfigData('password2')); return strtoupper($request['SignatureValue']) == strtoupper($crc); }
      
      







ここでは、スペースを節約するためにPHPDocを切り取ります。 最初の方法では、キャッシュデスクへの送信に必要なフィールドのセットを配列の形式で返します。2番目の方法では、攻撃者がロボキャッシュデスクからの応答を偽造するのを防ぐために、署名が再生成され、送信された値と比較されます。 ところで、2つのパスワードがキャッシュデスクで使用されます。1つは通信中にキーを生成するため、もう1つはmagento-> robokassa、2つ目です。 また、システムが標準のgetConfigDataメソッドを使用してブート時にパスワードを復号化するには、これらのフィールドがconfig.xmlファイルで暗号化されていることに言及する必要があります。



さらに、重要なメソッドgetOrderPlaceRedirectUrlがあります。このメソッドは、必要なページに注文を行った後にユーザーをリダイレクトします。 このページでは、モデルのデータを含むフォームを描画し、javascriptを使用してrobocashページに送信します。 その後、ユーザーには待望の支払い方法が表示されます。



このステップで、支払い方法が支払いページの方法のリストに表示されます。



3.コントローラー





1つのコントローラーがあり、3つの主なタスクに焦点を当てます。



1.購入を確認した後、注文に希望のステータスを追加します。

 $request = Mage::app()->getRequest()->getPost(); $paymentMethod = Mage::getModel('robokassa/redirect'); if (!$paymentMethod->validateRequest($request)) { return; } $order = Mage::getModel('sales/order')->loadByIncrementId($request['InvId']); /*          */ $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING); $order->setStatus('processing'); $order->setIsNotified(false); $order->save(); echo 'OK' . $request['InvId']; // ,     
      
      







2.成功したユーザーリターンの処理:

 $session = Mage::getSingleton('checkout/session'); $session->setQuoteId($session->getRobokassaQuoteId()); /*   ,    */ Mage::getSingleton('checkout/session')->getQuote()->setIsActive(false)->save(); $this->_redirect('checkout/onepage/success', array('_secure'=>true));
      
      







3.エラーを伴うリターン処理:

 $session = Mage::getSingleton('checkout/session'); $session->setQuoteId($session->getRobokassaQuoteId()); if ($session->getLastRealOrderId()) { $order = Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId()); if ($order->getId()) { $order->cancel()->save();// ,   "pending_payment"  } } $quote = Mage::getModel('sales/quote')->load($session->getRobokassaQuoteId()); if ($quote->getId()) { $quote->setActive(true);//    $quote->save(); } /*    */ $session->addError(Mage::helper('robokassa')->__('Payment failed. Pleas try again later.')); $this->_redirect('checkout/cart');
      
      







基本的にこれですべてです。このモジュールを使用すると、注文を出したり、キャッシュデスクからお金を引き出すことができます。 XML API robocashのサポートを実装することもできます。これにより、通貨を使用したより柔軟な作業が可能になります。 デバッグ、注文時の注文ステータスの選択など、支払い方法の他の標準機能を実装することもできます。



この記事はほとんどのソースコードを公開しました。 差分ファイルには、ソースコードの他の部分と一緒に、ビューレベルとコードのあまり重要でないセクションのみが欠落しています 。 diffファイルは、LinuxのpatchコマンドまたはWindowsのTortoiseSVNプログラムを使用して、Magentoの既存のコピーにインストールできます。



All Articles