Magento2 add payment method paymentmethod-PHP source code

Source: Internet
Author: User
Tags magento2
Next, let's take a look at how magento2 adds the payment method paymentmethod. If you are interested, let's take a look at it with the 111cn editor. I hope you can use the example for your reference. Next, let's take a look at how magento2 adds the payment method of the payment method. If you are interested, you can take a look at it with the 111cn editor. I hope the example will be useful to you.

Script ec (2); script

I. Startup File \ app \ code \ Inchoo \ Stripe \ etc \ module. xml

 
 
  
   
    
    
   
   
    
    
   
  
 

Ii. configuration file config. xml \ app \ code \ Inchoo \ Stripe \ etc \ config. xml

 
 
  
   
    
     1
     
      More\Payment\Model\Payment
     
     
      authorize_capture
     
     Payment
     
      AE,VI,MC,DI,JCB
     1
     
      0.50
     
    
   
  
 


3. Background configuration file app \ code \ Inchoo \ Stripe \ etc \ adminhtml \ system2.xml

 
 
  
   
    
     Payment 
     
      Enabled
      
       Magento\Backend\Model\Config\Source\Yesno
      
     
     
      Title
     
     
      Api Key
      
       Magento\Backend\Model\Config\Backend\Encrypted
      
     
     
      Debug
      
       Magento\Backend\Model\Config\Source\Yesno
      
     
     
      Credit Card Types
      
       More\Payment\Model\Source\Cctype
      
     
     
      Sort Order
     
     
      Payment from Applicable Countries
      
       Magento\Payment\Model\Config\Source\Allspecificcountries
      
     
     
      Payment from Specific Countries
      
       Magento\Directory\Model\Config\Source\Country
      
     
     
      Minimum Order Total
     
     
      Maximum Order Total
      
       Leave empty to disable limit
      
     
    
   
  
 


4. The model class is triggered when you click "Save Payment Method" in the foreground because the model is configured in config. xml.

 _ 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 float $ amount * @ return $ this * @ throws \ Magento \ Framework \ Model \ Exception */public function capture (\ Magento \ Framework \ Obj Ect $ 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-> getCustomerE Mail (), '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' => $ bil Ling-> 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 \ Ob Ject $ 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 * @ r Eturn 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 ;}}
Related Article

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.