| Total Complexity | 64 |
| Total Lines | 423 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Gateway often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Gateway, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Gateway extends Core_Gateway { |
||
| 22 | /** |
||
| 23 | * Slug of this gateway |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | const SLUG = 'mollie'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Client. |
||
| 31 | * |
||
| 32 | * @var Client |
||
| 33 | */ |
||
| 34 | protected $client; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Meta key for customer ID. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $meta_key_customer_id = '_pronamic_pay_mollie_customer_id'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructs and initializes an Mollie gateway |
||
| 45 | * |
||
| 46 | * @param Config $config Config. |
||
| 47 | */ |
||
| 48 | public function __construct( Config $config ) { |
||
| 49 | parent::__construct( $config ); |
||
| 50 | |||
| 51 | $this->supports = array( |
||
| 52 | 'payment_status_request', |
||
| 53 | 'recurring_direct_debit', |
||
| 54 | 'recurring_credit_card', |
||
| 55 | 'recurring', |
||
| 56 | ); |
||
| 57 | |||
| 58 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||
| 59 | $this->set_slug( self::SLUG ); |
||
| 60 | |||
| 61 | $this->client = new Client( $config->api_key ); |
||
| 62 | $this->client->set_mode( $config->mode ); |
||
| 63 | |||
| 64 | if ( self::MODE_TEST === $config->mode ) { |
||
| 65 | $this->meta_key_customer_id = '_pronamic_pay_mollie_customer_id_test'; |
||
| 66 | } |
||
| 67 | |||
| 68 | // Actions. |
||
| 69 | add_action( 'pronamic_payment_status_update', array( $this, 'copy_customer_id_to_wp_user' ), 99, 1 ); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get issuers |
||
| 74 | * |
||
| 75 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
| 76 | */ |
||
| 77 | public function get_issuers() { |
||
| 78 | $groups = array(); |
||
| 79 | |||
| 80 | $result = $this->client->get_issuers(); |
||
| 81 | |||
| 82 | if ( ! $result ) { |
||
|
|
|||
| 83 | $this->error = $this->client->get_error(); |
||
| 84 | |||
| 85 | return $groups; |
||
| 86 | } |
||
| 87 | |||
| 88 | $groups[] = array( |
||
| 89 | 'options' => $result, |
||
| 90 | ); |
||
| 91 | |||
| 92 | return $groups; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get available payment methods. |
||
| 97 | * |
||
| 98 | * @see Core_Gateway::get_available_payment_methods() |
||
| 99 | */ |
||
| 100 | public function get_available_payment_methods() { |
||
| 101 | $payment_methods = array(); |
||
| 102 | |||
| 103 | // Set recurring types to get payment methods for. |
||
| 104 | $recurring_types = array( null, Recurring::RECURRING, Recurring::FIRST ); |
||
| 105 | |||
| 106 | $results = array(); |
||
| 107 | |||
| 108 | foreach ( $recurring_types as $recurring_type ) { |
||
| 109 | // Get active payment methods for Mollie account. |
||
| 110 | $result = $this->client->get_payment_methods( $recurring_type ); |
||
| 111 | |||
| 112 | if ( ! $result ) { |
||
| 113 | $this->error = $this->client->get_error(); |
||
| 114 | |||
| 115 | break; |
||
| 116 | } |
||
| 117 | |||
| 118 | if ( Recurring::FIRST === $recurring_type ) { |
||
| 119 | foreach ( $result as $method => $title ) { |
||
| 120 | unset( $result[ $method ] ); |
||
| 121 | |||
| 122 | // Get WordPress payment method for direct debit method. |
||
| 123 | $method = Methods::transform_gateway_method( $method ); |
||
| 124 | $payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true ); |
||
| 125 | |||
| 126 | if ( $payment_method ) { |
||
| 127 | $results[ $payment_method ] = $title; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | $results = array_merge( $results, $result ); |
||
| 133 | } |
||
| 134 | |||
| 135 | // Transform to WordPress payment methods. |
||
| 136 | foreach ( $results as $method => $title ) { |
||
| 137 | if ( PaymentMethods::is_recurring_method( $method ) ) { |
||
| 138 | $payment_method = $method; |
||
| 139 | } else { |
||
| 140 | $payment_method = Methods::transform_gateway_method( $method ); |
||
| 141 | } |
||
| 142 | |||
| 143 | if ( $payment_method ) { |
||
| 144 | $payment_methods[] = $payment_method; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | $payment_methods = array_unique( $payment_methods ); |
||
| 149 | |||
| 150 | return $payment_methods; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get supported payment methods |
||
| 155 | * |
||
| 156 | * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods() |
||
| 157 | */ |
||
| 158 | public function get_supported_payment_methods() { |
||
| 173 | ); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get webhook URL for Mollie. |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function get_webhook_url() { |
||
| 182 | $url = home_url( '/' ); |
||
| 183 | |||
| 184 | $host = wp_parse_url( $url, PHP_URL_HOST ); |
||
| 185 | |||
| 186 | if ( is_array( $host ) ) { |
||
| 187 | // Parsing failure. |
||
| 188 | $host = ''; |
||
| 189 | } |
||
| 190 | |||
| 191 | if ( 'localhost' === $host ) { |
||
| 192 | // Mollie doesn't allow localhost. |
||
| 193 | return null; |
||
| 194 | } elseif ( '.dev' === substr( $host, -4 ) ) { |
||
| 195 | // Mollie doesn't allow the .dev TLD. |
||
| 196 | return null; |
||
| 197 | } elseif ( '.local' === substr( $host, -6 ) ) { |
||
| 198 | // Mollie doesn't allow the .local TLD. |
||
| 199 | return null; |
||
| 200 | } |
||
| 201 | |||
| 202 | $url = add_query_arg( 'mollie_webhook', '', $url ); |
||
| 203 | |||
| 204 | return $url; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Start |
||
| 209 | * |
||
| 210 | * @see Pronamic_WP_Pay_Gateway::start() |
||
| 211 | * |
||
| 212 | * @param Payment $payment Payment. |
||
| 213 | */ |
||
| 214 | public function start( Payment $payment ) { |
||
| 215 | $request = new PaymentRequest(); |
||
| 216 | |||
| 217 | // Locale. |
||
| 218 | $locale = null; |
||
| 219 | |||
| 220 | if ( null !== $payment->get_customer() ) { |
||
| 221 | $locale = $payment->get_customer()->get_locale(); |
||
| 222 | } |
||
| 223 | |||
| 224 | $request->amount = $payment->get_total_amount()->get_amount(); |
||
| 225 | $request->description = $payment->get_description(); |
||
| 226 | $request->redirect_url = $payment->get_return_url(); |
||
| 227 | $request->webhook_url = $this->get_webhook_url(); |
||
| 228 | $request->locale = LocaleHelper::transform( $locale ); |
||
| 229 | |||
| 230 | // Customer ID. |
||
| 231 | $customer_id = $this->get_customer_id_for_payment( $payment ); |
||
| 232 | |||
| 233 | if ( is_string( $customer_id ) && ! empty( $customer_id ) ) { |
||
| 234 | $request->customer_id = $customer_id; |
||
| 235 | } |
||
| 236 | |||
| 237 | // Payment method. |
||
| 238 | $payment_method = $payment->get_method(); |
||
| 239 | |||
| 240 | // Subscription. |
||
| 241 | $subscription = $payment->get_subscription(); |
||
| 242 | |||
| 243 | if ( $subscription && PaymentMethods::is_recurring_method( $payment_method ) ) { |
||
| 244 | $request->recurring_type = $payment->get_recurring() ? Recurring::RECURRING : Recurring::FIRST; |
||
| 245 | |||
| 246 | if ( Recurring::FIRST === $request->recurring_type ) { |
||
| 247 | $payment_method = PaymentMethods::get_first_payment_method( $payment_method ); |
||
| 248 | } |
||
| 249 | |||
| 250 | if ( Recurring::RECURRING === $request->recurring_type ) { |
||
| 251 | $payment->set_action_url( $payment->get_return_url() ); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | // Leap of faith if the WordPress payment method could not transform to a Mollie method? |
||
| 256 | $request->method = Methods::transform( $payment_method, $payment_method ); |
||
| 257 | |||
| 258 | // Issuer. |
||
| 259 | if ( Methods::IDEAL === $request->method ) { |
||
| 260 | // If payment method is iDEAL we set the user chosen issuer ID. |
||
| 261 | $request->issuer = $payment->get_issuer(); |
||
| 262 | } |
||
| 263 | |||
| 264 | // Create payment. |
||
| 265 | $result = $this->client->create_payment( $request ); |
||
| 266 | |||
| 267 | if ( ! $result ) { |
||
| 268 | $this->error = $this->client->get_error(); |
||
| 269 | |||
| 270 | return; |
||
| 271 | } |
||
| 272 | |||
| 273 | // Set transaction ID. |
||
| 274 | if ( isset( $result->id ) ) { |
||
| 275 | $payment->set_transaction_id( $result->id ); |
||
| 276 | } |
||
| 277 | |||
| 278 | // Set status. |
||
| 279 | if ( isset( $result->status ) ) { |
||
| 280 | $payment->set_status( Statuses::transform( $result->status ) ); |
||
| 281 | } |
||
| 282 | |||
| 283 | // Set action URL. |
||
| 284 | if ( isset( $result->links, $result->links->paymentUrl ) ) { |
||
| 285 | $payment->set_action_url( $result->links->paymentUrl ); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Update status of the specified payment |
||
| 291 | * |
||
| 292 | * @param Payment $payment Payment. |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | public function update_status( Payment $payment ) { |
||
| 297 | $mollie_payment = $this->client->get_payment( $payment->get_transaction_id() ); |
||
| 298 | |||
| 299 | if ( ! $mollie_payment ) { |
||
| 300 | $payment->set_status( Core_Statuses::FAILURE ); |
||
| 301 | |||
| 302 | $this->error = $this->client->get_error(); |
||
| 303 | |||
| 304 | return; |
||
| 305 | } |
||
| 306 | |||
| 307 | $payment->set_status( Statuses::transform( $mollie_payment->status ) ); |
||
| 308 | |||
| 309 | if ( isset( $mollie_payment->details ) ) { |
||
| 310 | $details = $mollie_payment->details; |
||
| 311 | |||
| 312 | /* |
||
| 313 | * @codingStandardsIgnoreStart |
||
| 314 | * |
||
| 315 | * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar |
||
| 316 | */ |
||
| 317 | if ( isset( $details->consumerName ) ) { |
||
| 318 | $payment->set_consumer_name( $details->consumerName ); |
||
| 319 | } |
||
| 320 | |||
| 321 | if ( isset( $details->cardHolder ) ) { |
||
| 322 | $payment->set_consumer_name( $details->cardHolder ); |
||
| 323 | } |
||
| 324 | |||
| 325 | if ( isset( $details->consumerAccount ) ) { |
||
| 326 | $payment->set_consumer_iban( $details->consumerAccount ); |
||
| 327 | } |
||
| 328 | |||
| 329 | if ( isset( $details->consumerBic ) ) { |
||
| 330 | $payment->set_consumer_bic( $details->consumerBic ); |
||
| 331 | } |
||
| 332 | // @codingStandardsIgnoreEnd |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get Mollie customer ID for payment. |
||
| 338 | * |
||
| 339 | * @param Payment $payment Payment. |
||
| 340 | * |
||
| 341 | * @return bool|string |
||
| 342 | */ |
||
| 343 | public function get_customer_id_for_payment( Payment $payment ) { |
||
| 344 | // Get Mollie customer ID from user meta. |
||
| 345 | $customer_id = $this->get_customer_id_by_wp_user_id( $payment->user_id ); |
||
| 346 | |||
| 347 | $subscription = $payment->get_subscription(); |
||
| 348 | |||
| 349 | // Get customer ID from subscription meta. |
||
| 350 | if ( $subscription ) { |
||
| 351 | $subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||
| 352 | |||
| 353 | // Try to get (legacy) customer ID from first payment. |
||
| 354 | if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) { |
||
| 355 | $first_payment = $subscription->get_first_payment(); |
||
| 356 | |||
| 357 | $subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' ); |
||
| 358 | } |
||
| 359 | |||
| 360 | if ( ! empty( $subscription_customer_id ) ) { |
||
| 361 | $customer_id = $subscription_customer_id; |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | // Create new customer if the customer does not exist at Mollie. |
||
| 366 | if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) { |
||
| 367 | $customer_id = $this->client->create_customer( $payment->get_email(), $payment->get_customer_name() ); |
||
| 368 | |||
| 369 | $this->update_wp_user_customer_id( $payment->user_id, $customer_id ); |
||
| 370 | } |
||
| 371 | |||
| 372 | // Store customer ID in subscription meta. |
||
| 373 | if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) { |
||
| 374 | $subscription->set_meta( 'mollie_customer_id', $customer_id ); |
||
| 375 | } |
||
| 376 | |||
| 377 | // Copy customer ID from subscription to user meta. |
||
| 378 | $this->copy_customer_id_to_wp_user( $payment ); |
||
| 379 | |||
| 380 | return $customer_id; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get Mollie customer ID by the specified WordPress user ID. |
||
| 385 | * |
||
| 386 | * @param int $user_id WordPress user ID. |
||
| 387 | * |
||
| 388 | * @return string|bool |
||
| 389 | */ |
||
| 390 | public function get_customer_id_by_wp_user_id( $user_id ) { |
||
| 391 | if ( empty( $user_id ) ) { |
||
| 392 | return false; |
||
| 393 | } |
||
| 394 | |||
| 395 | return get_user_meta( $user_id, $this->meta_key_customer_id, true ); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Update Mollie customer ID meta for WordPress user. |
||
| 400 | * |
||
| 401 | * @param int $user_id WordPress user ID. |
||
| 402 | * @param string $customer_id Mollie Customer ID. |
||
| 403 | * |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | private function update_wp_user_customer_id( $user_id, $customer_id ) { |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Copy Mollie customer ID from subscription meta to WordPress user meta. |
||
| 420 | * |
||
| 421 | * @param Payment $payment Payment. |
||
| 422 | * |
||
| 423 | * @return void |
||
| 424 | */ |
||
| 425 | public function copy_customer_id_to_wp_user( Payment $payment ) { |
||
| 444 | } |
||
| 445 | } |
||
| 446 | } |
||
| 447 |