Failed Conditions
Push — develop ( 2b78ec...9ec485 )
by Reüel
04:35
created

src/Extension.php (1 issue)

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