class-wc-gateway-stripe-saved-cards.php
3.29 KB
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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Gateway_Stripe_Saved_Cards class.
*/
class WC_Gateway_Stripe_Saved_Cards {
/**
* Constructor
*/
public function __construct() {
add_action( 'wp', array( $this, 'delete_card' ) );
add_action( 'wp', array( $this, 'default_card' ) );
add_action( 'woocommerce_after_my_account', array( $this, 'output' ) );
add_action( 'woocommerce_init', array( $this, 'add_notice' ) );
}
/**
* Adds notice when default card is saved
*
*/
public function add_notice() {
if ( ! empty( $_POST['stripe_default_card'] ) ) {
wc_add_notice( __( 'Default card saved', 'woocommerce-gateway-stripe' ) );
}
return true;
}
/**
* Display saved cards
*/
public function output() {
if ( ! is_user_logged_in() || ( ! $customer_id = get_user_meta( get_current_user_id(), '_stripe_customer_id', true ) ) || ! is_string( $customer_id ) ) {
return;
}
$stripe = new WC_Gateway_Stripe();
$cards = $stripe->get_saved_cards( $customer_id );
$default_card = get_user_meta( get_current_user_id(), '_wc_stripe_default_card', true );
if ( $cards ) {
wc_get_template( 'saved-cards.php', array( 'cards' => $cards, 'default_card' => $default_card ), 'woocommerce-gateway-stripe/', WC_STRIPE_TEMPLATE_PATH );
}
}
/**
* Delete a card
*/
public function delete_card() {
if ( ! isset( $_POST['stripe_delete_card'] ) || ! is_account_page() ) {
return;
}
if ( ! is_user_logged_in() || ( ! $customer_id = get_user_meta( get_current_user_id(), '_stripe_customer_id', true ) ) || ! is_string( $customer_id ) || ! wp_verify_nonce( $_POST['_wpnonce'], "stripe_del_card" ) ) {
wp_die( __( 'Unable to verify deletion, please try again', 'woocommerce-gateway-stripe' ) );
}
$stripe = new WC_Gateway_Stripe();
$result = $stripe->stripe_request( array(), 'customers/' . $customer_id . '/sources/' . sanitize_text_field( $_POST['stripe_delete_card'] ), 'DELETE' );
delete_transient( 'stripe_cards_' . $customer_id );
do_action( 'wc_stripe_delete_card', $customer_id, $result );
if ( is_wp_error( $result ) ) {
wc_add_notice( __( 'Unable to delete card.', 'woocommerce-gateway-stripe' ), 'error' );
} else {
wc_add_notice( __( 'Card deleted.', 'woocommerce-gateway-stripe' ), 'success' );
}
wp_safe_redirect( apply_filters( 'wc_stripe_manage_saved_cards_url', get_permalink( woocommerce_get_page_id( 'myaccount' ) ) ) );
exit;
}
/**
* Make a card as default method
*/
public function default_card() {
if ( ! isset( $_POST['stripe_default_card'] ) || ! is_account_page() ) {
return;
}
if ( ! is_user_logged_in() || ( ! $customer_id = get_user_meta( get_current_user_id(), '_stripe_customer_id', true ) ) || ! is_string( $customer_id ) || ! wp_verify_nonce( $_POST['_wpnonce'], "stripe_default_card" ) ) {
wp_die( __( 'Unable to make default card, please try again', 'woocommerce-gateway-stripe' ) );
}
do_action( 'wc_stripe_default_card', $customer_id, sanitize_text_field( $_POST['stripe_default_card'] ) );
update_user_meta( get_current_user_id(), '_wc_stripe_default_card', sanitize_text_field( $_POST['stripe_default_card'] ) );
wp_safe_redirect( apply_filters( 'wc_stripe_manage_saved_cards_url', get_permalink( woocommerce_get_page_id( 'myaccount' ) ) ) );
exit;
}
}
new WC_Gateway_Stripe_Saved_Cards();