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

162
		$line->set_product_url( /** @scrutinizer ignore-type */ get_permalink( $memberpress_product->ID ) );
Loading history...
163
164
		/*
165
		 * Return.
166
		 */
167
		return $payment;
168
	}
169
170
	/**
171
	 * Get Pronamic subscription from MemberPress transaction.
172
	 *
173
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
174
	 *
175
	 * @return Subscription|null
176
	 */
177
	public static function get_subscription( MeprTransaction $memberpress_transaction ) {
178
		$memberpress_product = $memberpress_transaction->product();
179
180
		if ( $memberpress_product->is_one_time_payment() ) {
181
			return null;
182
		}
183
184
		$memberpress_subscription = $memberpress_transaction->subscription();
185
186
		if ( ! $memberpress_subscription ) {
187
			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 Pronamic\WordPress\Pay\S...tions\Subscription|null.
Loading history...
188
		}
189
190
		// New subscription.
191
		$subscription = new Subscription();
192
193
		// Total periods.
194
		$total_periods = null;
195
196
		$limit_cycles_number = (int) $memberpress_subscription->limit_cycles_num;
197
198
		if ( $memberpress_subscription->limit_cycles && $limit_cycles_number > 0 ) {
199
			$total_periods = $limit_cycles_number;
200
		}
201
202
		// Phase.
203
		$start_date = new \DateTimeImmutable();
204
205
		$regular_phase = ( new SubscriptionPhaseBuilder() )
206
			->with_start_date( $start_date )
207
			->with_amount( new TaxedMoney( $memberpress_transaction->total, MemberPress::get_currency() ) )
208
			->with_interval( $memberpress_product->period, Core_Util::to_period( $memberpress_product->period_type ) )
209
			->with_total_periods( $total_periods )
210
			->create();
211
212
		$subscription->add_phase( $regular_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

212
		$subscription->/** @scrutinizer ignore-call */ 
213
                 add_phase( $regular_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...
213
214
		// Amount.
215
		$subscription->set_total_amount(
216
			new TaxedMoney(
217
				$memberpress_transaction->total,
218
				MemberPress::get_currency()
219
			)
220
		);
221
222
		return $subscription;
223
	}
224
}
225