登录  /  注册
首页 > php教程 > PHP源码 > 正文

magento2 添加支付方式payment method

php中文网
发布: 2016-06-08 17:20:14
原创
2795人浏览过

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

<script>ec(2);</script>

一:启动文件 \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="select" 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 = &#39;more_payment&#39;;

 

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(&#39;USD&#39;);

 

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(&#39;api_key&#39;)

// );

 

$this->_minAmount = $this->getConfigData(&#39;min_order_total&#39;);

$this->_maxAmount = $this->getConfigData(&#39;max_order_total&#39;);

}

 

/**

* 支付捕获方法

* *

* @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(

&#39;amount&#39; => $amount * 100,

&#39;currency&#39; => strtolower($order->getBaseCurrencyCode()),

&#39;description&#39; => sprintf(&#39;#%s, %s&#39;, $order->getIncrementId(), $order->getCustomerEmail()),

&#39;card&#39; => array(

&#39;number&#39; => $payment->getCcNumber(),

&#39;number&#39; => $payment->getCcNumber(),

&#39;exp_month&#39; => sprintf(&#39;%02d&#39;,$payment->getCcExpMonth()),

&#39;exp_year&#39; => $payment->getCcExpYear(),

&#39;cvc&#39; => $payment->getCcCid(),

&#39;name&#39; => $billing->getName(),

&#39;address_line1&#39; => $billing->getStreet(1),

&#39;address_line2&#39; => $billing->getStreet(2),

&#39;address_zip&#39; => $billing->getPostcode(),

&#39;address_state&#39; => $billing->getRegion(),

&#39;address_country&#39; => $billing->getCountry(),

),

));

 

$payment

->setTransactionId($charge->id)

->setIsTransactionClosed(0);

} catch (\Exception $e) {

$this->debugData($e->getMessage());

$this->_logger->logException(__(&#39;Payment capturing error.&#39;));

throw new \Magento\Framework\Model\Exception(__(&#39;Payment capturing error.&#39;));

}

 

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(__(&#39;Payment refunding error.&#39;));

throw new \Magento\Framework\Model\Exception(__(&#39;Payment refunding error.&#39;));

}

 

$payment

->setTransactionId($transactionId . &#39;-&#39; . \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(&#39;api_key&#39;)) {

// 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;

}

}
登录后复制

 

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号