Failed Conditions
Push — develop ( d3f65e...bf2dba )
by Remco
04:24
created

PaymentData::get_currency_alphabetic_code()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payment data
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 MeprOptions;
14
use MeprTransaction;
15
use MeprUser;
16
use Pronamic\WordPress\Money\Money;
17
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
18
use Pronamic\WordPress\Pay\Payments\PaymentData as Pay_PaymentData;
19
use Pronamic\WordPress\Pay\Payments\Item;
20
use Pronamic\WordPress\Pay\Payments\Items;
21
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
22
23
/**
24
 * WordPress pay MemberPress payment data
25
 *
26
 * @author  Remco Tolsma
27
 * @version 2.0.3
28
 * @since   1.0.0
29
 */
30
class PaymentData extends Pay_PaymentData {
31
	/**
32
	 * MemberPress transaction.
33
	 *
34
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php
35
	 *
36
	 * @var MeprTransaction
37
	 */
38
	private $transaction;
39
40
	/**
41
	 * MemberPress transaction user.
42
	 *
43
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php#L596-L600
44
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php
45
	 *
46
	 * @var MeprUser
47
	 */
48
	private $user;
49
50
	/**
51
	 * MemberPress transaction subscription.
52
	 *
53
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php#L602-L617
54
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php
55
	 *
56
	 * @var MeprSubscription|false
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\E...rPress\MeprSubscription was not found. Did you mean MeprSubscription? If so, make sure to prefix the type with \.
Loading history...
57
	 */
58
	private $subscription;
59
60
	/**
61
	 * Constructs and initialize payment data object.
62
	 *
63
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php
64
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php#L596-L600
65
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php
66
	 *
67
	 * @param MeprTransaction $transaction MemberPress transaction object.
68
	 * @param Gateway         $gateway     MemberPress gateway object.
69
	 */
70
	public function __construct( MeprTransaction $transaction, Gateway $gateway ) {
71
		parent::__construct();
72
73
		$this->transaction = $transaction;
74
		$this->gateway     = $gateway;
0 ignored issues
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...
75
76
		$this->user = $transaction->user();
77
78
		$this->subscription = $transaction->subscription();
79
80
		$this->recurring = $this->subscription && $this->subscription->txn_count > 1;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->subscription && $...cription->txn_count > 1 of type boolean is incompatible with the declared type Pronamic\WordPress\Pay\Payments\TODO of property $recurring.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
	}
82
83
	/**
84
	 * Get source slug.
85
	 *
86
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L56-L61
87
	 *
88
	 * @return string
89
	 */
90
	public function get_source() {
91
		return 'memberpress';
92
	}
93
94
	/**
95
	 * Get source ID.
96
	 *
97
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L63-L70
98
	 *
99
	 * @return string|int
100
	 */
101
	public function get_source_id() {
102
		return $this->transaction->id;
103
	}
104
105
	/**
106
	 * Get order ID.
107
	 *
108
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L88-L93
109
	 *
110
	 * @return string|int
111
	 */
112
	public function get_order_id() {
113
		return $this->transaction->id;
114
	}
115
116
	/**
117
	 * Get description.
118
	 *
119
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L81-L86
120
	 *
121
	 * @return string
122
	 */
123
	public function get_description() {
124
		return $this->transaction->product()->post_title;
125
	}
126
127
	/**
128
	 * Get items.
129
	 *
130
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L95-L100
131
	 *
132
	 * @return Items
133
	 */
134
	public function get_items() {
135
		$items = new Items();
0 ignored issues
show
Deprecated Code introduced by
The class Pronamic\WordPress\Pay\Payments\Items has been deprecated: Use `PaymentLines`. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

135
		$items = /** @scrutinizer ignore-deprecated */ new Items();
Loading history...
136
137
		/*
138
		 * Price.
139
		 *
140
		 * Note: MemberPress uses trial amount for prorated upgrade/downgrade too.
141
		 */
142
		$price = $this->transaction->total;
143
144
		$subscription = $this->transaction->subscription();
145
146
		if ( $subscription && $subscription->in_trial() ) {
147
			$price = $subscription->trial_amount;
148
		}
149
150
		// Item.
151
		$item = new Item();
0 ignored issues
show
Deprecated Code introduced by
The class Pronamic\WordPress\Pay\Payments\Item has been deprecated: Use `PaymentLine`. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

151
		$item = /** @scrutinizer ignore-deprecated */ new Item();
Loading history...
152
		$item->set_number( $this->get_order_id() );
153
		$item->set_description( $this->get_description() );
154
		$item->set_price( $price );
155
		$item->set_quantity( 1 );
156
157
		$items->addItem( $item );
0 ignored issues
show
Deprecated Code introduced by
The function Pronamic\WordPress\Pay\Payments\Items::addItem() has been deprecated: 2.0.8 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

157
		/** @scrutinizer ignore-deprecated */ $items->addItem( $item );

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
158
159
		return $items;
160
	}
161
162
	/**
163
	 * Get currency alphabetic code.
164
	 *
165
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L213-L218
166
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprOptions.php#L162-L163
167
	 *
168
	 * @return string
169
	 */
170
	public function get_currency_alphabetic_code() {
171
		$mepr_options = MeprOptions::fetch();
172
173
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L136-137
174
		return $mepr_options->currency_code;
175
	}
176
177
	/**
178
	 * Get email.
179
	 *
180
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L1103-L1105
181
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1229-L1231
182
	 *
183
	 * @return string
184
	 */
185
	public function get_email() {
186
		return $this->user->user_email;
187
	}
188
189
	/**
190
	 * Get first name.
191
	 *
192
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L316-L319
193
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L376-L378
194
	 *
195
	 * @return string
196
	 */
197
	public function get_first_name() {
198
		return $this->user->first_name;
199
	}
200
201
	/**
202
	 * Get last name.
203
	 *
204
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L316-L319
205
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L376-L378
206
	 *
207
	 * @return string
208
	 */
209
	public function get_last_name() {
210
		return $this->user->last_name;
211
	}
212
213
	/**
214
	 * Get customer name.
215
	 *
216
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L316-L319
217
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L376-L378
218
	 *
219
	 * @return string
220
	 */
221
	public function get_customer_name() {
222
		return $this->user->get_full_name();
223
	}
224
225
	/**
226
	 * Get address.
227
	 *
228
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
229
	 *
230
	 * @return string|null
231
	 */
232
	public function get_address() {
233
		$value = $this->user->address( 'one', false );
234
235
		if ( false === $value ) {
236
			return null;
237
		}
238
239
		return $value;
240
	}
241
242
	/**
243
	 * Get city.
244
	 *
245
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
246
	 *
247
	 * @return string|null
248
	 */
249
	public function get_city() {
250
		$value = $this->user->address( 'city', false );
251
252
		if ( false === $value ) {
253
			return null;
254
		}
255
256
		return $value;
257
	}
258
259
	/**
260
	 * Get ZIP.
261
	 *
262
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
263
	 *
264
	 * @return string|null
265
	 */
266
	public function get_zip() {
267
		$value = $this->user->address( 'zip', false );
268
269
		if ( false === $value ) {
270
			return null;
271
		}
272
273
		return $value;
274
	}
275
276
	/**
277
	 * Get country.
278
	 *
279
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
280
	 *
281
	 * @return string|null
282
	 */
283
	public function get_country() {
284
		$value = $this->user->address( 'country', false );
285
286
		if ( false === $value ) {
287
			return null;
288
		}
289
290
		return $value;
291
	}
292
293
	/**
294
	 * Get normal return URL.
295
	 *
296
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalStandardGateway.php#L1121
297
	 *
298
	 * @return string
299
	 */
300
	public function get_normal_return_url() {
301
		$mepr_options = MeprOptions::fetch();
302
303
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L768-782
304
		// @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprOptions.php#L806-L835
305
		return $mepr_options->thankyou_page_url( 'trans_num=' . $this->transaction->id );
306
	}
307
308
	/**
309
	 * Get cancel URL.
310
	 *
311
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L130-L135
312
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalStandardGateway.php#L1128-L1152
313
	 *
314
	 * @return string
315
	 */
316
	public function get_cancel_url() {
317
		if ( isset( $this->transaction->product_id ) && $this->transaction->product_id > 0 ) {
318
			$product = new MeprProduct( $this->transaction->product_id );
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\E...MemberPress\MeprProduct was not found. Did you mean MeprProduct? If so, make sure to prefix the type with \.
Loading history...
319
320
			$url = $this->gateway->message_page_url( $product, 'cancel' );
321
322
			if ( false !== $url ) {
323
				return $url;
324
			}
325
		}
326
327
		$mepr_options = MeprOptions::fetch();
328
329
		return $mepr_options->account_page_url( 'action=subscriptions' );
330
	}
331
332
	/**
333
	 * Get success URL.
334
	 *
335
	 * @return string
336
	 */
337
	public function get_success_url() {
338
		return $this->get_normal_return_url();
339
	}
340
341
	/**
342
	 * Get error URL.
343
	 *
344
	 * @return string
345
	 */
346
	public function get_error_url() {
347
		$mepr_options = MeprOptions::fetch();
348
349
		return $mepr_options->account_page_url( 'action=subscriptions' );
350
	}
351
352
	/**
353
	 * Get subscription.
354
	 *
355
	 * @since 2.0.0
356
	 *
357
	 * @return Subscription|false
358
	 */
359
	public function get_subscription() {
360
		$product = $this->transaction->product();
361
362
		if ( $product->is_one_time_payment() ) {
363
			return false;
364
		}
365
366
		$mp_subscription = $this->transaction->subscription();
367
368
		if ( ! $mp_subscription ) {
369
			return false;
370
		}
371
372
		$frequency = '';
373
374
		if ( $mp_subscription->limit_cycles ) {
375
			$frequency = $mp_subscription->limit_cycles;
376
		}
377
378
		$subscription                  = new Subscription();
379
		$subscription->frequency       = $frequency;
380
		$subscription->interval        = $product->period;
381
		$subscription->interval_period = Core_Util::to_period( $product->period_type );
382
		$subscription->description     = sprintf(
383
			'Order #%s - %s',
384
			$this->get_source_id(),
385
			$this->get_description()
386
		);
387
388
		$subscription->set_amount(
389
			new Money(
390
				$this->transaction->total,
391
				$this->get_currency_alphabetic_code()
392
			)
393
		);
394
395
		return $subscription;
396
	}
397
398
	/**
399
	 * Get subscription source ID.
400
	 *
401
	 * @since  2.0.0
402
	 * @return string
403
	 */
404
	public function get_subscription_source_id() {
405
		$subscription = $this->get_subscription();
406
407
		if ( ! $subscription ) {
408
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
409
		}
410
411
		if ( ! empty( $this->transaction->subscription_id ) ) {
412
			return $this->transaction->subscription_id;
413
		}
414
415
		return $this->get_source_id();
416
	}
417
}
418