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 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 payment method icon URL. |
35
|
|
|
* |
36
|
|
|
* @param string $method Payment method. |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public static function get_method_icon_url( $method ) { |
40
|
|
|
$method = \str_replace( '_', '-', $method ); |
41
|
|
|
|
42
|
|
|
return sprintf( 'https://cdn.wp-pay.org/jsdelivr.net/npm/@wp-pay/[email protected]/dist/methods/%1$s/method-%1$s-640x360.svg', $method ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get Pronamic payment from MemberPress transaction. |
47
|
|
|
* |
48
|
|
|
* @param MeprTransaction $memberpress_transaction MemberPress transaction object. |
49
|
|
|
* |
50
|
|
|
* @return Payment |
51
|
|
|
*/ |
52
|
|
|
public static function get_payment( MeprTransaction $memberpress_transaction ) { |
53
|
|
|
$payment = new Payment(); |
54
|
|
|
|
55
|
|
|
// MemberPress. |
56
|
|
|
$memberpress_user = $memberpress_transaction->user(); |
57
|
|
|
$memberpress_product = $memberpress_transaction->product(); |
58
|
|
|
$memberpress_subscription = $memberpress_transaction->subscription(); |
59
|
|
|
|
60
|
|
|
// Title. |
61
|
|
|
$title = sprintf( |
62
|
|
|
/* translators: %s: payment data title */ |
63
|
|
|
__( 'Payment for %s', 'pronamic_ideal' ), |
64
|
|
|
sprintf( |
65
|
|
|
/* translators: %s: order id */ |
66
|
|
|
__( 'MemberPress transaction %s', 'pronamic_ideal' ), |
67
|
|
|
$memberpress_transaction->id |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$payment->order_id = $memberpress_transaction->id; |
72
|
|
|
$payment->title = $title; |
73
|
|
|
$payment->description = $memberpress_product->post_title; |
74
|
|
|
$payment->user_id = $memberpress_user->ID; |
75
|
|
|
$payment->source = 'memberpress'; |
76
|
|
|
$payment->source_id = $memberpress_transaction->id; |
77
|
|
|
$payment->issuer = null; |
78
|
|
|
|
79
|
|
|
// Contact. |
80
|
|
|
$contact_name = new ContactName(); |
81
|
|
|
$contact_name->set_first_name( $memberpress_user->first_name ); |
82
|
|
|
$contact_name->set_last_name( $memberpress_user->last_name ); |
83
|
|
|
|
84
|
|
|
$customer = new Customer(); |
85
|
|
|
$customer->set_name( $contact_name ); |
86
|
|
|
$customer->set_email( $memberpress_user->user_email ); |
87
|
|
|
$customer->set_user_id( $memberpress_user->ID ); |
88
|
|
|
|
89
|
|
|
$payment->set_customer( $customer ); |
90
|
|
|
|
91
|
|
|
/* |
92
|
|
|
* Address. |
93
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprUser.php#L1191-L1216 |
94
|
|
|
*/ |
95
|
|
|
$address = new Address(); |
96
|
|
|
|
97
|
|
|
$address->set_name( $contact_name ); |
98
|
|
|
|
99
|
|
|
$address_fields = array( |
100
|
|
|
'one' => 'set_line_1', |
101
|
|
|
'two' => 'set_line_2', |
102
|
|
|
'city' => 'set_city', |
103
|
|
|
'state' => 'set_region', |
104
|
|
|
'zip' => 'set_postal_code', |
105
|
|
|
'country' => 'set_country_code', |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
foreach ( $address_fields as $field => $function ) { |
109
|
|
|
$value = $memberpress_user->address( $field, false ); |
110
|
|
|
|
111
|
|
|
if ( empty( $value ) ) { |
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
call_user_func( array( $address, $function ), $value ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$payment->set_billing_address( $address ); |
119
|
|
|
$payment->set_shipping_address( $address ); |
120
|
|
|
|
121
|
|
|
/* |
122
|
|
|
* Totals. |
123
|
|
|
*/ |
124
|
|
|
$payment->set_total_amount( |
125
|
|
|
new TaxedMoney( |
126
|
|
|
$memberpress_transaction->total, |
127
|
|
|
MemberPress::get_currency(), |
128
|
|
|
$memberpress_transaction->tax_amount, |
129
|
|
|
$memberpress_transaction->tax_rate |
130
|
|
|
) |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
/* |
134
|
|
|
* Vat number. |
135
|
|
|
* @link https://github.com/wp-premium/memberpress-business/search?utf8=%E2%9C%93&q=mepr_vat_number&type= |
136
|
|
|
* @todo |
137
|
|
|
*/ |
138
|
|
|
|
139
|
|
|
/* |
140
|
|
|
* Subscription. |
141
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprTransaction.php#L603-L618 |
142
|
|
|
*/ |
143
|
|
|
$payment->subscription = self::get_subscription( $memberpress_transaction ); |
144
|
|
|
|
145
|
|
|
if ( $payment->subscription ) { |
146
|
|
|
$payment->subscription_source_id = $memberpress_transaction->subscription_id; |
147
|
|
|
|
148
|
|
|
$payment->add_period( $payment->subscription->new_period() ); |
|
|
|
|
149
|
|
|
|
150
|
|
|
if ( $memberpress_subscription->in_trial() ) { |
151
|
|
|
$payment->set_total_amount( |
152
|
|
|
new TaxedMoney( |
153
|
|
|
$memberpress_subscription->trial_amount, |
154
|
|
|
MemberPress::get_currency(), |
155
|
|
|
null, // Calculate tax value based on tax percentage. |
156
|
|
|
$memberpress_transaction->tax_rate |
157
|
|
|
) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/* |
163
|
|
|
* Lines. |
164
|
|
|
*/ |
165
|
|
|
$payment->lines = new PaymentLines(); |
166
|
|
|
|
167
|
|
|
$line = $payment->lines->new_line(); |
168
|
|
|
|
169
|
|
|
$line->set_id( $memberpress_product->ID ); |
170
|
|
|
$line->set_name( $memberpress_product->post_title ); |
171
|
|
|
$line->set_quantity( 1 ); |
172
|
|
|
$line->set_unit_price( $payment->get_total_amount() ); |
173
|
|
|
$line->set_total_amount( $payment->get_total_amount() ); |
174
|
|
|
$line->set_product_url( get_permalink( $memberpress_product->ID ) ); |
|
|
|
|
175
|
|
|
|
176
|
|
|
/* |
177
|
|
|
* Return. |
178
|
|
|
*/ |
179
|
|
|
return $payment; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get Pronamic subscription from MemberPress transaction. |
184
|
|
|
* |
185
|
|
|
* @param MeprTransaction $memberpress_transaction MemberPress transaction object. |
186
|
|
|
* |
187
|
|
|
* @return Subscription|null |
188
|
|
|
*/ |
189
|
|
|
public static function get_subscription( MeprTransaction $memberpress_transaction ) { |
190
|
|
|
$memberpress_product = $memberpress_transaction->product(); |
191
|
|
|
|
192
|
|
|
if ( $memberpress_product->is_one_time_payment() ) { |
193
|
|
|
return null; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$memberpress_subscription = $memberpress_transaction->subscription(); |
197
|
|
|
|
198
|
|
|
if ( ! $memberpress_subscription ) { |
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Subscription. |
204
|
|
|
*/ |
205
|
|
|
$subscription = new Subscription(); |
206
|
|
|
|
207
|
|
|
$start_date = new \DateTimeImmutable(); |
208
|
|
|
|
209
|
|
|
// Trial phase. |
210
|
|
|
if ( $memberpress_subscription->in_trial() ) { |
211
|
|
|
$trial_phase = new SubscriptionPhase( |
212
|
|
|
$subscription, |
213
|
|
|
$start_date, |
214
|
|
|
new SubscriptionInterval( 'P' . $memberpress_subscription->trial_days . 'D' ), |
215
|
|
|
new TaxedMoney( $memberpress_subscription->trial_amount, MemberPress::get_currency() ) |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$trial_phase->set_total_periods( 1 ); |
219
|
|
|
$trial_phase->set_trial( true ); |
220
|
|
|
|
221
|
|
|
$subscription->add_phase( $trial_phase ); |
222
|
|
|
|
223
|
|
|
$start_date = $trial_phase->get_end_date(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// Total periods. |
227
|
|
|
$total_periods = null; |
228
|
|
|
|
229
|
|
|
$limit_cycles_number = (int) $memberpress_subscription->limit_cycles_num; |
230
|
|
|
|
231
|
|
|
if ( $memberpress_subscription->limit_cycles && $limit_cycles_number > 0 ) { |
232
|
|
|
$total_periods = $limit_cycles_number; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// Regular phase. |
236
|
|
|
$regular_phase = new SubscriptionPhase( |
237
|
|
|
$subscription, |
238
|
|
|
$start_date, |
|
|
|
|
239
|
|
|
new SubscriptionInterval( 'P' . $memberpress_product->period . Core_Util::to_period( $memberpress_product->period_type ) ), |
240
|
|
|
new TaxedMoney( $memberpress_transaction->total, MemberPress::get_currency() ) |
241
|
|
|
); |
242
|
|
|
|
243
|
|
|
$regular_phase->set_total_periods( $total_periods ); |
244
|
|
|
|
245
|
|
|
$subscription->add_phase( $regular_phase ); |
246
|
|
|
|
247
|
|
|
return $subscription; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|