Failed Conditions
Push — develop ( 2374f0...14a1b4 )
by Reüel
03:47
created

src/Integration.php (1 issue)

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
The property register_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
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
		// 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( 'Due date days', 'mollie', 'pronamic_ideal' ),
101 1
			'type'        => 'number',
102 1
			'min'         => 1,
103 1
			'max'         => 100,
104 1
			'default'     => 12,
105
			'classes'     => array( 'regular-text' ),
106 1
			'tooltip'     => __( 'Number of days after which a bank transfer payment expires.', 'pronamic_ideal' ),
107 1
			'description' => sprintf(
108
				/* translators: 1: <code>1</code>, 2: <code>100</code>, 3: <code>12</code> */
109 1
				__( 'Minimum %1$s and maximum %2$s days. Default: %3$s days.', 'pronamic_ideal' ),
110 1
				sprintf( '<code>%s</code>', '1' ),
111 1
				sprintf( '<code>%s</code>', '100' ),
112 1
				sprintf( '<code>%s</code>', '12' )
113
			),
114
		);
115
116
		// Webhook.
117 1
		$fields[] = array(
118 1
			'section'  => 'feedback',
119 1
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
120 1
			'type'     => 'text',
121
			'classes'  => array( 'large-text', 'code' ),
122 1
			'value'    => add_query_arg( 'mollie_webhook', '', home_url( '/' ) ),
123
			'readonly' => true,
124 1
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
125
		);
126
127 1
		return $fields;
128
	}
129
130
	/**
131
	 * Save post.
132
	 *
133
	 * @link https://developer.wordpress.org/reference/functions/get_post_meta/
134
	 *
135
	 * @param int $post_id Post ID.
136
	 */
137
	public function save_post( $post_id ) {
138
		$api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true );
139
140
		if ( ! is_string( $api_key ) ) {
141
			return;
142
		}
143
144
		$api_key_prefix = substr( $api_key, 0, 4 );
145
146
		switch ( $api_key_prefix ) {
147
			case 'live':
148
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE );
149
150
				return;
151
			case 'test':
152
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST );
153
154
				return;
155
		}
156
	}
157
158
	/**
159
	 * User profile.
160
	 *
161
	 * @param WP_User $user WordPress user.
162
	 *
163
	 * @since 1.1.6
164
	 * @link https://github.com/WordPress/WordPress/blob/4.5.2/wp-admin/user-edit.php#L578-L600
165
	 */
166
	public static function user_profile( $user ) {
167
		include __DIR__ . '/../views/html-admin-user-profile.php';
168
	}
169
170
	/**
171
	 * Payment provider URL.
172
	 *
173
	 * @param string  $url     Payment provider URL.
174
	 * @param Payment $payment Payment.
175
	 *
176
	 * @return string
177
	 */
178 1
	public function payment_provider_url( $url, Payment $payment ) {
179 1
		return sprintf(
180 1
			'https://www.mollie.com/dashboard/payments/%s',
181 1
			$payment->get_transaction_id()
182
		);
183
	}
184
	/**
185
	 * Get configuration by post ID.
186
	 *
187
	 * @param int $post_id Post ID.
188
	 *
189
	 * @return Config
190
	 */
191 2
	public function get_config( $post_id ) {
192 2
		$config = new Config();
193
194 2
		$config->id            = intval( $post_id );
195 2
		$config->api_key       = $this->get_meta( $post_id, 'mollie_api_key' );
196 2
		$config->mode          = $this->get_meta( $post_id, 'mode' );
197 2
		$config->due_date_days = $this->get_meta( $post_id, 'mollie_due_date_days' );
198
199 2
		return $config;
200
	}
201
202
	/**
203
	 * Get gateway.
204
	 *
205
	 * @param int $post_id Post ID.
206
	 * @return Gateway
207
	 */
208 1
	public function get_gateway( $post_id ) {
209 1
		return new Gateway( $this->get_config( $post_id ) );
210
	}
211
212
	/**
213
	 * Next payment delivery date.
214
	 *
215
	 * @param \DateTime $next_payment_delivery_date Next payment delivery date.
216
	 * @param Payment   $payment                    Payment.
217
	 *
218
	 * @return \DateTime
219
	 */
220
	public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) {
221
		// Check gateway.
222
		$gateway_id = \get_post_meta( $payment->get_config_id(), '_pronamic_gateway_id', true );
223
224
		if ( 'mollie' !== $gateway_id ) {
225
			return $next_payment_delivery_date;
226
		}
227
228
		// Check direct debit payment method.
229
		if ( ! PaymentMethods::is_direct_debit_method( $payment->get_method() ) ) {
230
			return $next_payment_delivery_date;
231
		}
232
233
		// Check subscription.
234
		$subscription = $payment->get_subscription();
235
236
		if ( null === $subscription ) {
237
			return $next_payment_delivery_date;
238
		}
239
240
		// Base delivery date on next payment date.
241
		$next_payment_date = $subscription->get_next_payment_date();
242
243
		if ( null === $next_payment_date ) {
244
			return $next_payment_delivery_date;
245
		}
246
247
		$next_payment_delivery_date = clone $next_payment_date;
248
249
		// Textual representation of the day of the week, Sunday through Saturday.
250
		$day_of_week = $next_payment_delivery_date->format( 'l' );
251
252
		/*
253
		 * Subtract days from next payment date for earlier delivery.
254
		 *
255
		 * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out-
256
		 * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times-
257
		 */
258
		switch ( $day_of_week ) {
259
			case 'Monday':
260
				$next_payment_delivery_date->sub( new \DateInterval( 'P3D' ) );
261
				break;
262
263
			case 'Saturday':
264
				$next_payment_delivery_date->sub( new \DateInterval( 'P2D' ) );
265
				break;
266
267
			case 'Sunday':
268
				$next_payment_delivery_date->sub( new \DateInterval( 'P3D' ) );
269
				break;
270
271
			default:
272
				$next_payment_delivery_date->sub( new \DateInterval( 'P1D' ) );
273
				break;
274
		}
275
276
		$next_payment_delivery_date->setTime( 0, 0, 0 );
277
278
		return $next_payment_delivery_date;
279
	}
280
}
281