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.5 |
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
|
|
|
* @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 ( empty( $value ) ) { |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
call_user_func( array( $address, $function ), $value ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$payment->set_billing_address( $address ); |
108
|
|
|
$payment->set_shipping_address( $address ); |
109
|
|
|
|
110
|
|
|
/* |
111
|
|
|
* Totals. |
112
|
|
|
*/ |
113
|
|
|
$payment->set_total_amount( |
114
|
|
|
new TaxedMoney( |
115
|
|
|
$memberpress_transaction->total, |
116
|
|
|
MemberPress::get_currency(), |
117
|
|
|
$memberpress_transaction->tax_amount, |
118
|
|
|
$memberpress_transaction->tax_rate |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
/* |
123
|
|
|
* Vat number. |
124
|
|
|
* @link https://github.com/wp-premium/memberpress-business/search?utf8=%E2%9C%93&q=mepr_vat_number&type= |
125
|
|
|
* @todo |
126
|
|
|
*/ |
127
|
|
|
|
128
|
|
|
/* |
129
|
|
|
* Subscription. |
130
|
|
|
* @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprTransaction.php#L603-L618 |
131
|
|
|
*/ |
132
|
|
|
$payment->subscription = self::get_subscription( $memberpress_transaction ); |
133
|
|
|
|
134
|
|
|
if ( $payment->subscription ) { |
135
|
|
|
$payment->subscription_source_id = $memberpress_transaction->subscription_id; |
136
|
|
|
|
137
|
|
|
if ( $memberpress_subscription->in_trial() ) { |
138
|
|
|
$payment->set_total_amount( |
139
|
|
|
new TaxedMoney( |
140
|
|
|
$memberpress_subscription->trial_amount, |
141
|
|
|
MemberPress::get_currency(), |
142
|
|
|
null, // Calculate tax value based on tax percentage. |
143
|
|
|
$memberpress_transaction->tax_rate |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/* |
150
|
|
|
* Lines. |
151
|
|
|
*/ |
152
|
|
|
$payment->lines = new PaymentLines(); |
153
|
|
|
|
154
|
|
|
$line = $payment->lines->new_line(); |
155
|
|
|
|
156
|
|
|
$line->set_id( $memberpress_product->ID ); |
157
|
|
|
$line->set_name( $memberpress_product->post_title ); |
158
|
|
|
$line->set_quantity( 1 ); |
159
|
|
|
$line->set_unit_price( $payment->get_total_amount() ); |
160
|
|
|
$line->set_total_amount( $payment->get_total_amount() ); |
161
|
|
|
$line->set_product_url( get_permalink( $memberpress_product->ID ) ); |
|
|
|
|
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
|
|
|
* |
174
|
|
|
* @return Subscription|null |
175
|
|
|
*/ |
176
|
|
|
public static function get_subscription( MeprTransaction $memberpress_transaction ) { |
177
|
|
|
$memberpress_product = $memberpress_transaction->product(); |
178
|
|
|
|
179
|
|
|
if ( $memberpress_product->is_one_time_payment() ) { |
180
|
|
|
return null; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$memberpress_subscription = $memberpress_transaction->subscription(); |
184
|
|
|
|
185
|
|
|
if ( ! $memberpress_subscription ) { |
186
|
|
|
return false; |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$frequency = null; |
190
|
|
|
|
191
|
|
|
if ( $memberpress_subscription->limit_cycles ) { |
192
|
|
|
$frequency = $memberpress_subscription->limit_cycles_num; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$subscription = new Subscription(); |
196
|
|
|
$subscription->frequency = (int) $frequency; |
197
|
|
|
$subscription->interval = $memberpress_product->period; |
198
|
|
|
$subscription->interval_period = Core_Util::to_period( $memberpress_product->period_type ); |
199
|
|
|
|
200
|
|
|
$subscription->set_total_amount( |
201
|
|
|
new TaxedMoney( |
202
|
|
|
$memberpress_transaction->total, |
203
|
|
|
MemberPress::get_currency() |
204
|
|
|
) |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
return $subscription; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|