当前位置:首页 > PHP教程 > php高级应用 > 列表

magento2 添加支付方式payment method

发布:smiling 来源: PHP粉丝网  添加日期:2016-01-20 16:14:48 浏览: 评论:0 

下面我们一直来看看magento2 添加支付方式payment method,有兴趣的可以和phpfensi小编一起来看看吧,希望例子对各位用.

一:启动文件 \app\code\Inchoo\Stripe\etc\module.xml

  1. <?xml version="1.0"?> 
  2.  
  3. <config xmlns:xsi=" 
  4. http://www.w3.org/2001/XMLSchema-instance 
  5. " xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> 
  6.  
  7. <module name="More_Payment" schema_version="1.0.0.0" active="true"> 
  8.  
  9. <sequence> 
  10.  
  11. <module name="Magento_Sales"/> 
  12.  
  13. <module name="Magento_Payment"/> 
  14.  
  15. </sequence> 
  16.  
  17. <depends> 
  18.  
  19. <module name="Magento_Sales"/> 
  20.  
  21. <module name="Magento_Payment"/> 
  22.  
  23. </depends> 
  24.  
  25. </module> 
  26.  
  27. </config> 

二:配置文件config.xml \app\code\Inchoo\Stripe\etc\config.xml

  1. <?xml version="1.0"?> 
  2.  
  3. <config xmlns:xsi=" 
  4. http://www.w3.org/2001/XMLSchema-instance 
  5. " xsi:noNamespaceSchemaLocation="../../../Magento/Core/etc/config.xsd"> 
  6.  
  7. <default> 
  8.  
  9. <payment> 
  10.  
  11. <more_payment> 
  12.  
  13. <active>1</active> 
  14.  
  15. <model>More\Payment\Model\Payment</model> 
  16.  
  17. <payment_action>authorize_capture</payment_action> 
  18.  
  19. <title>Payment</title> 
  20.  
  21. <api_key backend_model="Magento\Backend\Model\Config\Backend\Encrypted" /> 
  22.  
  23. <cctypes>AE,VI,MC,DI,JCB</cctypes> 
  24.  
  25. <allowspecific>1</allowspecific> 
  26.  
  27. <min_order_total>0.50</min_order_total> 
  28.  
  29. </more_payment> 
  30.  
  31. </payment> 
  32.  
  33. </default> 
  34.  
  35. </config> 

三:后台配置文件 app\code\Inchoo\Stripe\etc\adminhtml\system2.xml

  1. <?xml version="1.0"?> 
  2.  
  3. <config xmlns:xsi=" 
  4. http://www.w3.org/2001/XMLSchema-instance 
  5. " xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/system_file.xsd"> 
  6.  
  7. <system> 
  8.  
  9. <section id="payment" translate="label" type="text" sortOrder="400" showInDefault="1" showInWebsite="1" showInStore="1"> 
  10.  
  11. <group id="more_payment" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1"> 
  12.  
  13. <label>Payment</label> 
  14.  
  15. <field id="active" translate="label" type="<a href="/tags.php/select/" target="_blank">select</a>" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0"> 
  16.  
  17. <label>Enabled</label> 
  18.  
  19. <source_model>Magento\Backend\Model\Config\Source\Yesno</source_model> 
  20.  
  21. </field> 
  22.  
  23. <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1"> 
  24.  
  25. <label>Title</label> 
  26.  
  27. </field> 
  28.  
  29. <field id="api_key" translate="label" type="obscure" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0"> 
  30.  
  31. <label>Api Key</label> 
  32.  
  33. <backend_model>Magento\Backend\Model\Config\Backend\Encrypted</backend_model> 
  34.  
  35. </field> 
  36.  
  37. <field id="debug" translate="label" type="select" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="0"> 
  38.  
  39. <label>Debug</label> 
  40.  
  41. <source_model>Magento\Backend\Model\Config\Source\Yesno</source_model> 
  42.  
  43. </field> 
  44.  
  45. <field id="cctypes" translate="label" type="multiselect" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0"> 
  46.  
  47. <label>Credit Card Types</label> 
  48.  
  49. <source_model>More\Payment\Model\Source\Cctype</source_model> 
  50.  
  51. </field> 
  52.  
  53. <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0"> 
  54.  
  55. <label>Sort Order</label> 
  56.  
  57. </field> 
  58.  
  59. <field id="allowspecific" translate="label" type="allowspecific" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0"> 
  60.  
  61. <label>Payment from Applicable Countries</label> 
  62.  
  63. <source_model>Magento\Payment\Model\Config\Source\Allspecificcountries</source_model> 
  64.  
  65. </field> 
  66.  
  67. <field id="specificcountry" translate="label" type="multiselect" sortOrder="51" showInDefault="1" showInWebsite="1" showInStore="0"> 
  68.  
  69. <label>Payment from Specific Countries</label> 
  70.  
  71. <source_model>Magento\Directory\Model\Config\Source\Country</source_model> 
  72.  
  73. </field> 
  74.  
  75. <field id="min_order_total" translate="label" type="text" sortOrder="98" showInDefault="1" showInWebsite="1" showInStore="0"> 
  76.  
  77. <label>Minimum Order Total</label> 
  78.  
  79. </field> 
  80.  
  81. <field id="max_order_total" translate="label" type="text" sortOrder="99" showInDefault="1" showInWebsite="1" showInStore="0"> 
  82.  
  83. <label>Maximum Order Total</label> 
  84.  
  85. <comment>Leave empty to disable limit</comment> 
  86.  
  87. </field> 
  88.  
  89. </group> 
  90.  
  91. </section> 
  92.  
  93. </system> 
  94.  
  95. </config> 

四:model类 因为我们在config.xml配置了model,所以前台点击保存支付方式的时候 触发.

  1. <?php 
  2. namespace More\Payment\Model; 
  3.  
  4. class Payment extends \Magento\Payment\Model\Method\Cc 
  5.  
  6. { 
  7.  
  8. const CODE = 'more_payment'; 
  9.  
  10.    
  11.  
  12. protected $_code = self::CODE; 
  13.  
  14.    
  15.  
  16. protected $_isGateway = true; 
  17.  
  18. protected $_canCapture = true; 
  19.  
  20. protected $_canCapturePartial = true; 
  21.  
  22. protected $_canRefund = true; 
  23.  
  24. protected $_canRefundInvoicePartial = true; 
  25.  
  26.    
  27.  
  28. protected $_stripeApi = false; 
  29.  
  30.    
  31.  
  32. protected $_minAmount = null; 
  33.  
  34. protected $_maxAmount = null; 
  35.  
  36. protected $_supportedCurrencyCodes = array('USD'); 
  37.  
  38. public function __construct( 
  39.  
  40. \Magento\Framework\Event\ManagerInterface $eventManager, 
  41.  
  42. \Magento\Payment\Helper\Data $paymentData, 
  43.  
  44. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, 
  45.  
  46. \Magento\Framework\Logger\AdapterFactory $logAdapterFactory, 
  47.  
  48. \Magento\Framework\Logger $logger, 
  49.  
  50. \Magento\Framework\Module\ModuleListInterface $moduleList, 
  51.  
  52. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, 
  53.  
  54. \Magento\Centinel\Model\Service $centinelService, 
  55.  
  56. \Stripe\Api $stripe, 
  57.  
  58. array $data = array() 
  59.  
  60. ) { 
  61.  
  62. parent::__construct($eventManager, $paymentData, $scopeConfig, $logAdapterFactory, $logger, $moduleList, $localeDate, $centinelService, $data); 
  63.  
  64. $this->_stripeApi = $stripe; 
  65.  
  66. // $this->_stripeApi->setApiKey( 
  67.  
  68. // $this->getConfigData('api_key') 
  69.  
  70. // ); 
  71.  
  72. $this->_minAmount = $this->getConfigData('min_order_total'); 
  73.  
  74. $this->_maxAmount = $this->getConfigData('max_order_total'); 
  75.  
  76. } 
  77.  
  78. /** 
  79.  
  80. * 支付捕获方法 
  81.  
  82. * * 
  83.  
  84. * @param \Magento\Framework\Object $payment 
  85.  
  86. * @param float $amount 
  87.  
  88. * @return $this 
  89.  
  90. * @throws \Magento\Framework\Model\Exception 
  91.  
  92. */ 
  93.  
  94. public function capture(\Magento\Framework\Object $payment, $amount) 
  95.  
  96. { 
  97.  
  98. /** @var Magento\Sales\Model\Order $order */ 
  99.  
  100. $order = $payment->getOrder(); 
  101.  
  102. /** @var Magento\Sales\Model\Order\Address $billing */ 
  103.  
  104. $billing = $order->getBillingAddress(); 
  105.  
  106. try { 
  107.  
  108. $charge = \Stripe_Charge::create(array( 
  109.  
  110. 'amount' => $amount * 100, 
  111.  
  112. 'currency' => strtolower($order->getBaseCurrencyCode()), 
  113.  
  114. 'description' => sprintf('#%s, %s', $order->getIncrementId(), $order->getCustomerEmail()), 
  115.  
  116. 'card' => array( 
  117.  
  118. 'number' => $payment->getCcNumber(), 
  119.  
  120. 'number' => $payment->getCcNumber(), 
  121.  
  122. 'exp_month' => sprintf('%02d',$payment->getCcExpMonth()), 
  123.  
  124. 'exp_year' => $payment->getCcExpYear(), 
  125.  
  126. 'cvc' => $payment->getCcCid(), 
  127.  
  128. 'name' => $billing->getName(), 
  129.  
  130. 'address_line1' => $billing->getStreet(1), 
  131.  
  132. 'address_line2' => $billing->getStreet(2), 
  133.  
  134. 'address_zip' => $billing->getPostcode(), 
  135.  
  136. 'address_state' => $billing->getRegion(), 
  137.  
  138. 'address_country' => $billing->getCountry(), 
  139.  
  140. ), 
  141.  
  142. )); 
  143.  
  144. $payment 
  145.  
  146. ->setTransactionId($charge->id) 
  147.  
  148. ->setIsTransactionClosed(0); 
  149.  
  150. } <a href="/tags.php/catch/" target="_blank">catch</a> (\Exception $e) { 
  151.  
  152. $this->debugData($e->getMessage()); 
  153.  
  154. $this->_logger->logException(__('Payment capturing error.')); 
  155.  
  156. throw new \Magento\Framework\Model\Exception(__('Payment capturing error.')); 
  157.  
  158. } 
  159.  
  160. return $this; 
  161.  
  162. } 
  163.  
  164. /** 
  165.  
  166. * Payment refund 
  167.  
  168. * 
  169.  
  170. * @param \Magento\Framework\Object $payment 
  171.  
  172. * @param float $amount 
  173.  
  174. * @return $this 
  175.  
  176. * @throws \Magento\Framework\Model\Exception 
  177.  
  178. */ 
  179.  
  180. public function refund(\Magento\Framework\Object $payment, $amount) 
  181.  
  182. { 
  183.  
  184. $transactionId = $payment->getParentTransactionId(); 
  185.  
  186. try { 
  187.  
  188. \Stripe_Charge::retrieve($transactionId)->refund(); 
  189.  
  190. } catch (\Exception $e) { 
  191.  
  192. $this->debugData($e->getMessage()); 
  193.  
  194. $this->_logger->logException(__('Payment refunding error.')); 
  195.  
  196. throw new \Magento\Framework\Model\Exception(__('Payment refunding error.')); 
  197.  
  198. } 
  199.  
  200. $payment 
  201.  
  202. ->setTransactionId($transactionId . '-' . \Magento\Sales\Model\Order\Payment\Transaction::TYPE_REFUND) 
  203.  
  204. ->setParentTransactionId($transactionId) 
  205.  
  206. ->setIsTransactionClosed(1) 
  207.  
  208. ->setShouldCloseParentTransaction(1); 
  209.  
  210. return $this; 
  211.  
  212. } 
  213.  
  214. /** 
  215.  
  216. * Determine method availability based on quote amount and config data 
  217.  
  218. * 
  219.  
  220. * @param null $quote 
  221.  
  222. * @return bool 
  223.  
  224. */ 
  225.  
  226. public function isAvailable($quote = null) 
  227.  
  228. { 
  229.  
  230. if ($quote && ( 
  231.  
  232. $quote->getBaseGrandTotal() < $this->_minAmount 
  233.  
  234. || ($this->_maxAmount && $quote->getBaseGrandTotal() > $this->_maxAmount)) 
  235.  
  236. ) { 
  237.  
  238. return false; 
  239.  
  240. } 
  241.  
  242. // if (!$this->getConfigData('api_key')) { 
  243.  
  244. // return false; 
  245.  
  246. // } 
  247.  
  248. return parent::isAvailable($quote); 
  249.  
  250. } 
  251.  
  252. /** 
  253.  
  254. * Availability for currency 
  255.  
  256. * 
  257.  
  258. * @param string $currencyCode 
  259.  
  260. * @return bool 
  261.  
  262. */ 
  263.  
  264. public function canUseForCurrency($currencyCode) 
  265.  
  266. { 
  267.  
  268. if (!in_array($currencyCode, $this->_supportedCurrencyCodes)) { 
  269.  
  270. return false; 
  271.  
  272. } 
  273.  
  274. return true; 
  275.  
  276. } 
  277.  
  278. } 

Tags: magento2 payment method

分享到: