Passed
Push — develop ( a056ec...770b15 )
by Reüel
04:53
created

PaymentsModule::update_free_payments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payments Module
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Subscriptions
9
 */
10
11
namespace Pronamic\WordPress\Pay\Payments;
12
13
use Pronamic\WordPress\Pay\Plugin;
14
use Pronamic\WordPress\Pay\Core\Statuses;
15
16
/**
17
 * Title: Payments module
18
 * Description:
19
 * Copyright: Copyright (c) 2005 - 2018
20
 * Company: Pronamic
21
 *
22
 * @see https://woocommerce.com/2017/04/woocommerce-3-0-release/
23
 * @see https://woocommerce.wordpress.com/2016/10/27/the-new-crud-classes-in-woocommerce-2-7/
24
 * @author Remco Tolsma
25
 * @version 3.7.0
26
 * @since 3.7.0
27
 */
28
class PaymentsModule {
29
	/**
30
	 * Plugin.
31
	 *
32
	 * @var Plugin $plugin
33
	 */
34
	public $plugin;
35
36
	/**
37
	 * Free payments to complete at shutdown.
38
	 *
39
	 * @var array
40
	 */
41
	public $free = array();
42
43
	/**
44
	 * Construct and initialize a payments module object.
45
	 *
46
	 * @param Plugin $plugin The plugin.
47
	 */
48
	public function __construct( Plugin $plugin ) {
49
		$this->plugin = $plugin;
50
51
		// Exclude payment notes.
52
		add_filter( 'comments_clauses', array( $this, 'exclude_payment_comment_notes' ), 10, 2 );
53
54
		// Payment redirect URL.
55
		add_filter( 'pronamic_payment_redirect_url', array( $this, 'payment_redirect_url' ), 5, 2 );
56
57
		// Listen to payment status changes so we can log these in a note.
58
		add_action( 'pronamic_payment_status_update', array( $this, 'log_payment_status_update' ), 10, 4 );
59
60
		// Shutdown.
61
		add_action( 'shutdown', array( $this, 'update_free_payments' ) );
62
63
		// Payment Status Checker.
64
		$status_checker = new StatusChecker();
65
66
		// The 'pronamic_ideal_check_transaction_status' hook is scheduled to request the payment status.
67
		add_action( 'pronamic_ideal_check_transaction_status', array( $status_checker, 'check_status' ), 10, 3 );
68
	}
69
70
	/**
71
	 * Comments clauses.
72
	 *
73
	 * @param array             $clauses Array with query clauses for the comments query.
74
	 * @param \WP_Comment_Query $query   A WordPress comment query object.
75
	 *
76
	 * @return array
77
	 */
78
	public function exclude_payment_comment_notes( $clauses, $query ) {
79
		$type = $query->query_vars['type'];
80
81
		// Ignore payment notes comments if it's not specifically requested.
82
		if ( 'payment_note' !== $type ) {
83
			$clauses['where'] .= " AND comment_type != 'payment_note'";
84
		}
85
86
		return $clauses;
87
	}
88
89
	/**
90
	 * Payment redirect URL filter.
91
	 *
92
	 * @param string  $url     A payment redirect URL.
93
	 * @param Payment $payment The payment to get a redirect URL for.
94
	 *
95
	 * @return string
96
	 */
97
	public function payment_redirect_url( $url, $payment ) {
98
		$page_id = null;
99
100
		switch ( $payment->status ) {
101
			case Statuses::CANCELLED:
102
				$page_id = pronamic_pay_get_page_id( 'cancel' );
103
104
				break;
105
			case Statuses::EXPIRED:
106
				$page_id = pronamic_pay_get_page_id( 'expired' );
107
108
				break;
109
			case Statuses::FAILURE:
110
				$page_id = pronamic_pay_get_page_id( 'error' );
111
112
				break;
113
			case Statuses::OPEN:
114
				$page_id = pronamic_pay_get_page_id( 'unknown' );
115
116
				break;
117
			case Statuses::SUCCESS:
118
				$page_id = pronamic_pay_get_page_id( 'completed' );
119
120
				break;
121
			default:
122
				$page_id = pronamic_pay_get_page_id( 'unknown' );
123
124
				break;
125
		}
126
127
		if ( ! empty( $page_id ) ) {
128
			$page_url = get_permalink( $page_id );
129
130
			if ( false !== $page_url ) {
131
				$url = $page_url;
132
			}
133
		}
134
135
		return $url;
136
	}
137
138
	/**
139
	 * Payment status update.
140
	 *
141
	 * @param Payment $payment      The status updated payment.
142
	 * @param bool    $can_redirect Whether or not redirects should be performed.
143
	 * @param string  $old_status   Old meta status.
144
	 * @param string  $new_status   New meta status.
145
	 *
146
	 * @return void
147
	 */
148
	public function log_payment_status_update( $payment, $can_redirect, $old_status, $new_status ) {
149
		$note = sprintf(
150
			__( 'Payment status changed from "%1$s" to "%2$s".', 'pronamic_ideal' ),
151
			esc_html( $this->plugin->payments_data_store->get_meta_status_label( $old_status ) ),
152
			esc_html( $this->plugin->payments_data_store->get_meta_status_label( $new_status ) )
153
		);
154
155
		if ( null === $old_status ) {
0 ignored issues
show
introduced by
The condition null === $old_status is always false.
Loading history...
156
			$note = sprintf(
157
				__( 'Payment created with status "%1$s".', 'pronamic_ideal' ),
158
				esc_html( $this->plugin->payments_data_store->get_meta_status_label( $new_status ) )
159
			);
160
		}
161
162
		$payment->add_note( $note );
163
	}
164
165
	/**
166
	 * Update free payments.
167
	 */
168
	public function update_free_payments() {
169
		$can_redirect = false;
170
171
		foreach ( $this->free as $payment_id ) {
172
			$payment = get_pronamic_payment( $payment_id );
173
174
			Plugin::update_payment( $payment, $can_redirect );
175
		}
176
	}
177
}
178