• Resolved Denis ?oljom

    (@dingo_d)


    I’m building a plugin that sends orders to a 3rd party service.

    In order to integrate it with Braintree, I’m adding certain settings, based on which I execute the calls.

    For that, I need to list all the payment gateways. First I tried using WC()->payment_gateways() but for some odd reason, that broke the Payment settings screen. When I went to admin.php?page=wc-settings&tab=checkout&section=braintree_credit_card that page was empty.

    So instead, I used new WC_Payment_Gateways(); and then get the gateways using get_available_payment_gateways() method. This only returns the 4 default payment gateways.

    I don’t see WC_Gateway_Braintree_Credit_Card or WC_Gateway_Braintree_PayPal.

    Is the plugin not registering them correctly? Or am I missing how to fetch the available payment gateways from WooCommerce?

    When I use this with Corvus PG plugin I can see it in the list just fine.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi?@dingo_d,

    Thanks for writing to us about our plugin! I apologize for the delay in getting back to you. I am happy to help out here. ??

    You can use either of the following method calls to get an array of payment gateways or available payment gateways (respectfully) registered on your site:

    
    $all_payment_gateways       = WC()->payment_gateways()->payment_gateways();
    $available_payment_gateways = WC()->payment_gateways()->get_available_payment_gateways();
    

    I’ve tested both locally with the Braintree plugin and can confirm that the two gateways we register do appear in these arrays. If you are not able to see the Braintree gateways in these, I would recommend double-checking when your code runs as our plugin doesn’t get loaded until at least the plugins_loaded action.

    Would you please give the above method calls a try and let me know if they are helpful?

    Thanks,
    Tamara ??

    Thread Starter Denis ?oljom

    (@dingo_d)

    Ok, this is odd. My PG class is here: https://github.com/dingo-d/woo-solo-api/blob/feature/2.0.0-update/src/ECommerce/WooCommerce/WooPaymentGateways.php

    When I’m getting the gateways using the above way (where I instantiate the WC_Payment_Gateways class in the constructor) the $this->wcPaymentGateway->get_available_payment_gateways() doesn’t return the braintree one. If I try to error log either

    php
    error_log(print_r(\WC()->payment_gateways()->get_available_payment_gateways(), true));
    error_log(print_r((new \WC_Payment_Gateways())->get_available_payment_gateways(), true));
    

    I see it in. Not sure why this happens. I instantiate my service classes on plugin_init hook, so everything should be available. I tried with setting $this->wcPaymentGateway to \WC() but I get the same result.

    It’s odd to me that using my way (initializing the class in the constructor) works for Corvuspay, but not braintree.

    • This reply was modified 3 years, 11 months ago by Denis ?oljom.
    Thread Starter Denis ?oljom

    (@dingo_d)

    Ok so what I ended up doing is this:

    
    <?php
    
    /**
     * WooCommerce Payment gateway implementation
     *
     * @package MadeByDenis\WooSoloApi\ECommerce
     * @since 2.0.0
     */
    
    declare(strict_types=1);
    
    namespace MadeByDenis\WooSoloApi\ECommerce\WooCommerce;
    
    use MadeByDenis\WooSoloApi\ECommerce\PaymentGateways;
    
    use WC_Payment_Gateways;
    
    use function WC;
    
    /**
     * Woo payment gateways
     *
     * This is the implementation of WC_Payment_Gateways class from WooCommerce.
     * We are abstracting the implementation, in order to have a more testable code -
     * avoiding direct instantiations.
     *
     * @package MadeByDenis\WooSoloApi\ECommerce
     * @since 2.0.0
     */
    class WooPaymentGateways implements PaymentGateways
    {
    	/**
    	 * @inheritDoc
    	 */
    	public function getPaymentGateways(): WC_Payment_Gateways
    	{
    		return WC()->payment_gateways();
    	}
    
    	/**
    	 * @inheritDoc
    	 */
    	public function getPaymentGatewayIds(): array
    	{
    		return $this->getPaymentGateways()->get_payment_gateway_ids();
    	}
    
    	/**
    	 * @inheritDoc
    	 */
    	public function getAvailablePaymentGateways(): array
    	{
    		return $this->getPaymentGateways()->get_available_payment_gateways();
    	}
    }
    

    Had to change my interface a bit but this seems to be working so I guess it’s ok.

    Still find it a bit odd that it didn’t work the way it did.

    Hey @dingo_d,

    That’s great news, I’m happy to hear you’ve found a solution here. ??

    If you experience other issues or have further questions, please take a look at our documentation for more information and feel free to create a new thread if you have further questions.

    Thanks,
    Tamara ??

    Thread Starter Denis ?oljom

    (@dingo_d)

    Will do for another issue, but this one is not related to the current one XD Thanks ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Braintree payment not showing in settings’ is closed to new replies.