Test Failed
Push — develop ( 1d67a3...2db36f )
by Reüel
23:09 queued 17:17
created

src/Pronamic.php (1 issue)

Labels
Severity
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;
0 ignored issues
show
The type MeprTransaction 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...
14
use Pronamic\WordPress\Money\Money;
15
use Pronamic\WordPress\Money\TaxedMoney;
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 3.1.0
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
	 * @return Payment
39
	 * @throws \Exception Throws an exception as soon as no new subscription period can be created.
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->user_id     = $memberpress_user->ID;
63
		$payment->source      = 'memberpress_transaction';
64
		$payment->source_id   = $memberpress_transaction->id;
65
66
		$payment->set_description( $memberpress_product->post_title );
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(
86
			array(
87
				'line_1'       => $memberpress_user->address( 'one', false ),
88
				'line_2'       => $memberpress_user->address( 'two', false ),
89
				'postal_code'  => $memberpress_user->address( 'zip', false ),
90
				'city'         => $memberpress_user->address( 'city', false ),
91
				'region'       => $memberpress_user->address( 'state', false ),
92
				'country_code' => $memberpress_user->address( 'country', false ),
93
			)
94
		);
95
96
		if ( null !== $address ) {
97
			$address->set_name( $contact_name );
98
		}
99
100
		$payment->set_billing_address( $address );
101
		$payment->set_shipping_address( $address );
102
103
		/**
104
		 * Total.
105
		 */
106
		$payment->set_total_amount(
107
			new TaxedMoney(
108
				$memberpress_transaction->total,
109
				MemberPress::get_currency(),
110
				$memberpress_transaction->tax_amount,
111
				$memberpress_transaction->tax_rate
112
			)
113
		);
114
115
		/**
116
		 * Vat number.
117
		 *
118
		 * @link https://github.com/wp-premium/memberpress-business/search?utf8=%E2%9C%93&q=mepr_vat_number&type=
119
		 * @todo
120
		 */
121
122
		/**
123
		 * Subscription.
124
		 */
125
		$payment->subscription = self::get_subscription( $memberpress_transaction );
126
127
		if ( $payment->subscription ) {
128
			/**
129
			 * MemberPress transaction subscription ID.
130
			 * 
131
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L27
132
			 */
133
			$payment->subscription_source_id = $memberpress_transaction->subscription_id;
134
135
			$period = $payment->subscription->new_period();
136
137
			if ( null === $period ) {
138
				throw new \Exception( 'Could not create new period for subscription.' );
139
			}
140
141
			$payment->add_period( $period );
142
		}
143
144
		/*
145
		 * Lines.
146
		 */
147
		$payment->lines = new PaymentLines();
148
149
		$line = $payment->lines->new_line();
150
151
		$line->set_id( $memberpress_product->ID );
152
		$line->set_name( $memberpress_product->post_title );
153
		$line->set_quantity( 1 );
154
		$line->set_unit_price( $payment->get_total_amount() );
155
		$line->set_total_amount( $payment->get_total_amount() );
156
157
		$product_url = \get_permalink( $memberpress_product->ID );
158
159
		if ( false !== $product_url ) {     
160
			$line->set_product_url( $product_url );
161
		}
162
163
		/*
164
		 * Return.
165
		 */
166
		return $payment;
167
	}
168
169
	/**
170
	 * Get Pronamic subscription from MemberPress transaction.
171
	 *
172
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
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(
202
					$memberpress_subscription->trial_total,
203
					MemberPress::get_currency(),
204
					$memberpress_subscription->trial_tax_amount,
205
					$memberpress_subscription->tax_rate
206
				)
207
			);
208
209
			$trial_phase->set_total_periods( 1 );
210
			$trial_phase->set_trial( true );
211
212
			$subscription->add_phase( $trial_phase );
213
214
			$trail_end_date = $trial_phase->get_end_date();
215
216
			if ( null !== $trail_end_date ) {
217
				$start_date = $trail_end_date;
218
			}
219
		}
220
221
		// Total periods.
222
		$total_periods = null;
223
224
		$limit_cycles_number = (int) $memberpress_subscription->limit_cycles_num;
225
226
		if ( $memberpress_subscription->limit_cycles && $limit_cycles_number > 0 ) {
227
			$total_periods = $limit_cycles_number;
228
		}
229
230
		// Regular phase.
231
		$regular_phase = new SubscriptionPhase(
232
			$subscription,
233
			$start_date,
234
			new SubscriptionInterval( 'P' . $memberpress_product->period . Core_Util::to_period( $memberpress_product->period_type ) ),
235
			new Money( $memberpress_subscription->total, MemberPress::get_currency() )
236
		);
237
238
		$regular_phase->set_total_periods( $total_periods );
239
240
		$subscription->add_phase( $regular_phase );
241
242
		// Source.
243
		$subscription->source    = 'memberpress_subscription';
244
		$subscription->source_id = $memberpress_subscription->id;
245
246
		return $subscription;
247
	}
248
}
249