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
<?php
defined( 'ABSPATH' ) || exit();
use \PaymentPlugins\WC_Braintree_Constants as Constants;
class WC_Braintree_Payment_Method_Conversion {
public static function init() {
add_action( 'init', array( __CLASS__, 'update_user_payment_tokens' ), 100 );
add_filter( 'woocommerce_order_get_payment_method', array( __CLASS__, 'get_new_payment_method' ), 10, 2 );
add_filter( 'woocommerce_subscription_get_payment_method', array( __CLASS__, 'get_new_payment_method' ), 10, 2 );
}
public static function update_user_payment_tokens( $user_id = '' ) {
if ( is_user_logged_in() && did_action( 'woocommerce_loaded' ) ) {
$user_id = empty( $user_id ) ? get_current_user_id() : $user_id;
self::sync_payment_method_tokens( $user_id, wc_braintree_environment() );
}
}
public static function sync_payment_method_tokens( $user_id, $env ) {
$customer_id = wc_braintree_get_customer_id( $user_id, $env );
$next_update = get_user_meta( $user_id, Constants::TOKEN_CHECK, true );
if ( $customer_id && ( $next_update < time() ) ) {
$payment_gateways = WC()->payment_gateways()->payment_gateways();
try {
$gateway = new \Braintree\Gateway( wc_braintree_connection_settings() );
$customer = $gateway->customer()->find( $customer_id );
$payment_methods = $customer->paymentMethods;
if ( wcs_braintree_active() ) {
remove_action( 'woocommerce_payment_token_set_default', 'WCS_My_Account_Payment_Methods::display_default_payment_token_change_notice' );
}
foreach ( $payment_methods as $payment_method ) {
$gateway_id = '';
if ( $payment_method instanceof \Braintree\CreditCard ) {
$gateway_id = Constants::BRAINTREE_CC;
}
if ( $payment_method instanceof \Braintree\PayPalAccount ) {
$gateway_id = Constants::BRAINTREE_PAYPAL;
}
if ( $payment_method instanceof \Braintree\ApplePayCard ) {
$gateway_id = Constants::BRAINTREE_APPLEPAY;
}
if ( $payment_method instanceof \Braintree\AndroidPayCard ) {
$gateway_id = Constants::BRAINTREE_GOOGLEPAY;
}
if ( $payment_method instanceof \Braintree\VenmoAccount ) {
$gateway_id = Constants::BRAINTREE_VENMO;
}
$wc_gateway = isset( $payment_gateways[ $gateway_id ] ) ? $payment_gateways[ $gateway_id ] : null;
if ( $wc_gateway && ! $wc_gateway->token_exists( $payment_method->token, $user_id ) ) {
$token = $wc_gateway->get_payment_token( $payment_method );
$token->set_user_id( $user_id );
$token->set_environment( $env );
$token->save();
}
}
} catch ( \Braintree\Exception $e ) {
wc_braintree_log_error( sprintf( __( 'Error comparing payment methods in Braintree with WordPress. User ID: %1$s. Exception: %2$s', 'woo-payment-gateway' ), $user_id, get_class( $e ) ) );
} catch ( Exception $e ) {
wc_braintree_log_error( sprintf( __( 'Error comparing payment methods in Braintree with WordPress. User ID: %1$s. Exception: %2$s', 'woo-payment-gateway' ), $user_id, get_class( $e ) ) );
}
update_user_meta( $user_id, 'wc_braintree_token_check', self::get_next_update() );
}
}
private static function get_next_update() {
return apply_filters( 'wc_braintree_next_token_update', time() + ( MINUTE_IN_SECONDS * 60 * 24 * 14 ) );
}
public static function get_new_payment_method( $payment_method, $order ) {
$new_method = '';
switch ( $payment_method ) {
case 'braintree_payment_gateway':
case 'braintree_credit_card':
$new_method = Constants::BRAINTREE_CC;
break;
case 'braintree_paypal_payments':
case 'braintree_paypal_credit_payments':
$new_method = Constants::BRAINTREE_PAYPAL;
break;
case 'braintree_googlepay_gateway':
$new_method = Constants::BRAINTREE_GOOGLEPAY;
break;
case 'braintree_applepay_payments':
$new_method = Constants::BRAINTREE_APPLEPAY;
break;
}
if ( ! empty( $new_method ) ) {
$payment_method = $new_method;
update_post_meta( $order->get_id(), '_payment_method', $new_method );
}
return $payment_method;
}
}
WC_Braintree_Payment_Method_Conversion::init();