Passed
Push — develop ( b1637b...512b2b )
by Reüel
05:41
created

Extension::source_description()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Extension
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Extensions\MemberPress
9
 */
10
11
namespace Pronamic\WordPress\Pay\Extensions\MemberPress;
12
13
use MeprOptions;
14
use MeprProduct;
15
use MeprTransaction;
16
use MeprSubscription;
17
use Pronamic\WordPress\Pay\Core\Statuses;
18
use Pronamic\WordPress\Pay\Payments\Payment;
19
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
20
21
/**
22
 * WordPress pay MemberPress extension
23
 *
24
 * @author  Remco Tolsma
25
 * @version 2.0.0
26
 * @since   1.0.0
27
 */
28
class Extension {
29
	/**
30
	 * The slug of this addon
31
	 *
32
	 * @var string
33
	 */
34
	const SLUG = 'memberpress';
35
36
	/**
37
	 * Bootstrap
38
	 */
39
	public static function bootstrap() {
40
		new self();
41
	}
42
43
	/**
44
	 * Constructs and initializes the MemberPress extension.
45
	 */
46
	public function __construct() {
47
		// @see https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprGatewayFactory.php#L48-50
48
		add_filter( 'mepr-gateway-paths', array( $this, 'gateway_paths' ) );
49
50
		add_filter( 'pronamic_payment_redirect_url_' . self::SLUG, array( __CLASS__, 'redirect_url' ), 10, 2 );
51
		add_action( 'pronamic_payment_status_update_' . self::SLUG, array( __CLASS__, 'status_update' ), 10, 1 );
52
53
		add_filter( 'pronamic_payment_source_text_' . self::SLUG, array( __CLASS__, 'source_text' ), 10, 2 );
54
		add_filter( 'pronamic_payment_source_description_' . self::SLUG, array( __CLASS__, 'source_description' ), 10, 2 );
55
		add_filter( 'pronamic_payment_source_url_' . self::SLUG, array( __CLASS__, 'source_url' ), 10, 2 );
56
		add_filter( 'pronamic_subscription_source_text_' . self::SLUG, array( __CLASS__, 'subscription_source_text' ), 10, 2 );
57
		add_filter( 'pronamic_subscription_source_description_' . self::SLUG, array( __CLASS__, 'subscription_source_description' ), 10, 2 );
58
59
		add_action( 'mepr_subscription_pre_delete', array( $this, 'subscription_pre_delete' ), 10, 1 );
60
61
		add_action( 'mepr_subscription_transition_status', array( $this, 'memberpress_subscription_transition_status' ), 10, 3 );
62
	}
63
64
	/**
65
	 * Gateway paths.
66
	 *
67
	 * @see https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprGatewayFactory.php#L48-50
68
	 *
69
	 * @param array $paths Array with gateway paths.
70
	 * @return array
71
	 */
72
	public function gateway_paths( $paths ) {
73
		$paths[] = dirname( __FILE__ ) . '/../gateways/';
74
75
		return $paths;
76
	}
77
78
	/**
79
	 * Payment redirect URL filter.
80
	 *
81
	 * @since 1.0.1
82
	 *
83
	 * @param string  $url     Payment redirect URL.
84
	 * @param Payment $payment Payment to redirect for.
85
	 *
86
	 * @return string
87
	 */
88
	public static function redirect_url( $url, Payment $payment ) {
89
		global $transaction;
90
91
		$transaction_id = $payment->get_source_id();
92
93
		$transaction = new MeprTransaction( $transaction_id );
94
95
		switch ( $payment->get_status() ) {
96
			case Statuses::CANCELLED:
97
			case Statuses::EXPIRED:
98
			case Statuses::FAILURE:
99
				$product = $transaction->product();
100
101
				$url = add_query_arg(
102
					array(
103
						'action'   => 'payment_form',
104
						'txn'      => $transaction->trans_num,
105
						'_wpnonce' => wp_create_nonce( 'mepr_payment_form' ),
106
					),
107
					$product->url()
108
				);
109
110
				break;
111
			case Statuses::SUCCESS:
112
				// @see https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L768-782
113
				$mepr_options = MeprOptions::fetch();
114
115
				$product         = new MeprProduct( $transaction->product_id );
116
				$sanitized_title = sanitize_title( $product->post_title );
117
118
				$args = array(
119
					'membership_id' => $product->ID,
120
					'membership'    => $sanitized_title,
121
					'trans_num'     => $transaction->trans_num,
122
				);
123
124
				$url = $mepr_options->thankyou_page_url( http_build_query( $args ) );
125
126
				break;
127
			case Statuses::OPEN:
128
			default:
129
				break;
130
		}
131
132
		return $url;
133
	}
134
135
	/**
136
	 * Update lead status of the specified payment.
137
	 *
138
	 * @see https://github.com/Charitable/Charitable/blob/1.1.4/includes/gateways/class-charitable-gateway-paypal.php#L229-L357
139
	 *
140
	 * @param Payment $payment The payment whose status is updated.
141
	 */
142
	public static function status_update( Payment $payment ) {
143
		$transaction_id = $payment->get_source_id();
144
145
		$transaction = new MeprTransaction( $transaction_id );
146
147
		if ( $payment->get_recurring() ) {
148
			$subscription = $transaction->subscription();
149
150
			if ( empty( $subscription ) || empty( $subscription->id ) ) {
151
				$subscription_id = $payment->get_subscription()->get_source_id();
152
153
				$subscription = new MeprSubscription( $subscription_id );
154
			}
155
156
			// Same source ID and first transaction ID for recurring payment means we need to add a new transaction.
157
			if ( $payment->get_source_id() === $subscription->id ) {
158
				// First transaction.
159
				$first_txn = $subscription->first_txn();
160
161
				if ( false === $first_txn || ! ( $first_txn instanceof MeprTransaction ) ) {
162
					$first_txn             = new MeprTransaction();
163
					$first_txn->user_id    = $subscription->user_id;
164
					$first_txn->product_id = $subscription->product_id;
165
					$first_txn->coupon_id  = $subscription->coupon_id;
166
					$first_txn->gateway    = null;
1 ignored issue
show
Bug Best Practice introduced by
The property gateway does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
167
				}
168
169
				// Transaction number.
170
				$trans_num = $payment->get_transaction_id();
171
172
				if ( empty( $trans_num ) ) {
173
					$trans_num = uniqid();
174
				}
175
176
				// New transaction.
177
				$transaction                  = new MeprTransaction();
178
				$transaction->created_at      = $payment->post->post_date_gmt;
1 ignored issue
show
Bug Best Practice introduced by
The property created_at does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
179
				$transaction->user_id         = $first_txn->user_id;
180
				$transaction->product_id      = $first_txn->product_id;
181
				$transaction->coupon_id       = $first_txn->coupon_id;
182
				$transaction->gateway         = $first_txn->gateway;
183
				$transaction->trans_num       = $trans_num;
1 ignored issue
show
Bug Best Practice introduced by
The property trans_num does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
184
				$transaction->txn_type        = MeprTransaction::$payment_str;
1 ignored issue
show
Bug Best Practice introduced by
The property txn_type does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
185
				$transaction->status          = MeprTransaction::$pending_str;
1 ignored issue
show
Bug Best Practice introduced by
The property status does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
186
				$transaction->subscription_id = $subscription->id;
1 ignored issue
show
Bug Best Practice introduced by
The property subscription_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
187
188
				$transaction->set_gross( $payment->get_amount()->get_amount() );
189
190
				$transaction->store();
191
192
				// Set source ID.
193
				$payment->set_meta( 'source_id', $transaction->id );
194
195
				$payment->source_id = $transaction->id;
196
			}
197
		}
198
199
		$should_update = ! MemberPress::transaction_has_status( $transaction, array(
200
			MeprTransaction::$failed_str,
201
			MeprTransaction::$complete_str,
202
		) );
203
204
		if ( $should_update ) {
205
			$gateway = new Gateway();
206
207
			$gateway->mp_txn = $transaction;
208
209
			switch ( $payment->get_status() ) {
210
				case Statuses::CANCELLED:
211
				case Statuses::EXPIRED:
212
				case Statuses::FAILURE:
213
					$gateway->record_payment_failure();
214
215
					break;
216
				case Statuses::SUCCESS:
217
					if ( $payment->get_recurring() ) {
218
						$gateway->record_subscription_payment();
219
					} else {
220
						$gateway->record_payment();
221
					}
222
223
					break;
224
				case Statuses::OPEN:
225
				default:
226
					break;
227
			}
228
		}
229
	}
230
231
	/**
232
	 * Subscription deleted.
233
	 *
234
	 * @param int $subscription_id MemberPress subscription id.
235
	 */
236
	public function subscription_pre_delete( $subscription_id ) {
237
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $subscription_id );
238
239
		if ( ! $subscription ) {
240
			return;
241
		}
242
243
		// Add note.
244
		$note = sprintf(
245
			/* translators: %s: MemberPress */
246
			__( '%s subscription deleted.', 'pronamic_ideal' ),
247
			__( 'MemberPress', 'pronamic_ideal' )
248
		);
249
250
		$subscription->add_note( $note );
251
252
		// The status of canceled or completed subscriptions will not be changed automatically.
253
		if ( ! in_array( $subscription->get_status(), array( Statuses::CANCELLED, Statuses::COMPLETED ), true ) ) {
254
			$subscription->set_status( Statuses::CANCELLED );
255
256
			$subscription->save();
257
		}
258
	}
259
260
	/**
261
	 * Source text.
262
	 *
263
	 * @param string  $text    Source text.
264
	 * @param Payment $payment Payment to create the source text for.
265
	 *
266
	 * @return string
267
	 */
268
	public static function source_text( $text, Payment $payment ) {
269
		$text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />';
270
271
		$text .= sprintf(
272
			'<a href="%s">%s</a>',
273
			add_query_arg( array(
274
				'page'   => 'memberpress-trans',
275
				'action' => 'edit',
276
				'id'     => $payment->source_id,
277
			), admin_url( 'admin.php' ) ),
278
			/* translators: %s: payment source id */
279
			sprintf( __( 'Transaction %s', 'pronamic_ideal' ), $payment->source_id )
280
		);
281
282
		return $text;
283
	}
284
285
	/**
286
	 * Subscription source text.
287
	 *
288
	 * @param string       $text         Source text.
289
	 * @param Subscription $subscription Subscription to create the source text for.
290
	 *
291
	 * @return string
292
	 */
293
	public static function subscription_source_text( $text, Subscription $subscription ) {
294
		$text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />';
295
296
		$text .= sprintf(
297
			'<a href="%s">%s</a>',
298
			add_query_arg( array(
299
				'page'   => 'memberpress-trans',
300
				'action' => 'subscription',
301
				'id'     => $subscription->source_id,
302
			), admin_url( 'admin.php' ) ),
303
			/* translators: %s: payment source id */
304
			sprintf( __( 'Subscription %s', 'pronamic_ideal' ), $subscription->source_id )
305
		);
306
307
		return $text;
308
	}
309
310
	/**
311
	 * Source description.
312
	 *
313
	 * @param string  $description Description.
314
	 * @param Payment $payment     Payment to create the description for.
315
	 *
316
	 * @return string
317
	 */
318
	public static function source_description( $description, Payment $payment ) {
319
		return __( 'MemberPress Transaction', 'pronamic_ideal' );
320
	}
321
322
	/**
323
	 * Subscription source description.
324
	 *
325
	 * @param string       $description  Description.
326
	 * @param Subscription $subscription Subscription to create the description for.
327
	 *
328
	 * @return string
329
	 */
330
	public static function subscription_source_description( $description, Subscription $subscription ) {
331
		return __( 'MemberPress Subscription', 'pronamic_ideal' );
332
	}
333
334
	/**
335
	 * Source URL.
336
	 *
337
	 * @param string  $url     URL.
338
	 * @param Payment $payment The payment to create the source URL for.
339
	 *
340
	 * @return string
341
	 */
342
	public static function source_url( $url, Payment $payment ) {
343
		$url = add_query_arg( array(
344
			'page'   => 'memberpress-trans',
345
			'action' => 'edit',
346
			'id'     => $payment->source_id,
347
		), admin_url( 'admin.php' ) );
348
349
		return $url;
350
	}
351
352
	/**
353
	 * MemberPress update subscription.
354
	 *
355
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprSubscriptionsCtrl.php#L92-L111
356
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L100-L123
357
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L112
358
	 *
359
	 * @param string           $status_old               Old status identifier.
360
	 * @param string           $status_new               New status identifier.
361
	 * @param MeprSubscription $memberpress_subscription MemberPress subscription object.
362
	 */
363
	public function memberpress_subscription_transition_status( $status_old, $status_new, $memberpress_subscription ) {
364
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $memberpress_subscription->id );
365
366
		if ( empty( $subscription ) ) {
367
			return;
368
		}
369
370
		$status = SubscriptionStatuses::transform( $status_new );
371
372
		$subscription->set_status( $status );
373
374
		$subscription->save();
375
	}
376
}
377