wp-pay-gateways /
mollie
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * Mollie gateway. |
||||||
| 4 | * |
||||||
| 5 | * @author Pronamic <[email protected]> |
||||||
| 6 | * @copyright 2005-2020 Pronamic |
||||||
| 7 | * @license GPL-3.0-or-later |
||||||
| 8 | * @package Pronamic\WordPress\Pay |
||||||
| 9 | */ |
||||||
| 10 | |||||||
| 11 | namespace Pronamic\WordPress\Pay\Gateways\Mollie; |
||||||
| 12 | |||||||
| 13 | use DateInterval; |
||||||
| 14 | use Pronamic\WordPress\DateTime\DateTime; |
||||||
| 15 | use Pronamic\WordPress\Pay\Banks\BankAccountDetails; |
||||||
| 16 | use Pronamic\WordPress\Pay\Banks\BankTransferDetails; |
||||||
| 17 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||||||
| 18 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||||||
| 19 | use Pronamic\WordPress\Pay\Core\Recurring as Core_Recurring; |
||||||
| 20 | use Pronamic\WordPress\Pay\Payments\PaymentStatus; |
||||||
| 21 | use Pronamic\WordPress\Pay\Payments\Payment; |
||||||
| 22 | use Pronamic\WordPress\Pay\Subscriptions\Subscription; |
||||||
| 23 | |||||||
| 24 | /** |
||||||
| 25 | * Title: Mollie |
||||||
| 26 | * Description: |
||||||
| 27 | * Copyright: 2005-2020 Pronamic |
||||||
| 28 | * Company: Pronamic |
||||||
| 29 | * |
||||||
| 30 | * @author Remco Tolsma |
||||||
| 31 | * @version 2.0.9 |
||||||
| 32 | * @since 1.1.0 |
||||||
| 33 | */ |
||||||
| 34 | class Gateway extends Core_Gateway { |
||||||
| 35 | /** |
||||||
| 36 | * Client. |
||||||
| 37 | * |
||||||
| 38 | * @var Client |
||||||
| 39 | */ |
||||||
| 40 | protected $client; |
||||||
| 41 | |||||||
| 42 | /** |
||||||
| 43 | * Constructs and initializes an Mollie gateway |
||||||
| 44 | * |
||||||
| 45 | * @param Config $config Config. |
||||||
| 46 | */ |
||||||
| 47 | public function __construct( Config $config ) { |
||||||
| 48 | parent::__construct( $config ); |
||||||
| 49 | |||||||
| 50 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||||||
| 51 | |||||||
| 52 | // Supported features. |
||||||
| 53 | 39 | $this->supports = array( |
|||||
| 54 | 39 | 'payment_status_request', |
|||||
| 55 | 'recurring_direct_debit', |
||||||
| 56 | 39 | 'recurring_credit_card', |
|||||
| 57 | 'recurring', |
||||||
| 58 | 'webhook', |
||||||
| 59 | 39 | 'webhook_log', |
|||||
| 60 | 'webhook_no_config', |
||||||
| 61 | ); |
||||||
| 62 | |||||||
| 63 | // Client. |
||||||
| 64 | $this->client = new Client( \strval( $config->api_key ) ); |
||||||
| 65 | |||||||
| 66 | // Data Stores. |
||||||
| 67 | $this->profile_data_store = new ProfileDataStore(); |
||||||
| 68 | $this->customer_data_store = new CustomerDataStore(); |
||||||
| 69 | } |
||||||
| 70 | 39 | ||||||
| 71 | 39 | /** |
|||||
| 72 | * Get issuers |
||||||
| 73 | * |
||||||
| 74 | 39 | * @see Core_Gateway::get_issuers() |
|||||
| 75 | 38 | * @return array<int, array<string, array<string>>> |
|||||
| 76 | */ |
||||||
| 77 | public function get_issuers() { |
||||||
| 78 | $groups = array(); |
||||||
| 79 | 39 | ||||||
| 80 | 39 | try { |
|||||
| 81 | $result = $this->client->get_issuers(); |
||||||
| 82 | |||||||
| 83 | $groups[] = array( |
||||||
| 84 | 'options' => $result, |
||||||
| 85 | ); |
||||||
| 86 | } catch ( Error $e ) { |
||||||
| 87 | 3 | // Catch Mollie error. |
|||||
| 88 | 3 | $error = new \WP_Error( |
|||||
| 89 | 'mollie_error', |
||||||
| 90 | sprintf( '%1$s (%2$s) - %3$s', $e->get_title(), $e->getCode(), $e->get_detail() ) |
||||||
| 91 | 3 | ); |
|||||
| 92 | |||||||
| 93 | $this->set_error( $error ); |
||||||
| 94 | } catch ( \Exception $e ) { |
||||||
| 95 | // Catch exceptions. |
||||||
| 96 | 3 | $error = new \WP_Error( 'mollie_error', $e->getMessage() ); |
|||||
| 97 | |||||||
| 98 | 3 | $this->set_error( $error ); |
|||||
| 99 | 3 | } |
|||||
| 100 | 3 | ||||||
| 101 | return $groups; |
||||||
| 102 | } |
||||||
| 103 | 3 | ||||||
| 104 | /** |
||||||
| 105 | * Get available payment methods. |
||||||
| 106 | * |
||||||
| 107 | * @see Core_Gateway::get_available_payment_methods() |
||||||
| 108 | * @return array<string> |
||||||
| 109 | */ |
||||||
| 110 | public function get_available_payment_methods() { |
||||||
| 111 | 3 | $payment_methods = array(); |
|||||
| 112 | |||||||
| 113 | // Set sequence types to get payment methods for. |
||||||
| 114 | $sequence_types = array( Sequence::ONE_OFF, Sequence::RECURRING, Sequence::FIRST ); |
||||||
| 115 | |||||||
| 116 | $results = array(); |
||||||
| 117 | |||||||
| 118 | foreach ( $sequence_types as $sequence_type ) { |
||||||
| 119 | 2 | // Get active payment methods for Mollie account. |
|||||
| 120 | 2 | try { |
|||||
| 121 | $result = $this->client->get_payment_methods( $sequence_type ); |
||||||
| 122 | } catch ( Error $e ) { |
||||||
| 123 | 2 | // Catch Mollie error. |
|||||
| 124 | $error = new \WP_Error( |
||||||
| 125 | 2 | 'mollie_error', |
|||||
| 126 | sprintf( '%1$s (%2$s) - %3$s', $e->get_title(), $e->getCode(), $e->get_detail() ) |
||||||
| 127 | 2 | ); |
|||||
| 128 | |||||||
| 129 | $this->set_error( $error ); |
||||||
| 130 | 2 | ||||||
| 131 | 2 | break; |
|||||
| 132 | } catch ( \Exception $e ) { |
||||||
| 133 | // Catch exceptions. |
||||||
| 134 | $error = new \WP_Error( 'mollie_error', $e->getMessage() ); |
||||||
| 135 | |||||||
| 136 | $this->set_error( $error ); |
||||||
| 137 | |||||||
| 138 | break; |
||||||
| 139 | } |
||||||
| 140 | |||||||
| 141 | 2 | if ( Sequence::FIRST === $sequence_type ) { |
|||||
| 142 | foreach ( $result as $method => $title ) { |
||||||
| 143 | 2 | unset( $result[ $method ] ); |
|||||
| 144 | |||||||
| 145 | 2 | // Get WordPress payment method for direct debit method. |
|||||
| 146 | $method = Methods::transform_gateway_method( $method ); |
||||||
| 147 | 2 | $payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true ); |
|||||
| 148 | |||||||
| 149 | if ( $payment_method ) { |
||||||
| 150 | 2 | $results[ $payment_method ] = $title; |
|||||
| 151 | } |
||||||
| 152 | } |
||||||
| 153 | } |
||||||
| 154 | |||||||
| 155 | if ( is_array( $result ) ) { |
||||||
| 156 | $results = array_merge( $results, $result ); |
||||||
| 157 | } |
||||||
| 158 | } |
||||||
| 159 | |||||||
| 160 | // Transform to WordPress payment methods. |
||||||
| 161 | foreach ( $results as $method => $title ) { |
||||||
| 162 | if ( PaymentMethods::is_recurring_method( $method ) ) { |
||||||
| 163 | $payment_method = $method; |
||||||
| 164 | 2 | } else { |
|||||
| 165 | 2 | $payment_method = Methods::transform_gateway_method( $method ); |
|||||
| 166 | } |
||||||
| 167 | |||||||
| 168 | if ( $payment_method ) { |
||||||
| 169 | $payment_methods[] = $payment_method; |
||||||
| 170 | 2 | } |
|||||
| 171 | 2 | } |
|||||
| 172 | |||||||
| 173 | $payment_methods = array_unique( $payment_methods ); |
||||||
| 174 | 2 | ||||||
| 175 | return $payment_methods; |
||||||
| 176 | } |
||||||
| 177 | 2 | ||||||
| 178 | 2 | /** |
|||||
| 179 | * Get supported payment methods |
||||||
| 180 | * |
||||||
| 181 | * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods() |
||||||
| 182 | 2 | * @return array<string> |
|||||
| 183 | */ |
||||||
| 184 | 2 | public function get_supported_payment_methods() { |
|||||
| 185 | return array( |
||||||
| 186 | PaymentMethods::BANCONTACT, |
||||||
| 187 | PaymentMethods::BANK_TRANSFER, |
||||||
| 188 | PaymentMethods::BELFIUS, |
||||||
| 189 | PaymentMethods::CREDIT_CARD, |
||||||
| 190 | PaymentMethods::DIRECT_DEBIT, |
||||||
| 191 | PaymentMethods::DIRECT_DEBIT_BANCONTACT, |
||||||
| 192 | PaymentMethods::DIRECT_DEBIT_IDEAL, |
||||||
| 193 | 2 | PaymentMethods::DIRECT_DEBIT_SOFORT, |
|||||
| 194 | PaymentMethods::EPS, |
||||||
| 195 | 2 | PaymentMethods::GIROPAY, |
|||||
| 196 | PaymentMethods::IDEAL, |
||||||
| 197 | PaymentMethods::KBC, |
||||||
| 198 | PaymentMethods::PAYPAL, |
||||||
| 199 | PaymentMethods::SOFORT, |
||||||
| 200 | ); |
||||||
| 201 | } |
||||||
| 202 | |||||||
| 203 | /** |
||||||
| 204 | * Get webhook URL for Mollie. |
||||||
| 205 | * |
||||||
| 206 | * @return string|null |
||||||
| 207 | */ |
||||||
| 208 | public function get_webhook_url() { |
||||||
| 209 | $url = \rest_url( Integration::REST_ROUTE_NAMESPACE . '/webhook' ); |
||||||
| 210 | |||||||
| 211 | $host = wp_parse_url( $url, PHP_URL_HOST ); |
||||||
| 212 | |||||||
| 213 | if ( is_array( $host ) ) { |
||||||
| 214 | // Parsing failure. |
||||||
| 215 | $host = ''; |
||||||
| 216 | } |
||||||
| 217 | 4 | ||||||
| 218 | 4 | if ( 'localhost' === $host ) { |
|||||
| 219 | // Mollie doesn't allow localhost. |
||||||
| 220 | 4 | return null; |
|||||
| 221 | } elseif ( '.dev' === substr( $host, -4 ) ) { |
||||||
| 222 | 4 | // Mollie doesn't allow the .dev TLD. |
|||||
| 223 | return null; |
||||||
| 224 | } elseif ( '.local' === substr( $host, -6 ) ) { |
||||||
| 225 | // Mollie doesn't allow the .local TLD. |
||||||
| 226 | return null; |
||||||
| 227 | 4 | } elseif ( '.test' === substr( $host, -5 ) ) { |
|||||
| 228 | // Mollie doesn't allow the .test TLD. |
||||||
| 229 | 1 | return null; |
|||||
| 230 | 3 | } |
|||||
| 231 | |||||||
| 232 | 1 | return $url; |
|||||
| 233 | 2 | } |
|||||
| 234 | |||||||
| 235 | 1 | /** |
|||||
| 236 | 1 | * Start |
|||||
| 237 | * |
||||||
| 238 | * @see Pronamic_WP_Pay_Gateway::start() |
||||||
| 239 | * @param Payment $payment Payment. |
||||||
| 240 | * @return void |
||||||
| 241 | 1 | */ |
|||||
| 242 | public function start( Payment $payment ) { |
||||||
| 243 | 1 | $request = new PaymentRequest( |
|||||
| 244 | AmountTransformer::transform( $payment->get_total_amount() ), |
||||||
| 245 | \strval( $payment->get_description() ) |
||||||
| 246 | ); |
||||||
| 247 | |||||||
| 248 | $request->redirect_url = $payment->get_return_url(); |
||||||
| 249 | $request->webhook_url = $this->get_webhook_url(); |
||||||
| 250 | |||||||
| 251 | // Locale. |
||||||
| 252 | $customer = $payment->get_customer(); |
||||||
| 253 | |||||||
| 254 | if ( null !== $customer ) { |
||||||
| 255 | $request->locale = LocaleHelper::transform( $customer->get_locale() ); |
||||||
| 256 | } |
||||||
| 257 | |||||||
| 258 | // Customer ID. |
||||||
| 259 | $customer_id = $this->get_customer_id_for_payment( $payment ); |
||||||
| 260 | |||||||
| 261 | if ( null === $customer_id ) { |
||||||
| 262 | $customer_id = $this->create_customer_for_payment( $payment ); |
||||||
| 263 | } |
||||||
| 264 | |||||||
| 265 | if ( null !== $customer_id ) { |
||||||
| 266 | $request->customer_id = $customer_id; |
||||||
| 267 | } |
||||||
| 268 | |||||||
| 269 | // Payment method. |
||||||
| 270 | $payment_method = $payment->get_method(); |
||||||
| 271 | |||||||
| 272 | // Recurring payment method. |
||||||
| 273 | $is_recurring_method = ( $payment->get_subscription() && PaymentMethods::is_recurring_method( $payment_method ) ); |
||||||
| 274 | |||||||
| 275 | // Consumer bank details. |
||||||
| 276 | $consumer_bank_details = $payment->get_consumer_bank_details(); |
||||||
| 277 | |||||||
| 278 | if ( PaymentMethods::DIRECT_DEBIT === $payment_method && null !== $consumer_bank_details ) { |
||||||
| 279 | $consumer_name = $consumer_bank_details->get_name(); |
||||||
| 280 | $consumer_iban = $consumer_bank_details->get_iban(); |
||||||
| 281 | |||||||
| 282 | $request->consumer_name = $consumer_name; |
||||||
| 283 | $request->consumer_account = $consumer_iban; |
||||||
| 284 | |||||||
| 285 | // Check if one-off SEPA Direct Debit can be used, otherwise short circuit payment. |
||||||
| 286 | if ( null !== $customer_id ) { |
||||||
| 287 | // Find or create mandate. |
||||||
| 288 | $mandate_id = $this->client->has_valid_mandate( $customer_id, PaymentMethods::DIRECT_DEBIT, $consumer_iban ); |
||||||
| 289 | |||||||
| 290 | if ( false === $mandate_id ) { |
||||||
| 291 | $mandate = $this->client->create_mandate( $customer_id, $consumer_bank_details ); |
||||||
| 292 | |||||||
| 293 | $mandate_id = $mandate->id; |
||||||
| 294 | } |
||||||
| 295 | |||||||
| 296 | // Charge immediately on-demand. |
||||||
| 297 | $request->sequence_type = Sequence::RECURRING; |
||||||
| 298 | $request->mandate_id = $mandate_id; |
||||||
| 299 | |||||||
| 300 | $is_recurring_method = true; |
||||||
| 301 | |||||||
| 302 | $payment->recurring = true; |
||||||
| 303 | } |
||||||
| 304 | } |
||||||
| 305 | |||||||
| 306 | if ( false === $is_recurring_method ) { |
||||||
| 307 | // Always use 'direct debit mandate via iDEAL/Bancontact/Sofort' payment methods as recurring method. |
||||||
| 308 | $is_recurring_method = PaymentMethods::is_direct_debit_method( $payment_method ); |
||||||
| 309 | } |
||||||
| 310 | |||||||
| 311 | if ( $is_recurring_method ) { |
||||||
| 312 | $request->sequence_type = $payment->get_recurring() ? Sequence::RECURRING : Sequence::FIRST; |
||||||
| 313 | |||||||
| 314 | if ( Sequence::FIRST === $request->sequence_type ) { |
||||||
| 315 | $payment_method = PaymentMethods::get_first_payment_method( $payment_method ); |
||||||
| 316 | } |
||||||
| 317 | |||||||
| 318 | if ( Sequence::RECURRING === $request->sequence_type ) { |
||||||
| 319 | $payment->set_action_url( $payment->get_return_url() ); |
||||||
| 320 | } |
||||||
| 321 | } |
||||||
| 322 | |||||||
| 323 | // Leap of faith if the WordPress payment method could not transform to a Mollie method? |
||||||
| 324 | $request->method = Methods::transform( $payment_method, $payment_method ); |
||||||
| 325 | |||||||
| 326 | // Issuer. |
||||||
| 327 | if ( Methods::IDEAL === $request->method ) { |
||||||
| 328 | $request->issuer = $payment->get_issuer(); |
||||||
| 329 | } |
||||||
| 330 | |||||||
| 331 | // Due date. |
||||||
| 332 | try { |
||||||
| 333 | $due_date = new DateTime( sprintf( '+%s days', $this->config->due_date_days ) ); |
||||||
| 334 | } catch ( \Exception $e ) { |
||||||
| 335 | $due_date = null; |
||||||
| 336 | } |
||||||
| 337 | |||||||
| 338 | $request->set_due_date( $due_date ); |
||||||
| 339 | |||||||
| 340 | // Create payment. |
||||||
| 341 | $result = $this->client->create_payment( $request ); |
||||||
| 342 | |||||||
| 343 | // Set transaction ID. |
||||||
| 344 | if ( isset( $result->id ) ) { |
||||||
| 345 | $payment->set_transaction_id( $result->id ); |
||||||
| 346 | } |
||||||
| 347 | |||||||
| 348 | // Set expiry date. |
||||||
| 349 | /* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ |
||||||
| 350 | if ( isset( $result->expiresAt ) ) { |
||||||
| 351 | try { |
||||||
| 352 | $expires_at = new DateTime( $result->expiresAt ); |
||||||
| 353 | } catch ( \Exception $e ) { |
||||||
| 354 | $expires_at = null; |
||||||
| 355 | } |
||||||
| 356 | |||||||
| 357 | $payment->set_expiry_date( $expires_at ); |
||||||
| 358 | } |
||||||
| 359 | /* phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ |
||||||
| 360 | |||||||
| 361 | // Set status. |
||||||
| 362 | if ( isset( $result->status ) ) { |
||||||
| 363 | $payment->set_status( Statuses::transform( $result->status ) ); |
||||||
| 364 | } |
||||||
| 365 | |||||||
| 366 | // Set bank transfer recipient details. |
||||||
| 367 | if ( isset( $result->details ) ) { |
||||||
| 368 | $bank_transfer_recipient_details = $payment->get_bank_transfer_recipient_details(); |
||||||
| 369 | |||||||
| 370 | if ( null === $bank_transfer_recipient_details ) { |
||||||
| 371 | $bank_transfer_recipient_details = new BankTransferDetails(); |
||||||
| 372 | |||||||
| 373 | $payment->set_bank_transfer_recipient_details( $bank_transfer_recipient_details ); |
||||||
| 374 | } |
||||||
| 375 | |||||||
| 376 | $bank_details = $bank_transfer_recipient_details->get_bank_account(); |
||||||
| 377 | |||||||
| 378 | if ( null === $bank_details ) { |
||||||
| 379 | $bank_details = new BankAccountDetails(); |
||||||
| 380 | |||||||
| 381 | $bank_transfer_recipient_details->set_bank_account( $bank_details ); |
||||||
| 382 | } |
||||||
| 383 | |||||||
| 384 | $details = $result->details; |
||||||
| 385 | |||||||
| 386 | /* |
||||||
| 387 | * @codingStandardsIgnoreStart |
||||||
| 388 | * |
||||||
| 389 | * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar |
||||||
| 390 | */ |
||||||
| 391 | if ( isset( $details->bankName ) ) { |
||||||
| 392 | /** |
||||||
| 393 | * Set `bankName` as bank details name, as result "Stichting Mollie Payments" |
||||||
| 394 | * is not the name of a bank, but the account holder name. |
||||||
| 395 | */ |
||||||
| 396 | $bank_details->set_name( $details->bankName ); |
||||||
| 397 | } |
||||||
| 398 | |||||||
| 399 | if ( isset( $details->bankAccount ) ) { |
||||||
| 400 | $bank_details->set_iban( $details->bankAccount ); |
||||||
| 401 | } |
||||||
| 402 | |||||||
| 403 | if ( isset( $details->bankBic ) ) { |
||||||
| 404 | $bank_details->set_bic( $details->bankBic ); |
||||||
| 405 | } |
||||||
| 406 | |||||||
| 407 | if ( isset( $details->transferReference ) ) { |
||||||
| 408 | $bank_transfer_recipient_details->set_reference( $details->transferReference ); |
||||||
| 409 | } |
||||||
| 410 | // @codingStandardsIgnoreEnd |
||||||
| 411 | } |
||||||
| 412 | |||||||
| 413 | // Set action URL. |
||||||
| 414 | if ( isset( $result->_links ) ) { |
||||||
| 415 | if ( isset( $result->_links->checkout->href ) ) { |
||||||
| 416 | $payment->set_action_url( $result->_links->checkout->href ); |
||||||
| 417 | } |
||||||
| 418 | } |
||||||
| 419 | } |
||||||
| 420 | |||||||
| 421 | /** |
||||||
| 422 | * Update status of the specified payment |
||||||
| 423 | * |
||||||
| 424 | * @param Payment $payment Payment. |
||||||
| 425 | * @return void |
||||||
| 426 | */ |
||||||
| 427 | public function update_status( Payment $payment ) { |
||||||
| 428 | $transaction_id = $payment->get_transaction_id(); |
||||||
| 429 | |||||||
| 430 | if ( null === $transaction_id ) { |
||||||
| 431 | return; |
||||||
| 432 | } |
||||||
| 433 | |||||||
| 434 | $mollie_payment = $this->client->get_payment( $transaction_id ); |
||||||
| 435 | |||||||
| 436 | if ( isset( $mollie_payment->status ) ) { |
||||||
| 437 | $payment->set_status( Statuses::transform( $mollie_payment->status ) ); |
||||||
| 438 | } |
||||||
| 439 | |||||||
| 440 | /** |
||||||
| 441 | * Mollie profile. |
||||||
| 442 | */ |
||||||
| 443 | $mollie_profile = new Profile(); |
||||||
| 444 | $mollie_profile->set_id( $mollie_payment->profileId ); |
||||||
| 445 | |||||||
| 446 | $profile_internal_id = $this->profile_data_store->get_or_insert_profile( $mollie_profile ); |
||||||
| 447 | |||||||
| 448 | /** |
||||||
| 449 | * If the Mollie payment contains a customer ID we will try to connect |
||||||
| 450 | * this Mollie customer ID the WordPress user and subscription. |
||||||
| 451 | * This can be usefull in case when a WordPress user is created after |
||||||
| 452 | * a succesfull payment. |
||||||
| 453 | * |
||||||
| 454 | * @link https://www.gravityforms.com/add-ons/user-registration/ |
||||||
| 455 | */ |
||||||
| 456 | if ( isset( $mollie_payment->customerId ) ) { |
||||||
| 457 | $mollie_customer = new Customer(); |
||||||
| 458 | $mollie_customer->set_id( $mollie_payment->customerId ); |
||||||
| 459 | |||||||
| 460 | $customer_internal_id = $this->customer_data_store->get_or_insert_customer( |
||||||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||||||
| 461 | $mollie_customer, |
||||||
| 462 | array( |
||||||
|
0 ignored issues
–
show
The call to
Pronamic\WordPress\Pay\G...et_or_insert_customer() has too many arguments starting with array('profile_id' => $profile_internal_id).
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. Loading history...
|
|||||||
| 463 | 'profile_id' => $profile_internal_id, |
||||||
| 464 | ), |
||||||
| 465 | array( |
||||||
| 466 | 'profile_id' => '%s', |
||||||
| 467 | ) |
||||||
| 468 | ); |
||||||
| 469 | |||||||
| 470 | $customer = $payment->get_customer(); |
||||||
| 471 | |||||||
| 472 | if ( null !== $customer ) { |
||||||
| 473 | // Connect to user. |
||||||
| 474 | $user = \get_user_by( 'id', $customer->get_user_id() ); |
||||||
| 475 | |||||||
| 476 | if ( false !== $user ) { |
||||||
| 477 | $this->customer_data_store->connect_mollie_customer_to_wp_user( $mollie_customer, $user ); |
||||||
| 478 | } |
||||||
| 479 | } |
||||||
| 480 | |||||||
| 481 | $subscription = $payment->get_subscription(); |
||||||
| 482 | |||||||
| 483 | 10 | if ( null !== $subscription ) { |
|||||
| 484 | 10 | $customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
|||||
| 485 | |||||||
| 486 | if ( empty( $customer_id ) ) { |
||||||
| 487 | 10 | $subscription->set_meta( 'mollie_customer_id', $mollie_customer->get_id() ); |
|||||
| 488 | } |
||||||
| 489 | } |
||||||
| 490 | 10 | } |
|||||
| 491 | |||||||
| 492 | 10 | if ( isset( $mollie_payment->details ) ) { |
|||||
| 493 | $consumer_bank_details = $payment->get_consumer_bank_details(); |
||||||
| 494 | |||||||
| 495 | 10 | if ( null === $consumer_bank_details ) { |
|||||
| 496 | 10 | $consumer_bank_details = new BankAccountDetails(); |
|||||
| 497 | |||||||
| 498 | $payment->set_consumer_bank_details( $consumer_bank_details ); |
||||||
| 499 | 10 | } |
|||||
| 500 | 7 | ||||||
| 501 | $details = $mollie_payment->details; |
||||||
| 502 | 7 | ||||||
| 503 | /* |
||||||
| 504 | * @codingStandardsIgnoreStart |
||||||
| 505 | 10 | * |
|||||
| 506 | 4 | * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar |
|||||
| 507 | */ |
||||||
| 508 | if ( isset( $details->consumerName ) ) { |
||||||
| 509 | $consumer_bank_details->set_name( $details->consumerName ); |
||||||
| 510 | } |
||||||
| 511 | 10 | ||||||
| 512 | if ( isset( $details->cardHolder ) ) { |
||||||
| 513 | $consumer_bank_details->set_name( $details->cardHolder ); |
||||||
| 514 | } |
||||||
| 515 | |||||||
| 516 | if ( isset( $details->cardNumber ) ) { |
||||||
| 517 | // The last four digits of the card number. |
||||||
| 518 | $consumer_bank_details->set_account_number( $details->cardNumber ); |
||||||
| 519 | } |
||||||
| 520 | |||||||
| 521 | if ( isset( $details->cardCountryCode ) ) { |
||||||
| 522 | // The ISO 3166-1 alpha-2 country code of the country the card was issued in. |
||||||
| 523 | $consumer_bank_details->set_country( $details->cardCountryCode ); |
||||||
| 524 | 10 | } |
|||||
| 525 | |||||||
| 526 | if ( isset( $details->consumerAccount ) ) { |
||||||
| 527 | switch ( $mollie_payment->method ) { |
||||||
| 528 | case Methods::BELFIUS: |
||||||
| 529 | 10 | case Methods::DIRECT_DEBIT: |
|||||
| 530 | case Methods::IDEAL: |
||||||
| 531 | 10 | case Methods::KBC: |
|||||
| 532 | case Methods::SOFORT: |
||||||
| 533 | $consumer_bank_details->set_iban( $details->consumerAccount ); |
||||||
| 534 | |||||||
| 535 | break; |
||||||
| 536 | case Methods::BANCONTACT: |
||||||
| 537 | case Methods::BANKTRANSFER: |
||||||
| 538 | case Methods::PAYPAL: |
||||||
| 539 | default: |
||||||
| 540 | 27 | $consumer_bank_details->set_account_number( $details->consumerAccount ); |
|||||
| 541 | 27 | ||||||
| 542 | 11 | break; |
|||||
| 543 | } |
||||||
| 544 | } |
||||||
| 545 | 16 | ||||||
| 546 | if ( isset( $details->consumerBic ) ) { |
||||||
| 547 | $consumer_bank_details->set_bic( $details->consumerBic ); |
||||||
| 548 | } |
||||||
| 549 | // @codingStandardsIgnoreEnd |
||||||
| 550 | } |
||||||
| 551 | } |
||||||
| 552 | |||||||
| 553 | /** |
||||||
| 554 | * Get Mollie customer ID for payment. |
||||||
| 555 | 15 | * |
|||||
| 556 | 15 | * @param Payment $payment Payment. |
|||||
| 557 | 3 | * @return string|null |
|||||
| 558 | */ |
||||||
| 559 | public function get_customer_id_for_payment( Payment $payment ) { |
||||||
| 560 | 12 | $customer_ids = $this->get_customer_ids_for_payment( $payment ); |
|||||
| 561 | 11 | ||||||
| 562 | $customer_id = $this->get_first_existing_customer_id( $customer_ids ); |
||||||
| 563 | |||||||
| 564 | 4 | return $customer_id; |
|||||
| 565 | 4 | } |
|||||
| 566 | |||||||
| 567 | /** |
||||||
| 568 | * Get Mollie customers for the specified payment. |
||||||
| 569 | * |
||||||
| 570 | * @param Payment $payment Payment. |
||||||
| 571 | * @return array<string> |
||||||
| 572 | */ |
||||||
| 573 | 27 | private function get_customer_ids_for_payment( Payment $payment ) { |
|||||
| 574 | 27 | $customer_ids = array(); |
|||||
| 575 | 1 | ||||||
| 576 | // Customer ID from subscription meta. |
||||||
| 577 | $subscription = $payment->get_subscription(); |
||||||
| 578 | 26 | ||||||
| 579 | if ( null !== $subscription ) { |
||||||
| 580 | 26 | $customer_id = $this->get_customer_id_for_subscription( $payment->get_subscription() ); |
|||||
| 581 | |||||||
| 582 | if ( null !== $customer_id ) { |
||||||
| 583 | $customer_ids[] = $customer_id; |
||||||
| 584 | 26 | } |
|||||
| 585 | } |
||||||
| 586 | 26 | ||||||
| 587 | 1 | // Customer ID from WordPress user. |
|||||
| 588 | $customer = $payment->get_customer(); |
||||||
| 589 | |||||||
| 590 | 25 | if ( null !== $customer ) { |
|||||
| 591 | $user_id = $customer->get_user_id(); |
||||||
| 592 | 25 | ||||||
| 593 | 10 | if ( ! empty( $user_id ) ) { |
|||||
| 594 | $user_customer_ids = $this->get_customer_ids_for_user( $user_id ); |
||||||
| 595 | |||||||
| 596 | $customer_ids = \array_merge( $customer_ids, $user_customer_ids ); |
||||||
| 597 | 15 | } |
|||||
| 598 | } |
||||||
| 599 | 15 | ||||||
| 600 | return $customer_ids; |
||||||
| 601 | 15 | } |
|||||
| 602 | |||||||
| 603 | 15 | /** |
|||||
| 604 | * Get Mollie customers for the specified WordPress user ID. |
||||||
| 605 | 15 | * |
|||||
| 606 | * @param int $user_id WordPress user ID. |
||||||
| 607 | * @return array<string> |
||||||
| 608 | */ |
||||||
| 609 | private function get_customer_ids_for_user( $user_id ) { |
||||||
| 610 | $customer_query = new CustomerQuery( |
||||||
| 611 | array( |
||||||
| 612 | 'user_id' => $user_id, |
||||||
| 613 | ) |
||||||
| 614 | ); |
||||||
| 615 | |||||||
| 616 | $customers = $customer_query->get_customers(); |
||||||
| 617 | |||||||
| 618 | $customer_ids = wp_list_pluck( $customers, 'mollie_id' ); |
||||||
| 619 | |||||||
| 620 | return $customer_ids; |
||||||
| 621 | } |
||||||
| 622 | |||||||
| 623 | /** |
||||||
| 624 | * Get customer ID for subscription. |
||||||
| 625 | * |
||||||
| 626 | * @param Subscription $subscription Subscription. |
||||||
| 627 | * @return string|null |
||||||
| 628 | */ |
||||||
| 629 | private function get_customer_id_for_subscription( Subscription $subscription ) { |
||||||
| 630 | $customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||||||
| 631 | |||||||
| 632 | if ( empty( $customer_id ) ) { |
||||||
| 633 | // Try to get (legacy) customer ID from first payment. |
||||||
| 634 | $first_payment = $subscription->get_first_payment(); |
||||||
| 635 | |||||||
| 636 | if ( null !== $first_payment ) { |
||||||
| 637 | $customer_id = $first_payment->get_meta( 'mollie_customer_id' ); |
||||||
| 638 | } |
||||||
| 639 | } |
||||||
| 640 | |||||||
| 641 | if ( empty( $customer_id ) ) { |
||||||
| 642 | return null; |
||||||
| 643 | } |
||||||
| 644 | |||||||
| 645 | return $customer_id; |
||||||
| 646 | } |
||||||
| 647 | |||||||
| 648 | /** |
||||||
| 649 | * Get first existing customer from customers list. |
||||||
| 650 | * |
||||||
| 651 | * @param array $customer_ids Customers. |
||||||
| 652 | * @return string|null |
||||||
| 653 | */ |
||||||
| 654 | private function get_first_existing_customer_id( $customer_ids ) { |
||||||
| 655 | $customer_ids = \array_filter( $customer_ids ); |
||||||
| 656 | |||||||
| 657 | $customer_ids = \array_unique( $customer_ids ); |
||||||
| 658 | |||||||
| 659 | foreach ( $customer_ids as $customer_id ) { |
||||||
| 660 | $customer = $this->client->get_customer( $customer_id ); |
||||||
| 661 | |||||||
| 662 | if ( null !== $customer ) { |
||||||
| 663 | return $customer_id; |
||||||
| 664 | } |
||||||
| 665 | } |
||||||
| 666 | |||||||
| 667 | return null; |
||||||
| 668 | } |
||||||
| 669 | |||||||
| 670 | /** |
||||||
| 671 | * Create customer for payment. |
||||||
| 672 | * |
||||||
| 673 | * @param Payment $payment |
||||||
| 674 | * @return string|null |
||||||
| 675 | * @throws Error Throws Error when Mollie error occurs. |
||||||
| 676 | */ |
||||||
| 677 | private function create_customer_for_payment( Payment $payment ) { |
||||||
| 678 | $mollie_customer = new Customer(); |
||||||
| 679 | $mollie_customer->set_mode( $this->config->is_test_mode() ? 'test' : 'live' ); |
||||||
| 680 | $mollie_customer->set_email( $payment->get_email() ); |
||||||
| 681 | |||||||
| 682 | $pronamic_customer = $payment->get_customer(); |
||||||
| 683 | |||||||
| 684 | if ( null !== $pronamic_customer ) { |
||||||
| 685 | $name = $pronamic_customer->get_name(); |
||||||
| 686 | |||||||
| 687 | if ( null !== $name ) { |
||||||
| 688 | $mollie_customer->set_name( \strval( $name ) ); |
||||||
| 689 | } |
||||||
| 690 | } |
||||||
| 691 | |||||||
| 692 | // Create customer. |
||||||
| 693 | $mollie_customer = $this->client->create_customer( $mollie_customer ); |
||||||
| 694 | |||||||
| 695 | $customer_id = $this->customer_data_store->insert_customer( $mollie_customer ); |
||||||
| 696 | |||||||
| 697 | // Connect to user. |
||||||
| 698 | $user = \get_user_by( 'id', $pronamic_customer->get_user_id() ); |
||||||
| 699 | |||||||
| 700 | if ( false !== $user ) { |
||||||
| 701 | $this->customer_data_store->connect_mollie_customer_to_wp_user( $mollie_customer, $user ); |
||||||
| 702 | } |
||||||
| 703 | |||||||
| 704 | // Store customer ID in subscription meta. |
||||||
| 705 | $subscription = $payment->get_subscription(); |
||||||
| 706 | |||||||
| 707 | if ( null !== $subscription ) { |
||||||
| 708 | $subscription->set_meta( 'mollie_customer_id', $mollie_customer->get_id() ); |
||||||
| 709 | } |
||||||
| 710 | |||||||
| 711 | return $mollie_customer->get_id(); |
||||||
| 712 | } |
||||||
| 713 | } |
||||||
| 714 |