Home > php教程 > PHP源码 > magento2 添加支付方式payment method

magento2 添加支付方式payment method

WBOY
Release: 2016-06-08 17:20:14
Original
3017 people have browsed it

下面我们一直来看看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>
Copy after login

二:配置文件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>
Copy after login


三:后台配置文件 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>
Copy after login


四: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;

}

}
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template