加入收藏 | 设为首页 | 会员中心 | 我要投稿 阜阳站长网 (https://www.0558zz.cn/)- AI行业应用、低代码、混合云存储、数据仓库、物联网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

magento2 添加支付方式payment method

发布时间:2022-07-04 09:55:55 所属栏目:PHP教程 来源:互联网
导读:下面我们一直来看看magento2 添加支付方式payment method,有兴趣的可以和phpfensi小编一起来看看吧,希望例子对各位用. 一:启动文件 appcodeInchooStripeetcmodule.xml ?xml version=1.0? config xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance
  下面我们一直来看看magento2 添加支付方式payment method,有兴趣的可以和phpfensi小编一起来看看吧,希望例子对各位用.
 
  一:启动文件 appcodeInchooStripeetcmodule.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 appcodeInchooStripeetcconfig.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>MorePaymentModelPayment</model>
   
  <payment_action>authorize_capture</payment_action>
   
  <title>Payment</title>
   
  <api_key backend_model="MagentoBackendModelConfigBackendEncrypted" />
   
  <cctypes>AE,VI,MC,DI,JCB</cctypes>
   
  <allowspecific>1</allowspecific>
   
  <min_order_total>0.50</min_order_total>
   
  </more_payment>
   
  </payment>
   
  </default>
   
  </config>
  三:后台配置文件 appcodeInchooStripeetcadminhtmlsystem2.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>MagentoBackendModelConfigSourceYesno</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>MagentoBackendModelConfigBackendEncrypted</backend_model>
   
  </field>
   
  <field id="debug" translate="label" type="select" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="0">
   
  <label>Debug</label>
   
  <source_model>MagentoBackendModelConfigSourceYesno</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>MorePaymentModelSourceCctype</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>MagentoPaymentModelConfigSourceAllspecificcountries</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>MagentoDirectoryModelConfigSourceCountry</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 MorePaymentModel;
   
  class Payment extends MagentoPaymentModelMethodCc
   
  {
   
  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(
   
  MagentoFrameworkEventManagerInterface $eventManager,
   
  MagentoPaymentHelperData $paymentData,
   
  MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
   
  MagentoFrameworkLoggerAdapterFactory $logAdapterFactory,
   
  MagentoFrameworkLogger $logger,
   
  MagentoFrameworkModuleModuleListInterface $moduleList,
   
  MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
   
  MagentoCentinelModelService $centinelService,
   
  StripeApi $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 MagentoFrameworkObject $payment
   
  * @param float $amount
   
  * @return $this
   
  * @throws MagentoFrameworkModelException
   
  */
   
  public function capture(MagentoFrameworkObject $payment, $amount)
   
  {
   
  /** @var MagentoSalesModelOrder $order */
   
  $order = $payment->getOrder();
   
  /** @var MagentoSalesModelOrderAddress $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 MagentoFrameworkModelException(__('Payment capturing error.'));
   
  }
   
  return $this;
   
  }
   
  /**
   
  * Payment refund
   
  *
   
  * @param MagentoFrameworkObject $payment
   
  * @param float $amount
   
  * @return $this
   
  * @throws MagentoFrameworkModelException
   
  */
   
  public function refund(MagentoFrameworkObject $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 MagentoFrameworkModelException(__('Payment refunding error.'));
   
  }
   
  $payment
   
  ->setTransactionId($transactionId . '-' . MagentoSalesModelOrderPaymentTransaction::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;
   
  }
   
  }
 

(编辑:阜阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读