Passed
Push — develop ( 2a2acc...9eb85d )
by Remco
04:08
created

StatusChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Status Checker
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Payments
9
 */
10
11
namespace Pronamic\WordPress\Pay\Payments;
12
13
use Pronamic\WordPress\Pay\Core\Statuses;
14
use Pronamic\WordPress\Pay\Plugin;
15
16
/**
17
 * Title: WordPress payment status checker
18
 * Description:
19
 * Copyright: Copyright (c) 2005 - 2018
20
 * Company: Pronamic
21
 *
22
 * @author Remco Tolsma
23
 * @version 4.5.3
24
 * @since 1.0.0
25
 */
26
class StatusChecker {
27
	/**
28
	 * Schedule event.
29
	 *
30
	 * @param Payment $payment The payment to schedule the status check event.
31
	 */
32
	public static function schedule_event( $payment ) {
33
		/*
34
		 * Schedule status requests
35
		 * http://pronamic.nl/wp-content/uploads/2011/12/iDEAL_Advanced_PHP_EN_V2.2.pdf (page 19)
36
		 *
37
		 * @todo
38
		 * Considering the number of status requests per transaction:
39
		 * - Maximum of five times per transaction;
40
		 * - Maximum of two times during the expirationPeriod;
41
		 * - After the expirationPeriod not more often than once per 60 minutes;
42
		 * - No status request after a final status has been received for a transaction;
43
		 * - No status request for transactions older than 7 days.
44
		 */
45
46
		/*
47
		 * The function `wp_schedule_single_event` uses the arguments array as an key for the event,
48
		 * that's why we also add the time to this array, besides that it's also much clearer on
49
		 * the Cron View (http://wordpress.org/extend/plugins/cron-view/) page.
50
		 */
51
52
		$time = time();
53
54
		// 15 minutes after a transaction request is sent
55
		$delay = 15 * MINUTE_IN_SECONDS;
56
57
		wp_schedule_single_event(
58
			$time + $delay, 'pronamic_ideal_check_transaction_status', array(
59
				'payment_id' => $payment->get_id(),
60
				'seconds'    => $delay,
61
			)
62
		);
63
	}
64
65
	/**
66
	 * Get the delay seconds for the specified number of tries.
67
	 *
68
	 * @param int $number_tries The number of tries to get the delay seconds for.
69
	 *
70
	 * @return int
71
	 */
72
	private function get_delay_seconds( $number_tries ) {
73
		switch ( $number_tries ) {
74
			case 0:
75
				// 15 minutes after a transaction request is sent.
76
				return 15 * MINUTE_IN_SECONDS;
77
			case 1:
78
				// Half-way through an expirationPeriod.
79
				return 30 * MINUTE_IN_SECONDS;
80
			case 2:
81
				// Just after an expirationPeriod.
82
				return HOUR_IN_SECONDS;
83
			case 3:
84
			default:
85
				return DAY_IN_SECONDS;
86
		}
87
	}
88
89
	/**
90
	 * Check status of the specified payment.
91
	 *
92
	 * @param integer $payment_id   The ID of a payment to check.
93
	 * @param integer $seconds      The number of seconds this status check was delayed.
94
	 * @param integer $number_tries The number of status check tries.
95
	 *
96
	 * @internal param string $paymentId
97
	 */
98
	public function check_status( $payment_id = null, $seconds = null, $number_tries = 1 ) {
99
		$payment = new Payment( $payment_id );
100
101
		// Empty payment.
102
		if ( null === $payment ) {
103
			// Payment with the specified ID could not be found, can't check the status.
104
			return;
105
		}
106
107
		// Limit number tries.
108
		if ( $number_tries >= 4 ) {
109
			return;
110
		}
111
112
		// http://pronamic.nl/wp-content/uploads/2011/12/iDEAL_Advanced_PHP_EN_V2.2.pdf (page 19)
113
		// - No status request after a final status has been received for a transaction.
114
		if ( empty( $payment->status ) || Statuses::OPEN === $payment->status ) {
115
			Plugin::update_payment( $payment );
116
117
			if ( empty( $payment->status ) || Statuses::OPEN === $payment->status ) {
118
				$time = time();
119
120
				$seconds = $this->get_delay_seconds( $number_tries );
121
122
				wp_schedule_single_event(
123
					$time + $seconds, 'pronamic_ideal_check_transaction_status', array(
124
						'payment_id'   => $payment->get_id(),
125
						'seconds'      => $seconds,
126
						'number_tries' => $number_tries + 1,
127
					)
128
				);
129
			}
130
		}
131
	}
132
}
133