Failed Conditions
Push — develop ( 43fbc6...d4b8c1 )
by Reüel
04:52
created

src/Pronamic.php (3 issues)

1
<?php
2
/**
3
 * Pronamic
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 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\Subscription;
25
26
/**
27
 * Pronamic
28
 *
29
 * @author  Remco Tolsma
30
 * @version 2.0.1
31
 * @since   1.0.0
32
 */
33
class Pronamic {
34
	/**
35
	 * Get Pronamic payment from MemberPress transaction.
36
	 *
37
	 * @param MeprTransaction $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
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprUser.php#L1191-L1216
83
		 */
84
		$address = new Address();
85
86
		$address->set_name( $contact_name );
87
88
		$address_fields = array(
89
			'one'     => 'set_line_1',
90
			'two'     => 'set_line_2',
91
			'city'    => 'set_city',
92
			'state'   => 'set_region',
93
			'zip'     => 'set_postal_code',
94
			'country' => 'set_country_code',
95
		);
96
97
		foreach ( $address_fields as $field => $function ) {
98
			$value = $memberpress_user->address( $field, false );
99
100
			if ( false !== $value ) {
101
				call_user_func( array( $address, $function ), $value );
102
			}
103
		}
104
105
		$payment->set_billing_address( $address );
106
		$payment->set_shipping_address( $address );
107
108
		/*
109
		 * Totals.
110
		 */
111
		$payment->set_total_amount(
112
			new TaxedMoney(
113
				$memberpress_transaction->total,
114
				MemberPress::get_currency(),
115
				$memberpress_transaction->tax_amount,
116
				$memberpress_transaction->tax_rate
117
			)
118
		);
119
120
		/*
121
		 * Vat number.
122
		 * @link https://github.com/wp-premium/memberpress-business/search?utf8=%E2%9C%93&q=mepr_vat_number&type=
123
		 * @todo
124
		 */
125
126
		/*
127
		 * Lines.
128
		 */
129
		$payment->lines = new PaymentLines();
130
131
		$line = $payment->lines->new_line();
132
133
		$line->set_id( $memberpress_product->ID );
134
		$line->set_name( $memberpress_product->post_title );
135
		$line->set_quantity( 1 );
136
		$line->set_unit_price( $payment->get_total_amount() );
137
		$line->set_total_amount( $payment->get_total_amount() );
138
		$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

138
		$line->set_product_url( /** @scrutinizer ignore-type */ get_permalink( $memberpress_product->ID ) );
Loading history...
139
140
		/*
141
		 * Subscription.
142
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprTransaction.php#L603-L618
143
		 */
144
		$payment->subscription = self::get_subscription( $memberpress_transaction );
145
146
		if ( $payment->subscription ) {
147
			$payment->subscription_source_id = $memberpress_transaction->subscription_id;
148
		}
149
150
		/*
151
		 * Return.
152
		 */
153
		return $payment;
154
	}
155
156
	/**
157
	 * Get Pronamic subscription from MemberPress transaction.
158
	 *
159
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
160
	 *
161
	 * @return Subscription|null
162
	 */
163
	public static function get_subscription( MeprTransaction $memberpress_transaction ) {
164
		$memberpress_product = $memberpress_transaction->product();
165
166
		if ( $memberpress_product->is_one_time_payment() ) {
167
			return null;
168
		}
169
170
		$memberpress_subscription = $memberpress_transaction->subscription();
171
172
		if ( ! $memberpress_subscription ) {
173
			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...
174
		}
175
176
		$frequency = '';
177
178
		if ( $memberpress_subscription->limit_cycles ) {
179
			$frequency = $memberpress_subscription->limit_cycles;
180
		}
181
182
		$subscription                  = new Subscription();
183
		$subscription->frequency       = $frequency;
184
		$subscription->interval        = $memberpress_product->period;
185
		$subscription->interval_period = Core_Util::to_period( $memberpress_product->period_type );
186
187
		$subscription->set_total_amount(
0 ignored issues
show
The method set_total_amount() 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

187
		$subscription->/** @scrutinizer ignore-call */ 
188
                 set_total_amount(

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...
188
			new Money(
189
				$memberpress_transaction->total,
190
				MemberPress::get_currency()
191
			)
192
		);
193
194
		return $subscription;
195
	}
196
}
197