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
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Admin_User_Edit {
public static function init() {
add_action( 'edit_user_profile', array( __CLASS__, 'output' ) );
add_action( 'show_user_profile', array( __CLASS__, 'output' ) );
add_action( 'edit_user_profile_update', array( __CLASS__, 'save' ) );
add_action( 'personal_options_update', array( __CLASS__, 'save' ) );
}
public static function output( $user ) {
global $wpdb;
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
$environments = array( 'sandbox', 'production' );
$sandbox_tokens = array();
$production_tokens = array();
foreach ( $environments as $environment ) {
${"{$environment}_tokens"} = self::get_payment_tokens( $user->ID, $environment );
}
include braintree()->plugin_path() . 'includes/admin/views/user-edit.php';
}
public static function save( $user_id ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
global $wpdb;
$old_ids = array(
'production' => wc_braintree_get_customer_id( $user_id, 'production' ),
'sandbox' => wc_braintree_get_customer_id( $user_id, 'sandbox' ),
);
if ( isset( $_POST['wc_braintree_production_vault_id'] ) ) {
wc_braintree_delete_customer( $user_id, 'production', true );
wc_braintree_save_customer( $user_id, wc_clean( $_POST['wc_braintree_production_vault_id'] ), 'production' );
}
if ( isset( $_POST['wc_braintree_sandbox_vault_id'] ) ) {
wc_braintree_delete_customer( $user_id, 'sandbox', true );
wc_braintree_save_customer( $user_id, wc_clean( $_POST['wc_braintree_sandbox_vault_id'] ), 'sandbox' );
}
remove_action( 'woocommerce_payment_token_deleted', 'wc_braintree_woocommerce_payment_token_deleted' );
WC()->payment_gateways();
foreach ( $old_ids as $environment => $vault_id ) {
if ( $vault_id !== wc_braintree_get_customer_id( $user_id, $environment ) ) {
$results = self::get_payment_tokens( $user_id, $environment );
if ( $results ) {
foreach ( $results as $token ) {
WC_Payment_Tokens::delete( $token->get_id() );
}
}
try {
if ( ( $gateway = braintree()->gateway( $environment ) ) !== null ) {
$customer = $gateway->customer()->find( wc_braintree_get_customer_id( $user_id, $environment ) );
foreach ( $customer->paymentMethods as $payment_method ) {
$gateway_id = '';
if ( $payment_method instanceof \Braintree\CreditCard ) {
$gateway_id = 'braintree_cc';
}
if ( $payment_method instanceof \Braintree\PayPalAccount ) {
$gateway_id = 'braintree_paypal';
}
if ( $payment_method instanceof \Braintree\AndroidPayCard ) {
$gateway_id = 'braintree_googlepay';
}
if ( $payment_method instanceof \Braintree\ApplePayCard ) {
$gateway_id = 'braintree_applepay';
}
if ( $payment_method instanceof \Braintree\VenmoAccount ) {
$gateway_id = 'braintree_venmo';
}
$wc_gateway = WC()->payment_gateways()->payment_gateways()[ $gateway_id ];
$token = $wc_gateway->get_payment_token( $payment_method );
$token->set_user_id( $user_id );
$token->save();
}
}
} catch ( \Braintree\Exception $e ) {
wc_braintree_log_error( sprintf( __( 'Error saving customer\'s payment methods on User Profile page. Reason: %1$s', 'woo-payment-gateway' ), wc_braintree_errors_from_object( $e ) ) );
} catch ( Exception $e ) {
wc_braintree_log_error( sprintf( __( 'Error saving customer\'s payment methods on User Profile page. Reason: %1$s', 'woo-payment-gateway' ), $e->getMessage() ) );
}
}
}
}
public static function get_payment_tokens( $user_id, $environment ) {
return wc_braintree_get_payment_tokens( $user_id, $environment );
}
}
WC_Braintree_Admin_User_Edit::init();