Failed Conditions
Push — develop ( 12386e...3e9729 )
by Remco
06:20
created

src/Pronamic.php (3 issues)

1
<?php
2
/**
3
 * Pronamic
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 MeprTransaction;
14
use Pronamic\WordPress\Money\TaxedMoney;
15
use Pronamic\WordPress\Pay\Address;
16
use Pronamic\WordPress\Pay\AddressHelper;
17
use Pronamic\WordPress\Pay\Customer;
18
use Pronamic\WordPress\Pay\ContactName;
19
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
20
use Pronamic\WordPress\Pay\Payments\Payment;
21
use Pronamic\WordPress\Pay\Payments\PaymentLines;
22
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
23
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionInterval;
24
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionPhase;
25
26
/**
27
 * Pronamic
28
 *
29
 * @author  Remco Tolsma
30
 * @version 2.2.3
31
 * @since   2.0.5
32
 */
33
class Pronamic {
34
	/**
35
	 * Get Pronamic payment from MemberPress transaction.
36
	 *
37
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
38
	 *
39
	 * @return Payment
40
	 */
41
	public static function get_payment( MeprTransaction $memberpress_transaction ) {
42
		$payment = new Payment();
43
44
		// MemberPress.
45
		$memberpress_user         = $memberpress_transaction->user();
46
		$memberpress_product      = $memberpress_transaction->product();
47
		$memberpress_subscription = $memberpress_transaction->subscription();
48
49
		// Title.
50
		$title = sprintf(
51
			/* translators: %s: payment data title */
52
			__( 'Payment for %s', 'pronamic_ideal' ),
53
			sprintf(
54
				/* translators: %s: order id */
55
				__( 'MemberPress transaction %s', 'pronamic_ideal' ),
56
				$memberpress_transaction->id
57
			)
58
		);
59
60
		$payment->order_id    = $memberpress_transaction->id;
61
		$payment->title       = $title;
62
		$payment->description = $memberpress_product->post_title;
63
		$payment->user_id     = $memberpress_user->ID;
64
		$payment->source      = 'memberpress';
65
		$payment->source_id   = $memberpress_transaction->id;
66
		$payment->issuer      = null;
67
68
		// Contact.
69
		$contact_name = new ContactName();
70
		$contact_name->set_first_name( $memberpress_user->first_name );
71
		$contact_name->set_last_name( $memberpress_user->last_name );
72
73
		$customer = new Customer();
74
		$customer->set_name( $contact_name );
75
		$customer->set_email( $memberpress_user->user_email );
76
		$customer->set_user_id( $memberpress_user->ID );
77
78
		$payment->set_customer( $customer );
79
80
		/**
81
		 * Address.
82
		 *
83
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprUser.php#L1191-L1216
84
		 */
85
		$address = AddressHelper::from_array( array(
86
			'line_1'       => $memberpress_user->address( 'one', false ),
87
			'line_2'       => $memberpress_user->address( 'two', false ),
88
			'postal_code'  => $memberpress_user->address( 'zip', false ),
89
			'city'         => $memberpress_user->address( 'city', false ),
90
			'region'       => $memberpress_user->address( 'state', false ),
91
			'country_code' => $memberpress_user->address( 'country', false ),
92
		) );
93
94
		if ( null !== $address ) {
95
			$address->set_name( $contact_name );
96
		}
97
98
		$payment->set_billing_address( $address );
99
		$payment->set_shipping_address( $address );
100
101
		/*
102
		 * Totals.
103
		 */
104
		$payment->set_total_amount(
105
			new TaxedMoney(
106
				$memberpress_transaction->total,
107
				MemberPress::get_currency(),
108
				$memberpress_transaction->tax_amount,
109
				$memberpress_transaction->tax_rate
110
			)
111
		);
112
113
		/*
114
		 * Vat number.
115
		 * @link https://github.com/wp-premium/memberpress-business/search?utf8=%E2%9C%93&q=mepr_vat_number&type=
116
		 * @todo
117
		 */
118
119
		/*
120
		 * Subscription.
121
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprTransaction.php#L603-L618
122
		 */
123
		$payment->subscription = self::get_subscription( $memberpress_transaction );
124
125
		if ( $payment->subscription ) {
126
			$payment->subscription_source_id = $memberpress_transaction->subscription_id;
127
128
			$payment->add_period( $payment->subscription->new_period() );
0 ignored issues
show
It seems like $payment->subscription->new_period() can also be of type null; however, parameter $period of Pronamic\WordPress\Pay\P...s\Payment::add_period() does only seem to accept Pronamic\WordPress\Pay\S...ions\SubscriptionPeriod, 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

128
			$payment->add_period( /** @scrutinizer ignore-type */ $payment->subscription->new_period() );
Loading history...
129
130
			if ( $memberpress_subscription->in_trial() ) {
131
				$payment->set_total_amount(
132
					new TaxedMoney(
133
						$memberpress_subscription->trial_total,
134
						MemberPress::get_currency(),
135
						$memberpress_subscription->trial_tax_amount,
136
						$memberpress_subscription->tax_rate
137
					)
138
				);
139
			}
140
		}
141
142
		/*
143
		 * Lines.
144
		 */
145
		$payment->lines = new PaymentLines();
146
147
		$line = $payment->lines->new_line();
148
149
		$line->set_id( $memberpress_product->ID );
150
		$line->set_name( $memberpress_product->post_title );
151
		$line->set_quantity( 1 );
152
		$line->set_unit_price( $payment->get_total_amount() );
153
		$line->set_total_amount( $payment->get_total_amount() );
154
		$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

154
		$line->set_product_url( /** @scrutinizer ignore-type */ get_permalink( $memberpress_product->ID ) );
Loading history...
155
156
		/*
157
		 * Return.
158
		 */
159
		return $payment;
160
	}
161
162
	/**
163
	 * Get Pronamic subscription from MemberPress transaction.
164
	 *
165
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
166
	 *
167
	 * @return Subscription|null
168
	 */
169
	public static function get_subscription( MeprTransaction $memberpress_transaction ) {
170
		$memberpress_product = $memberpress_transaction->product();
171
172
		if ( $memberpress_product->is_one_time_payment() ) {
173
			return null;
174
		}
175
176
		$memberpress_subscription = $memberpress_transaction->subscription();
177
178
		if ( ! $memberpress_subscription ) {
179
			return null;
180
		}
181
182
		/**
183
		 * Subscription.
184
		 */
185
		$subscription = new Subscription();
186
187
		$start_date = new \DateTimeImmutable();
188
189
		// Trial phase.
190
		if ( $memberpress_subscription->in_trial() ) {
191
			$trial_phase = new SubscriptionPhase(
192
				$subscription,
193
				$start_date,
194
				new SubscriptionInterval( 'P' . $memberpress_subscription->trial_days . 'D' ),
195
				new TaxedMoney( $memberpress_subscription->trial_amount, MemberPress::get_currency() )
196
			);
197
198
			$trial_phase->set_total_periods( 1 );
199
			$trial_phase->set_trial( true );
200
201
			$subscription->add_phase( $trial_phase );
202
203
			$start_date = $trial_phase->get_end_date();
204
		}
205
206
		// Total periods.
207
		$total_periods = null;
208
209
		$limit_cycles_number = (int) $memberpress_subscription->limit_cycles_num;
210
211
		if ( $memberpress_subscription->limit_cycles && $limit_cycles_number > 0 ) {
212
			$total_periods = $limit_cycles_number;
213
		}
214
215
		// Regular phase.
216
		$regular_phase = new SubscriptionPhase(
217
			$subscription,
218
			$start_date,
0 ignored issues
show
It seems like $start_date can also be of type null; however, parameter $start_date of Pronamic\WordPress\Pay\S...ionPhase::__construct() does only seem to accept DateTimeImmutable, 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

218
			/** @scrutinizer ignore-type */ $start_date,
Loading history...
219
			new SubscriptionInterval( 'P' . $memberpress_product->period . Core_Util::to_period( $memberpress_product->period_type ) ),
220
			new TaxedMoney( $memberpress_transaction->total, MemberPress::get_currency() )
221
		);
222
223
		$regular_phase->set_total_periods( $total_periods );
224
225
		$subscription->add_phase( $regular_phase );
226
227
		return $subscription;
228
	}
229
}
230