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