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\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; |
23
|
|
|
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionPhase; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Pronamic |
27
|
|
|
* |
28
|
|
|
* @author Remco Tolsma |
29
|
|
|
* @version 2.2.3 |
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
|
|
|
$payment->add_period( $payment->subscription->new_period() ); |
|
|
|
|
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 ) ); |
|
|
|
|
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 null; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Subscription. |
192
|
|
|
*/ |
193
|
|
|
$subscription = new Subscription(); |
194
|
|
|
|
195
|
|
|
$start_date = new \DateTimeImmutable(); |
196
|
|
|
|
197
|
|
|
// Trial phase. |
198
|
|
|
if ( $memberpress_subscription->in_trial() ) { |
199
|
|
|
$trial_phase = new SubscriptionPhase( |
200
|
|
|
$subscription, |
201
|
|
|
$start_date, |
202
|
|
|
new SubscriptionInterval( 'P' . $memberpress_subscription->trial_days . 'D' ), |
203
|
|
|
new TaxedMoney( $memberpress_subscription->trial_amount, MemberPress::get_currency() ) |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
$trial_phase->set_total_periods( 1 ); |
207
|
|
|
$trial_phase->set_trial( true ); |
208
|
|
|
|
209
|
|
|
$subscription->add_phase( $trial_phase ); |
210
|
|
|
|
211
|
|
|
$start_date = $trial_phase->get_end_date(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// Total periods. |
215
|
|
|
$total_periods = null; |
216
|
|
|
|
217
|
|
|
$limit_cycles_number = (int) $memberpress_subscription->limit_cycles_num; |
218
|
|
|
|
219
|
|
|
if ( $memberpress_subscription->limit_cycles && $limit_cycles_number > 0 ) { |
220
|
|
|
$total_periods = $limit_cycles_number; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// Regular phase. |
224
|
|
|
$regular_phase = new SubscriptionPhase( |
225
|
|
|
$subscription, |
226
|
|
|
$start_date, |
|
|
|
|
227
|
|
|
new SubscriptionInterval( 'P' . $memberpress_product->period . Core_Util::to_period( $memberpress_product->period_type ) ), |
228
|
|
|
new TaxedMoney( $memberpress_transaction->total, MemberPress::get_currency() ) |
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
$regular_phase->set_total_periods( $total_periods ); |
232
|
|
|
|
233
|
|
|
$subscription->add_phase( $regular_phase ); |
234
|
|
|
|
235
|
|
|
return $subscription; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|