PaymentTokenData.php
220 lines
| 1 | <?php |
| 2 | |
| 3 | namespace GlobalPayments\WooCommercePaymentGatewayProvider\Data; |
| 4 | |
| 5 | use GlobalPayments\WooCommercePaymentGatewayProvider\Gateways\AbstractGateway; |
| 6 | use GlobalPayments\WooCommercePaymentGatewayProvider\Gateways\Requests\RequestInterface; |
| 7 | use WC_Payment_Tokens; |
| 8 | use WC_Payment_Token_CC; |
| 9 | |
| 10 | class PaymentTokenData { |
| 11 | const KEY_SHOULD_SAVE_TOKEN = 'should_save_for_later'; |
| 12 | |
| 13 | protected $card_type_map = array( |
| 14 | 'mastercard' => 'mastercard', |
| 15 | 'visa' => 'visa', |
| 16 | 'discover' => 'discover', |
| 17 | 'amex' => 'american express', |
| 18 | 'diners' => 'diners', |
| 19 | 'jcb' => 'jcb', |
| 20 | ); |
| 21 | |
| 22 | protected $card_type_map_transaction_api = array( |
| 23 | 'MasterCard' => 'mastercard', |
| 24 | 'Visa' => 'visa', |
| 25 | 'Discover' => 'discover', |
| 26 | 'American Express' => 'american express', |
| 27 | 'Diners Club' => 'diners', |
| 28 | 'JCB' => 'jcb', |
| 29 | ); |
| 30 | |
| 31 | /** |
| 32 | * Current request |
| 33 | * |
| 34 | * @var RequestInterface |
| 35 | */ |
| 36 | protected $request; |
| 37 | |
| 38 | /** |
| 39 | * Used w/TransIT gateway |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | public static $tsepCvv = null; |
| 44 | |
| 45 | /** |
| 46 | * Standardize getting single- and multi-use token data |
| 47 | * |
| 48 | * @param RequestInterface $request |
| 49 | * |
| 50 | * @return |
| 51 | */ |
| 52 | public function __construct( RequestInterface $request = null ) { |
| 53 | $this->request = $request; |
| 54 | } |
| 55 | |
| 56 | public function get_token() { |
| 57 | $token = $this->get_multi_use_token(); |
| 58 | |
| 59 | if ( null === $token ) { |
| 60 | $token = $this->get_single_use_token(); |
| 61 | } |
| 62 | |
| 63 | return $token; |
| 64 | } |
| 65 | |
| 66 | public function save_new_token( $multi_use_token, $card_brand_txn_id = null ) { |
| 67 | $user_id = get_current_user_id(); |
| 68 | $current_tokens = WC_Payment_Tokens::get_customer_tokens( $user_id, $this->request->gateway_id ); |
| 69 | |
| 70 | $token = $this->get_single_use_token(); |
| 71 | |
| 72 | if ( !empty( $token ) ) { |
| 73 | // a card number should only have a single token stored |
| 74 | foreach ( $current_tokens as $t ) { |
| 75 | if ( $t->get_token() === $multi_use_token ) { |
| 76 | $t->delete( true ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if ( ! $token->get_meta( self::KEY_SHOULD_SAVE_TOKEN, true ) ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | $token->set_token( $multi_use_token ); |
| 85 | $token->add_meta_data( 'card_brand_txn_id', $card_brand_txn_id ); |
| 86 | $token->set_user_id( $user_id ); |
| 87 | $token->set_gateway_id( $this->request->gateway_id ); |
| 88 | $token->add_meta_data( self::KEY_SHOULD_SAVE_TOKEN, false, true ); |
| 89 | $token->save(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public function get_single_use_token() { |
| 94 | if ( null === $this->request ) { |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | $gateway = $this->request->get_request_data( 'payment_method' ); |
| 99 | $request_data = $this->request->get_request_data( $gateway ); |
| 100 | if ( ! isset( $request_data['token_response'] ) ) { |
| 101 | return null; |
| 102 | } |
| 103 | |
| 104 | $data = json_decode( stripslashes( $request_data['token_response'] ) ); |
| 105 | |
| 106 | if ( empty( $data ) ) { |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | return $this->build_single_use_token( $data ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param $data |
| 115 | * @return WC_Payment_Token_CC |
| 116 | */ |
| 117 | private function build_single_use_token( $data ): WC_Payment_Token_CC { |
| 118 | $token = new WC_Payment_Token_CC(); |
| 119 | |
| 120 | // phpcs:disable WordPress.NamingConventions.ValidVariableName |
| 121 | $token->add_meta_data( self::KEY_SHOULD_SAVE_TOKEN, $this->get_should_save_for_later(), true ); |
| 122 | if ( isset( $data->paymentReference ) ) { |
| 123 | $this->fill_token_fields( $token, $data ); |
| 124 | } else { |
| 125 | $this->fill_token_fields_for_transaction_api( $token, $data ); |
| 126 | } |
| 127 | return $token; |
| 128 | // phpcs:enable WordPress.NamingConventions.ValidVariableName |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param WC_Payment_Token_CC $token |
| 133 | * @param $data |
| 134 | * @return void |
| 135 | */ |
| 136 | public function fill_token_fields( WC_Payment_Token_CC $token, $data ): void { |
| 137 | $token->set_token( $data->paymentReference ); |
| 138 | |
| 139 | if ( isset( $data->details->cardLast4 ) ) { |
| 140 | $token->set_last4( $data->details->cardLast4 ); |
| 141 | } |
| 142 | |
| 143 | if ( isset( $data->details->expiryYear ) ) { |
| 144 | $token->set_expiry_year( $data->details->expiryYear ); |
| 145 | } |
| 146 | |
| 147 | if ( isset( $data->details->expiryMonth ) ) { |
| 148 | $token->set_expiry_month( $data->details->expiryMonth ); |
| 149 | } |
| 150 | |
| 151 | static::$tsepCvv = isset( $data->details->cardSecurityCode ) ? $data->details->cardSecurityCode : null; |
| 152 | |
| 153 | if ( isset( $data->details->cardType ) && isset( $this->card_type_map[ $data->details->cardType ] ) ) { |
| 154 | $token->set_card_type( $this->card_type_map[ $data->details->cardType ] ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @param WC_Payment_Token_CC $token |
| 160 | * @param $data |
| 161 | * @return void |
| 162 | */ |
| 163 | public function fill_token_fields_for_transaction_api( WC_Payment_Token_CC $token, $data ): void { |
| 164 | $token->set_token( $data->temporary_token ); |
| 165 | |
| 166 | $card = $data->card; |
| 167 | if ( isset( $card->masked_card_number ) ) { |
| 168 | $token->set_last4( substr( $card->masked_card_number, -4 ) ); |
| 169 | } |
| 170 | |
| 171 | if ( isset( $card->expiry_year ) ) { |
| 172 | $token->set_expiry_year( $card->expiry_year + 2000 ); |
| 173 | } |
| 174 | |
| 175 | if ( isset( $card->expiry_month ) ) { |
| 176 | $token->set_expiry_month( $card->expiry_month ); |
| 177 | } |
| 178 | |
| 179 | if ( isset( $card->type ) && isset( $this->card_type_map_transaction_api[ $card->type ] ) ) { |
| 180 | $token->set_card_type( $this->card_type_map_transaction_api[ $card->type ] ); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public function get_multi_use_token() { |
| 185 | if ( null === $this->request ) { |
| 186 | return null; |
| 187 | } |
| 188 | |
| 189 | $gateway = $this->request->get_request_data( 'payment_method' ); |
| 190 | $token_id = $this->request->get_request_data( sprintf( 'wc-%s-payment-token', $gateway ) ); |
| 191 | |
| 192 | if ( 'new' === $token_id ) { |
| 193 | return null; |
| 194 | } |
| 195 | |
| 196 | $token = WC_Payment_Tokens::get( $token_id ); |
| 197 | if ( ! isset( $token ) ) { |
| 198 | return null; |
| 199 | } |
| 200 | |
| 201 | if ( $token->get_user_id() !== get_current_user_id() && ! wc_current_user_has_role( 'administrator' ) ) { |
| 202 | return null; |
| 203 | } |
| 204 | |
| 205 | if ( $token->get_meta( self::KEY_SHOULD_SAVE_TOKEN, true ) ) { |
| 206 | $token->add_meta_data( self::KEY_SHOULD_SAVE_TOKEN, false, true ); |
| 207 | } |
| 208 | |
| 209 | return $token; |
| 210 | } |
| 211 | |
| 212 | protected function get_should_save_for_later() { |
| 213 | $gateway = $this->request->get_request_data( 'payment_method' ); |
| 214 | return // Verify transactions always mean we're storing a token |
| 215 | $this->request->get_transaction_type() === AbstractGateway::TXN_TYPE_VERIFY || |
| 216 | // Merchant has enabled card storage. Customer has elected to store card. |
| 217 | $this->request->get_request_data( sprintf( 'wc-%s-new-payment-method', $gateway ) ) === 'true'; |
| 218 | } |
| 219 | } |
| 220 |