wp-pay-gateways /
mollie
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * Mollie gateway. |
||||||
| 4 | * |
||||||
| 5 | * @author Pronamic <[email protected]> |
||||||
| 6 | * @copyright 2005-2021 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 Pronamic\WordPress\DateTime\DateTime; |
||||||
| 14 | use Pronamic\WordPress\Money\Money; |
||||||
| 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\Payments\FailureReason; |
||||||
| 20 | use Pronamic\WordPress\Pay\Payments\Payment; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 21 | use Pronamic\WordPress\Pay\Payments\PaymentStatus; |
||||||
| 22 | use Pronamic\WordPress\Pay\Subscriptions\Subscription; |
||||||
| 23 | use Pronamic\WordPress\Pay\Subscriptions\SubscriptionStatus; |
||||||
| 24 | |||||||
| 25 | /** |
||||||
| 26 | * Title: Mollie |
||||||
| 27 | * Description: |
||||||
| 28 | * Copyright: 2005-2021 Pronamic |
||||||
| 29 | * Company: Pronamic |
||||||
| 30 | * |
||||||
| 31 | * @author Remco Tolsma |
||||||
| 32 | * @version 2.1.4 |
||||||
| 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 | * Config |
||||||
| 45 | * |
||||||
| 46 | * @var Config |
||||||
| 47 | */ |
||||||
| 48 | protected $config; |
||||||
| 49 | |||||||
| 50 | /** |
||||||
| 51 | * Profile data store. |
||||||
| 52 | * |
||||||
| 53 | * @var ProfileDataStore |
||||||
| 54 | */ |
||||||
| 55 | private $profile_data_store; |
||||||
| 56 | |||||||
| 57 | /** |
||||||
| 58 | * Customer data store. |
||||||
| 59 | * |
||||||
| 60 | * @var CustomerDataStore |
||||||
| 61 | */ |
||||||
| 62 | private $customer_data_store; |
||||||
| 63 | |||||||
| 64 | /** |
||||||
| 65 | * Constructs and initializes an Mollie gateway |
||||||
| 66 | * |
||||||
| 67 | * @param Config $config Config. |
||||||
| 68 | 39 | */ |
|||||
| 69 | 39 | public function __construct( Config $config ) { |
|||||
| 70 | parent::__construct( $config ); |
||||||
| 71 | 39 | ||||||
| 72 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||||||
| 73 | |||||||
| 74 | 39 | // Supported features. |
|||||
| 75 | $this->supports = array( |
||||||
| 76 | 'payment_status_request', |
||||||
| 77 | 'recurring_direct_debit', |
||||||
| 78 | 'recurring_credit_card', |
||||||
| 79 | 'recurring', |
||||||
| 80 | 'webhook', |
||||||
| 81 | 'webhook_log', |
||||||
| 82 | 'webhook_no_config', |
||||||
| 83 | ); |
||||||
| 84 | |||||||
| 85 | 39 | // Client. |
|||||
| 86 | $this->client = new Client( (string) $config->api_key ); |
||||||
| 87 | |||||||
| 88 | 39 | // Data Stores. |
|||||
| 89 | 39 | $this->profile_data_store = new ProfileDataStore(); |
|||||
| 90 | $this->customer_data_store = new CustomerDataStore(); |
||||||
| 91 | |||||||
| 92 | 39 | // Actions. |
|||||
| 93 | 39 | add_action( 'pronamic_payment_status_update', array( $this, 'copy_customer_id_to_wp_user' ), 99, 1 ); |
|||||
| 94 | } |
||||||
| 95 | |||||||
| 96 | /** |
||||||
| 97 | * Get issuers |
||||||
| 98 | * |
||||||
| 99 | * @see Core_Gateway::get_issuers() |
||||||
| 100 | * @return array<int, array<string, array<string>>> |
||||||
| 101 | 3 | */ |
|||||
| 102 | 3 | public function get_issuers() { |
|||||
| 103 | $groups = array(); |
||||||
| 104 | |||||||
| 105 | 3 | try { |
|||||
| 106 | $result = $this->client->get_issuers(); |
||||||
| 107 | |||||||
| 108 | $groups[] = array( |
||||||
| 109 | 'options' => $result, |
||||||
| 110 | 3 | ); |
|||||
| 111 | } catch ( Error $e ) { |
||||||
| 112 | 3 | // Catch Mollie error. |
|||||
| 113 | 3 | $error = new \WP_Error( |
|||||
| 114 | 3 | 'mollie_error', |
|||||
| 115 | sprintf( '%1$s (%2$s) - %3$s', $e->get_title(), $e->getCode(), $e->get_detail() ) |
||||||
| 116 | ); |
||||||
| 117 | 3 | ||||||
| 118 | $this->set_error( $error ); |
||||||
| 119 | } catch ( \Exception $e ) { |
||||||
| 120 | // Catch exceptions. |
||||||
| 121 | $error = new \WP_Error( 'mollie_error', $e->getMessage() ); |
||||||
| 122 | |||||||
| 123 | $this->set_error( $error ); |
||||||
| 124 | } |
||||||
| 125 | 3 | ||||||
| 126 | return $groups; |
||||||
| 127 | } |
||||||
| 128 | |||||||
| 129 | /** |
||||||
| 130 | * Get available payment methods. |
||||||
| 131 | * |
||||||
| 132 | * @see Core_Gateway::get_available_payment_methods() |
||||||
| 133 | * @return array<int, string> |
||||||
| 134 | 2 | */ |
|||||
| 135 | 2 | public function get_available_payment_methods() { |
|||||
| 136 | $payment_methods = array(); |
||||||
| 137 | |||||||
| 138 | 2 | // Set sequence types to get payment methods for. |
|||||
| 139 | $sequence_types = array( Sequence::ONE_OFF, Sequence::RECURRING, Sequence::FIRST ); |
||||||
| 140 | 2 | ||||||
| 141 | $results = array(); |
||||||
| 142 | 2 | ||||||
| 143 | foreach ( $sequence_types as $sequence_type ) { |
||||||
| 144 | // Get active payment methods for Mollie account. |
||||||
| 145 | 2 | try { |
|||||
| 146 | 2 | $result = $this->client->get_payment_methods( $sequence_type ); |
|||||
| 147 | } catch ( Error $e ) { |
||||||
| 148 | // Catch Mollie error. |
||||||
| 149 | $error = new \WP_Error( |
||||||
| 150 | 'mollie_error', |
||||||
| 151 | sprintf( '%1$s (%2$s) - %3$s', $e->get_title(), $e->getCode(), $e->get_detail() ) |
||||||
| 152 | ); |
||||||
| 153 | |||||||
| 154 | $this->set_error( $error ); |
||||||
| 155 | |||||||
| 156 | 2 | break; |
|||||
| 157 | } catch ( \Exception $e ) { |
||||||
| 158 | 2 | // Catch exceptions. |
|||||
| 159 | $error = new \WP_Error( 'mollie_error', $e->getMessage() ); |
||||||
| 160 | 2 | ||||||
| 161 | $this->set_error( $error ); |
||||||
| 162 | 2 | ||||||
| 163 | break; |
||||||
| 164 | } |
||||||
| 165 | 2 | ||||||
| 166 | if ( Sequence::FIRST === $sequence_type ) { |
||||||
| 167 | foreach ( $result as $method => $title ) { |
||||||
| 168 | unset( $result[ $method ] ); |
||||||
| 169 | |||||||
| 170 | // Get WordPress payment method for direct debit method. |
||||||
| 171 | $method = Methods::transform_gateway_method( $method ); |
||||||
| 172 | $payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true ); |
||||||
| 173 | |||||||
| 174 | if ( $payment_method ) { |
||||||
| 175 | $results[ $payment_method ] = $title; |
||||||
| 176 | } |
||||||
| 177 | } |
||||||
| 178 | } |
||||||
| 179 | 2 | ||||||
| 180 | 2 | if ( is_array( $result ) ) { |
|||||
| 181 | $results = array_merge( $results, $result ); |
||||||
| 182 | } |
||||||
| 183 | } |
||||||
| 184 | |||||||
| 185 | 2 | // Transform to WordPress payment methods. |
|||||
| 186 | 2 | foreach ( $results as $method => $title ) { |
|||||
| 187 | $method = (string) $method; |
||||||
| 188 | 2 | ||||||
| 189 | $payment_method = Methods::transform_gateway_method( $method ); |
||||||
| 190 | 2 | ||||||
| 191 | if ( PaymentMethods::is_recurring_method( $method ) ) { |
||||||
| 192 | $payment_method = $method; |
||||||
| 193 | } |
||||||
| 194 | 2 | ||||||
| 195 | 2 | if ( null !== $payment_method ) { |
|||||
| 196 | $payment_methods[] = (string) $payment_method; |
||||||
| 197 | } |
||||||
| 198 | } |
||||||
| 199 | 2 | ||||||
| 200 | $payment_methods = array_unique( $payment_methods ); |
||||||
| 201 | 2 | ||||||
| 202 | return $payment_methods; |
||||||
| 203 | } |
||||||
| 204 | |||||||
| 205 | /** |
||||||
| 206 | * Get supported payment methods |
||||||
| 207 | * |
||||||
| 208 | * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods() |
||||||
| 209 | * @return array<string> |
||||||
| 210 | 2 | */ |
|||||
| 211 | public function get_supported_payment_methods() { |
||||||
| 212 | 2 | return array( |
|||||
| 213 | PaymentMethods::APPLE_PAY, |
||||||
| 214 | PaymentMethods::BANCONTACT, |
||||||
| 215 | PaymentMethods::BANK_TRANSFER, |
||||||
| 216 | PaymentMethods::BELFIUS, |
||||||
| 217 | PaymentMethods::CREDIT_CARD, |
||||||
| 218 | PaymentMethods::DIRECT_DEBIT, |
||||||
| 219 | PaymentMethods::DIRECT_DEBIT_BANCONTACT, |
||||||
| 220 | PaymentMethods::DIRECT_DEBIT_IDEAL, |
||||||
| 221 | PaymentMethods::DIRECT_DEBIT_SOFORT, |
||||||
| 222 | PaymentMethods::EPS, |
||||||
| 223 | PaymentMethods::GIROPAY, |
||||||
| 224 | PaymentMethods::IDEAL, |
||||||
| 225 | PaymentMethods::KBC, |
||||||
| 226 | PaymentMethods::PAYPAL, |
||||||
| 227 | PaymentMethods::PRZELEWY24, |
||||||
| 228 | PaymentMethods::SOFORT, |
||||||
| 229 | ); |
||||||
| 230 | } |
||||||
| 231 | |||||||
| 232 | /** |
||||||
| 233 | * Get webhook URL for Mollie. |
||||||
| 234 | * |
||||||
| 235 | * @return string|null |
||||||
| 236 | 4 | */ |
|||||
| 237 | 4 | public function get_webhook_url() { |
|||||
| 238 | $url = \rest_url( Integration::REST_ROUTE_NAMESPACE . '/webhook' ); |
||||||
| 239 | 4 | ||||||
| 240 | $host = wp_parse_url( $url, PHP_URL_HOST ); |
||||||
| 241 | 4 | ||||||
| 242 | if ( is_array( $host ) ) { |
||||||
| 243 | // Parsing failure. |
||||||
| 244 | $host = ''; |
||||||
| 245 | } |
||||||
| 246 | 4 | ||||||
| 247 | if ( 'localhost' === $host ) { |
||||||
| 248 | 1 | // Mollie doesn't allow localhost. |
|||||
| 249 | 3 | return null; |
|||||
| 250 | } elseif ( '.dev' === substr( $host, -4 ) ) { |
||||||
| 251 | 1 | // Mollie doesn't allow the .dev TLD. |
|||||
| 252 | 2 | return null; |
|||||
| 253 | } elseif ( '.local' === substr( $host, -6 ) ) { |
||||||
| 254 | 1 | // Mollie doesn't allow the .local TLD. |
|||||
| 255 | 1 | return null; |
|||||
| 256 | } elseif ( '.test' === substr( $host, -5 ) ) { |
||||||
| 257 | // Mollie doesn't allow the .test TLD. |
||||||
| 258 | return null; |
||||||
| 259 | } |
||||||
| 260 | 1 | ||||||
| 261 | return $url; |
||||||
| 262 | } |
||||||
| 263 | |||||||
| 264 | /** |
||||||
| 265 | * Start |
||||||
| 266 | * |
||||||
| 267 | * @see Pronamic_WP_Pay_Gateway::start() |
||||||
| 268 | * @param Payment $payment Payment. |
||||||
| 269 | * @return void |
||||||
| 270 | * @throws \Exception Throws exception on error creating Mollie customer for payment. |
||||||
| 271 | */ |
||||||
| 272 | public function start( Payment $payment ) { |
||||||
| 273 | $request = new PaymentRequest( |
||||||
| 274 | AmountTransformer::transform( $payment->get_total_amount() ), |
||||||
| 275 | (string) $payment->get_description() |
||||||
| 276 | ); |
||||||
| 277 | |||||||
| 278 | $request->redirect_url = $payment->get_return_url(); |
||||||
| 279 | $request->webhook_url = $this->get_webhook_url(); |
||||||
| 280 | |||||||
| 281 | // Locale. |
||||||
| 282 | $customer = $payment->get_customer(); |
||||||
| 283 | |||||||
| 284 | if ( null !== $customer ) { |
||||||
| 285 | $request->locale = LocaleHelper::transform( $customer->get_locale() ); |
||||||
| 286 | } |
||||||
| 287 | |||||||
| 288 | // Customer ID. |
||||||
| 289 | $customer_id = $this->get_customer_id_for_payment( $payment ); |
||||||
| 290 | |||||||
| 291 | if ( null === $customer_id ) { |
||||||
| 292 | $customer_id = $this->create_customer_for_payment( $payment ); |
||||||
| 293 | } |
||||||
| 294 | |||||||
| 295 | if ( null !== $customer_id ) { |
||||||
| 296 | $request->customer_id = $customer_id; |
||||||
| 297 | } |
||||||
| 298 | |||||||
| 299 | // Payment method. |
||||||
| 300 | $payment_method = $payment->get_method(); |
||||||
| 301 | |||||||
| 302 | // Recurring payment method. |
||||||
| 303 | $subscription = $payment->get_subscription(); |
||||||
| 304 | |||||||
| 305 | $is_recurring_method = ( $subscription && PaymentMethods::is_recurring_method( (string) $payment_method ) ); |
||||||
| 306 | |||||||
| 307 | // Consumer bank details. |
||||||
| 308 | $consumer_bank_details = $payment->get_consumer_bank_details(); |
||||||
| 309 | |||||||
| 310 | if ( PaymentMethods::DIRECT_DEBIT === $payment_method && null !== $consumer_bank_details ) { |
||||||
| 311 | $consumer_name = $consumer_bank_details->get_name(); |
||||||
| 312 | $consumer_iban = $consumer_bank_details->get_iban(); |
||||||
| 313 | |||||||
| 314 | $request->consumer_name = $consumer_name; |
||||||
| 315 | $request->consumer_account = $consumer_iban; |
||||||
| 316 | |||||||
| 317 | // Check if one-off SEPA Direct Debit can be used, otherwise short circuit payment. |
||||||
| 318 | if ( null !== $customer_id ) { |
||||||
| 319 | // Find or create mandate. |
||||||
| 320 | $mandate_id = $this->client->has_valid_mandate( $customer_id, PaymentMethods::DIRECT_DEBIT, $consumer_iban ); |
||||||
| 321 | |||||||
| 322 | if ( false === $mandate_id ) { |
||||||
| 323 | $mandate = $this->client->create_mandate( $customer_id, $consumer_bank_details ); |
||||||
| 324 | |||||||
| 325 | if ( ! \property_exists( $mandate, 'id' ) ) { |
||||||
| 326 | throw new \Exception( 'Missing mandate ID.' ); |
||||||
| 327 | } |
||||||
| 328 | |||||||
| 329 | $mandate_id = $mandate->id; |
||||||
| 330 | } |
||||||
| 331 | |||||||
| 332 | // Charge immediately on-demand. |
||||||
| 333 | $request->set_sequence_type( Sequence::RECURRING ); |
||||||
| 334 | $request->set_mandate_id( (string) $mandate_id ); |
||||||
| 335 | |||||||
| 336 | $is_recurring_method = true; |
||||||
| 337 | |||||||
| 338 | $payment->recurring = true; |
||||||
| 339 | } |
||||||
| 340 | } |
||||||
| 341 | |||||||
| 342 | if ( false === $is_recurring_method && null !== $payment_method ) { |
||||||
| 343 | // Always use 'direct debit mandate via iDEAL/Bancontact/Sofort' payment methods as recurring method. |
||||||
| 344 | $is_recurring_method = PaymentMethods::is_direct_debit_method( $payment_method ); |
||||||
| 345 | |||||||
| 346 | // Check for non-recurring methods for subscription payments. |
||||||
| 347 | if ( false === $is_recurring_method && null !== $payment->get_periods() ) { |
||||||
| 348 | $direct_debit_methods = PaymentMethods::get_direct_debit_methods(); |
||||||
| 349 | |||||||
| 350 | $is_recurring_method = \in_array( $payment_method, $direct_debit_methods, true ); |
||||||
| 351 | } |
||||||
| 352 | } |
||||||
| 353 | |||||||
| 354 | if ( $is_recurring_method ) { |
||||||
| 355 | $request->sequence_type = $payment->get_recurring() ? Sequence::RECURRING : Sequence::FIRST; |
||||||
| 356 | |||||||
| 357 | if ( Sequence::FIRST === $request->sequence_type ) { |
||||||
| 358 | $payment_method = PaymentMethods::get_first_payment_method( $payment_method ); |
||||||
| 359 | } |
||||||
| 360 | |||||||
| 361 | if ( Sequence::RECURRING === $request->sequence_type ) { |
||||||
| 362 | // Use mandate from subscription. |
||||||
| 363 | if ( $subscription && empty( $request->mandate_id ) ) { |
||||||
| 364 | $subscription_mandate_id = $subscription->get_meta( 'mollie_mandate_id' ); |
||||||
| 365 | |||||||
| 366 | if ( false !== $subscription_mandate_id ) { |
||||||
| 367 | $request->set_mandate_id( $subscription_mandate_id ); |
||||||
| 368 | } |
||||||
| 369 | } |
||||||
| 370 | |||||||
| 371 | // Use credit card for recurring Apple Pay payments. |
||||||
| 372 | if ( PaymentMethods::APPLE_PAY === $payment_method ) { |
||||||
| 373 | $payment_method = PaymentMethods::CREDIT_CARD; |
||||||
| 374 | } |
||||||
| 375 | |||||||
| 376 | $direct_debit_methods = PaymentMethods::get_direct_debit_methods(); |
||||||
| 377 | |||||||
| 378 | $recurring_method = \array_search( $payment_method, $direct_debit_methods, true ); |
||||||
| 379 | |||||||
| 380 | if ( \is_string( $recurring_method ) ) { |
||||||
| 381 | $payment_method = $recurring_method; |
||||||
| 382 | } |
||||||
| 383 | |||||||
| 384 | $payment->set_action_url( $payment->get_return_url() ); |
||||||
| 385 | } |
||||||
| 386 | } |
||||||
| 387 | |||||||
| 388 | // Leap of faith if the WordPress payment method could not transform to a Mollie method? |
||||||
| 389 | $request->method = Methods::transform( $payment_method, $payment_method ); |
||||||
| 390 | |||||||
| 391 | /** |
||||||
| 392 | * Metadata. |
||||||
| 393 | * |
||||||
| 394 | * Provide any data you like, for example a string or a JSON object. |
||||||
| 395 | * We will save the data alongside the payment. Whenever you fetch |
||||||
| 396 | * the payment with our API, we’ll also include the metadata. You |
||||||
| 397 | * can use up to approximately 1kB. |
||||||
| 398 | * |
||||||
| 399 | * @link https://docs.mollie.com/reference/v2/payments-api/create-payment |
||||||
| 400 | * @link https://en.wikipedia.org/wiki/Metadata |
||||||
| 401 | */ |
||||||
| 402 | $metadata = null; |
||||||
| 403 | |||||||
| 404 | /** |
||||||
| 405 | * Filters the Mollie metadata. |
||||||
| 406 | * |
||||||
| 407 | * @since 2.2.0 |
||||||
| 408 | * |
||||||
| 409 | * @param mixed $metadata Metadata. |
||||||
| 410 | * @param Payment $payment Payment. |
||||||
| 411 | */ |
||||||
| 412 | $metadata = \apply_filters( 'pronamic_pay_mollie_payment_metadata', $metadata, $payment ); |
||||||
| 413 | |||||||
| 414 | $request->set_metadata( $metadata ); |
||||||
| 415 | |||||||
| 416 | // Issuer. |
||||||
| 417 | if ( Methods::IDEAL === $request->method ) { |
||||||
| 418 | $request->issuer = $payment->get_issuer(); |
||||||
| 419 | } |
||||||
| 420 | |||||||
| 421 | // Billing email. |
||||||
| 422 | $billing_email = $payment->get_email(); |
||||||
| 423 | |||||||
| 424 | /** |
||||||
| 425 | * Filters the Mollie payment billing email used for bank transfer payment instructions. |
||||||
| 426 | * |
||||||
| 427 | * @since 2.2.0 |
||||||
| 428 | * |
||||||
| 429 | * @param string|null $billing_email Billing email. |
||||||
| 430 | * @param Payment $payment Payment. |
||||||
| 431 | */ |
||||||
| 432 | $billing_email = \apply_filters( 'pronamic_pay_mollie_payment_billing_email', $billing_email, $payment ); |
||||||
| 433 | |||||||
| 434 | $request->set_billing_email( $billing_email ); |
||||||
| 435 | |||||||
| 436 | // Due date. |
||||||
| 437 | if ( ! empty( $this->config->due_date_days ) ) { |
||||||
| 438 | try { |
||||||
| 439 | $due_date = new DateTime( sprintf( '+%s days', $this->config->due_date_days ) ); |
||||||
| 440 | } catch ( \Exception $e ) { |
||||||
| 441 | $due_date = null; |
||||||
| 442 | } |
||||||
| 443 | |||||||
| 444 | $request->set_due_date( $due_date ); |
||||||
| 445 | } |
||||||
| 446 | |||||||
| 447 | // Create payment. |
||||||
| 448 | $result = $this->client->create_payment( $request ); |
||||||
| 449 | |||||||
| 450 | // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object. |
||||||
| 451 | |||||||
| 452 | // Set transaction ID. |
||||||
| 453 | if ( isset( $result->id ) ) { |
||||||
| 454 | $payment->set_transaction_id( $result->id ); |
||||||
| 455 | } |
||||||
| 456 | |||||||
| 457 | // Set expiry date. |
||||||
| 458 | if ( isset( $result->expiresAt ) ) { |
||||||
| 459 | try { |
||||||
| 460 | $expires_at = new DateTime( $result->expiresAt ); |
||||||
| 461 | } catch ( \Exception $e ) { |
||||||
| 462 | $expires_at = null; |
||||||
| 463 | } |
||||||
| 464 | |||||||
| 465 | $payment->set_expiry_date( $expires_at ); |
||||||
| 466 | } |
||||||
| 467 | |||||||
| 468 | // Set status. |
||||||
| 469 | if ( isset( $result->status ) ) { |
||||||
| 470 | $payment->set_status( Statuses::transform( $result->status ) ); |
||||||
| 471 | } |
||||||
| 472 | |||||||
| 473 | // Set bank transfer recipient details. |
||||||
| 474 | if ( isset( $result->details ) ) { |
||||||
| 475 | $bank_transfer_recipient_details = $payment->get_bank_transfer_recipient_details(); |
||||||
| 476 | |||||||
| 477 | if ( null === $bank_transfer_recipient_details ) { |
||||||
| 478 | $bank_transfer_recipient_details = new BankTransferDetails(); |
||||||
| 479 | |||||||
| 480 | $payment->set_bank_transfer_recipient_details( $bank_transfer_recipient_details ); |
||||||
| 481 | } |
||||||
| 482 | |||||||
| 483 | $bank_details = $bank_transfer_recipient_details->get_bank_account(); |
||||||
| 484 | |||||||
| 485 | if ( null === $bank_details ) { |
||||||
| 486 | $bank_details = new BankAccountDetails(); |
||||||
| 487 | |||||||
| 488 | $bank_transfer_recipient_details->set_bank_account( $bank_details ); |
||||||
| 489 | } |
||||||
| 490 | |||||||
| 491 | $details = $result->details; |
||||||
| 492 | |||||||
| 493 | if ( isset( $details->bankName ) ) { |
||||||
| 494 | /** |
||||||
| 495 | * Set `bankName` as bank details name, as result "Stichting Mollie Payments" |
||||||
| 496 | * is not the name of a bank, but the account holder name. |
||||||
| 497 | */ |
||||||
| 498 | $bank_details->set_name( $details->bankName ); |
||||||
| 499 | } |
||||||
| 500 | |||||||
| 501 | if ( isset( $details->bankAccount ) ) { |
||||||
| 502 | $bank_details->set_iban( $details->bankAccount ); |
||||||
| 503 | } |
||||||
| 504 | |||||||
| 505 | if ( isset( $details->bankBic ) ) { |
||||||
| 506 | $bank_details->set_bic( $details->bankBic ); |
||||||
| 507 | } |
||||||
| 508 | |||||||
| 509 | if ( isset( $details->transferReference ) ) { |
||||||
| 510 | $bank_transfer_recipient_details->set_reference( $details->transferReference ); |
||||||
| 511 | } |
||||||
| 512 | } |
||||||
| 513 | |||||||
| 514 | // Handle links. |
||||||
| 515 | if ( isset( $result->_links ) ) { |
||||||
| 516 | $links = $result->_links; |
||||||
| 517 | |||||||
| 518 | // Action URL. |
||||||
| 519 | if ( isset( $links->checkout->href ) ) { |
||||||
| 520 | $payment->set_action_url( $links->checkout->href ); |
||||||
| 521 | } |
||||||
| 522 | |||||||
| 523 | // Change payment state URL. |
||||||
| 524 | if ( isset( $links->changePaymentState->href ) ) { |
||||||
| 525 | $payment->set_meta( 'mollie_change_payment_state_url', $links->changePaymentState->href ); |
||||||
| 526 | } |
||||||
| 527 | } |
||||||
| 528 | |||||||
| 529 | // phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object. |
||||||
| 530 | } |
||||||
| 531 | |||||||
| 532 | /** |
||||||
| 533 | * Update status of the specified payment |
||||||
| 534 | * |
||||||
| 535 | * @param Payment $payment Payment. |
||||||
| 536 | * @return void |
||||||
| 537 | */ |
||||||
| 538 | public function update_status( Payment $payment ) { |
||||||
| 539 | $transaction_id = $payment->get_transaction_id(); |
||||||
| 540 | |||||||
| 541 | if ( null === $transaction_id ) { |
||||||
| 542 | return; |
||||||
| 543 | } |
||||||
| 544 | |||||||
| 545 | $mollie_payment = $this->client->get_payment( $transaction_id ); |
||||||
| 546 | |||||||
| 547 | $payment->set_status( Statuses::transform( $mollie_payment->get_status() ) ); |
||||||
| 548 | |||||||
| 549 | /** |
||||||
| 550 | * Mollie profile. |
||||||
| 551 | */ |
||||||
| 552 | $mollie_profile = new Profile(); |
||||||
| 553 | |||||||
| 554 | $mollie_profile->set_id( $mollie_payment->get_profile_id() ); |
||||||
| 555 | |||||||
| 556 | $profile_internal_id = $this->profile_data_store->get_or_insert_profile( $mollie_profile ); |
||||||
| 557 | |||||||
| 558 | /** |
||||||
| 559 | * If the Mollie payment contains a customer ID we will try to connect |
||||||
| 560 | * this Mollie customer ID the WordPress user and subscription. |
||||||
| 561 | * This can be usefull in case when a WordPress user is created after |
||||||
| 562 | * a succesfull payment. |
||||||
| 563 | * |
||||||
| 564 | * @link https://www.gravityforms.com/add-ons/user-registration/ |
||||||
| 565 | */ |
||||||
| 566 | $mollie_customer_id = $mollie_payment->get_customer_id(); |
||||||
| 567 | |||||||
| 568 | if ( null !== $mollie_customer_id ) { |
||||||
| 569 | $mollie_customer = new Customer( $mollie_customer_id ); |
||||||
| 570 | |||||||
| 571 | $customer_internal_id = $this->customer_data_store->get_or_insert_customer( |
||||||
| 572 | $mollie_customer, |
||||||
| 573 | array( |
||||||
| 574 | 'profile_id' => $profile_internal_id, |
||||||
| 575 | ), |
||||||
| 576 | array( |
||||||
| 577 | 'profile_id' => '%s', |
||||||
| 578 | ) |
||||||
| 579 | ); |
||||||
| 580 | |||||||
| 581 | // Meta. |
||||||
| 582 | $customer_id = $payment->get_meta( 'mollie_customer_id' ); |
||||||
| 583 | |||||||
| 584 | if ( empty( $customer_id ) ) { |
||||||
| 585 | $payment->set_meta( 'mollie_customer_id', $mollie_customer->get_id() ); |
||||||
| 586 | } |
||||||
| 587 | |||||||
| 588 | // Customer. |
||||||
| 589 | $customer = $payment->get_customer(); |
||||||
| 590 | |||||||
| 591 | if ( null !== $customer ) { |
||||||
| 592 | // Connect to user. |
||||||
| 593 | $user_id = $customer->get_user_id(); |
||||||
| 594 | |||||||
| 595 | if ( null !== $user_id ) { |
||||||
| 596 | $user = \get_user_by( 'id', $user_id ); |
||||||
| 597 | |||||||
| 598 | if ( false !== $user ) { |
||||||
| 599 | $this->customer_data_store->connect_mollie_customer_to_wp_user( $mollie_customer, $user ); |
||||||
| 600 | } |
||||||
| 601 | } |
||||||
| 602 | } |
||||||
| 603 | |||||||
| 604 | // Subscription. |
||||||
| 605 | $subscription = $payment->get_subscription(); |
||||||
| 606 | |||||||
| 607 | if ( null !== $subscription ) { |
||||||
| 608 | $customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||||||
| 609 | |||||||
| 610 | if ( empty( $customer_id ) ) { |
||||||
| 611 | $subscription->set_meta( 'mollie_customer_id', $mollie_customer->get_id() ); |
||||||
| 612 | } |
||||||
| 613 | |||||||
| 614 | // Update mandate in subscription meta. |
||||||
| 615 | $mollie_mandate_id = $mollie_payment->get_mandate_id(); |
||||||
| 616 | |||||||
| 617 | if ( null !== $mollie_mandate_id ) { |
||||||
| 618 | $mandate_id = $subscription->get_meta( 'mollie_mandate_id' ); |
||||||
| 619 | |||||||
| 620 | // Only update if no mandate has been set yet or if payment succeeded. |
||||||
| 621 | if ( empty( $mandate_id ) || PaymentStatus::SUCCESS === $payment->get_status() ) { |
||||||
| 622 | $this->update_subscription_mandate( $subscription, $mollie_mandate_id, $payment->get_method() ); |
||||||
| 623 | } |
||||||
| 624 | } |
||||||
| 625 | } |
||||||
| 626 | } |
||||||
| 627 | |||||||
| 628 | // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object. |
||||||
| 629 | $mollie_payment_details = $mollie_payment->get_details(); |
||||||
| 630 | |||||||
| 631 | if ( null !== $mollie_payment_details ) { |
||||||
| 632 | $consumer_bank_details = $payment->get_consumer_bank_details(); |
||||||
| 633 | |||||||
| 634 | if ( null === $consumer_bank_details ) { |
||||||
| 635 | $consumer_bank_details = new BankAccountDetails(); |
||||||
| 636 | |||||||
| 637 | $payment->set_consumer_bank_details( $consumer_bank_details ); |
||||||
| 638 | } |
||||||
| 639 | |||||||
| 640 | if ( isset( $mollie_payment_details->consumerName ) ) { |
||||||
| 641 | $consumer_bank_details->set_name( $mollie_payment_details->consumerName ); |
||||||
| 642 | } |
||||||
| 643 | |||||||
| 644 | if ( isset( $mollie_payment_details->cardHolder ) ) { |
||||||
| 645 | $consumer_bank_details->set_name( $mollie_payment_details->cardHolder ); |
||||||
| 646 | } |
||||||
| 647 | |||||||
| 648 | if ( isset( $mollie_payment_details->cardNumber ) ) { |
||||||
| 649 | // The last four digits of the card number. |
||||||
| 650 | $consumer_bank_details->set_account_number( $mollie_payment_details->cardNumber ); |
||||||
| 651 | } |
||||||
| 652 | |||||||
| 653 | if ( isset( $mollie_payment_details->cardCountryCode ) ) { |
||||||
| 654 | // The ISO 3166-1 alpha-2 country code of the country the card was issued in. |
||||||
| 655 | $consumer_bank_details->set_country( $mollie_payment_details->cardCountryCode ); |
||||||
| 656 | } |
||||||
| 657 | |||||||
| 658 | if ( isset( $mollie_payment_details->consumerAccount ) ) { |
||||||
| 659 | switch ( $mollie_payment->get_method() ) { |
||||||
| 660 | case Methods::BELFIUS: |
||||||
| 661 | case Methods::DIRECT_DEBIT: |
||||||
| 662 | case Methods::IDEAL: |
||||||
| 663 | case Methods::KBC: |
||||||
| 664 | case Methods::SOFORT: |
||||||
| 665 | $consumer_bank_details->set_iban( $mollie_payment_details->consumerAccount ); |
||||||
| 666 | |||||||
| 667 | break; |
||||||
| 668 | case Methods::BANCONTACT: |
||||||
| 669 | case Methods::BANKTRANSFER: |
||||||
| 670 | case Methods::PAYPAL: |
||||||
| 671 | default: |
||||||
| 672 | $consumer_bank_details->set_account_number( $mollie_payment_details->consumerAccount ); |
||||||
| 673 | |||||||
| 674 | break; |
||||||
| 675 | } |
||||||
| 676 | } |
||||||
| 677 | |||||||
| 678 | if ( isset( $mollie_payment_details->consumerBic ) ) { |
||||||
| 679 | $consumer_bank_details->set_bic( $mollie_payment_details->consumerBic ); |
||||||
| 680 | } |
||||||
| 681 | |||||||
| 682 | /* |
||||||
| 683 | * Failure reason. |
||||||
| 684 | */ |
||||||
| 685 | $failure_reason = $payment->get_failure_reason(); |
||||||
| 686 | |||||||
| 687 | if ( null === $failure_reason ) { |
||||||
| 688 | $failure_reason = new FailureReason(); |
||||||
| 689 | |||||||
| 690 | $payment->set_failure_reason( $failure_reason ); |
||||||
| 691 | } |
||||||
| 692 | |||||||
| 693 | // SEPA Direct Debit. |
||||||
| 694 | if ( isset( $mollie_payment_details->bankReasonCode ) ) { |
||||||
| 695 | $failure_reason->set_code( $mollie_payment_details->bankReasonCode ); |
||||||
| 696 | } |
||||||
| 697 | |||||||
| 698 | if ( isset( $mollie_payment_details->bankReason ) ) { |
||||||
| 699 | $failure_reason->set_message( $mollie_payment_details->bankReason ); |
||||||
| 700 | } |
||||||
| 701 | |||||||
| 702 | // Credit card. |
||||||
| 703 | if ( isset( $mollie_payment_details->failureReason ) ) { |
||||||
| 704 | $failure_reason->set_code( $mollie_payment_details->failureReason ); |
||||||
| 705 | } |
||||||
| 706 | |||||||
| 707 | if ( isset( $mollie_payment_details->failureMessage ) ) { |
||||||
| 708 | $failure_reason->set_message( $mollie_payment_details->failureMessage ); |
||||||
| 709 | } |
||||||
| 710 | } |
||||||
| 711 | |||||||
| 712 | $links = $mollie_payment->get_links(); |
||||||
| 713 | |||||||
| 714 | // Change payment state URL. |
||||||
| 715 | if ( \property_exists( $links, 'changePaymentState' ) ) { |
||||||
| 716 | $payment->set_meta( 'mollie_change_payment_state_url', $links->changePaymentState->href ); |
||||||
| 717 | } |
||||||
| 718 | |||||||
| 719 | // phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object. |
||||||
| 720 | |||||||
| 721 | if ( $mollie_payment->has_chargebacks() ) { |
||||||
| 722 | $mollie_chargebacks = $this->client->get_payment_chargebacks( |
||||||
| 723 | $mollie_payment->get_id(), |
||||||
| 724 | array( 'limit' => 1 ) |
||||||
| 725 | ); |
||||||
| 726 | |||||||
| 727 | $mollie_chargeback = \reset( $mollie_chargebacks ); |
||||||
| 728 | |||||||
| 729 | if ( false !== $mollie_chargeback ) { |
||||||
| 730 | $subscriptions = array_filter( |
||||||
| 731 | $payment->get_subscriptions(), |
||||||
| 732 | function( $subscription ) { |
||||||
| 733 | return SubscriptionStatus::ACTIVE === $subscription->get_status(); |
||||||
| 734 | } |
||||||
| 735 | ); |
||||||
| 736 | |||||||
| 737 | foreach ( $subscriptions as $subscription ) { |
||||||
| 738 | if ( $mollie_chargeback->get_created_at() > $subscription->get_activated_at() ) { |
||||||
| 739 | $subscription->set_status( SubscriptionStatus::ON_HOLD ); |
||||||
| 740 | |||||||
| 741 | $subscription->add_note( |
||||||
| 742 | \sprintf( |
||||||
| 743 | /* translators: 1: Mollie chargeback ID, 2: Mollie payment ID */ |
||||||
| 744 | \__( 'Subscription put on hold due to chargeback `%1$s` of payment `%2$s`.', 'pronamic_ideal' ), |
||||||
| 745 | \esc_html( $mollie_chargeback->get_id() ), |
||||||
| 746 | \esc_html( $mollie_payment->get_id() ) |
||||||
| 747 | ) |
||||||
| 748 | ); |
||||||
| 749 | |||||||
| 750 | $subscription->save(); |
||||||
| 751 | } |
||||||
| 752 | } |
||||||
| 753 | } |
||||||
| 754 | } |
||||||
| 755 | |||||||
| 756 | // Refunds. |
||||||
| 757 | $amount_refunded = $mollie_payment->get_amount_refunded(); |
||||||
| 758 | |||||||
| 759 | if ( null !== $amount_refunded ) { |
||||||
| 760 | $refunded_amount = new Money( $amount_refunded->get_value(), $amount_refunded->get_currency() ); |
||||||
| 761 | |||||||
| 762 | $payment->set_refunded_amount( $refunded_amount ); |
||||||
|
0 ignored issues
–
show
The method
set_refunded_amount() does not exist on Pronamic\WordPress\Pay\Payments\Payment.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 763 | } |
||||||
| 764 | } |
||||||
| 765 | |||||||
| 766 | /** |
||||||
| 767 | * Update subscription mandate. |
||||||
| 768 | * |
||||||
| 769 | * @param Subscription $subscription Subscription. |
||||||
| 770 | * @param string $mandate_id Mollie mandate ID. |
||||||
| 771 | * @param string|null $payment_method Payment method. |
||||||
| 772 | * @return void |
||||||
| 773 | * @throws \Exception Throws exception if subscription note could not be added. |
||||||
| 774 | */ |
||||||
| 775 | public function update_subscription_mandate( Subscription $subscription, $mandate_id, $payment_method = null ) { |
||||||
| 776 | $customer_id = (string) $subscription->get_meta( 'mollie_customer_id' ); |
||||||
| 777 | |||||||
| 778 | $mandate = $this->client->get_mandate( $mandate_id, $customer_id ); |
||||||
| 779 | |||||||
| 780 | if ( ! \is_object( $mandate ) ) { |
||||||
| 781 | return; |
||||||
| 782 | } |
||||||
| 783 | |||||||
| 784 | // Update mandate. |
||||||
| 785 | $old_mandate_id = $subscription->get_meta( 'mollie_mandate_id' ); |
||||||
| 786 | |||||||
| 787 | $subscription->set_meta( 'mollie_mandate_id', $mandate_id ); |
||||||
| 788 | |||||||
| 789 | if ( ! empty( $old_mandate_id ) && $old_mandate_id !== $mandate_id ) { |
||||||
| 790 | $note = \sprintf( |
||||||
| 791 | /* translators: 1: old mandate ID, 2: new mandate ID */ |
||||||
| 792 | \__( 'Mandate for subscription changed from "%1$s" to "%2$s".', 'pronamic_ideal' ), |
||||||
| 793 | \esc_html( $old_mandate_id ), |
||||||
| 794 | \esc_html( $mandate_id ) |
||||||
| 795 | ); |
||||||
| 796 | |||||||
| 797 | $subscription->add_note( $note ); |
||||||
| 798 | } |
||||||
| 799 | |||||||
| 800 | // Update payment method. |
||||||
| 801 | $old_method = $subscription->payment_method; |
||||||
| 802 | $new_method = ( null === $payment_method && \property_exists( $mandate, 'method' ) ? Methods::transform_gateway_method( $mandate->method ) : $payment_method ); |
||||||
| 803 | |||||||
| 804 | // `Direct Debit` is not a recurring method, use `Direct Debit (mandate via ...)` instead. |
||||||
| 805 | if ( PaymentMethods::DIRECT_DEBIT === $new_method ) { |
||||||
| 806 | $new_method = PaymentMethods::DIRECT_DEBIT_IDEAL; |
||||||
| 807 | |||||||
| 808 | // Use `Direct Debit (mandate via Bancontact)` if consumer account starts with `BE`. |
||||||
| 809 | if ( \property_exists( $mandate, 'details' ) && 'BE' === \substr( $mandate->details->consumerAccount, 0, 2 ) ) { |
||||||
| 810 | $new_method = PaymentMethods::DIRECT_DEBIT_BANCONTACT; |
||||||
| 811 | } |
||||||
| 812 | } |
||||||
| 813 | |||||||
| 814 | if ( ! empty( $old_method ) && $old_method !== $new_method ) { |
||||||
| 815 | $subscription->payment_method = $new_method; |
||||||
| 816 | |||||||
| 817 | // Add note. |
||||||
| 818 | $note = \sprintf( |
||||||
| 819 | /* translators: 1: old payment method, 2: new payment method */ |
||||||
| 820 | \__( 'Payment method for subscription changed from "%1$s" to "%2$s".', 'pronamic_ideal' ), |
||||||
| 821 | \esc_html( (string) PaymentMethods::get_name( $old_method ) ), |
||||||
| 822 | \esc_html( (string) PaymentMethods::get_name( $new_method ) ) |
||||||
| 823 | ); |
||||||
| 824 | |||||||
| 825 | $subscription->add_note( $note ); |
||||||
| 826 | } |
||||||
| 827 | 10 | ||||||
| 828 | 10 | $subscription->save(); |
|||||
| 829 | } |
||||||
| 830 | 10 | ||||||
| 831 | /** |
||||||
| 832 | 10 | * Get Mollie customer ID for payment. |
|||||
| 833 | * |
||||||
| 834 | * @param Payment $payment Payment. |
||||||
| 835 | * @return string|null |
||||||
| 836 | */ |
||||||
| 837 | public function get_customer_id_for_payment( Payment $payment ) { |
||||||
| 838 | $customer_ids = $this->get_customer_ids_for_payment( $payment ); |
||||||
| 839 | |||||||
| 840 | $customer_id = $this->get_first_existing_customer_id( $customer_ids ); |
||||||
| 841 | 10 | ||||||
| 842 | 10 | return $customer_id; |
|||||
| 843 | } |
||||||
| 844 | |||||||
| 845 | 10 | /** |
|||||
| 846 | * Get Mollie customers for the specified payment. |
||||||
| 847 | 10 | * |
|||||
| 848 | 10 | * @param Payment $payment Payment. |
|||||
| 849 | * @return array<string> |
||||||
| 850 | 10 | */ |
|||||
| 851 | 4 | private function get_customer_ids_for_payment( Payment $payment ) { |
|||||
| 852 | $customer_ids = array(); |
||||||
| 853 | |||||||
| 854 | // Customer ID from subscription meta. |
||||||
| 855 | $subscription = $payment->get_subscription(); |
||||||
| 856 | 10 | ||||||
| 857 | if ( null !== $subscription ) { |
||||||
| 858 | 10 | $customer_id = $this->get_customer_id_for_subscription( $subscription ); |
|||||
| 859 | 10 | ||||||
| 860 | if ( null !== $customer_id ) { |
||||||
| 861 | 10 | $customer_ids[] = $customer_id; |
|||||
| 862 | 7 | } |
|||||
| 863 | } |
||||||
| 864 | 7 | ||||||
| 865 | // Customer ID from WordPress user. |
||||||
| 866 | $customer = $payment->get_customer(); |
||||||
| 867 | |||||||
| 868 | 10 | if ( null !== $customer ) { |
|||||
| 869 | $user_id = $customer->get_user_id(); |
||||||
| 870 | |||||||
| 871 | if ( ! empty( $user_id ) ) { |
||||||
| 872 | $user_customer_ids = $this->get_customer_ids_for_user( $user_id ); |
||||||
| 873 | |||||||
| 874 | $customer_ids = \array_merge( $customer_ids, $user_customer_ids ); |
||||||
| 875 | } |
||||||
| 876 | } |
||||||
| 877 | 24 | ||||||
| 878 | 24 | return $customer_ids; |
|||||
| 879 | } |
||||||
| 880 | 24 | ||||||
| 881 | /** |
||||||
| 882 | * Get Mollie customers for the specified WordPress user ID. |
||||||
| 883 | * |
||||||
| 884 | 24 | * @param int $user_id WordPress user ID. |
|||||
| 885 | * @return array<string> |
||||||
| 886 | 24 | */ |
|||||
| 887 | public function get_customer_ids_for_user( $user_id ) { |
||||||
| 888 | 24 | $customer_query = new CustomerQuery( |
|||||
| 889 | array( |
||||||
| 890 | 'user_id' => $user_id, |
||||||
| 891 | ) |
||||||
| 892 | ); |
||||||
| 893 | |||||||
| 894 | $customers = $customer_query->get_customers(); |
||||||
| 895 | |||||||
| 896 | $customer_ids = wp_list_pluck( $customers, 'mollie_id' ); |
||||||
| 897 | 10 | ||||||
| 898 | 10 | return $customer_ids; |
|||||
| 899 | } |
||||||
| 900 | 10 | ||||||
| 901 | /** |
||||||
| 902 | 7 | * Get customer ID for subscription. |
|||||
| 903 | * |
||||||
| 904 | 7 | * @param Subscription $subscription Subscription. |
|||||
| 905 | 7 | * @return string|null |
|||||
| 906 | */ |
||||||
| 907 | private function get_customer_id_for_subscription( Subscription $subscription ) { |
||||||
| 908 | $customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||||||
| 909 | 10 | ||||||
| 910 | 6 | if ( empty( $customer_id ) ) { |
|||||
| 911 | // Try to get (legacy) customer ID from first payment. |
||||||
| 912 | $first_payment = $subscription->get_first_payment(); |
||||||
| 913 | 4 | ||||||
| 914 | if ( null !== $first_payment ) { |
||||||
| 915 | $customer_id = $first_payment->get_meta( 'mollie_customer_id' ); |
||||||
| 916 | } |
||||||
| 917 | } |
||||||
| 918 | |||||||
| 919 | if ( empty( $customer_id ) ) { |
||||||
| 920 | return null; |
||||||
| 921 | } |
||||||
| 922 | |||||||
| 923 | 10 | return $customer_id; |
|||||
| 924 | 10 | } |
|||||
| 925 | |||||||
| 926 | 10 | /** |
|||||
| 927 | * Get first existing customer from customers list. |
||||||
| 928 | 10 | * |
|||||
| 929 | * @param array<string> $customer_ids Customers. |
||||||
| 930 | 4 | * @return string|null |
|||||
| 931 | * @throws Error Throws error on Mollie error. |
||||||
| 932 | */ |
||||||
| 933 | private function get_first_existing_customer_id( $customer_ids ) { |
||||||
| 934 | $customer_ids = \array_filter( $customer_ids ); |
||||||
| 935 | |||||||
| 936 | $customer_ids = \array_unique( $customer_ids ); |
||||||
| 937 | |||||||
| 938 | foreach ( $customer_ids as $customer_id ) { |
||||||
| 939 | try { |
||||||
| 940 | 4 | $customer = $this->client->get_customer( $customer_id ); |
|||||
| 941 | 4 | } catch ( Error $error ) { |
|||||
| 942 | // Check for status 410 ("Gone - The customer is no longer available"). |
||||||
| 943 | if ( 410 === $error->get_status() ) { |
||||||
| 944 | continue; |
||||||
| 945 | 6 | } |
|||||
| 946 | |||||||
| 947 | throw $error; |
||||||
| 948 | } |
||||||
| 949 | |||||||
| 950 | if ( null !== $customer ) { |
||||||
| 951 | return $customer_id; |
||||||
| 952 | } |
||||||
| 953 | } |
||||||
| 954 | |||||||
| 955 | return null; |
||||||
| 956 | } |
||||||
| 957 | |||||||
| 958 | /** |
||||||
| 959 | * Create customer for payment. |
||||||
| 960 | * |
||||||
| 961 | * @param Payment $payment Payment. |
||||||
| 962 | * @return string|null |
||||||
| 963 | * @throws Error Throws Error when Mollie error occurs. |
||||||
| 964 | * @throws \Exception Throws exception when error in customer data store occurs. |
||||||
| 965 | */ |
||||||
| 966 | private function create_customer_for_payment( Payment $payment ) { |
||||||
| 967 | $mollie_customer = new Customer(); |
||||||
| 968 | $mollie_customer->set_mode( $this->config->is_test_mode() ? 'test' : 'live' ); |
||||||
| 969 | $mollie_customer->set_email( $payment->get_email() ); |
||||||
| 970 | |||||||
| 971 | $pronamic_customer = $payment->get_customer(); |
||||||
| 972 | |||||||
| 973 | if ( null !== $pronamic_customer ) { |
||||||
| 974 | // Name. |
||||||
| 975 | $name = (string) $pronamic_customer->get_name(); |
||||||
| 976 | |||||||
| 977 | if ( '' !== $name ) { |
||||||
| 978 | $mollie_customer->set_name( $name ); |
||||||
| 979 | } |
||||||
| 980 | |||||||
| 981 | // Locale. |
||||||
| 982 | $locale = $pronamic_customer->get_locale(); |
||||||
| 983 | |||||||
| 984 | if ( null !== $locale ) { |
||||||
| 985 | $mollie_customer->set_locale( LocaleHelper::transform( $locale ) ); |
||||||
| 986 | } |
||||||
| 987 | } |
||||||
| 988 | |||||||
| 989 | // Try to get name from consumer bank details. |
||||||
| 990 | $consumer_bank_details = $payment->get_consumer_bank_details(); |
||||||
| 991 | |||||||
| 992 | if ( null === $mollie_customer->get_name() && null !== $consumer_bank_details ) { |
||||||
| 993 | $name = $consumer_bank_details->get_name(); |
||||||
| 994 | |||||||
| 995 | if ( null !== $name ) { |
||||||
| 996 | $mollie_customer->set_name( $name ); |
||||||
| 997 | } |
||||||
| 998 | } |
||||||
| 999 | |||||||
| 1000 | // Create customer. |
||||||
| 1001 | $mollie_customer = $this->client->create_customer( $mollie_customer ); |
||||||
| 1002 | |||||||
| 1003 | $customer_id = $this->customer_data_store->insert_customer( $mollie_customer ); |
||||||
| 1004 | |||||||
| 1005 | // Connect to user. |
||||||
| 1006 | if ( null !== $pronamic_customer ) { |
||||||
| 1007 | $user_id = $pronamic_customer->get_user_id(); |
||||||
| 1008 | |||||||
| 1009 | if ( null !== $user_id ) { |
||||||
| 1010 | $user = \get_user_by( 'id', $user_id ); |
||||||
| 1011 | |||||||
| 1012 | if ( false !== $user ) { |
||||||
| 1013 | $this->customer_data_store->connect_mollie_customer_to_wp_user( $mollie_customer, $user ); |
||||||
| 1014 | } |
||||||
| 1015 | } |
||||||
| 1016 | } |
||||||
| 1017 | |||||||
| 1018 | // Store customer ID in subscription meta. |
||||||
| 1019 | $subscription = $payment->get_subscription(); |
||||||
| 1020 | |||||||
| 1021 | if ( null !== $subscription ) { |
||||||
| 1022 | $subscription->set_meta( 'mollie_customer_id', $mollie_customer->get_id() ); |
||||||
| 1023 | } |
||||||
| 1024 | 27 | ||||||
| 1025 | 27 | return $mollie_customer->get_id(); |
|||||
| 1026 | 1 | } |
|||||
| 1027 | |||||||
| 1028 | /** |
||||||
| 1029 | * Copy Mollie customer ID from subscription meta to WordPress user meta. |
||||||
| 1030 | 26 | * |
|||||
| 1031 | * @param Payment $payment Payment. |
||||||
| 1032 | * @return void |
||||||
| 1033 | 26 | */ |
|||||
| 1034 | public function copy_customer_id_to_wp_user( Payment $payment ) { |
||||||
| 1035 | 26 | if ( $this->config->id !== $payment->config_id ) { |
|||||
| 1036 | 16 | return; |
|||||
| 1037 | } |
||||||
| 1038 | |||||||
| 1039 | 26 | // Subscription. |
|||||
| 1040 | $subscription = $payment->get_subscription(); |
||||||
| 1041 | |||||||
| 1042 | // Customer. |
||||||
| 1043 | $customer = $payment->get_customer(); |
||||||
| 1044 | 26 | ||||||
| 1045 | if ( null === $customer && null !== $subscription ) { |
||||||
| 1046 | 26 | $customer = $subscription->get_customer(); |
|||||
| 1047 | 3 | } |
|||||
| 1048 | |||||||
| 1049 | if ( null === $customer ) { |
||||||
| 1050 | 23 | return; |
|||||
| 1051 | } |
||||||
| 1052 | 23 | ||||||
| 1053 | 12 | // WordPress user. |
|||||
| 1054 | $user_id = $customer->get_user_id(); |
||||||
| 1055 | |||||||
| 1056 | if ( null === $user_id ) { |
||||||
| 1057 | 11 | return; |
|||||
| 1058 | } |
||||||
| 1059 | |||||||
| 1060 | 11 | $user = \get_user_by( 'id', $user_id ); |
|||||
| 1061 | |||||||
| 1062 | if ( false === $user ) { |
||||||
| 1063 | 11 | return; |
|||||
| 1064 | 11 | } |
|||||
| 1065 | |||||||
| 1066 | // Customer IDs. |
||||||
| 1067 | $customer_ids = array(); |
||||||
| 1068 | 11 | ||||||
| 1069 | 11 | // Payment. |
|||||
| 1070 | $customer_ids[] = $payment->get_meta( 'mollie_customer_id' ); |
||||||
| 1071 | 11 | ||||||
| 1072 | 2 | // Subscription. |
|||||
| 1073 | if ( null !== $subscription ) { |
||||||
| 1074 | 2 | $customer_ids[] = $subscription->get_meta( 'mollie_customer_id' ); |
|||||
| 1075 | } |
||||||
| 1076 | 2 | ||||||
| 1077 | // Connect. |
||||||
| 1078 | 11 | $customer_ids = \array_filter( $customer_ids ); |
|||||
| 1079 | $customer_ids = \array_unique( $customer_ids ); |
||||||
| 1080 | |||||||
| 1081 | foreach ( $customer_ids as $customer_id ) { |
||||||
| 1082 | $customer = new Customer( $customer_id ); |
||||||
| 1083 | |||||||
| 1084 | $this->customer_data_store->get_or_insert_customer( $customer ); |
||||||
| 1085 | |||||||
| 1086 | $this->customer_data_store->connect_mollie_customer_to_wp_user( $customer, $user ); |
||||||
| 1087 | } |
||||||
| 1088 | } |
||||||
| 1089 | } |
||||||
| 1090 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: