Passed
Push — develop ( e354ef...b1637b )
by Remco
04:46
created

PaymentData::get_items()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payment data
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 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.0
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like $transaction->subscription() can also be of type MeprSubscription. However, the property $subscription is declared as type Pronamic\WordPress\Pay\E...\MeprSubscription|false. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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();
136
137
		$item = new Item();
138
		$item->setNumber( $this->get_order_id() );
139
		$item->setDescription( $this->get_description() );
140
		$item->setPrice( $this->transaction->total );
141
		$item->setQuantity( 1 );
142
143
		$items->addItem( $item );
144
145
		return $items;
146
	}
147
148
	/**
149
	 * Get currency alphabetic code.
150
	 *
151
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L213-L218
152
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprOptions.php#L162-L163
153
	 *
154
	 * @return string
155
	 */
156
	public function get_currency_alphabetic_code() {
157
		$mepr_options = MeprOptions::fetch();
158
159
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L136-137
160
		return $mepr_options->currency_code;
161
	}
162
163
	/**
164
	 * Get email.
165
	 *
166
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L1103-L1105
167
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1229-L1231
168
	 *
169
	 * @return string
170
	 */
171
	public function get_email() {
172
		return $this->user->user_email;
173
	}
174
175
	/**
176
	 * Get first name.
177
	 *
178
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L316-L319
179
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L376-L378
180
	 *
181
	 * @return string
182
	 */
183
	public function get_first_name() {
184
		return $this->user->first_name;
185
	}
186
187
	/**
188
	 * Get last name.
189
	 *
190
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L316-L319
191
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L376-L378
192
	 *
193
	 * @return string
194
	 */
195
	public function get_last_name() {
196
		return $this->user->last_name;
197
	}
198
199
	/**
200
	 * Get customer name.
201
	 *
202
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L316-L319
203
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L376-L378
204
	 *
205
	 * @return string
206
	 */
207
	public function get_customer_name() {
208
		return $this->user->get_full_name();
209
	}
210
211
	/**
212
	 * Get address.
213
	 *
214
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
215
	 *
216
	 * @return string|null
217
	 */
218
	public function get_address() {
219
		$value = $this->user->address( 'one', false );
220
221
		if ( false === $value ) {
222
			return null;
223
		}
224
225
		return $value;
226
	}
227
228
	/**
229
	 * Get city.
230
	 *
231
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
232
	 *
233
	 * @return string|null
234
	 */
235
	public function get_city() {
236
		$value = $this->user->address( 'city', false );
237
238
		if ( false === $value ) {
239
			return null;
240
		}
241
242
		return $value;
243
	}
244
245
	/**
246
	 * Get ZIP.
247
	 *
248
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
249
	 *
250
	 * @return string|null
251
	 */
252
	public function get_zip() {
253
		$value = $this->user->address( 'zip', false );
254
255
		if ( false === $value ) {
256
			return null;
257
		}
258
259
		return $value;
260
	}
261
262
	/**
263
	 * Get country.
264
	 *
265
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
266
	 *
267
	 * @return string|null
268
	 */
269
	public function get_country() {
270
		$value = $this->user->address( 'country', false );
271
272
		if ( false === $value ) {
273
			return null;
274
		}
275
276
		return $value;
277
	}
278
279
	/**
280
	 * Get normal return URL.
281
	 *
282
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalStandardGateway.php#L1121
283
	 *
284
	 * @return string
285
	 */
286
	public function get_normal_return_url() {
287
		$mepr_options = MeprOptions::fetch();
288
289
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L768-782
290
		// @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprOptions.php#L806-L835
291
		return $mepr_options->thankyou_page_url( 'trans_num=' . $this->transaction->id );
292
	}
293
294
	/**
295
	 * Get cancel URL.
296
	 *
297
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L130-L135
298
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalStandardGateway.php#L1128-L1152
299
	 *
300
	 * @return string
301
	 */
302
	public function get_cancel_url() {
303
		if ( isset( $this->transaction->product_id ) && $this->transaction->product_id > 0 ) {
304
			$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...
305
306
			$url = $this->gateway->message_page_url( $product, 'cancel' );
307
308
			if ( false !== $url ) {
309
				return $url;
310
			}
311
		}
312
313
		$mepr_options = MeprOptions::fetch();
314
315
		return $mepr_options->account_page_url( 'action=subscriptions' );
316
	}
317
318
	/**
319
	 * Get success URL.
320
	 *
321
	 * @return string
322
	 */
323
	public function get_success_url() {
324
		return $this->get_normal_return_url();
325
	}
326
327
	/**
328
	 * Get error URL.
329
	 *
330
	 * @return string
331
	 */
332
	public function get_error_url() {
333
		$mepr_options = MeprOptions::fetch();
334
335
		return $mepr_options->account_page_url( 'action=subscriptions' );
336
	}
337
338
	/**
339
	 * Get subscription.
340
	 *
341
	 * @since 2.0.0
342
	 *
343
	 * @return Subscription|false
344
	 */
345
	public function get_subscription() {
346
		$product = $this->transaction->product();
347
348
		if ( $product->is_one_time_payment() ) {
349
			return false;
350
		}
351
352
		$mp_subscription = $this->transaction->subscription();
353
354
		if ( ! $mp_subscription ) {
355
			return false;
356
		}
357
358
		$frequency = '';
359
360
		if ( $mp_subscription->limit_cycles ) {
361
			$frequency = $mp_subscription->limit_cycles;
362
		}
363
364
		$subscription                  = new Subscription();
365
		$subscription->frequency       = $frequency;
366
		$subscription->interval        = $product->period;
367
		$subscription->interval_period = Core_Util::to_period( $product->period_type );
0 ignored issues
show
Documentation Bug introduced by
The property $interval_period was declared of type integer, but Pronamic\WordPress\Pay\C...($product->period_type) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
368
		$subscription->description     = sprintf(
369
			'Order #%s - %s',
370
			$this->get_source_id(),
371
			$this->get_description()
372
		);
373
374
		$subscription->set_amount( new Money(
375
			$this->transaction->total,
376
			$this->get_currency_alphabetic_code()
377
		) );
378
379
		return $subscription;
380
	}
381
382
	/**
383
	 * Get subscription source ID.
384
	 *
385
	 * @since  2.0.0
386
	 * @return string
387
	 */
388
	public function get_subscription_source_id() {
389
		$subscription = $this->get_subscription();
390
391
		if ( ! $subscription ) {
392
			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...
393
		}
394
395
		return $this->get_source_id();
396
	}
397
}
398