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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
<?php
defined( 'ABSPATH' ) || exit();
require_once( WC_STRIPE_PLUGIN_FILE_PATH . 'includes/traits/wc-stripe-payment-token-traits.php' );
abstract class WC_Payment_Token_Stripe extends WC_Payment_Token {
protected $has_expiration = false;
protected $object_type = 'payment_token';
protected $stripe_payment_type;
protected $extra_data = array(
'format' => '',
'environment' => 'production',
'brand' => '',
'customer_id' => '',
);
protected $stripe_data = array();
protected $format = '';
public function __construct( $token = '' ) {
$this->extra_data = array_merge( $this->extra_data, $this->get_stripe_data( $this ) );
parent::__construct( $token );
}
public abstract function details_to_props( $details );
public function set_format( $value ) {
$this->format = $value;
}
public function set_environment( $value ) {
$this->set_prop( 'environment', $value );
}
public function set_customer_id( $value ) {
$this->set_prop( 'customer_id', $value );
}
public function get_format() {
return $this->format;
}
public function get_environment() {
return $this->get_prop( 'environment' );
}
public function get_customer_id() {
return $this->get_prop( 'customer_id' );
}
public function get_payment_method_title( $format = '' ) {
$format = empty( $format ) ? $this->get_format() : $format;
$format = $this->get_formats()[ $format ]['format'];
$data = $this->get_props_data();
return apply_filters( 'wc_stripe_payment_method_title', str_replace( array_keys( $data ), $data, $format ), $this );
}
public function get_props_data() {
$data = array();
foreach ( $this->extra_data as $k => $v ) {
if ( method_exists( $this, "get_{$k}" ) ) {
$data[ '{' . $k . '}' ] = $this->{"get_$k"}();
} else {
$data[ '{' . $k . '}' ] = $this->get_prop( $k );
}
}
$data['{short_title}'] = $this->get_basic_payment_method_title();
return $data;
}
public function get_stripe_data( $instance ) {
$data = array();
try {
$class = new ReflectionClass( $instance );
$props = $class->getDefaultProperties();
if ( isset( $props['stripe_data'] ) ) {
$data = $props['stripe_data'];
}
if ( is_subclass_of( get_parent_class( $instance ), 'WC_Payment_Token_Stripe' ) ) {
$data = array_merge( $this->get_stripe_data( get_parent_class( $instance ) ), $data );
}
return $data;
} catch ( Exception $e ) {
return array();
}
}
public function to_json() {
return apply_filters(
'wc_stripe_get_' . $this->object_type . '_json',
array(
'id' => $this->get_id(),
'type' => $this->get_type(),
'token' => $this->get_token(),
'title' => $this->get_payment_method_title(),
'gateway' => $this->get_gateway_id(),
)
);
}
public abstract function get_formats();
public function get_brand( $context = 'view' ) {
return wc_get_credit_card_type_label( $this->get_prop( 'brand', $context ) );
}
public function set_brand( $value ) {
$this->set_prop( 'brand', $value );
}
public function get_html_classes() {
return '';
}
public function has_expiration() {
return $this->has_expiration;
}
public abstract function delete_from_stripe();
public abstract function save_payment_method();
public static function token_exists( $token_id, $user_id ) {
global $wpdb;
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token = %s AND gateway_id LIKE %s AND user_id = %d", $token_id, '%stripe_%', $user_id ) );
return $count > 0;
}
public function get_basic_payment_method_title() {
return '';
}
}