magento2 添加支付方式payment method
发布:smiling 来源: PHP粉丝网 添加日期:2016-01-20 16:14:48 浏览: 评论:0
下面我们一直来看看magento2 添加支付方式payment method,有兴趣的可以和phpfensi小编一起来看看吧,希望例子对各位用.
一:启动文件 \app\code\Inchoo\Stripe\etc\module.xml
- <?xml version="1.0"?>
- <config xmlns:xsi="
- http://www.w3.org/2001/XMLSchema-instance
- " xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
- <module name="More_Payment" schema_version="1.0.0.0" active="true">
- <sequence>
- <module name="Magento_Sales"/>
- <module name="Magento_Payment"/>
- </sequence>
- <depends>
- <module name="Magento_Sales"/>
- <module name="Magento_Payment"/>
- </depends>
- </module>
- </config>
二:配置文件config.xml \app\code\Inchoo\Stripe\etc\config.xml
- <?xml version="1.0"?>
- <config xmlns:xsi="
- http://www.w3.org/2001/XMLSchema-instance
- " xsi:noNamespaceSchemaLocation="../../../Magento/Core/etc/config.xsd">
- <default>
- <payment>
- <more_payment>
- <active>1</active>
- <model>More\Payment\Model\Payment</model>
- <payment_action>authorize_capture</payment_action>
- <title>Payment</title>
- <api_key backend_model="Magento\Backend\Model\Config\Backend\Encrypted" />
- <cctypes>AE,VI,MC,DI,JCB</cctypes>
- <allowspecific>1</allowspecific>
- <min_order_total>0.50</min_order_total>
- </more_payment>
- </payment>
- </default>
- </config>
三:后台配置文件 app\code\Inchoo\Stripe\etc\adminhtml\system2.xml
- <?xml version="1.0"?>
- <config xmlns:xsi="
- http://www.w3.org/2001/XMLSchema-instance
- " xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/system_file.xsd">
- <system>
- <section id="payment" translate="label" type="text" sortOrder="400" showInDefault="1" showInWebsite="1" showInStore="1">
- <group id="more_payment" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
- <label>Payment</label>
- <field id="active" translate="label" type="<a href="/tags.php/select/" target="_blank">select</a>" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
- <label>Enabled</label>
- <source_model>Magento\Backend\Model\Config\Source\Yesno</source_model>
- </field>
- <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
- <label>Title</label>
- </field>
- <field id="api_key" translate="label" type="obscure" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0">
- <label>Api Key</label>
- <backend_model>Magento\Backend\Model\Config\Backend\Encrypted</backend_model>
- </field>
- <field id="debug" translate="label" type="select" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="0">
- <label>Debug</label>
- <source_model>Magento\Backend\Model\Config\Source\Yesno</source_model>
- </field>
- <field id="cctypes" translate="label" type="multiselect" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0">
- <label>Credit Card Types</label>
- <source_model>More\Payment\Model\Source\Cctype</source_model>
- </field>
- <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
- <label>Sort Order</label>
- </field>
- <field id="allowspecific" translate="label" type="allowspecific" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
- <label>Payment from Applicable Countries</label>
- <source_model>Magento\Payment\Model\Config\Source\Allspecificcountries</source_model>
- </field>
- <field id="specificcountry" translate="label" type="multiselect" sortOrder="51" showInDefault="1" showInWebsite="1" showInStore="0">
- <label>Payment from Specific Countries</label>
- <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
- </field>
- <field id="min_order_total" translate="label" type="text" sortOrder="98" showInDefault="1" showInWebsite="1" showInStore="0">
- <label>Minimum Order Total</label>
- </field>
- <field id="max_order_total" translate="label" type="text" sortOrder="99" showInDefault="1" showInWebsite="1" showInStore="0">
- <label>Maximum Order Total</label>
- <comment>Leave empty to disable limit</comment>
- </field>
- </group>
- </section>
- </system>
- </config>
四:model类 因为我们在config.xml配置了model,所以前台点击保存支付方式的时候 触发.
- <?php
- namespace More\Payment\Model;
- class Payment extends \Magento\Payment\Model\Method\Cc
- {
- const CODE = 'more_payment';
- protected $_code = self::CODE;
- protected $_isGateway = true;
- protected $_canCapture = true;
- protected $_canCapturePartial = true;
- protected $_canRefund = true;
- protected $_canRefundInvoicePartial = true;
- protected $_stripeApi = false;
- protected $_minAmount = null;
- protected $_maxAmount = null;
- protected $_supportedCurrencyCodes = array('USD');
- public function __construct(
- \Magento\Framework\Event\ManagerInterface $eventManager,
- \Magento\Payment\Helper\Data $paymentData,
- \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
- \Magento\Framework\Logger\AdapterFactory $logAdapterFactory,
- \Magento\Framework\Logger $logger,
- \Magento\Framework\Module\ModuleListInterface $moduleList,
- \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
- \Magento\Centinel\Model\Service $centinelService,
- \Stripe\Api $stripe,
- array $data = array()
- ) {
- parent::__construct($eventManager, $paymentData, $scopeConfig, $logAdapterFactory, $logger, $moduleList, $localeDate, $centinelService, $data);
- $this->_stripeApi = $stripe;
- // $this->_stripeApi->setApiKey(
- // $this->getConfigData('api_key')
- // );
- $this->_minAmount = $this->getConfigData('min_order_total');
- $this->_maxAmount = $this->getConfigData('max_order_total');
- }
- /**
- * 支付捕获方法
- * *
- * @param \Magento\Framework\Object $payment
- * @param float $amount
- * @return $this
- * @throws \Magento\Framework\Model\Exception
- */
- public function capture(\Magento\Framework\Object $payment, $amount)
- {
- /** @var Magento\Sales\Model\Order $order */
- $order = $payment->getOrder();
- /** @var Magento\Sales\Model\Order\Address $billing */
- $billing = $order->getBillingAddress();
- try {
- $charge = \Stripe_Charge::create(array(
- 'amount' => $amount * 100,
- 'currency' => strtolower($order->getBaseCurrencyCode()),
- 'description' => sprintf('#%s, %s', $order->getIncrementId(), $order->getCustomerEmail()),
- 'card' => array(
- 'number' => $payment->getCcNumber(),
- 'number' => $payment->getCcNumber(),
- 'exp_month' => sprintf('%02d',$payment->getCcExpMonth()),
- 'exp_year' => $payment->getCcExpYear(),
- 'cvc' => $payment->getCcCid(),
- 'name' => $billing->getName(),
- 'address_line1' => $billing->getStreet(1),
- 'address_line2' => $billing->getStreet(2),
- 'address_zip' => $billing->getPostcode(),
- 'address_state' => $billing->getRegion(),
- 'address_country' => $billing->getCountry(),
- ),
- ));
- $payment
- ->setTransactionId($charge->id)
- ->setIsTransactionClosed(0);
- } <a href="/tags.php/catch/" target="_blank">catch</a> (\Exception $e) {
- $this->debugData($e->getMessage());
- $this->_logger->logException(__('Payment capturing error.'));
- throw new \Magento\Framework\Model\Exception(__('Payment capturing error.'));
- }
- return $this;
- }
- /**
- * Payment refund
- *
- * @param \Magento\Framework\Object $payment
- * @param float $amount
- * @return $this
- * @throws \Magento\Framework\Model\Exception
- */
- public function refund(\Magento\Framework\Object $payment, $amount)
- {
- $transactionId = $payment->getParentTransactionId();
- try {
- \Stripe_Charge::retrieve($transactionId)->refund();
- } catch (\Exception $e) {
- $this->debugData($e->getMessage());
- $this->_logger->logException(__('Payment refunding error.'));
- throw new \Magento\Framework\Model\Exception(__('Payment refunding error.'));
- }
- $payment
- ->setTransactionId($transactionId . '-' . \Magento\Sales\Model\Order\Payment\Transaction::TYPE_REFUND)
- ->setParentTransactionId($transactionId)
- ->setIsTransactionClosed(1)
- ->setShouldCloseParentTransaction(1);
- return $this;
- }
- /**
- * Determine method availability based on quote amount and config data
- *
- * @param null $quote
- * @return bool
- */
- public function isAvailable($quote = null)
- {
- if ($quote && (
- $quote->getBaseGrandTotal() < $this->_minAmount
- || ($this->_maxAmount && $quote->getBaseGrandTotal() > $this->_maxAmount))
- ) {
- return false;
- }
- // if (!$this->getConfigData('api_key')) {
- // return false;
- // }
- return parent::isAvailable($quote);
- }
- /**
- * Availability for currency
- *
- * @param string $currencyCode
- * @return bool
- */
- public function canUseForCurrency($currencyCode)
- {
- if (!in_array($currencyCode, $this->_supportedCurrencyCodes)) {
- return false;
- }
- return true;
- }
- }
Tags: magento2 payment method
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)