-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathclass-wc-stripe-upe-payment-method-bacs-debit.php
114 lines (101 loc) · 4.17 KB
/
class-wc-stripe-upe-payment-method-bacs-debit.php
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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The Bacs Direct Debit Payment Method class extending UPE base class.
*
* @since x.x.x
*/
class WC_Stripe_UPE_Payment_Method_Bacs_Debit extends WC_Stripe_UPE_Payment_Method {
use WC_Stripe_Subscriptions_Trait;
/**
* The Stripe ID for the payment method.
*/
const STRIPE_ID = WC_Stripe_Payment_Methods::BACS_DEBIT;
/**
* Constructor for Bacs Direct Debit payment method.
*/
public function __construct() {
parent::__construct();
$this->stripe_id = self::STRIPE_ID;
$this->title = __( 'Bacs Direct Debit', 'woocommerce-gateway-stripe' );
$this->is_reusable = true;
$this->supported_currencies = [ WC_Stripe_Currency_Code::POUND_STERLING ];
$this->supported_countries = [ 'GB' ];
$this->accept_only_domestic_payment = true;
$this->label = __( 'Bacs Direct Debit', 'woocommerce-gateway-stripe' );
$this->description = __( 'Bacs Direct Debit enables customers in the UK to pay by providing their bank account details.', 'woocommerce-gateway-stripe' );
// Check if subscriptions are enabled and add support for them.
$this->maybe_init_subscriptions();
// Remove Bacs from the “Add Payment Method” page for now, as its implementation will be handled later.
if ( is_wc_endpoint_url( 'add-payment-method' ) ) {
unset( $this->supports['tokenization'] );
}
$this->hide_bacs_for_subscriptions_with_free_trials();
}
/**
* Determines if the Stripe Account country supports Bacs Direct Debit.
*
* @return bool
*/
public function is_available_for_account_country() {
return in_array( WC_Stripe::get_instance()->account->get_account_country(), $this->supported_countries, true );
}
/**
* Returns true if Bacs Direct Debit is available for processing payments.
*
* @return bool
*/
public function is_enabled_at_checkout( $order_id = null, $account_domestic_currency = null ) {
if ( ! WC_Stripe_Feature_Flags::is_bacs_lpm_enabled() ) {
return false;
}
return parent::is_enabled_at_checkout( $order_id, $account_domestic_currency );
}
/**
* Returns a string representing payment method type to query for when retrieving saved payment methods from Stripe.
*
* @return string The payment method type.
*/
public function get_retrievable_type() {
return $this->get_id();
}
/**
* Creates a Bacs Direct Debit payment token for the customer.
*
* @param int $user_id The customer ID the payment token is associated with.
* @param stdClass $payment_method The payment method object.
*
* @return WC_Payment_Token The payment token created.
*/
public function create_payment_token_for_user( $user_id, $payment_method ) {
$token = new WC_Payment_Token_Bacs_Debit();
$token->set_token( $payment_method->id );
$token->set_gateway_id( WC_Stripe_Payment_Tokens::UPE_REUSABLE_GATEWAYS_BY_PAYMENT_METHOD[ self::STRIPE_ID ] );
$token->set_last4( $payment_method->bacs_debit->last4 );
$token->set_fingerprint( $payment_method->bacs_debit->fingerprint );
$token->set_payment_method_type( $this->get_id() );
$token->set_user_id( $user_id );
$token->save();
return $token;
}
public function hide_bacs_for_subscriptions_with_free_trials() {
add_filter(
'woocommerce_available_payment_gateways',
function ( $available_gateways ) {
global $post;
$is_checkout_shortcode_page = wc_post_content_has_shortcode( 'woocommerce_checkout' ) || has_block( 'woocommerce/classic-shortcode', $post );
$is_update_order_review_ajax_request = defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['wc-ajax'] ) && 'update_order_review' === $_REQUEST['wc-ajax'];
if ( $is_checkout_shortcode_page || $is_update_order_review_ajax_request ) {
// Checking if the amount is zero allows us to process orders that include subscriptions with a free trial,
// as long as another product increases the total amount, ensuring compatibility with Bacs.
if ( WC_Subscriptions_Cart::cart_contains_free_trial() && (float) WC()->cart->total === 0.00 ) {
unset( $available_gateways['stripe_bacs_debit'] );
}
}
return $available_gateways;
}
);
}
}