Failed Conditions
Push — develop ( a4fe88...b37712 )
by Remco
15:08
created

src/Pronamic.php (4 issues)

1
<?php
2
/**
3
 * Pronamic
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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 MeprTransaction;
14
use Pronamic\WordPress\Money\TaxedMoney;
15
use Pronamic\WordPress\Pay\Address;
16
use Pronamic\WordPress\Pay\Customer;
17
use Pronamic\WordPress\Pay\ContactName;
18
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
19
use Pronamic\WordPress\Pay\Payments\Payment;
20
use Pronamic\WordPress\Pay\Payments\PaymentLines;
21
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
22
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionInterval;
0 ignored issues
show
The type Pronamic\WordPress\Pay\S...ns\SubscriptionInterval was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionPhase;
0 ignored issues
show
The type Pronamic\WordPress\Pay\S...tions\SubscriptionPhase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
/**
26
 * Pronamic
27
 *
28
 * @author  Remco Tolsma
29
 * @version 2.0.10
30
 * @since   2.0.5
31
 */
32
class Pronamic {
33
	/**
34
	 * Get Pronamic payment from MemberPress transaction.
35
	 *
36
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
37
	 *
38
	 * @return Payment
39
	 */
40
	public static function get_payment( MeprTransaction $memberpress_transaction ) {
41
		$payment = new Payment();
42
43
		// MemberPress.
44
		$memberpress_user         = $memberpress_transaction->user();
45
		$memberpress_product      = $memberpress_transaction->product();
46
		$memberpress_subscription = $memberpress_transaction->subscription();
47
48
		// Title.
49
		$title = sprintf(
50
			/* translators: %s: payment data title */
51
			__( 'Payment for %s', 'pronamic_ideal' ),
52
			sprintf(
53
				/* translators: %s: order id */
54
				__( 'MemberPress transaction %s', 'pronamic_ideal' ),
55
				$memberpress_transaction->id
56
			)
57
		);
58
59
		$payment->order_id    = $memberpress_transaction->id;
60
		$payment->title       = $title;
61
		$payment->description = $memberpress_product->post_title;
62
		$payment->user_id     = $memberpress_user->ID;
63
		$payment->source      = 'memberpress';
64
		$payment->source_id   = $memberpress_transaction->id;
65
		$payment->issuer      = null;
66
67
		// Contact.
68
		$contact_name = new ContactName();
69
		$contact_name->set_first_name( $memberpress_user->first_name );
70
		$contact_name->set_last_name( $memberpress_user->last_name );
71
72
		$customer = new Customer();
73
		$customer->set_name( $contact_name );
74
		$customer->set_email( $memberpress_user->user_email );
75
		$customer->set_user_id( $memberpress_user->ID );
76
77
		$payment->set_customer( $customer );
78
79
		/*
80
		 * Address.
81
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprUser.php#L1191-L1216
82
		 */
83
		$address = new Address();
84
85
		$address->set_name( $contact_name );
86
87
		$address_fields = array(
88
			'one'     => 'set_line_1',
89
			'two'     => 'set_line_2',
90
			'city'    => 'set_city',
91
			'state'   => 'set_region',
92
			'zip'     => 'set_postal_code',
93
			'country' => 'set_country_code',
94
		);
95
96
		foreach ( $address_fields as $field => $function ) {
97
			$value = $memberpress_user->address( $field, false );
98
99
			if ( empty( $value ) ) {
100
				continue;
101
			}
102
103
			call_user_func( array( $address, $function ), $value );
104
		}
105
106
		$payment->set_billing_address( $address );
107
		$payment->set_shipping_address( $address );
108
109
		/*
110
		 * Totals.
111
		 */
112
		$payment->set_total_amount(
113
			new TaxedMoney(
114
				$memberpress_transaction->total,
115
				MemberPress::get_currency(),
116
				$memberpress_transaction->tax_amount,
117
				$memberpress_transaction->tax_rate
118
			)
119
		);
120
121
		/*
122
		 * Vat number.
123
		 * @link https://github.com/wp-premium/memberpress-business/search?utf8=%E2%9C%93&q=mepr_vat_number&type=
124
		 * @todo
125
		 */
126
127
		/*
128
		 * Subscription.
129
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprTransaction.php#L603-L618
130
		 */
131
		$payment->subscription = self::get_subscription( $memberpress_transaction );
132
133
		if ( $payment->subscription ) {
134
			$payment->subscription_source_id = $memberpress_transaction->subscription_id;
135
136
			if ( $memberpress_subscription->in_trial() ) {
137
				$payment->set_total_amount(
138
					new TaxedMoney(
139
						$memberpress_subscription->trial_amount,
140
						MemberPress::get_currency(),
141
						null, // Calculate tax value based on tax percentage.
142
						$memberpress_transaction->tax_rate
143
					)
144
				);
145
			}
146
		}
147
148
		/*
149
		 * Lines.
150
		 */
151
		$payment->lines = new PaymentLines();
152
153
		$line = $payment->lines->new_line();
154
155
		$line->set_id( $memberpress_product->ID );
156
		$line->set_name( $memberpress_product->post_title );
157
		$line->set_quantity( 1 );
158
		$line->set_unit_price( $payment->get_total_amount() );
159
		$line->set_total_amount( $payment->get_total_amount() );
160
		$line->set_product_url( get_permalink( $memberpress_product->ID ) );
0 ignored issues
show
It seems like get_permalink($memberpress_product->ID) can also be of type false; however, parameter $product_url of Pronamic\WordPress\Pay\P...Line::set_product_url() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

160
		$line->set_product_url( /** @scrutinizer ignore-type */ get_permalink( $memberpress_product->ID ) );
Loading history...
161
162
		/*
163
		 * Return.
164
		 */
165
		return $payment;
166
	}
167
168
	/**
169
	 * Get Pronamic subscription from MemberPress transaction.
170
	 *
171
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
172
	 *
173
	 * @return Subscription|null
174
	 */
175
	public static function get_subscription( MeprTransaction $memberpress_transaction ) {
176
		$memberpress_product = $memberpress_transaction->product();
177
178
		if ( $memberpress_product->is_one_time_payment() ) {
179
			return null;
180
		}
181
182
		$memberpress_subscription = $memberpress_transaction->subscription();
183
184
		if ( ! $memberpress_subscription ) {
185
			return null;
186
		}
187
188
		/**
189
		 * Subscription.
190
		 */
191
		$subscription = new Subscription();
192
193
		$start_date = new \DateTimeImmutable();
194
195
		// Trial phase.
196
		if ( $memberpress_subscription->in_trial() ) {
197
			$trial_phase = new SubscriptionPhase(
198
				$subscription,
199
				$start_date,
200
				new SubscriptionInterval( 'P' . $memberpress_subscription->trial_days . 'D' ),
201
				new TaxedMoney( $memberpress_subscription->trial_amount, MemberPress::get_currency() )
202
			);
203
204
			$trial_phase->set_total_periods( 1 );
205
			$trial_phase->set_trial( true );
206
207
			$subscription->add_phase( $trial_phase );
0 ignored issues
show
The method add_phase() does not exist on Pronamic\WordPress\Pay\Subscriptions\Subscription. ( Ignorable by Annotation )

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

207
			$subscription->/** @scrutinizer ignore-call */ 
208
                  add_phase( $trial_phase );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
208
209
			$start_date = $trial_phase->get_end_date();
210
		}
211
212
		// Total periods.
213
		$total_periods = null;
214
215
		$limit_cycles_number = (int) $memberpress_subscription->limit_cycles_num;
216
217
		if ( $memberpress_subscription->limit_cycles && $limit_cycles_number > 0 ) {
218
			$total_periods = $limit_cycles_number;
219
		}
220
221
		// Regular phase.
222
		$regular_phase = new SubscriptionPhase(
223
			$subscription,
224
			$start_date,
225
			new SubscriptionInterval( 'P' . $memberpress_product->period . Core_Util::to_period( $memberpress_product->period_type ) ),
226
			new TaxedMoney( $memberpress_transaction->total, MemberPress::get_currency() )
227
		);
228
229
		$regular_phase->set_total_periods( $total_periods );
230
231
		$subscription->add_phase( $regular_phase );
232
233
		return $subscription;
234
	}
235
}
236