wp-pay-gateways /
mollie
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Mollie integration. |
||
| 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 Pronamic\WordPress\Pay\Core\PaymentMethods; |
||
| 14 | use Pronamic\WordPress\Pay\AbstractGatewayIntegration; |
||
| 15 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
| 16 | use Pronamic\WordPress\Pay\Subscriptions\Subscription as CoreSubscription; |
||
| 17 | use WP_User; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Title: Mollie integration |
||
| 21 | * Description: |
||
| 22 | * Copyright: 2005-2020 Pronamic |
||
| 23 | * Company: Pronamic |
||
| 24 | * |
||
| 25 | * @author Remco Tolsma |
||
| 26 | * @version 2.1.4 |
||
| 27 | * @since 1.0.0 |
||
| 28 | */ |
||
| 29 | class Integration extends AbstractGatewayIntegration { |
||
| 30 | /** |
||
| 31 | * REST route namespace. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | const REST_ROUTE_NAMESPACE = 'pronamic-pay/mollie/v1'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Register URL. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | public $register_url; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Construct and intialize Mollie integration. |
||
| 46 | * |
||
| 47 | * @param array<string, array> $args Arguments. |
||
| 48 | */ |
||
| 49 | 7 | public function __construct( $args = array() ) { |
|
| 50 | 7 | $args = wp_parse_args( |
|
| 51 | 7 | $args, |
|
| 52 | array( |
||
| 53 | 7 | 'id' => 'mollie', |
|
| 54 | 7 | 'name' => 'Mollie', |
|
| 55 | 7 | 'version' => '2.1.0', |
|
| 56 | 7 | 'url' => 'http://www.mollie.com/en/', |
|
| 57 | 7 | 'product_url' => \__( 'https://www.mollie.com/en/pricing', 'pronamic_ideal' ), |
|
| 58 | 7 | 'dashboard_url' => 'https://www.mollie.com/dashboard/', |
|
| 59 | 7 | 'provider' => 'mollie', |
|
| 60 | 'supports' => array( |
||
| 61 | 'payment_status_request', |
||
| 62 | 'recurring_direct_debit', |
||
| 63 | 'recurring_credit_card', |
||
| 64 | 'recurring', |
||
| 65 | 'webhook', |
||
| 66 | 'webhook_log', |
||
| 67 | 'webhook_no_config', |
||
| 68 | ), |
||
| 69 | 7 | 'version_option_name' => 'pronamic_pay_mollie_version', |
|
| 70 | 7 | 'db_version_option_name' => 'pronamic_pay_mollie_db_version', |
|
| 71 | ) |
||
| 72 | ); |
||
| 73 | |||
| 74 | 7 | parent::__construct( $args ); |
|
| 75 | |||
| 76 | // Filters. |
||
| 77 | 7 | $function = array( $this, 'next_payment_delivery_date' ); |
|
| 78 | |||
| 79 | 7 | if ( ! \has_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function ) ) { |
|
| 80 | 7 | \add_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function, 10, 2 ); |
|
| 81 | } |
||
| 82 | |||
| 83 | 7 | add_filter( 'pronamic_payment_provider_url_mollie', array( $this, 'payment_provider_url' ), 10, 2 ); |
|
| 84 | |||
| 85 | // Tables. |
||
| 86 | 7 | $this->register_tables(); |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Install. |
||
| 90 | */ |
||
| 91 | 7 | new Install( $this ); |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Admin |
||
| 95 | */ |
||
| 96 | 7 | if ( \is_admin() ) { |
|
| 97 | new Admin(); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * CLI. |
||
| 102 | * |
||
| 103 | * @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-woocommerce.php#L453-L455 |
||
| 104 | */ |
||
| 105 | 7 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 106 | new CLI(); |
||
| 107 | } |
||
| 108 | 7 | } |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Setup gateway integration. |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function setup() { |
||
| 116 | // Check if dependencies are met and integration is active. |
||
| 117 | if ( ! $this->is_active() ) { |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | |||
| 121 | // Webhook controller. |
||
| 122 | $webhook_controller = new WebhookController(); |
||
| 123 | |||
| 124 | $webhook_controller->setup(); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Register tables. |
||
| 129 | * |
||
| 130 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L894-L937 |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | 7 | private function register_tables() { |
|
| 134 | 7 | global $wpdb; |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Tables. |
||
| 138 | */ |
||
| 139 | 7 | $wpdb->pronamic_pay_mollie_organizations = $wpdb->base_prefix . 'pronamic_pay_mollie_organizations'; |
|
| 140 | 7 | $wpdb->pronamic_pay_mollie_profiles = $wpdb->base_prefix . 'pronamic_pay_mollie_profiles'; |
|
| 141 | 7 | $wpdb->pronamic_pay_mollie_customers = $wpdb->base_prefix . 'pronamic_pay_mollie_customers'; |
|
| 142 | 7 | $wpdb->pronamic_pay_mollie_customer_users = $wpdb->base_prefix . 'pronamic_pay_mollie_customer_users'; |
|
| 143 | 7 | } |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Get settings fields. |
||
| 147 | * |
||
| 148 | * @return array<int, array<string, array<int, string>|int|string|true>> |
||
| 149 | */ |
||
| 150 | 1 | public function get_settings_fields() { |
|
| 151 | 1 | $fields = array(); |
|
| 152 | |||
| 153 | // API Key. |
||
| 154 | 1 | $fields[] = array( |
|
| 155 | 1 | 'section' => 'general', |
|
| 156 | 1 | 'filter' => FILTER_SANITIZE_STRING, |
|
| 157 | 1 | 'meta_key' => '_pronamic_gateway_mollie_api_key', |
|
| 158 | 1 | 'title' => _x( 'API Key', 'mollie', 'pronamic_ideal' ), |
|
| 159 | 1 | 'type' => 'text', |
|
| 160 | 'classes' => array( 'regular-text', 'code' ), |
||
| 161 | 1 | 'tooltip' => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ), |
|
| 162 | ); |
||
| 163 | |||
| 164 | // Due date days. |
||
| 165 | 1 | $fields[] = array( |
|
| 166 | 1 | 'section' => 'advanced', |
|
| 167 | 'filter' => \FILTER_SANITIZE_NUMBER_INT, |
||
| 168 | 1 | 'meta_key' => '_pronamic_gateway_mollie_due_date_days', |
|
| 169 | 1 | 'title' => _x( 'Due date days', 'mollie', 'pronamic_ideal' ), |
|
| 170 | 1 | 'type' => 'number', |
|
| 171 | 1 | 'min' => 1, |
|
| 172 | 1 | 'max' => 100, |
|
| 173 | 'classes' => array( 'regular-text' ), |
||
| 174 | 1 | 'tooltip' => __( 'Number of days after which a bank transfer payment expires.', 'pronamic_ideal' ), |
|
| 175 | 1 | 'description' => sprintf( |
|
| 176 | /* translators: 1: <code>1</code>, 2: <code>100</code>, 3: <code>12</code> */ |
||
| 177 | 1 | __( 'Minimum %1$s and maximum %2$s days. Default: %3$s days.', 'pronamic_ideal' ), |
|
| 178 | 1 | sprintf( '<code>%s</code>', '1' ), |
|
| 179 | 1 | sprintf( '<code>%s</code>', '100' ), |
|
| 180 | 1 | sprintf( '<code>%s</code>', '12' ) |
|
| 181 | ), |
||
| 182 | ); |
||
| 183 | |||
| 184 | // Webhook. |
||
| 185 | 1 | $fields[] = array( |
|
| 186 | 1 | 'section' => 'feedback', |
|
| 187 | 1 | 'title' => __( 'Webhook URL', 'pronamic_ideal' ), |
|
| 188 | 1 | 'type' => 'text', |
|
| 189 | 'classes' => array( 'large-text', 'code' ), |
||
| 190 | 1 | 'value' => rest_url( self::REST_ROUTE_NAMESPACE . '/webhook' ), |
|
| 191 | 'readonly' => true, |
||
| 192 | 1 | 'tooltip' => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ), |
|
| 193 | ); |
||
| 194 | |||
| 195 | 1 | return $fields; |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Save post. |
||
| 200 | * |
||
| 201 | * @link https://developer.wordpress.org/reference/functions/get_post_meta/ |
||
| 202 | * @param int $post_id Post ID. |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function save_post( $post_id ) { |
||
| 206 | $api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true ); |
||
| 207 | |||
| 208 | if ( ! is_string( $api_key ) ) { |
||
| 209 | return; |
||
| 210 | } |
||
| 211 | |||
| 212 | $api_key_prefix = substr( $api_key, 0, 4 ); |
||
| 213 | |||
| 214 | switch ( $api_key_prefix ) { |
||
| 215 | case 'live': |
||
| 216 | update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE ); |
||
| 217 | |||
| 218 | return; |
||
| 219 | case 'test': |
||
| 220 | update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST ); |
||
| 221 | |||
| 222 | return; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Payment provider URL. |
||
| 228 | * |
||
| 229 | * @param string|null $url Payment provider URL. |
||
| 230 | * @param Payment $payment Payment. |
||
| 231 | * @return string|null |
||
| 232 | */ |
||
| 233 | 1 | public function payment_provider_url( $url, Payment $payment ) { |
|
| 234 | 1 | $transaction_id = $payment->get_transaction_id(); |
|
| 235 | |||
| 236 | 1 | if ( null === $transaction_id ) { |
|
| 237 | return $url; |
||
| 238 | } |
||
| 239 | |||
| 240 | 1 | return sprintf( |
|
| 241 | 1 | 'https://www.mollie.com/dashboard/payments/%s', |
|
| 242 | $transaction_id |
||
| 243 | ); |
||
| 244 | } |
||
| 245 | /** |
||
| 246 | * Get configuration by post ID. |
||
| 247 | * |
||
| 248 | * @param int $post_id Post ID. |
||
| 249 | * @return Config |
||
| 250 | */ |
||
| 251 | 2 | public function get_config( $post_id ) { |
|
| 252 | 2 | $config = new Config(); |
|
| 253 | |||
| 254 | 2 | $config->id = intval( $post_id ); |
|
| 255 | 2 | $config->api_key = $this->get_meta( $post_id, 'mollie_api_key' ); |
|
| 256 | 2 | $config->mode = $this->get_meta( $post_id, 'mode' ); |
|
| 257 | 2 | $config->due_date_days = $this->get_meta( $post_id, 'mollie_due_date_days' ); |
|
| 258 | |||
| 259 | 2 | return $config; |
|
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get gateway. |
||
| 264 | * |
||
| 265 | * @param int $post_id Post ID. |
||
| 266 | * @return Gateway |
||
| 267 | */ |
||
| 268 | 1 | public function get_gateway( $post_id ) { |
|
| 269 | 1 | return new Gateway( $this->get_config( $post_id ) ); |
|
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Next payment delivery date. |
||
| 274 | * |
||
| 275 | * @param \DateTime $next_payment_delivery_date Next payment delivery date. |
||
| 276 | * @param CoreSubscription $subscription Subscription. |
||
| 277 | * @return \DateTime |
||
| 278 | */ |
||
| 279 | 10 | public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, CoreSubscription $subscription ) { |
|
| 280 | 10 | $config_id = $subscription->get_config_id(); |
|
| 281 | |||
| 282 | 10 | if ( null === $config_id ) { |
|
| 283 | return $next_payment_delivery_date; |
||
| 284 | } |
||
| 285 | |||
| 286 | // Check gateway. |
||
| 287 | 10 | $gateway_id = \get_post_meta( $config_id, '_pronamic_gateway_id', true ); |
|
| 288 | |||
| 289 | 10 | if ( 'mollie' !== $gateway_id ) { |
|
| 290 | 10 | return $next_payment_delivery_date; |
|
| 291 | } |
||
| 292 | |||
| 293 | // Check direct debit payment method. |
||
| 294 | $method = $subscription->get_method(); |
||
| 295 | |||
| 296 | if ( null === $method ) { |
||
| 297 | return $next_payment_delivery_date; |
||
| 298 | } |
||
| 299 | |||
| 300 | if ( ! PaymentMethods::is_direct_debit_method( $method ) ) { |
||
| 301 | return $next_payment_delivery_date; |
||
| 302 | } |
||
| 303 | |||
| 304 | // Base delivery date on next payment date. |
||
| 305 | $next_payment_date = $subscription->get_next_payment_date(); |
||
| 306 | |||
| 307 | if ( null === $next_payment_date ) { |
||
| 308 | return $next_payment_delivery_date; |
||
| 309 | } |
||
| 310 | |||
| 311 | $next_payment_delivery_date = clone $next_payment_date; |
||
| 312 | |||
| 313 | // Textual representation of the day of the week, Sunday through Saturday. |
||
| 314 | $day_of_week = $next_payment_delivery_date->format( 'l' ); |
||
| 315 | |||
| 316 | /* |
||
| 317 | * Subtract days from next payment date for earlier delivery. |
||
| 318 | * |
||
| 319 | * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out- |
||
| 320 | * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times- |
||
| 321 | */ |
||
| 322 | switch ( $day_of_week ) { |
||
| 323 | case 'Monday': |
||
| 324 | $next_payment_delivery_date->modify( '-3 days' ); |
||
| 325 | |||
| 326 | break; |
||
| 327 | case 'Saturday': |
||
| 328 | $next_payment_delivery_date->modify( '-2 days' ); |
||
| 329 | |||
| 330 | break; |
||
| 331 | case 'Sunday': |
||
| 332 | $next_payment_delivery_date->modify( '-3 days' ); |
||
| 333 | |||
| 334 | break; |
||
| 335 | default: |
||
| 336 | $next_payment_delivery_date->modify( '-1 day' ); |
||
| 337 | |||
| 338 | break; |
||
| 339 | } |
||
| 340 | |||
| 341 | $next_payment_delivery_date->setTime( 0, 0, 0 ); |
||
| 342 | |||
| 343 | return $next_payment_delivery_date; |
||
| 344 | } |
||
| 345 | } |
||
| 346 |