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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Customer_Manager {
public function __construct() {
add_action( 'woocommerce_checkout_update_customer', array( $this, 'checkout_update_customer' ), 10, 2 );
add_filter( 'wc_braintree_get_customer_id', array( $this, 'handle_user_migration' ), 10, 3 );
}
public function checkout_update_customer( $customer, $data ) {
if ( $this->should_create_customer( $customer->get_id() ) ) {
$result = $this->create_customer( $customer );
if ( is_wp_error( $result ) ) {
wc_add_notice( sprintf( __( 'Error creating customer in Braintree. Reason: %s', 'woo-payment-gateway' ), $result->get_error_message() ) );
}
} elseif ( $this->should_update_customer( $customer ) ) {
$result = $this->update_customer( $customer );
if ( is_wp_error( $result ) ) {
wc_add_notice( sprintf( __( 'Error updating customer in Braintree. Reason: %s', 'woo-payment-gateway' ), $result->get_error_message() ) );
}
}
}
public function create_customer( $customer ) {
if ( braintree()->gateway() ) {
try {
$args = apply_filters( 'wc_braintree_create_customer_args', $this->get_customer_args( $customer ) );
$response = braintree()->gateway()->customer()->create( $args );
if ( $response->success ) {
wc_braintree_save_customer( $customer->get_id(), $response->customer->id );
return $response->customer->id;
} else {
return new WP_Error( 'customer-error', wc_braintree_errors_from_object( $response ) );
}
} catch ( \Braintree\Exception $e ) {
wc_braintree_log_error( sprintf( 'Error creating Braintree customer. User ID: %s. Exception: %s', $customer->get_id(), get_class( $e ) ) );
return new WP_Error( 'customer-error', sprintf( '%1$s', 'woo-payment-gateway' ), wc_braintree_errors_from_object( $e ) );
}
}
}
public function update_customer( $customer ) {
if ( braintree()->gateway() ) {
try {
$vault_id = wc_braintree_get_customer_id( $customer->get_id() );
$response = braintree()->gateway()->customer()->update( $vault_id, apply_filters( 'wc_braintree_update_customer_args', $this->get_customer_args( $customer ) ) );
if ( ! $response->success ) {
wc_braintree_log_error( sprintf( __( 'Error updating customer %1$s in gateway. Reason: %2$s', 'woo-payment-gateway' ), $customer->get_id(), wc_braintree_errors_from_object( $response ) ) );
throw new Exception( sprintf( '%1$s' ), wc_braintree_errors_from_object( $response ) );
}
return true;
} catch ( \Braintree\Exception $e ) {
wc_braintree_log_error( sprintf( 'Error updating Braintree customer. User ID: %s. Exception: %s', $customer->get_id(), get_class( $e ) ) );
return new WP_Error( 'customer-error', sprintf( '%s' ), wc_braintree_errors_from_object( $e ) );
}
}
}
private function should_create_customer( $user_id ) {
$vault_id = wc_braintree_get_customer_id( $user_id );
return empty( $vault_id ) && $user_id > 0;
}
private function should_update_customer( $customer ) {
$vault_id = wc_braintree_get_customer_id( $customer->get_id() );
if ( $vault_id ) {
$changes = $customer->get_changes();
if ( ! empty( $changes ) && array_intersect_key( $changes, array_flip( $this->get_customer_keys() ) ) ) {
return true;
}
}
return false;
}
private function get_customer_keys() {
return array( 'first_name', 'last_name', 'email' );
}
private function get_customer_args( $customer ) {
return array(
'firstName' => $customer->get_first_name(),
'lastName' => $customer->get_last_name(),
'company' => $customer->get_billing_company(),
'email' => $customer->get_email(),
'phone' => $customer->get_billing_phone(),
);
}
public function handle_user_migration( $customer_id, $user_id, $env ) {
if ( ! $customer_id && $env === 'production' ) {
$user = get_userdata( $user_id );
if ( $user && $user->has_prop( 'wc_braintree_customer_id' ) ) {
$customer_id = $user->get( 'wc_braintree_customer_id' );
if ( $customer_id ) {
wc_braintree_save_customer( $user_id, $customer_id, $env );
}
}
}
return $customer_id;
}
}