From e4bcb0023e756292fafba4e0519dbf673ec00dcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reu=CC=88el=20van=20der=20Steege?= Date: Mon, 24 Jul 2017 12:31:08 +0200 Subject: [PATCH] Add `Direct Debit (mandate via Bancontact)` gateway. --- src/DirectDebitBancontactGateway.php | 82 ++++++++++++++++++++++++++++ src/Extension.php | 3 +- 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/DirectDebitBancontactGateway.php diff --git a/src/DirectDebitBancontactGateway.php b/src/DirectDebitBancontactGateway.php new file mode 100644 index 0000000..9008733 --- /dev/null +++ b/src/DirectDebitBancontactGateway.php @@ -0,0 +1,82 @@ +id = self::ID; + $this->method_title = __( 'Direct Debit (mandate via Bancontact)', 'pronamic_ideal' ); + $this->payment_method = Pronamic_WP_Pay_PaymentMethods::DIRECT_DEBIT_BANCONTACT; + + // @since unreleased + $this->supports = array( + 'products', + 'subscriptions', + 'subscription_cancellation', + 'subscription_reactivation', + 'subscription_suspension', + ); + + // Handle subscription payments + add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'process_subscription_payment' ), 10, 2 ); + + // Filters + add_filter( 'woocommerce_available_payment_gateways', array( $this, 'get_available_payment_gateways' ) ); + + parent::__construct(); + } + + ////////////////////////////////////////////////// + + /** + * Initialise form fields + */ + function init_form_fields() { + parent::init_form_fields(); + + $this->form_fields['enabled']['label'] = __( 'Enable Direct Debit (mandate via Bancontact)', 'pronamic_ideal' ); + $this->form_fields['description']['default'] = __( 'By using this payment method you authorize us via Bancontact to debit payments from your bank account.', 'pronamic_ideal' ); + $this->form_fields['icon']['default'] = plugins_url( 'images/sepa-bancontact/wc-sepa-bancontact.png', Pronamic_WP_Pay_Plugin::$file ); + } + + /** + * Only show gateway if cart or order contains a subscription product. + * + * @since unreleased + */ + public function get_available_payment_gateways( $available_gateways ) { + if ( ! class_exists( 'WC_Subscriptions_Cart' ) || ! function_exists( 'wcs_order_contains_subscription' ) ) { + return $available_gateways; + } + + if ( WC_Subscriptions_Cart::cart_contains_subscription() || ( isset( $_GET['order_id'] ) && wcs_order_contains_subscription( $_GET['order_id'] ) ) ) { + return $available_gateways; + } + + if ( isset( $available_gateways[ self::ID ] ) ) { + unset( $available_gateways[ self::ID ] ); + } + + return $available_gateways; + } +} diff --git a/src/Extension.php b/src/Extension.php index d47493e..2497fe1 100644 --- a/src/Extension.php +++ b/src/Extension.php @@ -82,7 +82,8 @@ public static function payment_gateways( $gateways ) { $gateways[] = 'Pronamic_WP_Pay_Extensions_WooCommerce_KbcGateway'; // @since unreleased - $gateways[] = 'Pronamic_WP_Pay_Extensions_WooCommerce_BunqGateway'; + $gateways[] = 'Pronamic_WP_Pay_Extensions_WooCommerce_BunqGateway'; + $gateways[] = 'Pronamic_WP_Pay_Extensions_WooCommerce_DirectDebitBancontactGateway'; return $gateways; }