wp-pay-gateways /
mollie
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Mollie gateway. |
||
| 4 | * |
||
| 5 | * @author Pronamic <[email protected]> |
||
| 6 | * @copyright 2005-2019 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\Core\Gateway as Core_Gateway; |
||
| 16 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||
| 17 | use Pronamic\WordPress\Pay\Core\Recurring as Core_Recurring; |
||
| 18 | use Pronamic\WordPress\Pay\Payments\PaymentStatus; |
||
| 19 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Title: Mollie |
||
| 23 | * Description: |
||
| 24 | * Copyright: 2005-2019 Pronamic |
||
| 25 | * Company: Pronamic |
||
| 26 | * |
||
| 27 | * @author Remco Tolsma |
||
| 28 | * @version 2.0.8 |
||
| 29 | * @since 1.1.0 |
||
| 30 | */ |
||
| 31 | class Gateway extends Core_Gateway { |
||
| 32 | /** |
||
| 33 | * Client. |
||
| 34 | * |
||
| 35 | * @var Client |
||
| 36 | */ |
||
| 37 | protected $client; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Meta key for customer ID. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $meta_key_customer_id = '_pronamic_pay_mollie_customer_id'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructs and initializes an Mollie gateway |
||
| 48 | * |
||
| 49 | * @param Config $config Config. |
||
| 50 | */ |
||
| 51 | 38 | public function __construct( Config $config ) { |
|
| 52 | 38 | parent::__construct( $config ); |
|
| 53 | |||
| 54 | 38 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
|
| 55 | |||
| 56 | // Supported features. |
||
| 57 | 38 | $this->supports = array( |
|
| 58 | 'payment_status_request', |
||
| 59 | 'recurring_direct_debit', |
||
| 60 | 'recurring_credit_card', |
||
| 61 | 'recurring', |
||
| 62 | ); |
||
| 63 | |||
| 64 | // Client. |
||
| 65 | 38 | $this->client = new Client( $config->api_key ); |
|
| 66 | 38 | $this->client->set_mode( $config->mode ); |
|
| 67 | |||
| 68 | // Mollie customer ID meta key. |
||
| 69 | 38 | if ( self::MODE_TEST === $config->mode ) { |
|
| 70 | 37 | $this->meta_key_customer_id = '_pronamic_pay_mollie_customer_id_test'; |
|
| 71 | } |
||
| 72 | |||
| 73 | // Actions. |
||
| 74 | 38 | add_action( 'pronamic_payment_status_update', array( $this, 'copy_customer_id_to_wp_user' ), 99, 1 ); |
|
| 75 | 38 | } |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Get issuers |
||
| 79 | * |
||
| 80 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
| 81 | */ |
||
| 82 | 2 | public function get_issuers() { |
|
| 83 | 2 | $groups = array(); |
|
| 84 | |||
| 85 | 2 | $result = $this->client->get_issuers(); |
|
| 86 | |||
| 87 | 2 | if ( ! $result ) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 88 | 2 | $this->error = $this->client->get_error(); |
|
| 89 | |||
| 90 | 2 | return $groups; |
|
| 91 | } |
||
| 92 | |||
| 93 | $groups[] = array( |
||
| 94 | 'options' => $result, |
||
| 95 | ); |
||
| 96 | |||
| 97 | return $groups; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get available payment methods. |
||
| 102 | * |
||
| 103 | * @see Core_Gateway::get_available_payment_methods() |
||
| 104 | */ |
||
| 105 | 1 | public function get_available_payment_methods() { |
|
| 106 | 1 | $payment_methods = array(); |
|
| 107 | |||
| 108 | // Set sequence types to get payment methods for. |
||
| 109 | 1 | $sequence_types = array( Sequence::ONE_OFF, Sequence::RECURRING, Sequence::FIRST ); |
|
| 110 | |||
| 111 | 1 | $results = array(); |
|
| 112 | |||
| 113 | 1 | foreach ( $sequence_types as $sequence_type ) { |
|
| 114 | // Get active payment methods for Mollie account. |
||
| 115 | 1 | $result = $this->client->get_payment_methods( $sequence_type ); |
|
| 116 | |||
| 117 | 1 | if ( ! $result ) { |
|
| 118 | 1 | $this->error = $this->client->get_error(); |
|
| 119 | |||
| 120 | 1 | break; |
|
| 121 | } |
||
| 122 | |||
| 123 | if ( Sequence::FIRST === $sequence_type ) { |
||
| 124 | foreach ( $result as $method => $title ) { |
||
| 125 | unset( $result[ $method ] ); |
||
| 126 | |||
| 127 | // Get WordPress payment method for direct debit method. |
||
| 128 | $method = Methods::transform_gateway_method( $method ); |
||
| 129 | $payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true ); |
||
| 130 | |||
| 131 | if ( $payment_method ) { |
||
| 132 | $results[ $payment_method ] = $title; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | if ( is_array( $result ) ) { |
||
| 138 | $results = array_merge( $results, $result ); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | // Transform to WordPress payment methods. |
||
| 143 | 1 | foreach ( $results as $method => $title ) { |
|
| 144 | if ( PaymentMethods::is_recurring_method( $method ) ) { |
||
| 145 | $payment_method = $method; |
||
| 146 | } else { |
||
| 147 | $payment_method = Methods::transform_gateway_method( $method ); |
||
| 148 | } |
||
| 149 | |||
| 150 | if ( $payment_method ) { |
||
| 151 | $payment_methods[] = $payment_method; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | 1 | $payment_methods = array_unique( $payment_methods ); |
|
| 156 | |||
| 157 | 1 | return $payment_methods; |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get supported payment methods |
||
| 162 | * |
||
| 163 | * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods() |
||
| 164 | */ |
||
| 165 | 2 | public function get_supported_payment_methods() { |
|
| 166 | return array( |
||
| 167 | 2 | PaymentMethods::BANCONTACT, |
|
| 168 | PaymentMethods::BANK_TRANSFER, |
||
| 169 | PaymentMethods::BELFIUS, |
||
| 170 | PaymentMethods::BITCOIN, |
||
| 171 | PaymentMethods::CREDIT_CARD, |
||
| 172 | PaymentMethods::DIRECT_DEBIT, |
||
| 173 | PaymentMethods::DIRECT_DEBIT_BANCONTACT, |
||
| 174 | PaymentMethods::DIRECT_DEBIT_IDEAL, |
||
| 175 | PaymentMethods::DIRECT_DEBIT_SOFORT, |
||
| 176 | PaymentMethods::EPS, |
||
| 177 | PaymentMethods::GIROPAY, |
||
| 178 | PaymentMethods::IDEAL, |
||
| 179 | PaymentMethods::KBC, |
||
| 180 | PaymentMethods::PAYPAL, |
||
| 181 | PaymentMethods::SOFORT, |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get webhook URL for Mollie. |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | 4 | public function get_webhook_url() { |
|
| 191 | 4 | $url = home_url( '/' ); |
|
| 192 | |||
| 193 | 4 | $host = wp_parse_url( $url, PHP_URL_HOST ); |
|
| 194 | |||
| 195 | 4 | if ( is_array( $host ) ) { |
|
| 196 | // Parsing failure. |
||
| 197 | $host = ''; |
||
| 198 | } |
||
| 199 | |||
| 200 | 4 | if ( 'localhost' === $host ) { |
|
| 201 | // Mollie doesn't allow localhost. |
||
| 202 | 1 | return null; |
|
| 203 | 3 | } elseif ( '.dev' === substr( $host, -4 ) ) { |
|
| 204 | // Mollie doesn't allow the .dev TLD. |
||
| 205 | 1 | return null; |
|
| 206 | 2 | } elseif ( '.local' === substr( $host, -6 ) ) { |
|
| 207 | // Mollie doesn't allow the .local TLD. |
||
| 208 | 1 | return null; |
|
| 209 | 1 | } elseif ( '.test' === substr( $host, -5 ) ) { |
|
| 210 | // Mollie doesn't allow the .test TLD. |
||
| 211 | return null; |
||
| 212 | } |
||
| 213 | |||
| 214 | 1 | $url = add_query_arg( 'mollie_webhook', '', $url ); |
|
| 215 | |||
| 216 | 1 | return $url; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Start |
||
| 221 | * |
||
| 222 | * @see Pronamic_WP_Pay_Gateway::start() |
||
| 223 | * |
||
| 224 | * @param Payment $payment Payment. |
||
| 225 | */ |
||
| 226 | public function start( Payment $payment ) { |
||
| 227 | $request = new PaymentRequest(); |
||
| 228 | $request->amount = AmountTransformer::transform( $payment->get_total_amount() ); |
||
| 229 | $request->description = $payment->get_description(); |
||
| 230 | $request->redirect_url = $payment->get_return_url(); |
||
| 231 | $request->webhook_url = $this->get_webhook_url(); |
||
| 232 | |||
| 233 | // Locale. |
||
| 234 | if ( null !== $payment->get_customer() ) { |
||
| 235 | $request->locale = LocaleHelper::transform( $payment->get_customer()->get_locale() ); |
||
| 236 | } |
||
| 237 | |||
| 238 | // Customer ID. |
||
| 239 | $customer_id = $this->get_customer_id_for_payment( $payment ); |
||
| 240 | |||
| 241 | if ( is_string( $customer_id ) && ! empty( $customer_id ) ) { |
||
| 242 | $request->customer_id = $customer_id; |
||
| 243 | } |
||
| 244 | |||
| 245 | // Payment method. |
||
| 246 | $payment_method = $payment->get_method(); |
||
| 247 | |||
| 248 | // Recurring payment method. |
||
| 249 | $is_recurring_method = ( $payment->get_subscription() && PaymentMethods::is_recurring_method( $payment_method ) ); |
||
| 250 | |||
| 251 | if ( false === $is_recurring_method ) { |
||
| 252 | // Always use 'direct debit mandate via iDEAL/Bancontact/Sofort' payment methods as recurring method. |
||
| 253 | $is_recurring_method = PaymentMethods::is_direct_debit_method( $payment_method ); |
||
| 254 | } |
||
| 255 | |||
| 256 | if ( $is_recurring_method ) { |
||
| 257 | $request->sequence_type = $payment->get_recurring() ? Sequence::RECURRING : Sequence::FIRST; |
||
| 258 | |||
| 259 | if ( Sequence::FIRST === $request->sequence_type ) { |
||
| 260 | $payment_method = PaymentMethods::get_first_payment_method( $payment_method ); |
||
| 261 | } |
||
| 262 | |||
| 263 | if ( Sequence::RECURRING === $request->sequence_type ) { |
||
| 264 | $payment->set_action_url( $payment->get_return_url() ); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | // Leap of faith if the WordPress payment method could not transform to a Mollie method? |
||
| 269 | $request->method = Methods::transform( $payment_method, $payment_method ); |
||
| 270 | |||
| 271 | // Issuer. |
||
| 272 | if ( Methods::IDEAL === $request->method ) { |
||
| 273 | $request->issuer = $payment->get_issuer(); |
||
| 274 | } |
||
| 275 | |||
| 276 | // Create payment. |
||
| 277 | $result = $this->client->create_payment( $request ); |
||
| 278 | |||
| 279 | if ( ! $result ) { |
||
| 280 | $this->error = $this->client->get_error(); |
||
| 281 | |||
| 282 | return; |
||
| 283 | } |
||
| 284 | |||
| 285 | // Set transaction ID. |
||
| 286 | if ( isset( $result->id ) ) { |
||
| 287 | $payment->set_transaction_id( $result->id ); |
||
| 288 | } |
||
| 289 | |||
| 290 | // Set status. |
||
| 291 | if ( isset( $result->status ) ) { |
||
| 292 | $payment->set_status( Statuses::transform( $result->status ) ); |
||
| 293 | } |
||
| 294 | |||
| 295 | // Set action URL. |
||
| 296 | if ( isset( $result->_links->checkout->href ) ) { |
||
| 297 | $payment->set_action_url( $result->_links->checkout->href ); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Update status of the specified payment |
||
| 303 | * |
||
| 304 | * @param Payment $payment Payment. |
||
| 305 | * |
||
| 306 | * @return void |
||
| 307 | */ |
||
| 308 | public function update_status( Payment $payment ) { |
||
| 309 | $mollie_payment = $this->client->get_payment( $payment->get_transaction_id() ); |
||
| 310 | |||
| 311 | if ( ! $mollie_payment ) { |
||
| 312 | $payment->set_status( PaymentStatus::FAILURE ); |
||
| 313 | |||
| 314 | $this->error = $this->client->get_error(); |
||
| 315 | |||
| 316 | return; |
||
| 317 | } |
||
| 318 | |||
| 319 | $payment->set_status( Statuses::transform( $mollie_payment->status ) ); |
||
| 320 | |||
| 321 | if ( isset( $mollie_payment->details ) ) { |
||
| 322 | $details = $mollie_payment->details; |
||
| 323 | |||
| 324 | /* |
||
| 325 | * @codingStandardsIgnoreStart |
||
| 326 | * |
||
| 327 | * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar |
||
| 328 | */ |
||
| 329 | if ( isset( $details->consumerName ) ) { |
||
| 330 | $payment->set_consumer_name( $details->consumerName ); |
||
| 331 | } |
||
| 332 | |||
| 333 | if ( isset( $details->cardHolder ) ) { |
||
| 334 | $payment->set_consumer_name( $details->cardHolder ); |
||
| 335 | } |
||
| 336 | |||
| 337 | if ( isset( $details->consumerAccount ) ) { |
||
| 338 | $payment->set_consumer_iban( $details->consumerAccount ); |
||
| 339 | } |
||
| 340 | |||
| 341 | if ( isset( $details->consumerBic ) ) { |
||
| 342 | $payment->set_consumer_bic( $details->consumerBic ); |
||
| 343 | } |
||
| 344 | // @codingStandardsIgnoreEnd |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get Mollie customer ID for payment. |
||
| 350 | * |
||
| 351 | * @param Payment $payment Payment. |
||
| 352 | * |
||
| 353 | * @return bool|string |
||
| 354 | */ |
||
| 355 | 11 | public function get_customer_id_for_payment( Payment $payment ) { |
|
| 356 | // Get WordPress user ID from payment customer. |
||
| 357 | 11 | $user_id = ( null === $payment->get_customer() ? null : $payment->get_customer()->get_user_id() ); |
|
| 358 | |||
| 359 | // Get Mollie customer ID from user meta. |
||
| 360 | 11 | $customer_id = $this->get_customer_id_by_wp_user_id( $user_id ); |
|
| 361 | |||
| 362 | 11 | $subscription = $payment->get_subscription(); |
|
| 363 | |||
| 364 | // Get customer ID from subscription meta. |
||
| 365 | 11 | if ( $subscription ) { |
|
| 366 | 11 | $subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
|
| 367 | |||
| 368 | // Try to get (legacy) customer ID from first payment. |
||
| 369 | 11 | if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) { |
|
| 370 | 7 | $first_payment = $subscription->get_first_payment(); |
|
| 371 | |||
| 372 | 7 | $subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' ); |
|
| 373 | } |
||
| 374 | |||
| 375 | 11 | if ( ! empty( $subscription_customer_id ) ) { |
|
| 376 | 5 | $customer_id = $subscription_customer_id; |
|
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | // Create new customer if the customer does not exist at Mollie. |
||
| 381 | 11 | if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) { |
|
| 382 | $customer_name = null; |
||
| 383 | |||
| 384 | if ( null !== $payment->get_customer() && null !== $payment->get_customer()->get_name() ) { |
||
| 385 | $customer_name = strval( $payment->get_customer()->get_name() ); |
||
| 386 | } |
||
| 387 | |||
| 388 | $customer_id = $this->client->create_customer( $payment->get_email(), $customer_name ); |
||
| 389 | |||
| 390 | $this->update_wp_user_customer_id( $user_id, $customer_id ); |
||
| 391 | } |
||
| 392 | |||
| 393 | // Store customer ID in subscription meta. |
||
| 394 | 11 | if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) { |
|
| 395 | $subscription->set_meta( 'mollie_customer_id', $customer_id ); |
||
| 396 | } |
||
| 397 | |||
| 398 | // Copy customer ID from subscription to user meta. |
||
| 399 | 11 | $this->copy_customer_id_to_wp_user( $payment ); |
|
| 400 | |||
| 401 | 11 | return $customer_id; |
|
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get Mollie customer ID by the specified WordPress user ID. |
||
| 406 | * |
||
| 407 | * @param int $user_id WordPress user ID. |
||
| 408 | * |
||
| 409 | * @return string|bool |
||
| 410 | */ |
||
| 411 | 28 | public function get_customer_id_by_wp_user_id( $user_id ) { |
|
| 412 | 28 | if ( empty( $user_id ) ) { |
|
| 413 | 11 | return false; |
|
| 414 | } |
||
| 415 | |||
| 416 | 17 | return get_user_meta( $user_id, $this->meta_key_customer_id, true ); |
|
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Update Mollie customer ID meta for WordPress user. |
||
| 421 | * |
||
| 422 | * @param int $user_id WordPress user ID. |
||
| 423 | * @param string $customer_id Mollie Customer ID. |
||
| 424 | * |
||
| 425 | * @return bool |
||
| 426 | */ |
||
| 427 | private function update_wp_user_customer_id( $user_id, $customer_id ) { |
||
| 428 | if ( empty( $user_id ) || is_bool( $user_id ) ) { |
||
| 429 | return false; |
||
| 430 | } |
||
| 431 | |||
| 432 | if ( ! is_string( $customer_id ) || empty( $customer_id ) || 1 === strlen( $customer_id ) ) { |
||
| 433 | return false; |
||
| 434 | } |
||
| 435 | |||
| 436 | update_user_meta( $user_id, $this->meta_key_customer_id, $customer_id ); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Copy Mollie customer ID from subscription meta to WordPress user meta. |
||
| 441 | * |
||
| 442 | * @param Payment $payment Payment. |
||
| 443 | * |
||
| 444 | * @return void |
||
| 445 | */ |
||
| 446 | 28 | public function copy_customer_id_to_wp_user( Payment $payment ) { |
|
| 447 | 28 | if ( $this->config->id !== $payment->config_id ) { |
|
| 448 | 1 | return; |
|
| 449 | } |
||
| 450 | |||
| 451 | 27 | $subscription = $payment->get_subscription(); |
|
| 452 | |||
| 453 | 27 | if ( ! $subscription || empty( $subscription->user_id ) ) { |
|
| 454 | 27 | return; |
|
| 455 | } |
||
| 456 | |||
| 457 | // Get customer ID from subscription meta. |
||
| 458 | $customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||
| 459 | |||
| 460 | $user_customer_id = $this->get_customer_id_by_wp_user_id( $subscription->user_id ); |
||
| 461 | |||
| 462 | if ( empty( $user_customer_id ) ) { |
||
| 463 | // Set customer ID as user meta. |
||
| 464 | $this->update_wp_user_customer_id( $subscription->user_id, $customer_id ); |
||
| 465 | } |
||
| 466 | } |
||
| 467 | } |
||
| 468 |