Last active
January 25, 2024 18:13
-
-
Save ScottDeLuzio/72cd4e660df7e1c39b133a2acb5be717 to your computer and use it in GitHub Desktop.
Add card to customer account in Stripe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Function to add a new card for your customer. | |
function sd_process_add_customer_card(){ | |
$redirect = false; | |
if ( isset( $_POST['action'] ) && $_POST['action'] == 'add_customer_card' && wp_verify_nonce( $_POST['stripe_nonce'], 'stripe-nonce' ) ){ | |
if ( !isset( $_POST['card_number'] ) || !isset( $_POST['card_cvc'] ) || !isset( $_POST['card_exp_month'] ) || !isset( $_POST['card_exp_year'] ) ){ | |
$redirect = add_query_arg( array( | |
'card' => 'not-created', | |
), $_POST['redirect'] ); | |
} else { | |
global $stripe_options; // this variable includes your Stripe API keys and whether or not you are using test mode | |
if ( !class_exists( 'Stripe' ) ) | |
require_once( //PATH TO Stripe.php ); | |
// check if we are using test mode | |
if ( isset( $stripe_options['test_mode'] ) && $stripe_options['test_mode'] ) { | |
$secret_key = trim( $stripe_options['test_secret_key'] ); | |
} else { | |
$secret_key = trim( $stripe_options['live_secret_key'] ); | |
} | |
\Stripe\Stripe::setApiKey( $secret_key ); | |
$card = \Stripe\Token::create(array( | |
'card' => array( | |
'number' => $_POST['card_number'], | |
'exp_month' => $_POST['card_exp_month'], | |
'exp_year' => $_POST['card_exp_year'], | |
'cvc' => $_POST['card_cvc'], | |
) | |
)); | |
if ( $card ){ | |
$customer = \Stripe\Customer::retrieve( $_POST['customer_id'] ); | |
$customer->sources->create(array( | |
'source' => $card | |
)); | |
$redirect = add_query_arg( array( | |
'card' => 'added', | |
), $_POST['redirect'] ); | |
} | |
} | |
if ($redirect) { | |
wp_redirect( $redirect ); exit; | |
} | |
} | |
} | |
add_action( 'init', 'sd_process_add_customer_card' ); | |
function sd_form_add_new_card(){ | |
if( !is_user_logged_in() ){ | |
return false; | |
} else { | |
// retrieve customer id from user meta if it exists | |
$customer_id = get_user_meta( get_current_user_id(), '_stripe_customer_id', true ); | |
// if customer id doesn't exist create a new customer | |
$stripe_customer_id = $customer_id ? $customer_id : sd_create_customer(); | |
$display = '<div id="add-new-card">' . __( 'Add a New Card' ) . '</div> | |
<form action="" method="POST" id="add-customer-card"> | |
<label>' . __('Card Number') . '</label> | |
<input type="text" size="20" autocomplete="off" name="card_number" class="card-number"/> | |
<label>' . __('CVC') . '</label> | |
<input type="text" size="4" autocomplete="off" name="card_cvc" class="card-cvc"/> | |
<label>' . __('Expiration (MM/YYYY)') . '</label> | |
<input type="text" size="2" name="card_exp_month" class="card-expiry-month"/> | |
<span> / </span> | |
<input type="text" size="4" name="card_exp_year" class="card-expiry-year"/> | |
<input type="hidden" name="redirect" value="' . get_permalink() . '"/> | |
<input type="hidden" name="stripe_nonce" value="' . wp_create_nonce('stripe-nonce') . '"/> | |
<input type="hidden" name="action" value="add_customer_card" /> | |
<input type="hidden" name="customer_id" value="' . $stripe_customer_id . '" /> | |
<button type="submit" id="stripe-submit">' . __('Add Card') . '</button> | |
</form>'; | |
return $display; | |
} | |
} | |
} | |
add_shortcode( 'add_customer_card', 'sd_form_add_new_card' ); | |
function sd_create_customer(){ | |
global $stripe_options; // this variable includes your Stripe API keys and whether or not you are using test mode | |
if ( !class_exists( 'Stripe' ) ) | |
require_once( //PATH TO Stripe.php ); | |
// check if we are using test mode | |
if ( isset( $stripe_options['test_mode'] ) && $stripe_options['test_mode'] ) { | |
$secret_key = trim( $stripe_options['test_secret_key'] ); | |
} else { | |
$secret_key = trim( $stripe_options['live_secret_key'] ); | |
} | |
\Stripe\Stripe::setApiKey( $secret_key ); | |
// create a brand new customer | |
$customer = \Stripe\Customer::create( array( | |
'description' => 'Some description for my customer', | |
) | |
); | |
if ( is_user_logged_in () ) { | |
update_user_meta( get_current_user_id(), '_stripe_customer_id', $customer->id ); | |
} | |
return $customer->id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment