wp-pay-gateways /
mollie
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Mollie integration. |
||
| 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 Pronamic\WordPress\Pay\Core\PaymentMethods; |
||
| 14 | use Pronamic\WordPress\Pay\Gateways\Common\AbstractIntegration; |
||
| 15 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
| 16 | use WP_User; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Title: Mollie integration |
||
| 20 | * Description: |
||
| 21 | * Copyright: 2005-2019 Pronamic |
||
| 22 | * Company: Pronamic |
||
| 23 | * |
||
| 24 | * @author Remco Tolsma |
||
| 25 | * @version 2.0.8 |
||
| 26 | * @since 1.0.0 |
||
| 27 | */ |
||
| 28 | class Integration extends AbstractIntegration { |
||
| 29 | /** |
||
| 30 | * Construct and intialize Mollie integration. |
||
| 31 | */ |
||
| 32 | 7 | public function __construct() { |
|
| 33 | 7 | $this->id = 'mollie'; |
|
| 34 | 7 | $this->name = 'Mollie'; |
|
| 35 | 7 | $this->url = 'http://www.mollie.com/en/'; |
|
| 36 | 7 | $this->product_url = __( 'https://www.mollie.com/en/pricing', 'pronamic_ideal' ); |
|
| 37 | 7 | $this->dashboard_url = 'https://www.mollie.com/dashboard/'; |
|
| 38 | 7 | $this->register_url = 'https://www.mollie.com/nl/signup/665327'; |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 39 | 7 | $this->provider = 'mollie'; |
|
| 40 | 7 | $this->supports = array( |
|
| 41 | 'payment_status_request', |
||
| 42 | 'webhook', |
||
| 43 | 'webhook_log', |
||
| 44 | 'webhook_no_config', |
||
| 45 | ); |
||
| 46 | |||
| 47 | // Actions. |
||
| 48 | 7 | $function = array( __NAMESPACE__ . '\Listener', 'listen' ); |
|
| 49 | |||
| 50 | 7 | if ( ! has_action( 'wp_loaded', $function ) ) { |
|
| 51 | 7 | add_action( 'wp_loaded', $function ); |
|
| 52 | } |
||
| 53 | |||
| 54 | 7 | if ( is_admin() ) { |
|
| 55 | $function = array( __CLASS__, 'user_profile' ); |
||
| 56 | |||
| 57 | if ( ! has_action( 'show_user_profile', $function ) ) { |
||
| 58 | add_action( 'show_user_profile', $function ); |
||
| 59 | } |
||
| 60 | |||
| 61 | if ( ! has_action( 'edit_user_profile', $function ) ) { |
||
| 62 | add_action( 'edit_user_profile', $function ); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | // Filters. |
||
| 67 | 7 | $function = array( $this, 'next_payment_delivery_date' ); |
|
| 68 | |||
| 69 | 7 | if ( ! \has_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function ) ) { |
|
| 70 | 7 | \add_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function, 10, 2 ); |
|
| 71 | } |
||
| 72 | |||
| 73 | 7 | add_filter( 'pronamic_payment_provider_url_mollie', array( $this, 'payment_provider_url' ), 10, 2 ); |
|
| 74 | 7 | } |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Get settings fields. |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | 1 | public function get_settings_fields() { |
|
| 82 | 1 | $fields = array(); |
|
| 83 | |||
| 84 | // API Key. |
||
| 85 | 1 | $fields[] = array( |
|
| 86 | 1 | 'section' => 'general', |
|
| 87 | 1 | 'filter' => FILTER_SANITIZE_STRING, |
|
| 88 | 1 | 'meta_key' => '_pronamic_gateway_mollie_api_key', |
|
| 89 | 1 | 'title' => _x( 'API Key', 'mollie', 'pronamic_ideal' ), |
|
| 90 | 1 | 'type' => 'text', |
|
| 91 | 'classes' => array( 'regular-text', 'code' ), |
||
| 92 | 1 | 'tooltip' => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ), |
|
| 93 | ); |
||
| 94 | |||
| 95 | // Bank transfer due date days. |
||
| 96 | 1 | $fields[] = array( |
|
| 97 | 1 | 'section' => 'advanced', |
|
| 98 | 'filter' => \FILTER_SANITIZE_NUMBER_INT, |
||
| 99 | 1 | 'meta_key' => '_pronamic_gateway_mollie_due_date_days', |
|
| 100 | 1 | 'title' => _x( 'Bank transfer due date days', 'mollie', 'pronamic_ideal' ), |
|
| 101 | 1 | 'type' => 'text', |
|
| 102 | 'classes' => array( 'regular-text' ), |
||
| 103 | 1 | 'tooltip' => __( 'Number of days after which a bank transfer payment expires.', 'pronamic_ideal' ), |
|
| 104 | 1 | 'description' => sprintf( |
|
| 105 | /* translators: 1: <code>1</code>, 2: <code>100</code>, 3: <code>12</code> */ |
||
| 106 | 1 | __( 'Minimum %1$s and maximum %2$s days. Default: %3$s days.', 'pronamic_ideal' ), |
|
| 107 | 1 | sprintf( '<code>%s</code>', '1' ), |
|
| 108 | 1 | sprintf( '<code>%s</code>', '100' ), |
|
| 109 | 1 | sprintf( '<code>%s</code>', '12' ) |
|
| 110 | ), |
||
| 111 | ); |
||
| 112 | |||
| 113 | // Webhook. |
||
| 114 | 1 | $fields[] = array( |
|
| 115 | 1 | 'section' => 'feedback', |
|
| 116 | 1 | 'title' => __( 'Webhook URL', 'pronamic_ideal' ), |
|
| 117 | 1 | 'type' => 'text', |
|
| 118 | 'classes' => array( 'large-text', 'code' ), |
||
| 119 | 1 | 'value' => add_query_arg( 'mollie_webhook', '', home_url( '/' ) ), |
|
| 120 | 'readonly' => true, |
||
| 121 | 1 | 'tooltip' => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ), |
|
| 122 | ); |
||
| 123 | |||
| 124 | 1 | return $fields; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Save post. |
||
| 129 | * |
||
| 130 | * @link https://developer.wordpress.org/reference/functions/get_post_meta/ |
||
| 131 | * |
||
| 132 | * @param int $post_id Post ID. |
||
| 133 | */ |
||
| 134 | public function save_post( $post_id ) { |
||
| 135 | $api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true ); |
||
| 136 | |||
| 137 | if ( ! is_string( $api_key ) ) { |
||
| 138 | return; |
||
| 139 | } |
||
| 140 | |||
| 141 | $api_key_prefix = substr( $api_key, 0, 4 ); |
||
| 142 | |||
| 143 | switch ( $api_key_prefix ) { |
||
| 144 | case 'live': |
||
| 145 | update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE ); |
||
| 146 | |||
| 147 | return; |
||
| 148 | case 'test': |
||
| 149 | update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST ); |
||
| 150 | |||
| 151 | return; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * User profile. |
||
| 157 | * |
||
| 158 | * @param WP_User $user WordPress user. |
||
| 159 | * |
||
| 160 | * @since 1.1.6 |
||
| 161 | * @link https://github.com/WordPress/WordPress/blob/4.5.2/wp-admin/user-edit.php#L578-L600 |
||
| 162 | */ |
||
| 163 | public static function user_profile( $user ) { |
||
| 164 | include __DIR__ . '/../views/html-admin-user-profile.php'; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Payment provider URL. |
||
| 169 | * |
||
| 170 | * @param string $url Payment provider URL. |
||
| 171 | * @param Payment $payment Payment. |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 1 | public function payment_provider_url( $url, Payment $payment ) { |
|
| 176 | 1 | return sprintf( |
|
| 177 | 1 | 'https://www.mollie.com/dashboard/payments/%s', |
|
| 178 | 1 | $payment->get_transaction_id() |
|
| 179 | ); |
||
| 180 | } |
||
| 181 | /** |
||
| 182 | * Get configuration by post ID. |
||
| 183 | * |
||
| 184 | * @param int $post_id Post ID. |
||
| 185 | * |
||
| 186 | * @return Config |
||
| 187 | */ |
||
| 188 | 2 | public function get_config( $post_id ) { |
|
| 189 | 2 | $config = new Config(); |
|
| 190 | |||
| 191 | 2 | $config->id = intval( $post_id ); |
|
| 192 | 2 | $config->api_key = $this->get_meta( $post_id, 'mollie_api_key' ); |
|
| 193 | 2 | $config->mode = $this->get_meta( $post_id, 'mode' ); |
|
| 194 | 2 | $config->due_date_days = $this->get_meta( $post_id, 'mollie_due_date_days' ); |
|
| 195 | |||
| 196 | 2 | return $config; |
|
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get gateway. |
||
| 201 | * |
||
| 202 | * @param int $post_id Post ID. |
||
| 203 | * @return Gateway |
||
| 204 | */ |
||
| 205 | 1 | public function get_gateway( $post_id ) { |
|
| 206 | 1 | return new Gateway( $this->get_config( $post_id ) ); |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Next payment delivery date. |
||
| 211 | * |
||
| 212 | * @param \DateTime $next_payment_delivery_date Next payment delivery date. |
||
| 213 | * @param Payment $payment Payment. |
||
| 214 | * |
||
| 215 | * @return \DateTime |
||
| 216 | */ |
||
| 217 | public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) { |
||
| 218 | // Check gateway. |
||
| 219 | $gateway_id = \get_post_meta( $payment->get_config_id(), '_pronamic_gateway_id', true ); |
||
| 220 | |||
| 221 | if ( 'mollie' !== $gateway_id ) { |
||
| 222 | return $next_payment_delivery_date; |
||
| 223 | } |
||
| 224 | |||
| 225 | // Check direct debit payment method. |
||
| 226 | if ( ! PaymentMethods::is_direct_debit_method( $payment->get_method() ) ) { |
||
| 227 | return $next_payment_delivery_date; |
||
| 228 | } |
||
| 229 | |||
| 230 | // Check subscription. |
||
| 231 | $subscription = $payment->get_subscription(); |
||
| 232 | |||
| 233 | if ( null === $subscription ) { |
||
| 234 | return $next_payment_delivery_date; |
||
| 235 | } |
||
| 236 | |||
| 237 | // Base delivery date on next payment date. |
||
| 238 | $next_payment_date = $subscription->get_next_payment_date(); |
||
| 239 | |||
| 240 | if ( null === $next_payment_date ) { |
||
| 241 | return $next_payment_delivery_date; |
||
| 242 | } |
||
| 243 | |||
| 244 | $next_payment_delivery_date = clone $next_payment_date; |
||
| 245 | |||
| 246 | // Textual representation of the day of the week, Sunday through Saturday. |
||
| 247 | $day_of_week = $next_payment_delivery_date->format( 'l' ); |
||
| 248 | |||
| 249 | /* |
||
| 250 | * Subtract days from next payment date for earlier delivery. |
||
| 251 | * |
||
| 252 | * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out- |
||
| 253 | * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times- |
||
| 254 | */ |
||
| 255 | switch ( $day_of_week ) { |
||
| 256 | case 'Monday': |
||
| 257 | $next_payment_delivery_date->sub( new \DateInterval( 'P3D' ) ); |
||
| 258 | break; |
||
| 259 | |||
| 260 | case 'Saturday': |
||
| 261 | $next_payment_delivery_date->sub( new \DateInterval( 'P2D' ) ); |
||
| 262 | break; |
||
| 263 | |||
| 264 | case 'Sunday': |
||
| 265 | $next_payment_delivery_date->sub( new \DateInterval( 'P3D' ) ); |
||
| 266 | break; |
||
| 267 | |||
| 268 | default: |
||
| 269 | $next_payment_delivery_date->sub( new \DateInterval( 'P1D' ) ); |
||
| 270 | break; |
||
| 271 | } |
||
| 272 | |||
| 273 | $next_payment_delivery_date->setTime( 0, 0, 0 ); |
||
| 274 | |||
| 275 | return $next_payment_delivery_date; |
||
| 276 | } |
||
| 277 | } |
||
| 278 |