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.0 |
||
| 26 | * @since 1.0.0 |
||
| 27 | */ |
||
| 28 | class Integration extends AbstractIntegration { |
||
| 29 | /** |
||
| 30 | * Construct and intialize Mollie integration. |
||
| 31 | 7 | */ |
|
| 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 | $this->supports = array( |
||
| 41 | 'payment_status_request', |
||
| 42 | 'webhook', |
||
| 43 | 'webhook_log', |
||
| 44 | 'webhook_no_config', |
||
| 45 | ); |
||
| 46 | |||
| 47 | 7 | // Actions. |
|
| 48 | $function = array( __NAMESPACE__ . '\Listener', 'listen' ); |
||
| 49 | 7 | ||
| 50 | 7 | if ( ! has_action( 'wp_loaded', $function ) ) { |
|
| 51 | add_action( 'wp_loaded', $function ); |
||
| 52 | } |
||
| 53 | 7 | ||
| 54 | 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 | 7 | ||
| 66 | 7 | // Filters. |
|
| 67 | $function = array( $this, 'next_payment_delivery_date' ); |
||
| 68 | |||
| 69 | if ( ! \has_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function ) ) { |
||
| 70 | add_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function, 10, 2 ); |
||
| 71 | } |
||
| 72 | |||
| 73 | 1 | add_filter( 'pronamic_payment_provider_url_mollie', array( $this, 'payment_provider_url' ), 10, 2 ); |
|
| 74 | 1 | } |
|
| 75 | |||
| 76 | /** |
||
| 77 | 1 | * Get settings fields. |
|
| 78 | 1 | * |
|
| 79 | 1 | * @return array |
|
| 80 | 1 | */ |
|
| 81 | 1 | public function get_settings_fields() { |
|
| 82 | 1 | $fields = array(); |
|
| 83 | |||
| 84 | 1 | // API Key. |
|
| 85 | $fields[] = array( |
||
| 86 | 'section' => 'general', |
||
| 87 | '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 | 1 | 'classes' => array( 'regular-text', 'code' ), |
|
| 92 | 'tooltip' => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ), |
||
| 93 | 1 | ); |
|
| 94 | |||
| 95 | 1 | // Webhook. |
|
| 96 | $fields[] = array( |
||
| 97 | 'section' => 'feedback', |
||
| 98 | 1 | 'title' => __( 'Webhook URL', 'pronamic_ideal' ), |
|
| 99 | 'type' => 'text', |
||
| 100 | 'classes' => array( 'large-text', 'code' ), |
||
| 101 | 'value' => add_query_arg( 'mollie_webhook', '', home_url( '/' ) ), |
||
| 102 | 'readonly' => true, |
||
| 103 | 'tooltip' => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ), |
||
| 104 | ); |
||
| 105 | |||
| 106 | return $fields; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Save post. |
||
| 111 | * |
||
| 112 | * @link https://developer.wordpress.org/reference/functions/get_post_meta/ |
||
| 113 | * |
||
| 114 | * @param int $post_id Post ID. |
||
| 115 | */ |
||
| 116 | public function save_post( $post_id ) { |
||
| 117 | $api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true ); |
||
| 118 | |||
| 119 | if ( ! is_string( $api_key ) ) { |
||
| 120 | return; |
||
| 121 | } |
||
| 122 | |||
| 123 | $api_key_prefix = substr( $api_key, 0, 4 ); |
||
| 124 | |||
| 125 | switch ( $api_key_prefix ) { |
||
| 126 | case 'live': |
||
| 127 | update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE ); |
||
| 128 | |||
| 129 | return; |
||
| 130 | case 'test': |
||
| 131 | update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST ); |
||
| 132 | |||
| 133 | return; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * User profile. |
||
| 139 | * |
||
| 140 | * @param WP_User $user WordPress user. |
||
| 141 | * |
||
| 142 | * @since 1.1.6 |
||
| 143 | * @link https://github.com/WordPress/WordPress/blob/4.5.2/wp-admin/user-edit.php#L578-L600 |
||
| 144 | */ |
||
| 145 | public static function user_profile( $user ) { |
||
| 146 | include __DIR__ . '/../views/html-admin-user-profile.php'; |
||
| 147 | } |
||
| 148 | |||
| 149 | 1 | /** |
|
| 150 | 1 | * Payment provider URL. |
|
| 151 | 1 | * |
|
| 152 | 1 | * @param string $url Payment provider URL. |
|
| 153 | * @param Payment $payment Payment. |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function payment_provider_url( $url, Payment $payment ) { |
||
| 158 | return sprintf( |
||
| 159 | 'https://www.mollie.com/dashboard/payments/%s', |
||
| 160 | $payment->get_transaction_id() |
||
| 161 | ); |
||
| 162 | 2 | } |
|
| 163 | 2 | /** |
|
| 164 | * Get configuration by post ID. |
||
| 165 | 2 | * |
|
| 166 | 2 | * @param int $post_id Post ID. |
|
| 167 | 2 | * |
|
| 168 | * @return Config |
||
| 169 | 2 | */ |
|
| 170 | public function get_config( $post_id ) { |
||
| 171 | $config = new Config(); |
||
| 172 | |||
| 173 | $config->id = intval( $post_id ); |
||
| 174 | $config->api_key = $this->get_meta( $post_id, 'mollie_api_key' ); |
||
| 175 | $config->mode = $this->get_meta( $post_id, 'mode' ); |
||
| 176 | |||
| 177 | return $config; |
||
| 178 | 1 | } |
|
| 179 | 1 | ||
| 180 | /** |
||
| 181 | * Get gateway. |
||
| 182 | * |
||
| 183 | * @param int $post_id Post ID. |
||
| 184 | * @return Gateway |
||
| 185 | */ |
||
| 186 | public function get_gateway( $post_id ) { |
||
| 187 | return new Gateway( $this->get_config( $post_id ) ); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Next payment delivery date. |
||
| 192 | * |
||
| 193 | * @param \DateTime $next_payment_delivery_date Next payment delivery date. |
||
| 194 | * @param Payment $payment Payment. |
||
| 195 | * |
||
| 196 | * @return \DateTime |
||
| 197 | */ |
||
| 198 | public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) { |
||
| 199 | // Check gateway. |
||
| 200 | $gateway_id = \get_post_meta( $payment->get_config_id(), '_pronamic_gateway_id', true ); |
||
| 201 | |||
| 202 | if ( 'mollie' !== $gateway_id ) { |
||
| 203 | return $next_payment_delivery_date; |
||
| 204 | } |
||
| 205 | |||
| 206 | // Check direct debit payment method. |
||
| 207 | if ( ! PaymentMethods::is_direct_debit_method( $payment->get_method() ) ) { |
||
| 208 | return $next_payment_delivery_date; |
||
| 209 | } |
||
| 210 | |||
| 211 | // Check subscription. |
||
| 212 | $subscription = $payment->get_subscription(); |
||
| 213 | |||
| 214 | if ( null === $subscription ) { |
||
| 215 | return $next_payment_delivery_date; |
||
| 216 | } |
||
| 217 | |||
| 218 | // Base delivery date on next payment date. |
||
| 219 | $next_payment_date = $subscription->get_next_payment_date(); |
||
| 220 | |||
| 221 | if ( null === $next_payment_date ) { |
||
| 222 | return $next_payment_delivery_date; |
||
| 223 | } |
||
| 224 | |||
| 225 | $next_payment_delivery_date = clone $next_payment_date; |
||
| 226 | |||
| 227 | // Textual representation of the day of the week, Sunday through Saturday. |
||
| 228 | $day_of_week = $next_payment_delivery_date->format( 'l' ); |
||
| 229 | |||
| 230 | /* |
||
| 231 | * Subtract days from next payment date for earlier delivery. |
||
| 232 | * |
||
| 233 | * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out- |
||
| 234 | * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times- |
||
| 235 | */ |
||
| 236 | switch ( $day_of_week ) { |
||
| 237 | case 'Monday': |
||
| 238 | $next_payment_delivery_date->sub( new \DateInterval( 'P3D' ) ); |
||
| 239 | break; |
||
| 240 | |||
| 241 | case 'Saturday': |
||
| 242 | $next_payment_delivery_date->sub( new \DateInterval( 'P2D' ) ); |
||
| 243 | break; |
||
| 244 | |||
| 245 | case 'Sunday': |
||
| 246 | $next_payment_delivery_date->sub( new \DateInterval( 'P3D' ) ); |
||
| 247 | break; |
||
| 248 | |||
| 249 | default: |
||
| 250 | $next_payment_delivery_date->sub( new \DateInterval( 'P1D' ) ); |
||
| 251 | break; |
||
| 252 | } |
||
| 253 | |||
| 254 | $next_payment_delivery_date->setTime( 0, 0, 0 ); |
||
| 255 | |||
| 256 | return $next_payment_delivery_date; |
||
| 257 | } |
||
| 258 | } |
||
| 259 |