Magento2 Add Payment Way Payment method

Source: Internet
Author: User
Tags getmessage sprintf magento2

One: Startup file \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>

II: Configuration file 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>


Three: Background configuration file 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= " showindefault=" 1 " showinwebsite=" 1 " showinstore=" 1 "> <group id= "More_payment"  translate= "label"  type= "text"  sortorder= "M"   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= "a"  showindefault

= "1"  showinwebsite= "1"  showinstore= "0" > <label>Sort Order</label> </field> <field id= "Allowspecific"  translate= "label"  type= "Allowspecific"  sortorder= "M"   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="  sortorder= "MultiSelect"  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="  showindefault= "1"  showinwebsite= "1"  showinstore= "0" > <label>Minimum Order Total</label> </field> <field id= "Max_order_total"  translate= "label"  type= "text"  sortorder= " showindefault=" "1"  showinwebsite= "1"  showinstore= "0" > <label>Maximum Order Total</label> <comment>Leave  empty to disable limit</comment> </field> </group> </section> </system > </config>


Four: Model class because we configured model in Config.xml, the front desk clicks on the Save payment method to trigger the

<?php   namespace more\payment\model;   CLASS&NBSP;PAYMENT&NBSP;EXTENDS&NBSP;\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 ');  /** *  Payment Capture method * * *  @param  \Magento\Framework\Object  $payment *  @param  fl oat  $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  *  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);

} catch  (\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 &n bsp;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;

&NBSP}   return parent::isavailable ($quote); }  /** * availability for currency * *  @param  string  $currencyCode *  @retur N bool/public function canuseforcurrency ($currencyCode) {if  (!in_array ($currencyCode,

  $this->_supportedcurrencycodes))  {return false;

} return true; }

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.