Magento 2: Get Payment Methods (All/Active/Used)
This article shows how to get payment methods in Magento 2. We will be fetching 3 types of payment methods. They are:
1) All Payment Methods
2) Active/Enabled Payment Methods
3) Payment Methods that have been used while placing orders
I will be showing both ways (Dependency Injection & Object Manager way) to get payment methods.
Using Dependency Injection (DI)
Here is the code that you need to write in your Block class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | /** * Order Payment * * @var \Magento\Sales\Model\ResourceModel\Order\Payment\Collection */ protected $_orderPayment; /** * Payment Helper Data * * @var \Magento\Payment\Helper\Data */ protected $_paymentHelper; /** * Payment Model Config * * @var \Magento\Payment\Model\Config */ protected $_paymentConfig; /** * @param \Magento\Backend\Block\Template\Context $context * @param \Magento\Sales\Model\ResourceModel\Order\Payment\Collection $orderPayment * @param \Magento\Payment\Helper\Data $paymentHelper * @param \Magento\Payment\Model\Config $paymentConfig * @param array $data */ public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Sales\Model\ResourceModel\Order\Payment\Collection $orderPayment, \Magento\Payment\Helper\Data $paymentHelper, \Magento\Payment\Model\Config $paymentConfig, array $data = [] ) { $this->_orderPayment = $orderPayment; $this->_paymentHelper = $paymentHelper; $this->_paymentConfig = $paymentConfig; parent::__construct($context, $data); } /** * Get all payment methods * * @return array */ public function getAllPaymentMethods() { return $this->_paymentHelper->getPaymentMethods(); } /** * Get key-value pair of all payment methods * key = method code & value = method name * * @return array */ public function getAllPaymentMethodsList() { return $this->_paymentHelper->getPaymentMethodList(); } /** * Get active/enabled payment methods * * @return array */ public function getActivePaymentMethods() { return $this->_paymentConfig->getActiveMethods(); } /** * Get payment methods that have been used for orders * * @return array */ public function getUsedPaymentMethods() { $collection = $this->_orderPayment; $collection->getSelect()->group('method'); $paymentMethods[] = array('value' => '', 'label' => 'Any'); foreach ($collection as $col) { $paymentMethods[] = array('value' => $col->getMethod(), 'label' => $col->getAdditionalInformation()['method_title']); } return $paymentMethods; } |
Using Object Manager
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $paymentHelper = $objectManager->get('Magento\Payment\Helper\Data'); $allPaymentMethods = $paymentHelper->getPaymentMethods(); $allPaymentMethodsArray = $paymentHelper->getPaymentMethodList(); var_dump($allPaymentMethodsArray); var_dump($allPaymentMethods); $paymentConfig = $objectManager->get('Magento\Payment\Model\Config'); $activePaymentMethods = $paymentConfig->getActiveMethods(); var_dump(array_keys($activePaymentMethods)); $orderPaymentCollection = $objectManager->get('\Magento\Sales\Model\ResourceModel\Order\Payment\Collection'); $orderPaymentCollection->getSelect()->group('method'); $paymentMethods[] = array('value' => '', 'label' => 'Any'); foreach ($orderPaymentCollection as $col) { $paymentMethods[] = array('value' => $col->getMethod(), 'label' => $col->getAdditionalInformation()['method_title']); } var_dump($paymentMethods); |
Sample Output of getUsedPaymentMethods():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Array ( [0] => Array ( [value] => [label] => Any ) [1] => Array ( [value] => cashondelivery [label] => Cash On Delivery ) [2] => Array ( [value] => checkmo [label] => Check / Money order ) ) |
Sample Output of getAllPaymentMethodsList():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Array ( [vault] => [substitution] => [banktransfer] => Bank Transfer Payment [cashondelivery] => Cash On Delivery [checkmo] => Check / Money order [payflowpro] => Credit Card [payflow_link] => Credit Card [payflow_advanced] => Credit Card [braintree] => Credit Card (Braintree) [authorizenet_directpost] => Credit Card Direct Post (Authorize.net) [free] => No Payment Information Required [braintree_paypal] => PayPal (Braintree) [paypal_billing_agreement] => PayPal Billing Agreement [payflow_express_bml] => PayPal Credit [paypal_express_bml] => PayPal Credit [paypal_express] => PayPal Express Checkout [payflow_express] => PayPal Express Checkout Payflow Edition [hosted_pro] => Payment by cards or by PayPal account [purchaseorder] => Purchase Order [braintree_cc_vault] => Stored Cards (Braintree) [payflowpro_cc_vault] => Stored Cards (Payflow Pro) ) |
Sample Output of getAllPaymentMethods():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | Array ( [free] => Array ( [active] => 1 [model] => Magento\Payment\Model\Method\Free [order_status] => pending [title] => No Payment Information Required [allowspecific] => 0 [sort_order] => 1 [group] => offline ) [substitution] => Array ( [active] => 0 [model] => Magento\Payment\Model\Method\Substitution [allowspecific] => 0 ) [vault] => Array ( [debug] => 1 [model] => Magento\Vault\Model\VaultPaymentInterface ) [checkmo] => Array ( [active] => 1 [model] => Magento\OfflinePayments\Model\Checkmo [order_status] => pending [title] => Check / Money order [allowspecific] => 0 [group] => offline ) [purchaseorder] => Array ( [active] => 0 [model] => Magento\OfflinePayments\Model\Purchaseorder [order_status] => pending [title] => Purchase Order [allowspecific] => 0 [group] => offline ) [banktransfer] => Array ( [active] => 0 [model] => Magento\OfflinePayments\Model\Banktransfer [order_status] => pending [title] => Bank Transfer Payment [allowspecific] => 0 [group] => offline ) [cashondelivery] => Array ( [active] => 0 [model] => Magento\OfflinePayments\Model\Cashondelivery [order_status] => pending [title] => Cash On Delivery [allowspecific] => 0 [group] => offline ) [paypal_express] => Array ( [model] => Magento\Paypal\Model\Express [title] => PayPal Express Checkout [payment_action] => Authorization [solution_type] => Mark [line_items_enabled] => 1 [visible_on_cart] => 1 [visible_on_product] => 1 [allow_ba_signup] => never [group] => paypal [authorization_honor_period] => 3 [order_valid_period] => 29 [child_authorization_number] => 1 [verify_peer] => 1 [skip_order_review_step] => 1 ) [paypal_express_bml] => Array ( [model] => Magento\Paypal\Model\Bml [title] => PayPal Credit [group] => paypal ) [payflow_express] => Array ( [title] => PayPal Express Checkout Payflow Edition [payment_action] => Authorization [line_items_enabled] => 1 [visible_on_cart] => 1 [visible_on_product] => 1 [group] => paypal [verify_peer] => 1 [model] => Magento\Paypal\Model\PayflowExpress ) [payflow_express_bml] => Array ( [model] => Magento\Paypal\Model\Payflow\Bml [title] => PayPal Credit [group] => paypal ) [payflowpro] => Array ( [model] => Magento\Paypal\Model\Payflow\Transparent [title] => Credit Card [payment_action] => Authorization [cctypes] => AE,VI [useccv] => 1 [tender] => C [verbosity] => MEDIUM [user] => [pwd] => [group] => paypal [verify_peer] => 1 [date_delim] => [ccfields] => csc,expdate,acct [place_order_url] => paypal/transparent/requestSecureToken [cgi_url_test_mode] => https://pilot-payflowlink.paypal.com [cgi_url] => https://payflowlink.paypal.com [transaction_url_test_mode] => https://pilot-payflowpro.paypal.com [transaction_url] => https://payflowpro.paypal.com [avs_street] => 0 [avs_zip] => 0 [avs_international] => 0 [avs_security_code] => 1 [cc_year_length] => 2 [can_authorize_vault] => 1 [can_capture_vault] => 1 ) [payflowpro_cc_vault] => Array ( [model] => PayflowProCreditCardVaultFacade [title] => Stored Cards (Payflow Pro) ) [paypal_billing_agreement] => Array ( [active] => 1 [allow_billing_agreement_wizard] => 1 [model] => Magento\Paypal\Model\Method\Agreement [title] => PayPal Billing Agreement [group] => paypal [verify_peer] => 1 ) [payflow_link] => Array ( [model] => Magento\Paypal\Model\Payflowlink [payment_action] => Authorization [verbosity] => HIGH [user] => [pwd] => [group] => paypal [title] => Credit Card [partner] => PayPal [csc_required] => 1 [csc_editable] => 1 [url_method] => GET [email_confirmation] => 0 [verify_peer] => 1 [transaction_url_test_mode] => https://pilot-payflowpro.paypal.com [transaction_url] => https://payflowpro.paypal.com [cgi_url_test_mode] => https://pilot-payflowlink.paypal.com [cgi_url] => https://payflowlink.paypal.com ) [payflow_advanced] => Array ( [model] => Magento\Paypal\Model\Payflowadvanced [payment_action] => Authorization [verbosity] => HIGH [user] => PayPal [pwd] => [group] => paypal [title] => Credit Card [partner] => PayPal [vendor] => PayPal [csc_required] => 1 [csc_editable] => 1 [url_method] => GET [email_confirmation] => 0 [verify_peer] => 1 [transaction_url_test_mode] => https://pilot-payflowpro.paypal.com [transaction_url] => https://payflowpro.paypal.com [cgi_url_test_mode] => https://pilot-payflowlink.paypal.com [cgi_url] => https://payflowlink.paypal.com ) [hosted_pro] => Array ( [model] => Magento\Paypal\Model\Hostedpro [title] => Payment by cards or by PayPal account [payment_action] => Authorization [group] => paypal [display_ec] => 0 [verify_peer] => 1 ) [authorizenet_directpost] => Array ( [active] => 0 [cctypes] => AE,VI,MC,DI [debug] => 0 [email_customer] => 0 [login] => [merchant_email] => [model] => Magento\Authorizenet\Model\Directpost [order_status] => processing [payment_action] => authorize [test] => 1 [title] => Credit Card Direct Post (Authorize.net) [trans_key] => [trans_md5] => [allowspecific] => 0 [currency] => USD [create_order_before] => 1 [date_delim] => / [ccfields] => x_card_code,x_exp_date,x_card_num [place_order_url] => authorizenet/directpost_payment/place [cgi_url_test_mode] => https://test.authorize.net/gateway/transact.dll [cgi_url] => https://secure.authorize.net/gateway/transact.dll [cgi_url_td_test_mode] => https://apitest.authorize.net/xml/v1/request.api [cgi_url_td] => https://api2.authorize.net/xml/v1/request.api ) [braintree] => Array ( [model] => BraintreeFacade [title] => Credit Card (Braintree) [payment_action] => authorize [active] => 0 [is_gateway] => 1 [can_use_checkout] => 1 [can_authorize] => 1 [can_capture] => 1 [can_capture_partial] => 1 [can_authorize_vault] => 1 [can_capture_vault] => 1 [can_use_internal] => 1 [can_refund_partial_per_invoice] => 1 [can_refund] => 1 [can_void] => 1 [can_cancel] => 1 [cctypes] => AE,VI,MC,DI,JCB,CUP,DN,MI [useccv] => 1 [cctypes_braintree_mapper] => {"american-express":"AE","discover":"DI","jcb":"JCB","mastercard":"MC","master-card":"MC","visa":"VI","maestro":"MI","diners-club":"DN","unionpay":"CUP"} [order_status] => processing [environment] => sandbox [allowspecific] => 0 [sdk_url] => https://js.braintreegateway.com/js/braintree-2.17.6.min.js [public_key] => [private_key] => [masked_fields] => cvv,number [privateInfoKeys] => avsPostalCodeResponseCode,avsStreetAddressResponseCode,cvvResponseCode,processorAuthorizationCode,processorResponseCode,processorResponseText,liabilityShifted,liabilityShiftPossible,riskDataId,riskDataDecision [paymentInfoKeys] => cc_type,cc_number,avsPostalCodeResponseCode,avsStreetAddressResponseCode,cvvResponseCode,processorAuthorizationCode,processorResponseCode,processorResponseText,liabilityShifted,liabilityShiftPossible,riskDataId,riskDataDecision ) [braintree_paypal] => Array ( [model] => BraintreePayPalFacade [title] => PayPal (Braintree) [active] => 0 [payment_action] => authorize [allowspecific] => 0 [require_billing_address] => 0 [allow_shipping_address_override] => 1 [display_on_shopping_cart] => 1 [order_status] => processing [is_gateway] => 1 [can_use_checkout] => 1 [can_authorize] => 1 [can_capture] => 1 [can_capture_partial] => 1 [can_refund] => 1 [can_refund_partial_per_invoice] => 1 [can_void] => 1 [can_cancel] => 1 [privateInfoKeys] => processorResponseCode,processorResponseText,paymentId [paymentInfoKeys] => processorResponseCode,processorResponseText,paymentId,payerEmail ) [braintree_cc_vault] => Array ( [model] => BraintreeCreditCardVaultFacade [title] => Stored Cards (Braintree) ) |
The output array of of getActivePaymentMethods() is very long. So, I have just printed out the keys of the array. Keys of the output array contains active method’s code.
Sample Output of array_keys(getActivePaymentMethods()):
1 2 3 4 5 6 7 8 | Array ( [0] => free [1] => checkmo [2] => purchaseorder [3] => cashondelivery [4] => paypal_billing_agreement ) |
Hope this helps. Thanks.





Mukesh Chapagain is a graduate of Kathmandu University (Dhulikhel, Nepal) from where he holds a Masters degree in Computer Engineering. Mukesh is a passionate web developer who has keen interest in open source technologies, programming & blogging.