1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Extension |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2021 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\AbstractPluginIntegration; |
20
|
|
|
use Pronamic\WordPress\Pay\Payments\PaymentStatus; |
21
|
|
|
use Pronamic\WordPress\Pay\Extensions\MemberPress\Gateways\Gateway; |
22
|
|
|
use Pronamic\WordPress\Pay\Payments\Payment; |
23
|
|
|
use Pronamic\WordPress\Pay\Subscriptions\Subscription; |
24
|
|
|
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionStatus; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* WordPress pay MemberPress extension |
28
|
|
|
* |
29
|
|
|
* @author Remco Tolsma |
30
|
|
|
* @version 2.2.3 |
31
|
|
|
* @since 1.0.0 |
32
|
|
|
*/ |
33
|
|
|
class Extension extends AbstractPluginIntegration { |
34
|
|
|
/** |
35
|
|
|
* The slug of this addon |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
const SLUG = 'memberpress'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Construct MemberPress plugin integration. |
43
|
|
|
* |
44
|
|
|
* @param array<string, mixed> $args Arguments. |
45
|
|
|
*/ |
46
|
|
|
public function __construct( $args = array() ) { |
47
|
|
|
$args['name'] = __( 'MemberPress', 'pronamic_ideal' ); |
48
|
|
|
|
49
|
|
|
parent::__construct( $args ); |
50
|
|
|
|
51
|
|
|
// Dependencies. |
52
|
|
|
$dependencies = $this->get_dependencies(); |
53
|
|
|
|
54
|
|
|
$dependencies->add( new MemberPressDependency() ); |
55
|
|
|
|
56
|
|
|
// Upgrades. |
57
|
|
|
$upgrades = $this->get_upgrades(); |
58
|
|
|
|
59
|
|
|
$upgrades->set_executable( true ); |
60
|
|
|
|
61
|
|
|
$upgrades->add( new Upgrade310() ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Setup. |
66
|
|
|
*/ |
67
|
|
|
public function setup() { |
68
|
|
|
\add_filter( 'pronamic_subscription_source_description_memberpress_subscription', array( $this, 'subscription_source_description' ), 10, 2 ); |
69
|
|
|
\add_filter( 'pronamic_payment_source_description_memberpress_transaction', array( $this, 'source_description' ), 10, 2 ); |
70
|
|
|
|
71
|
|
|
// Check if dependencies are met and integration is active. |
72
|
|
|
if ( ! $this->is_active() ) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprGatewayFactory.php#L48-50 |
77
|
|
|
\add_filter( 'mepr-gateway-paths', array( $this, 'gateway_paths' ) ); |
78
|
|
|
|
79
|
|
|
\add_filter( 'pronamic_payment_redirect_url_memberpress_transaction', array( $this, 'redirect_url' ), 10, 2 ); |
80
|
|
|
\add_action( 'pronamic_payment_status_update_memberpress_transaction', array( $this, 'status_update' ), 10, 1 ); |
81
|
|
|
|
82
|
|
|
\add_action( 'pronamic_pay_new_payment', array( $this, 'maybe_create_memberpress_transaction' ), 10, 1 ); |
83
|
|
|
|
84
|
|
|
\add_filter( 'pronamic_subscription_source_text_memberpress_subscription', array( $this, 'subscription_source_text' ), 10, 2 ); |
85
|
|
|
\add_filter( 'pronamic_subscription_source_url_memberpress_subscription', array( $this, 'subscription_source_url' ), 10, 2 ); |
86
|
|
|
|
87
|
|
|
\add_filter( 'pronamic_payment_source_text_memberpress_transaction', array( $this, 'source_text' ), 10, 2 ); |
88
|
|
|
\add_filter( 'pronamic_payment_source_url_memberpress_transaction', array( $this, 'source_url' ), 10, 2 ); |
89
|
|
|
|
90
|
|
|
\add_action( 'mepr_subscription_pre_delete', array( $this, 'subscription_pre_delete' ), 10, 1 ); |
91
|
|
|
|
92
|
|
|
\add_action( 'mepr_subscription_transition_status', array( $this, 'memberpress_subscription_transition_status' ), 10, 3 ); |
93
|
|
|
|
94
|
|
|
// MemberPress subscription email parameters. |
95
|
|
|
\add_filter( 'mepr_subscription_email_params', array( $this, 'subscription_email_params' ), 10, 2 ); |
96
|
|
|
\add_filter( 'mepr_transaction_email_params', array( $this, 'transaction_email_params' ), 10, 2 ); |
97
|
|
|
|
98
|
|
|
// Hide MemberPress columns for payments and subscriptions. |
99
|
|
|
\add_action( 'registered_post_type', array( $this, 'post_type_columns_hide' ), 15, 1 ); |
100
|
|
|
|
101
|
|
|
if ( \is_admin() ) { |
102
|
|
|
$admin_subscriptions = new Admin\AdminSubscriptions(); |
103
|
|
|
$admin_transactions = new Admin\AdminTransactions(); |
104
|
|
|
|
105
|
|
|
$admin_subscriptions->setup(); |
106
|
|
|
$admin_transactions->setup(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gateway paths. |
112
|
|
|
* |
113
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprGatewayFactory.php#L49 |
114
|
|
|
* @param string[] $paths Array with gateway paths. |
115
|
|
|
* @return string[] |
116
|
|
|
*/ |
117
|
|
|
public function gateway_paths( $paths ) { |
118
|
|
|
$paths[] = dirname( __FILE__ ) . '/../gateways/'; |
119
|
|
|
|
120
|
|
|
return $paths; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Hide MemberPress columns for payments and subscriptions. |
125
|
|
|
* |
126
|
|
|
* @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/controllers/MeprAppCtrl.php#L129-146 |
127
|
|
|
* |
128
|
|
|
* @param string $post_type Registered post type. |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function post_type_columns_hide( $post_type ) { |
133
|
|
|
if ( ! in_array( $post_type, array( 'pronamic_payment', 'pronamic_pay_subscr' ), true ) ) { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
remove_filter( 'manage_edit-' . $post_type . '_columns', 'MeprAppCtrl::columns' ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Payment redirect URL filter. |
142
|
|
|
* |
143
|
|
|
* @since 1.0.1 |
144
|
|
|
* |
145
|
|
|
* @param string $url Payment redirect URL. |
146
|
|
|
* @param Payment $payment Payment to redirect for. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function redirect_url( $url, Payment $payment ) { |
151
|
|
|
global $transaction; |
152
|
|
|
|
153
|
|
|
$transaction_id = $payment->get_source_id(); |
154
|
|
|
|
155
|
|
|
$transaction = new MeprTransaction( $transaction_id ); |
156
|
|
|
|
157
|
|
|
switch ( $payment->get_status() ) { |
158
|
|
|
case PaymentStatus::CANCELLED: |
159
|
|
|
case PaymentStatus::EXPIRED: |
160
|
|
|
case PaymentStatus::FAILURE: |
161
|
|
|
$product = $transaction->product(); |
162
|
|
|
|
163
|
|
|
$url = add_query_arg( |
164
|
|
|
array( |
165
|
|
|
'action' => 'payment_form', |
166
|
|
|
'txn' => $transaction->trans_num, |
167
|
|
|
'errors' => array( |
168
|
|
|
__( 'Payment failed. Please try again.', 'pronamic_ideal' ), |
169
|
|
|
), |
170
|
|
|
'_wpnonce' => wp_create_nonce( 'mepr_payment_form' ), |
171
|
|
|
), |
172
|
|
|
$product->url() |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
break; |
176
|
|
|
case PaymentStatus::SUCCESS: |
177
|
|
|
// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L768-782 |
178
|
|
|
$mepr_options = MeprOptions::fetch(); |
179
|
|
|
|
180
|
|
|
$product = new MeprProduct( $transaction->product_id ); |
181
|
|
|
$sanitized_title = sanitize_title( $product->post_title ); |
182
|
|
|
|
183
|
|
|
$args = array( |
184
|
|
|
'membership_id' => $product->ID, |
185
|
|
|
'membership' => $sanitized_title, |
186
|
|
|
'trans_num' => $transaction->trans_num, |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$url = $mepr_options->thankyou_page_url( http_build_query( $args ) ); |
190
|
|
|
|
191
|
|
|
break; |
192
|
|
|
case PaymentStatus::OPEN: |
193
|
|
|
default: |
194
|
|
|
break; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $url; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Maybe create create MemberPress transaction for the Pronamic payment. |
202
|
|
|
* |
203
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprSubscription.php |
204
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714 |
205
|
|
|
* @param Payment $payment Payment. |
206
|
|
|
* @return void |
207
|
|
|
* @throws \Exception Throws an exception when the MemberPress subscription cannot be found. |
208
|
|
|
*/ |
209
|
|
|
public function maybe_create_memberpress_transaction( Payment $payment ) { |
210
|
|
|
if ( 'memberpress_subscription' !== $payment->get_source() ) { |
211
|
|
|
return; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$memberpress_subscription_id = $payment->get_source_id(); |
215
|
|
|
|
216
|
|
|
$memberpress_subscription = MemberPress::get_subscription_by_id( $memberpress_subscription_id ); |
217
|
|
|
|
218
|
|
|
if ( null === $memberpress_subscription ) { |
219
|
|
|
throw new \Exception( |
220
|
|
|
\sprintf( |
221
|
|
|
'Could not find MemberPress subscription with ID: %s.', |
222
|
|
|
$memberpress_subscription_id |
223
|
|
|
) |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* If the payment method is changed we have to update the MemberPress |
229
|
|
|
* subscription. |
230
|
|
|
* |
231
|
|
|
* @link https://github.com/wp-pay-extensions/memberpress/commit/3631bcb24f376fb637c1317e15f540cb1f9136f4#diff-6f62438f6bf291e85f644dbdbb14b2a71a9a7ed205b01ce44290ed85abe2aa07L259-L290 |
232
|
|
|
*/ |
233
|
|
|
$memberpress_gateways = MeprOptions::fetch()->payment_methods(); |
234
|
|
|
|
235
|
|
|
foreach ( $memberpress_gateways as $memberpress_gateway ) { |
236
|
|
|
if ( ! $memberpress_gateway instanceof Gateway ) { |
237
|
|
|
continue; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if ( $memberpress_gateway->get_payment_method() === $payment->get_method() ) { |
241
|
|
|
$memberpress_subscription->gateway = $memberpress_gateway->id; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Payment method. |
247
|
|
|
* |
248
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637 |
249
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811 |
250
|
|
|
*/ |
251
|
|
|
$memberpress_gateway = $memberpress_subscription->payment_method(); |
252
|
|
|
|
253
|
|
|
if ( ! $memberpress_gateway instanceof Gateway ) { |
254
|
|
|
return; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* At this point we should call `MeprBaseRealGateway->record_subscription_payment`. |
259
|
|
|
* |
260
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714 |
261
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L205-L255 |
262
|
|
|
*/ |
263
|
|
|
$memberpress_gateway->record_subscription_payment(); |
264
|
|
|
|
265
|
|
|
$memberpress_transaction = new MeprTransaction(); |
266
|
|
|
|
267
|
|
|
$memberpress_transaction->user_id = $memberpress_subscription->user_id; |
268
|
|
|
$memberpress_transaction->product_id = $memberpress_subscription->product_id; |
|
|
|
|
269
|
|
|
$memberpress_transaction->txn_type = MeprTransaction::$subscription_confirmation_str; |
|
|
|
|
270
|
|
|
$memberpress_transaction->status = MeprTransaction::$confirmed_str; |
|
|
|
|
271
|
|
|
$memberpress_transaction->coupon_id = $memberpress_subscription->coupon_id; |
|
|
|
|
272
|
|
|
$memberpress_transaction->trans_num = $payment->get_transaction_id(); |
|
|
|
|
273
|
|
|
$memberpress_transaction->subscription_id = $memberpress_subscription->id; |
|
|
|
|
274
|
|
|
$memberpress_transaction->gateway = $memberpress_gateway->id; |
|
|
|
|
275
|
|
|
|
276
|
|
|
$end_date = $payment->get_end_date(); |
277
|
|
|
|
278
|
|
|
if ( null !== $end_date ) { |
279
|
|
|
$memberpress_transaction->expires_at = MeprUtils::ts_to_mysql_date( $end_date->getTimestamp(), 'Y-m-d 23:59:59' ); |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Gross. |
284
|
|
|
* |
285
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L1013-L1021 |
286
|
|
|
*/ |
287
|
|
|
$memberpress_transaction->set_gross( $payment->get_total_amount()->get_value() ); |
288
|
|
|
|
289
|
|
|
$memberpress_transaction->store(); |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Store the MemberPress subscription in case of gateway changes. |
293
|
|
|
*/ |
294
|
|
|
$memberpress_subscription->store(); |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Update payment source. |
298
|
|
|
* |
299
|
|
|
* @link https://github.com/wp-pay-extensions/restrict-content-pro/blob/3.0.0/src/Extension.php#L770-L776 |
300
|
|
|
*/ |
301
|
|
|
$payment->source = 'memberpress_transaction'; |
302
|
|
|
$payment->source_id = $memberpress_transaction->id; |
303
|
|
|
|
304
|
|
|
$payment->save(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Process transition. |
309
|
|
|
* |
310
|
|
|
* @param MeprTransaction|MeprSubscription $memberpress_item Item. |
311
|
|
|
* @param Gateway $memberpress_gateway Gateway. |
312
|
|
|
* @return void |
313
|
|
|
*/ |
314
|
|
|
private function process_transition( $memberpress_item, Gateway $memberpress_gateway ) { |
315
|
|
|
/** |
316
|
|
|
* Upgrade/downgrade magic. |
317
|
|
|
* |
318
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprPayPalProGateway.php#L350-L354 |
319
|
|
|
*/ |
320
|
|
|
$is_upgrade = $memberpress_item->is_upgrade(); |
321
|
|
|
$is_downgrade = $memberpress_item->is_downgrade(); |
322
|
|
|
|
323
|
|
|
$event_txn = $memberpress_item->maybe_cancel_old_sub(); |
324
|
|
|
|
325
|
|
|
if ( $is_upgrade ) { |
326
|
|
|
/** |
327
|
|
|
* Upgrade subscription. |
328
|
|
|
* |
329
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L602-L611 |
330
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprArtificialGateway.php#L109-L122 |
331
|
|
|
*/ |
332
|
|
|
$memberpress_gateway->upgraded_sub( $memberpress_item, $event_txn ); |
333
|
|
|
} elseif ( $is_downgrade ) { |
334
|
|
|
/** |
335
|
|
|
* Downgraded subscription. |
336
|
|
|
* |
337
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L613-L622 |
338
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprArtificialGateway.php#L109-L122 |
339
|
|
|
*/ |
340
|
|
|
$memberpress_gateway->downgraded_sub( $memberpress_item, $event_txn ); |
341
|
|
|
} else { |
342
|
|
|
/** |
343
|
|
|
* New subscription. |
344
|
|
|
* |
345
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L624-L634 |
346
|
|
|
*/ |
347
|
|
|
$memberpress_gateway->new_sub( $memberpress_item ); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Update lead status of the specified payment. |
353
|
|
|
* |
354
|
|
|
* @param Payment $payment The payment whose status is updated. |
355
|
|
|
* @return void |
356
|
|
|
*/ |
357
|
|
|
public function status_update( Payment $payment ) { |
358
|
|
|
$payment_source_id = $payment->get_source_id(); |
359
|
|
|
|
360
|
|
|
$memberpress_transaction = MemberPress::get_transaction_by_id( $payment_source_id ); |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* If we can't find a MemberPress transaction by the payment source ID |
364
|
|
|
* we can't update the MemberPress transaction, bail out early. |
365
|
|
|
*/ |
366
|
|
|
if ( null === $memberpress_transaction ) { |
367
|
|
|
return; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* We don't update MemberPress transactions that already have the |
372
|
|
|
* status 'failed' or 'complete'. |
373
|
|
|
*/ |
374
|
|
|
if ( MemberPress::transaction_has_status( |
375
|
|
|
$memberpress_transaction, |
376
|
|
|
array( |
377
|
|
|
MeprTransaction::$failed_str, |
378
|
|
|
MeprTransaction::$complete_str, |
379
|
|
|
) |
380
|
|
|
) ) { |
381
|
|
|
return; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Payment method. |
386
|
|
|
* |
387
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637 |
388
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811 |
389
|
|
|
*/ |
390
|
|
|
$memberpress_gateway = $memberpress_transaction->payment_method(); |
391
|
|
|
|
392
|
|
|
if ( ! $memberpress_gateway instanceof Gateway ) { |
393
|
|
|
return; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Ok. |
398
|
|
|
*/ |
399
|
|
|
switch ( $payment->get_status() ) { |
400
|
|
|
case PaymentStatus::FAILURE: |
401
|
|
|
case PaymentStatus::CANCELLED: |
402
|
|
|
case PaymentStatus::EXPIRED: |
403
|
|
|
$memberpress_gateway->record_payment_failure(); |
404
|
|
|
|
405
|
|
|
$memberpress_transaction->txn_type = MeprTransaction::$payment_str; |
|
|
|
|
406
|
|
|
$memberpress_transaction->status = MeprTransaction::$failed_str; |
|
|
|
|
407
|
|
|
|
408
|
|
|
$memberpress_transaction->store(); |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* MemberPress subscription. |
412
|
|
|
* |
413
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620 |
414
|
|
|
*/ |
415
|
|
|
$memberpress_subscription = $memberpress_transaction->subscription(); |
416
|
|
|
|
417
|
|
|
if ( $memberpress_subscription instanceof MeprSubscription ) { |
418
|
|
|
$memberpress_subscription->expire_txns(); |
419
|
|
|
|
420
|
|
|
$memberpress_subscription->store(); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Send failed transaction notices. |
425
|
|
|
* |
426
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1515-L1528 |
427
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L299 |
428
|
|
|
*/ |
429
|
|
|
MeprUtils::send_failed_txn_notices( $memberpress_transaction ); |
430
|
|
|
|
431
|
|
|
break; |
432
|
|
|
case PaymentStatus::SUCCESS: |
433
|
|
|
$memberpress_gateway->record_payment(); |
434
|
|
|
|
435
|
|
|
$memberpress_transaction->trans_num = $payment->get_transaction_id(); |
|
|
|
|
436
|
|
|
$memberpress_transaction->txn_type = MeprTransaction::$payment_str; |
437
|
|
|
$memberpress_transaction->status = MeprTransaction::$complete_str; |
438
|
|
|
|
439
|
|
|
$this->process_transition( $memberpress_transaction, $memberpress_gateway ); |
440
|
|
|
|
441
|
|
|
$memberpress_transaction->store(); |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* MemberPress subscription. |
445
|
|
|
* |
446
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620 |
447
|
|
|
*/ |
448
|
|
|
$memberpress_subscription = $memberpress_transaction->subscription(); |
449
|
|
|
|
450
|
|
|
if ( $memberpress_subscription instanceof MeprSubscription ) { |
451
|
|
|
$memberpress_subscription->status = MeprSubscription::$active_str; |
452
|
|
|
|
453
|
|
|
$this->process_transition( $memberpress_subscription, $memberpress_gateway ); |
454
|
|
|
|
455
|
|
|
$memberpress_subscription->store(); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Send signup notices. |
460
|
|
|
* |
461
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1361-L1390 |
462
|
|
|
*/ |
463
|
|
|
MeprUtils::send_signup_notices( $memberpress_transaction ); |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Send transaction receipt notices. |
467
|
|
|
* |
468
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418 |
469
|
|
|
*/ |
470
|
|
|
MeprUtils::send_transaction_receipt_notices( $memberpress_transaction ); |
471
|
|
|
|
472
|
|
|
break; |
473
|
|
|
case PaymentStatus::OPEN: |
474
|
|
|
default: |
475
|
|
|
break; |
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Subscription deleted. |
481
|
|
|
* |
482
|
|
|
* @param int $subscription_id MemberPress subscription id. |
483
|
|
|
* @return void |
484
|
|
|
*/ |
485
|
|
|
public function subscription_pre_delete( $subscription_id ) { |
486
|
|
|
$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', (string) $subscription_id ); |
487
|
|
|
|
488
|
|
|
if ( ! $subscription ) { |
489
|
|
|
return; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
// Add note. |
493
|
|
|
$note = sprintf( |
494
|
|
|
/* translators: %s: MemberPress */ |
495
|
|
|
__( '%s subscription deleted.', 'pronamic_ideal' ), |
496
|
|
|
__( 'MemberPress', 'pronamic_ideal' ) |
497
|
|
|
); |
498
|
|
|
|
499
|
|
|
$subscription->add_note( $note ); |
500
|
|
|
|
501
|
|
|
// The status of canceled or completed subscriptions will not be changed automatically. |
502
|
|
|
if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) { |
503
|
|
|
$subscription->set_status( SubscriptionStatus::CANCELLED ); |
504
|
|
|
|
505
|
|
|
$subscription->save(); |
506
|
|
|
} |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* Subscription email parameters. |
511
|
|
|
* |
512
|
|
|
* @param array<string, string> $params Email parameters. |
513
|
|
|
* @param MeprSubscription $memberpress_subscription MemberPress subscription. |
514
|
|
|
* @return array<string, string> |
515
|
|
|
*/ |
516
|
|
|
public function subscription_email_params( $params, MeprSubscription $memberpress_subscription ) { |
517
|
|
|
$subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress_subscription', $memberpress_subscription->id ); |
518
|
|
|
|
519
|
|
|
if ( empty( $subscriptions ) ) { |
520
|
|
|
return $params; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
$subscription = reset( $subscriptions ); |
524
|
|
|
|
525
|
|
|
// Add parameters. |
526
|
|
|
$next_payment_date = $subscription->get_next_payment_date(); |
527
|
|
|
|
528
|
|
|
return \array_merge( |
529
|
|
|
$params, |
530
|
|
|
array( |
531
|
|
|
'pronamic_subscription_id' => (string) $subscription->get_id(), |
532
|
|
|
'pronamic_subscription_cancel_url' => $subscription->get_cancel_url(), |
533
|
|
|
'pronamic_subscription_renewal_url' => $subscription->get_renewal_url(), |
534
|
|
|
'pronamic_subscription_renewal_date' => null === $next_payment_date ? '' : \date_i18n( \get_option( 'date_format' ), $next_payment_date->getTimestamp() ), |
|
|
|
|
535
|
|
|
) |
536
|
|
|
); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Transaction email parameters. |
541
|
|
|
* |
542
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/helpers/MeprTransactionsHelper.php#L233 |
543
|
|
|
* @param array<string, string> $params Parameters. |
544
|
|
|
* @param MeprTransaction $transaction MemberPress transaction. |
545
|
|
|
* @return array<string, string> |
546
|
|
|
*/ |
547
|
|
|
public function transaction_email_params( $params, MeprTransaction $transaction ) { |
548
|
|
|
$payments = \get_pronamic_payments_by_source( 'memberpress_transaction', $transaction->id ); |
549
|
|
|
|
550
|
|
|
if ( null === $payments ) { |
|
|
|
|
551
|
|
|
return $params; |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
$payment = \reset( $payments ); |
555
|
|
|
|
556
|
|
|
if ( false === $payment ) { |
557
|
|
|
return $params; |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
// Get subscription. |
561
|
|
|
$periods = $payment->get_periods(); |
562
|
|
|
|
563
|
|
|
if ( null === $periods ) { |
564
|
|
|
return $params; |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
$period = \reset( $periods ); |
568
|
|
|
|
569
|
|
|
if ( false === $period ) { |
570
|
|
|
return $params; |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
$subscription = $period->get_phase()->get_subscription(); |
574
|
|
|
|
575
|
|
|
// Add parameters. |
576
|
|
|
$memberpress_subscription = new MeprSubscription( $subscription->get_source_id() ); |
577
|
|
|
|
578
|
|
|
return $this->subscription_email_params( $params, $memberpress_subscription ); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Source text. |
583
|
|
|
* |
584
|
|
|
* @param string $text Source text. |
585
|
|
|
* @param Payment $payment Payment to create the source text for. |
586
|
|
|
* |
587
|
|
|
* @return string |
588
|
|
|
*/ |
589
|
|
|
public function source_text( $text, Payment $payment ) { |
590
|
|
|
$text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />'; |
591
|
|
|
|
592
|
|
|
$text .= sprintf( |
593
|
|
|
'<a href="%s">%s</a>', |
594
|
|
|
add_query_arg( |
595
|
|
|
array( |
596
|
|
|
'page' => 'memberpress-trans', |
597
|
|
|
'action' => 'edit', |
598
|
|
|
'id' => $payment->source_id, |
599
|
|
|
), |
600
|
|
|
admin_url( 'admin.php' ) |
601
|
|
|
), |
602
|
|
|
/* translators: %s: payment source id */ |
603
|
|
|
sprintf( __( 'Transaction %s', 'pronamic_ideal' ), $payment->source_id ) |
604
|
|
|
); |
605
|
|
|
|
606
|
|
|
return $text; |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* Subscription source text. |
611
|
|
|
* |
612
|
|
|
* @param string $text Source text. |
613
|
|
|
* @param Subscription $subscription Subscription to create the source text for. |
614
|
|
|
* |
615
|
|
|
* @return string |
616
|
|
|
*/ |
617
|
|
|
public function subscription_source_text( $text, Subscription $subscription ) { |
618
|
|
|
$text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />'; |
619
|
|
|
|
620
|
|
|
$text .= sprintf( |
621
|
|
|
'<a href="%s">%s</a>', |
622
|
|
|
add_query_arg( |
623
|
|
|
array( |
624
|
|
|
'page' => 'memberpress-subscriptions', |
625
|
|
|
'subscription' => $subscription->source_id, |
626
|
|
|
), |
627
|
|
|
admin_url( 'admin.php' ) |
628
|
|
|
), |
629
|
|
|
/* translators: %s: payment source id */ |
630
|
|
|
sprintf( __( 'Subscription %s', 'pronamic_ideal' ), $subscription->source_id ) |
631
|
|
|
); |
632
|
|
|
|
633
|
|
|
return $text; |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* Source description. |
638
|
|
|
* |
639
|
|
|
* @param string $description Description. |
640
|
|
|
* @param Payment $payment Payment to create the description for. |
641
|
|
|
* |
642
|
|
|
* @return string |
643
|
|
|
*/ |
644
|
|
|
public function source_description( $description, Payment $payment ) { |
645
|
|
|
return __( 'MemberPress Transaction', 'pronamic_ideal' ); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Subscription source description. |
650
|
|
|
* |
651
|
|
|
* @param string $description Description. |
652
|
|
|
* @param Subscription $subscription Subscription to create the description for. |
653
|
|
|
* |
654
|
|
|
* @return string |
655
|
|
|
*/ |
656
|
|
|
public function subscription_source_description( $description, Subscription $subscription ) { |
657
|
|
|
return __( 'MemberPress Subscription', 'pronamic_ideal' ); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* Source URL. |
662
|
|
|
* |
663
|
|
|
* @param string $url URL. |
664
|
|
|
* @param Payment $payment The payment to create the source URL for. |
665
|
|
|
* |
666
|
|
|
* @return string |
667
|
|
|
*/ |
668
|
|
|
public function source_url( $url, Payment $payment ) { |
669
|
|
|
$url = add_query_arg( |
670
|
|
|
array( |
671
|
|
|
'page' => 'memberpress-trans', |
672
|
|
|
'action' => 'edit', |
673
|
|
|
'id' => $payment->source_id, |
674
|
|
|
), |
675
|
|
|
admin_url( 'admin.php' ) |
676
|
|
|
); |
677
|
|
|
|
678
|
|
|
return $url; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Subscription source URL. |
683
|
|
|
* |
684
|
|
|
* @param string $url URL. |
685
|
|
|
* @param Subscription $subscription Subscription. |
686
|
|
|
* |
687
|
|
|
* @return string |
688
|
|
|
*/ |
689
|
|
|
public function subscription_source_url( $url, Subscription $subscription ) { |
690
|
|
|
$url = add_query_arg( |
691
|
|
|
array( |
692
|
|
|
'page' => 'memberpress-subscriptions', |
693
|
|
|
'subscription' => $subscription->source_id, |
694
|
|
|
), |
695
|
|
|
admin_url( 'admin.php' ) |
696
|
|
|
); |
697
|
|
|
|
698
|
|
|
return $url; |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
/** |
702
|
|
|
* MemberPress update subscription. |
703
|
|
|
* |
704
|
|
|
* @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprSubscriptionsCtrl.php#L92-L111 |
705
|
|
|
* @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L100-L123 |
706
|
|
|
* @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L112 |
707
|
|
|
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprSubscription.php#L122 |
708
|
|
|
* @param string $status_old Old status identifier. |
709
|
|
|
* @param string $status_new New status identifier. |
710
|
|
|
* @param MeprSubscription $memberpress_subscription MemberPress subscription object. |
711
|
|
|
* @return void |
712
|
|
|
*/ |
713
|
|
|
public function memberpress_subscription_transition_status( $status_old, $status_new, $memberpress_subscription ) { |
714
|
|
|
$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $memberpress_subscription->id ); |
715
|
|
|
|
716
|
|
|
if ( empty( $subscription ) ) { |
717
|
|
|
return; |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
$status = SubscriptionStatuses::transform( $status_new ); |
721
|
|
|
|
722
|
|
|
if ( null === $status ) { |
723
|
|
|
return; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
$subscription->set_status( $status ); |
727
|
|
|
|
728
|
|
|
$subscription->save(); |
729
|
|
|
} |
730
|
|
|
} |
731
|
|
|
|