· 9 years ago · Dec 22, 2016, 04:56 PM
1<?php
2// Function to add a new card for your customer.
3function sd_process_add_customer_card(){
4 $redirect = false;
5
6 if ( isset( $_POST['action'] ) && $_POST['action'] == 'add_customer_card' && wp_verify_nonce( $_POST['stripe_nonce'], 'stripe-nonce' ) ){
7 if ( !isset( $_POST['card_number'] ) || !isset( $_POST['card_cvc'] ) || !isset( $_POST['card_exp_month'] ) || !isset( $_POST['card_exp_year'] ) ){
8 $redirect = add_query_arg( array(
9 'card' => 'not-created',
10 ), $_POST['redirect'] );
11 } else {
12 global $stripe_options; // this variable includes your Stripe API keys and whether or not you are using test mode
13 if ( !class_exists( 'Stripe' ) )
14 require_once( //PATH TO Stripe.php );
15
16 // check if we are using test mode
17 if ( isset( $stripe_options['test_mode'] ) && $stripe_options['test_mode'] ) {
18 $secret_key = trim( $stripe_options['test_secret_key'] );
19 } else {
20 $secret_key = trim( $stripe_options['live_secret_key'] );
21 }
22
23 \Stripe\Stripe::setApiKey( $secret_key );
24
25 $card = \Stripe\Token::create(array(
26 'card' => array(
27 'number' => $_POST['card_number'],
28 'exp_month' => $_POST['card_exp_month'],
29 'exp_year' => $_POST['card_exp_year'],
30 'cvc' => $_POST['card_cvc'],
31 )
32 ));
33 if ( $card ){
34 $customer = \Stripe\Customer::retrieve( $_POST['customer_id'] );
35 $customer->sources->create(array(
36 'source' => $card
37 ));
38 $redirect = add_query_arg( array(
39 'card' => 'added',
40 ), $_POST['redirect'] );
41 }
42 }
43 if ($redirect) {
44 wp_redirect( $redirect ); exit;
45 }
46 }
47}
48add_action( 'init', 'sd_process_add_customer_card' );
49
50function sd_form_add_new_card(){
51 if( !is_user_logged_in() ){
52 return false;
53 } else {
54 // retrieve customer id from user meta if it exists
55 $customer_id = get_user_meta( get_current_user_id(), '_stripe_customer_id', true );
56 // if customer id doesn't exist create a new customer
57 $stripe_customer_id = $customer_id ? $customer_id : sd_create_customer();
58 $display = '<div id="add-new-card">' . __( 'Add a New Card' ) . '</div>
59 <form action="" method="POST" id="add-customer-card">
60 <label>' . __('Card Number') . '</label>
61 <input type="text" size="20" autocomplete="off" name="card_number" class="card-number"/>
62 <label>' . __('CVC') . '</label>
63 <input type="text" size="4" autocomplete="off" name="card_cvc" class="card-cvc"/>
64 <label>' . __('Expiration (MM/YYYY)') . '</label>
65 <input type="text" size="2" name="card_exp_month" class="card-expiry-month"/>
66 <span> / </span>
67 <input type="text" size="4" name="card_exp_year" class="card-expiry-year"/>
68 <input type="hidden" name="redirect" value="' . get_permalink() . '"/>
69 <input type="hidden" name="stripe_nonce" value="' . wp_create_nonce('stripe-nonce') . '"/>
70 <input type="hidden" name="action" value="add_customer_card" />
71 <input type="hidden" name="customer_id" value="' . $stripe_customer_id . '" />
72 <button type="submit" id="stripe-submit">' . __('Add Card') . '</button>
73 </form>';
74 return $display;
75 }
76 }
77}
78add_shortcode( 'add_customer_card', 'sd_form_add_new_card' );
79
80function sd_create_customer(){
81 global $stripe_options; // this variable includes your Stripe API keys and whether or not you are using test mode
82 if ( !class_exists( 'Stripe' ) )
83 require_once( //PATH TO Stripe.php );
84
85 // check if we are using test mode
86 if ( isset( $stripe_options['test_mode'] ) && $stripe_options['test_mode'] ) {
87 $secret_key = trim( $stripe_options['test_secret_key'] );
88 } else {
89 $secret_key = trim( $stripe_options['live_secret_key'] );
90 }
91
92 \Stripe\Stripe::setApiKey( $secret_key );
93 // create a brand new customer
94 $customer = \Stripe\Customer::create( array(
95 'description' => 'Some description for my customer',
96 )
97 );
98 if ( is_user_logged_in () ) {
99 update_user_meta( get_current_user_id(), '_stripe_customer_id', $customer->id );
100 }
101 return $customer->id;
102}