Skip to content

Commit

Permalink
Hook up the tokenization API to the "add payment method" button in my…
Browse files Browse the repository at this point in the history
… account settings.
  • Loading branch information
justinshreve committed Mar 1, 2016
1 parent 9db1f65 commit 31bae0c
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 50 deletions.
4 changes: 1 addition & 3 deletions includes/class-wc-form-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,15 @@ public static function add_payment_method_action() {
$payment_method = wc_clean( $_POST['payment_method'] );

$available_gateways = WC()->payment_gateways->get_available_payment_gateways();

// Validate
$available_gateways[ $payment_method ]->validate_fields();

// Process
if ( wc_notice_count( 'wc_errors' ) == 0 ) {
$result = $available_gateways[ $payment_method ]->add_payment_method();

// Redirect to success/confirmation/payment page
if ( $result['result'] == 'success' ) {
wc_add_message( __( 'Payment method added.', 'woocommerce' ) );
wc_add_notice( __( 'Payment method added.', 'woocommerce' ) );
wp_redirect( $result['redirect'] );
exit();
}
Expand Down
9 changes: 5 additions & 4 deletions includes/class-wc-payment-gateway-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public function __construct( $gateway ) {
$this->gateway = $gateway;
$gateway_id = $this->gateway->id;

if ( is_user_logged_in() && $this->gateway->supports( 'tokenization' ) ) {
if ( is_user_logged_in() && $this->gateway->supports( 'tokenization' ) && is_checkout() ) {
$this->tokens = $this->filter_tokens_by_gateway(
$gateway_id,
WC_Payment_Tokens::get_customer_tokens( get_current_user_id() )
);
$this->saved_payment_methods();
}

if ( $this->gateway->supports( 'tokenization' ) ) {
if ( $this->gateway->supports( 'tokenization' ) && is_checkout() ) {
$this->use_new_payment_method_checkbox();
}

Expand Down Expand Up @@ -124,12 +124,12 @@ public function saved_payment_method_title( $token ) {
public function payment_fields() {
if ( $this->gateway->supports( 'credit_card_form' ) ) {
echo $this->credit_card_payment_fields();
if ( $this->gateway->supports( 'tokenization' ) ) {
if ( $this->gateway->supports( 'tokenization' ) && is_checkout() ) {
$this->save_payment_method_checkbox();
}
} else if ( $this->gateway->supports( 'echeck_form' ) ) {
echo $this->echeck_payment_fields();
if ( $this->gateway->supports( 'tokenization' ) ) {
if ( $this->gateway->supports( 'tokenization' ) && is_checkout() ) {
$this->save_payment_method_checkbox();
}
} else {
Expand Down Expand Up @@ -284,6 +284,7 @@ public function load_scripts() {
'gatewayID' => $this->gateway->id,
'userLoggedIn' => (bool) is_user_logged_in(),
) );

}

}
4 changes: 3 additions & 1 deletion includes/class-wc-payment-gateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public function get_available_payment_gateways() {
if ( $gateway->is_available() ) {
if ( ! is_add_payment_method_page() ) {
$_available_gateways[ $gateway->id ] = $gateway;
} elseif( $gateway->supports( 'add_payment_method' ) ) {
} else if( $gateway->supports( 'add_payment_method' ) ) {
$_available_gateways[ $gateway->id ] = $gateway;
} else if ( $gateway->supports( 'tokenization' ) ) {
$_available_gateways[ $gateway->id ] = $gateway;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

// Form handler
function simplifyFormHandler() {
var $form = $( 'form.checkout, form#order_review' );
var $form = $( 'form.checkout, form#order_review, orm#add_payment_method' );

if ( $( '#payment_method_simplify_commerce' ).is( ':checked' ) && $( '#wc-simplify_commerce-new' ).is( ':checked' ) ) {
if ( ( $( '#payment_method_simplify_commerce' ).is( ':checked' ) && $( '#wc-simplify_commerce-new' ).is( ':checked' ) ) || ( '1' === $( '#woocommerce_add_payment_method' ).val() ) ) {

if ( 0 === $( 'input.simplify-token' ).length ) {

Expand Down Expand Up @@ -56,7 +56,7 @@
// Handle Simplify response
function simplifyResponseHandler( data ) {

var $form = $( 'form.checkout, form#order_review' ),
var $form = $( 'form.checkout, form#order_review, form#add_payment_method' ),
ccForm = $( '#wc-simplify_commerce-cc-form' );

if ( data.error ) {
Expand Down Expand Up @@ -102,8 +102,13 @@
return simplifyFormHandler();
});

/* Pay Page Form */
$( 'form#add_payment_method' ).on( 'submit', function () {
return simplifyFormHandler();
});

/* Both Forms */
$( 'form.checkout, form#order_review' ).on( 'change', '#wc-simplify_commerce-cc-form input', function() {
$( 'form.checkout, form#order_review, form#add_payment_method' ).on( 'change', '#wc-simplify_commerce-cc-form input', function() {
$( '.simplify-token' ).remove();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,16 @@ public function payment_fields() {
* Outputs scripts used for simplify payment.
*/
public function payment_scripts() {
if ( ! is_checkout() || ! $this->is_available() ) {
$load_scripts = false;

if ( is_checkout() ) {
$load_scripts = true;
}
if ( $this->is_available() ) {
$load_scripts = true;
}

if ( false === $load_scripts ) {
return;
}

Expand All @@ -302,6 +311,77 @@ public function payment_scripts() {
) );
}

public function add_payment_method() {
if ( empty ( $_POST['simplify_token'] ) ) {
wc_add_notice( __( 'There was a problem adding this card.', 'woocommerce' ), 'error' );
return;
}

$cart_token = wc_clean( $_POST['simplify_token'] );
$customer_token = $this->get_users_token();
$current_user = wp_get_current_user();
$customer_info = array(
'email' => $current_user->user_email,
'name' => $current_user->display_name,
);

$token = $this->save_token( $customer_token, $cart_token, $customer_info );
if ( is_null( $token ) ) {
wc_add_notice( __( 'There was a problem adding this card.', 'woocommerce' ), 'error' );
return;
}

return array(
'result' => 'success',
'redirect' => wc_get_endpoint_url( 'payment-methods' ),
);
}

/**
* Actualy saves a customer token to the database.
*
* @param WC_Payment_Token $customer_token Payment Token
* @param string $cart_token CC Token
* @param array $customer_info 'email', 'name'
*/
public function save_token( $customer_token, $cart_token, $customer_info ) {
if ( ! is_null( $customer_token ) ) {
$customer = Simplify_Customer::findCustomer( $customer_token->get_token() );
$updates = array( 'token' => $cart_token );
$customer->setAll( $updates );
$customer->updateCustomer();
$customer = Simplify_Customer::findCustomer( $customer_token->get_token() ); // get updated customer with new set card
$token = $customer_token;
} else {
$customer = Simplify_Customer::createCustomer( array(
'token' => $cart_token,
'email' => $customer_info['email'],
'name' => $customer_info['name'],
) );
$token = new WC_Payment_Token_CC();
$token->set_token( $customer->id );
}

// If we were able to create an save our card, save the data on our side too
if ( is_object( $customer ) && '' != $customer->id ) {
$customer_properties = $customer->getProperties();
$card = $customer_properties['card'];
$token->set_gateway_id( $this->id );
$token->set_card_type( strtolower( $card->type ) );
$token->set_last4( $card->last4 );
$expiry_month = ( 1 === strlen( $card->expMonth ) ? '0' . $card->expMonth : $card->expMonth );
$token->set_expiry_month( $expiry_month );
$token->set_expiry_year( '20' . $card->expYear );
if ( is_user_logged_in() ) {
$token->set_user_id( get_current_user_id() );
}
$token->save();
return $token;
}

return null;
}

/**
* Process customer: updating or creating a new customer/saved CC
*
Expand All @@ -312,39 +392,12 @@ public function payment_scripts() {
protected function process_customer( $order, $customer_token = null, $cart_token = '' ) {
// Are we saving a new payment method?
if ( is_user_logged_in() && isset( $_POST['wc-simplify_commerce-new-payment-method'] ) && true === (bool) $_POST['wc-simplify_commerce-new-payment-method'] ) {
// Update or Create a new Customer on Simplify and start building a WC token object
if ( ! is_null( $customer_token ) ) {
$customer = Simplify_Customer::findCustomer( $customer_token->get_token() );
$updates = array( 'token' => $cart_token );
$customer->setAll( $updates );
$customer->updateCustomer();
$customer = Simplify_Customer::findCustomer( $customer_token->get_token() ); // get updated customer with new set card
$token = $customer_token;
} else {
$customer = Simplify_Customer::createCustomer( array(
'token' => $cart_token,
'email' => $order->billing_email,
'name' => trim( $order->get_formatted_billing_full_name() ),
'reference' => $order->id,
) );
$token = new WC_Payment_Token_CC();
$token->set_token( $customer->id );
}

// If we were able to create an save our card, save the data on our side too
if ( is_object( $customer ) && '' != $customer->id ) {
$customer_properties = $customer->getProperties();
$card = $customer_properties['card'];
$token->set_gateway_id( $this->id );
$token->set_card_type( strtolower( $card->type ) );
$token->set_last4( $card->last4 );
$expiry_month = ( 1 === strlen( $card->expMonth ) ? '0' . $card->expMonth : $card->expMonth );
$token->set_expiry_month( $expiry_month );
$token->set_expiry_year( '20' . $card->expYear );
if ( is_user_logged_in() ) {
$token->set_user_id( get_current_user_id() );
}
$token->save();
$customer_info = array(
'email' => $order->billing_email,
'name' => trim( $order->get_formatted_billing_full_name() ),
);
$token = $this->save_token( $customer_token, $cart_token, $customer_info );
if ( ! is_null( $token ) ) {
$order->add_payment_token( $token );
}
}
Expand Down Expand Up @@ -524,7 +577,7 @@ public function process_payment( $order_id ) {
return $this->process_hosted_payments( $order );
}

// Don't create a customer - this user is logged out or did not create an account/save a payment method
// New CC info was entered
if ( isset( $_POST['simplify_token'] ) ) {
$cart_token = wc_clean( $_POST['simplify_token'] );
$customer_token = $this->get_users_token();
Expand All @@ -533,7 +586,7 @@ public function process_payment( $order_id ) {
return $this->process_standard_payments( $order, $cart_token, $customer_token_value );
}

// Create (or update) customer, save payment token, and then process the payment
// Possibly Create (or update) customer/save payment token, use an existing token, and then process the payment
if ( isset( $_POST['wc-simplify_commerce-payment-token'] ) && 'new' !== $_POST['wc-simplify_commerce-payment-token'] ) {
$token_id = wc_clean( $_POST['wc-simplify_commerce-payment-token'] );
$token = WC_Payment_Tokens::get( $token_id );
Expand Down
2 changes: 2 additions & 0 deletions includes/shortcodes/class-wc-shortcode-my-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ public static function delete_payment_method( $id ) {
}

WC_Payment_Tokens::delete( $id );
wc_add_notice( __( 'Payment method deleted.', 'woocommerce' ) );
woocommerce_account_payment_methods();
}

Expand Down Expand Up @@ -404,6 +405,7 @@ public static function set_default_payment_method( $id ) {
}

WC_Payment_Tokens::set_users_default( $token->get_user_id(), intval( $id ) );
wc_add_notice( __( 'This payment method was successfully set as your default.', 'woocommerce' ) );
woocommerce_account_payment_methods();
}

Expand Down
2 changes: 1 addition & 1 deletion templates/myaccount/form-add-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<div class="form-row">
<?php wp_nonce_field( 'woocommerce-add-payment-method' ); ?>
<input type="submit" class="button alt" id="place_order" value="<?php esc_attr_e( 'Add Payment Method', 'woocommerce' ); ?>" />
<input type="hidden" name="woocommerce_add_payment_method" value="1" />
<input type="hidden" name="woocommerce_add_payment_method" id="woocommerce_add_payment_method" value="1" />
</div>
</div>
</form>
Expand Down
2 changes: 1 addition & 1 deletion templates/myaccount/payment-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

<?php else : ?>

<p><?php esc_html_e( 'No saved method found.', 'woocommerce' ); ?></p>
<p><?php esc_html_e( 'No saved methods found.', 'woocommerce' ); ?></p>

<?php endif; ?>

Expand Down

0 comments on commit 31bae0c

Please sign in to comment.