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