1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
4
|
|
|
use EventEspresso\core\exceptions\InvalidEntityException; |
5
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
6
|
|
|
use EventEspresso\core\services\currency\CurrencyFactory; |
7
|
|
|
use EventEspresso\core\services\currency\MoneyFactory; |
8
|
|
|
use EventEspresso\core\services\formatters\AsciiOnly; |
9
|
|
|
use EventEspresso\core\services\loaders\LoaderFactory; |
10
|
|
|
use EventEspresso\core\services\payment_methods\gateways\GatewayDataFormatter; |
11
|
|
|
|
12
|
|
|
//prevents a fatal error because somehow these aren't autoloaded elsewhere and are required in this class |
13
|
|
|
EE_Registry::instance()->load_lib('Gateway'); |
14
|
|
|
EE_Registry::instance()->load_lib('Onsite_Gateway'); |
15
|
|
|
EE_Registry::instance()->load_lib('Offsite_Gateway'); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* Class EE_PMT_Base |
20
|
|
|
* |
21
|
|
|
* Description |
22
|
|
|
* |
23
|
|
|
* @package Event Espresso |
24
|
|
|
* @subpackage core |
25
|
|
|
* @author Mike Nelson |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
abstract class EE_PMT_Base |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
const onsite = 'on-site'; |
32
|
|
|
const offsite = 'off-site'; |
33
|
|
|
const offline = 'off-line'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var EE_Payment_Method |
37
|
|
|
*/ |
38
|
|
|
protected $_pm_instance = NULL; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var boolean |
42
|
|
|
*/ |
43
|
|
|
protected $_requires_https = FALSE; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var boolean |
47
|
|
|
*/ |
48
|
|
|
protected $_has_billing_form; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var EE_Gateway |
52
|
|
|
*/ |
53
|
|
|
protected $_gateway = NULL; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var EE_Payment_Method_Form |
57
|
|
|
*/ |
58
|
|
|
protected $_settings_form = NULL; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var EE_Form_Section_Proper |
62
|
|
|
*/ |
63
|
|
|
protected $_billing_form = NULL; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var boolean |
67
|
|
|
*/ |
68
|
|
|
protected $_cache_billing_form = TRUE; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* String of the absolute path to the folder containing this file, with a trailing slash. |
72
|
|
|
* eg '/public_html/wp-site/wp-content/plugins/event-espresso/payment_methods/Invoice/' |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $_file_folder = NULL; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* String to the absolute URL to this file (useful for getting its web-accessible resources |
79
|
|
|
* like images, js, or css) |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
protected $_file_url = NULL; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Pretty name for the payment method |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
protected $_pretty_name = NULL; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* |
92
|
|
|
* @var string |
93
|
|
|
*/ |
94
|
|
|
protected $_default_button_url = NULL; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
* @var string |
99
|
|
|
*/ |
100
|
|
|
protected $_default_description = NULL; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var MoneyFactory |
104
|
|
|
*/ |
105
|
|
|
protected $money_factory; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @var CurrencyFactory |
109
|
|
|
*/ |
110
|
|
|
protected $currency_factory; |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* |
115
|
|
|
* @param EE_Payment_Method $pm_instance |
116
|
|
|
* @param MoneyFactory|null $money_factory |
117
|
|
|
* @param CurrencyFactory $currency_factory |
118
|
|
|
* @throws InvalidArgumentException |
119
|
|
|
* @throws InvalidInterfaceException |
120
|
|
|
* @throws InvalidDataTypeException |
121
|
|
|
* @throws InvalidEntityException |
122
|
|
|
* @throws EE_Error |
123
|
|
|
*/ |
124
|
|
|
public function __construct( |
125
|
|
|
$pm_instance = NULL, |
126
|
|
|
MoneyFactory $money_factory = null, |
127
|
|
|
CurrencyFactory $currency_factory = null |
128
|
|
|
) { |
129
|
|
|
if (! $money_factory instanceof MoneyFactory) { |
130
|
|
|
$money_factory = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\currency\MoneyFactory'); |
131
|
|
|
} |
132
|
|
|
if (! $currency_factory instanceof CurrencyFactory) { |
133
|
|
|
$currency_factory = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\currency\CurrencyFactory'); |
134
|
|
|
} |
135
|
|
|
$this->currency_factory = $currency_factory; |
136
|
|
|
$this->money_factory = $money_factory; |
137
|
|
|
if ($pm_instance instanceof EE_Payment_Method) { |
138
|
|
|
$this->set_instance($pm_instance); |
139
|
|
|
} |
140
|
|
|
if ($this->_gateway) { |
141
|
|
|
$this->_gateway->set_payment_model(EEM_Payment::instance()); |
142
|
|
|
$this->_gateway->set_payment_log(EEM_Change_Log::instance()); |
143
|
|
|
$this->_gateway->set_template_helper(new EEH_Template()); |
144
|
|
|
$this->_gateway->set_line_item_helper(new EEH_Line_Item()); |
145
|
|
|
$this->_gateway->set_money_helper(new EEH_Money()); |
146
|
|
|
$this->_gateway->set_gateway_data_formatter(new GatewayDataFormatter()); |
147
|
|
|
$this->_gateway->set_unsupported_character_remover(new AsciiOnly()); |
148
|
|
|
do_action('AHEE__EE_PMT_Base___construct__done_initializing_gateway_class', $this, $this->_gateway); |
149
|
|
|
} |
150
|
|
|
if (!isset($this->_has_billing_form)) { |
151
|
|
|
// by default, On Site gateways have a billing form |
152
|
|
|
if ($this->payment_occurs() == EE_PMT_Base::onsite) { |
153
|
|
|
$this->set_has_billing_form(true); |
154
|
|
|
} else { |
155
|
|
|
$this->set_has_billing_form(false); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (!$this->_pretty_name) { |
160
|
|
|
throw new EE_Error(sprintf(__("You must set the pretty name for the Payment Method Type in the constructor (_pretty_name), and please make it internationalized", "event_espresso"))); |
161
|
|
|
} |
162
|
|
|
//if the child didn't specify a default button, use the credit card one |
163
|
|
|
if ($this->_default_button_url === NULL) { |
164
|
|
|
$this->_default_button_url = EE_PLUGIN_DIR_URL . 'payment_methods' . DS . 'pay-by-credit-card.png'; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param boolean $has_billing_form |
171
|
|
|
*/ |
172
|
|
|
public function set_has_billing_form($has_billing_form) |
173
|
|
|
{ |
174
|
|
|
$this->_has_billing_form = filter_var($has_billing_form, FILTER_VALIDATE_BOOLEAN); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* sets the file_folder property |
180
|
|
|
* @throws ReflectionException |
181
|
|
|
*/ |
182
|
|
|
protected function _set_file_folder() |
183
|
|
|
{ |
184
|
|
|
$reflector = new ReflectionClass(get_class($this)); |
185
|
|
|
$fn = $reflector->getFileName(); |
186
|
|
|
$this->_file_folder = dirname($fn) . DS; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* sets the file URL with a trailing slash for this PMT |
192
|
|
|
*/ |
193
|
|
|
protected function _set_file_url() |
194
|
|
|
{ |
195
|
|
|
$plugins_dir_fixed = str_replace('\\', DS, WP_PLUGIN_DIR); |
196
|
|
|
$file_folder_fixed = str_replace('\\', DS, $this->file_folder()); |
197
|
|
|
$file_path = str_replace($plugins_dir_fixed, WP_PLUGIN_URL, $file_folder_fixed); |
198
|
|
|
$this->_file_url = $file_path; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Gets the default description on all payment methods of this type |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function default_description() |
206
|
|
|
{ |
207
|
|
|
return $this->_default_description; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns the folder containing the PMT child class, with a trailing slash |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function file_folder() |
216
|
|
|
{ |
217
|
|
|
if (!$this->_file_folder) { |
218
|
|
|
$this->_set_file_folder(); |
219
|
|
|
} |
220
|
|
|
return $this->_file_folder; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function file_url() |
228
|
|
|
{ |
229
|
|
|
if (!$this->_file_url) { |
230
|
|
|
$this->_set_file_url(); |
231
|
|
|
} |
232
|
|
|
return $this->_file_url; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Sets the payment method instance this payment method type is for. |
238
|
|
|
* Its important teh payment method instance is set before |
239
|
|
|
* @param EE_Payment_Method $payment_method_instance |
240
|
|
|
*/ |
241
|
|
|
function set_instance($payment_method_instance) |
242
|
|
|
{ |
243
|
|
|
$this->_pm_instance = $payment_method_instance; |
244
|
|
|
//if they have already requested the settings form, make sure its |
245
|
|
|
//data matches this model object |
246
|
|
|
if ($this->_settings_form) { |
247
|
|
|
$this->settings_form()->populate_model_obj($payment_method_instance); |
248
|
|
|
} |
249
|
|
|
if ($this->_gateway && $this->_gateway instanceof EE_Gateway) { |
250
|
|
|
$this->_gateway->set_settings($payment_method_instance->settings_array()); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Gets teh form for displaying to admins where they setup the payment method |
257
|
|
|
* @return EE_Payment_Method_Form |
258
|
|
|
* @throws EE_Error |
259
|
|
|
*/ |
260
|
|
|
function settings_form() |
261
|
|
|
{ |
262
|
|
|
if (!$this->_settings_form) { |
263
|
|
|
$this->_settings_form = $this->generate_new_settings_form(); |
264
|
|
|
$this->_settings_form->set_payment_method_type($this); |
265
|
|
|
//if we have already assigned a model object to this pmt, make |
266
|
|
|
//sure its reflected in teh form we just generated |
267
|
|
|
if ($this->_pm_instance) { |
268
|
|
|
$this->_settings_form->populate_model_obj($this->_pm_instance); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
return $this->_settings_form; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Gets the form for all the settings related to this payment method type |
277
|
|
|
* @return EE_Payment_Method_Form |
278
|
|
|
*/ |
279
|
|
|
abstract function generate_new_settings_form(); |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Sets the form for settings. This may be useful if we have already received |
284
|
|
|
* a form submission and have form data it in, and want to use it anytime we're showing |
285
|
|
|
* this payment method type's settings form later in the request |
286
|
|
|
* @param EE_Payment_Method_Form $form |
287
|
|
|
*/ |
288
|
|
|
public function set_settings_form($form) |
289
|
|
|
{ |
290
|
|
|
$this->_settings_form = $form; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return boolean |
296
|
|
|
*/ |
297
|
|
|
public function has_billing_form() |
298
|
|
|
{ |
299
|
|
|
return $this->_has_billing_form; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Gets the form for displaying to attendees where they can enter their billing info |
305
|
|
|
* which will be sent to teh gateway (can be null) |
306
|
|
|
* |
307
|
|
|
* @param EE_Transaction $transaction |
308
|
|
|
* @param array $extra_args |
309
|
|
|
* @return EE_Billing_Attendee_Info_Form|EE_Billing_Info_Form|null |
310
|
|
|
* @throws EE_Error |
311
|
|
|
*/ |
312
|
|
|
public function billing_form(EE_Transaction $transaction = NULL, $extra_args = array()) |
313
|
|
|
{ |
314
|
|
|
// has billing form already been regenerated ? or overwrite cache? |
315
|
|
|
if (!$this->_billing_form instanceof EE_Billing_Info_Form || !$this->_cache_billing_form) { |
316
|
|
|
$this->_billing_form = $this->generate_new_billing_form($transaction, $extra_args); |
317
|
|
|
} |
318
|
|
|
//if we know who the attendee is, and this is a billing form |
319
|
|
|
//that uses attendee info, populate it |
320
|
|
|
if ( |
321
|
|
|
apply_filters( |
322
|
|
|
'FHEE__populate_billing_form_fields_from_attendee', |
323
|
|
|
( |
324
|
|
|
$this->_billing_form instanceof EE_Billing_Attendee_Info_Form |
325
|
|
|
&& $transaction instanceof EE_Transaction |
326
|
|
|
&& $transaction->primary_registration() instanceof EE_Registration |
327
|
|
|
&& $transaction->primary_registration()->attendee() instanceof EE_Attendee |
328
|
|
|
), |
329
|
|
|
$this->_billing_form, |
330
|
|
|
$transaction |
331
|
|
|
) |
332
|
|
|
) { |
333
|
|
|
$this->_billing_form->populate_from_attendee($transaction->primary_registration()->attendee()); |
|
|
|
|
334
|
|
|
} |
335
|
|
|
return $this->_billing_form; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Creates the billing form for this payment method type |
341
|
|
|
* @param EE_Transaction $transaction |
342
|
|
|
* @return EE_Billing_Info_Form |
343
|
|
|
*/ |
344
|
|
|
abstract function generate_new_billing_form(EE_Transaction $transaction = NULL); |
345
|
|
|
|
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* apply_billing_form_debug_settings |
349
|
|
|
* applies debug data to the form |
350
|
|
|
* |
351
|
|
|
* @param EE_Billing_Info_Form $billing_form |
352
|
|
|
* @return EE_Billing_Info_Form |
353
|
|
|
*/ |
354
|
|
|
public function apply_billing_form_debug_settings(EE_Billing_Info_Form $billing_form) |
355
|
|
|
{ |
356
|
|
|
return $billing_form; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Sets the billing form for this payment method type. You may want to use this |
362
|
|
|
* if you have form |
363
|
|
|
* @param EE_Payment_Method $form |
364
|
|
|
*/ |
365
|
|
|
public function set_billing_form($form) |
366
|
|
|
{ |
367
|
|
|
$this->_billing_form = $form; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Returns whether or not this payment method requires HTTPS to be used |
373
|
|
|
* @return boolean |
374
|
|
|
*/ |
375
|
|
|
function requires_https() |
376
|
|
|
{ |
377
|
|
|
return $this->_requires_https; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* |
383
|
|
|
* @param EE_Transaction $transaction |
384
|
|
|
* @param float $amount |
385
|
|
|
* @param EE_Billing_Info_Form $billing_info |
386
|
|
|
* @param string $return_url |
387
|
|
|
* @param string $fail_url |
388
|
|
|
* @param string $method |
389
|
|
|
* @param bool $by_admin |
390
|
|
|
* @return EE_Payment |
391
|
|
|
* @throws InvalidArgumentException |
392
|
|
|
* @throws InvalidInterfaceException |
393
|
|
|
* @throws InvalidDataTypeException |
394
|
|
|
* @throws EE_Error |
395
|
|
|
*/ |
396
|
|
|
function process_payment(EE_Transaction $transaction, $amount = null, $billing_info = null, $return_url = null, $fail_url = '', $method = 'CART', $by_admin = false) |
397
|
|
|
{ |
398
|
|
|
// @todo: add surcharge for the payment method, if any |
399
|
|
|
if ($this->_gateway) { |
400
|
|
|
//there is a gateway, so we're going to make a payment object |
401
|
|
|
//but wait! do they already have a payment in progress that we thought was failed? |
402
|
|
|
$duplicate_properties = array( |
403
|
|
|
'STS_ID' => EEM_Payment::status_id_failed, |
404
|
|
|
'TXN_ID' => $transaction->ID(), |
405
|
|
|
'PMD_ID' => $this->_pm_instance->ID(), |
406
|
|
|
'PAY_source' => $method, |
407
|
|
|
'PAY_amount' => $amount !== null ? $amount : $transaction->remaining(), |
408
|
|
|
'PAY_gateway_response' => null, |
409
|
|
|
); |
410
|
|
|
$payment = EEM_Payment::instance()->get_one(array($duplicate_properties)); |
411
|
|
|
//if we didn't already have a payment in progress for the same thing, |
412
|
|
|
//then we actually want to make a new payment |
413
|
|
|
if (!$payment instanceof EE_Payment) { |
414
|
|
|
$payment = EE_Payment::new_instance( |
415
|
|
|
array_merge( |
416
|
|
|
$duplicate_properties, |
417
|
|
|
array( |
418
|
|
|
'PAY_timestamp' => time(), |
419
|
|
|
'PAY_txn_id_chq_nmbr' => null, |
420
|
|
|
'PAY_po_number' => null, |
421
|
|
|
'PAY_extra_accntng' => null, |
422
|
|
|
'PAY_details' => null, |
423
|
|
|
) |
424
|
|
|
) |
425
|
|
|
); |
426
|
|
|
} |
427
|
|
|
//make sure the payment has been saved to show we started it, and so it has an ID should the gateway try to log it |
428
|
|
|
$payment->save(); |
429
|
|
|
$billing_values = $this->_get_billing_values_from_form($billing_info); |
|
|
|
|
430
|
|
|
|
431
|
|
|
// Offsite Gateway |
432
|
|
|
if ($this->_gateway instanceof EE_Offsite_Gateway) { |
433
|
|
|
|
434
|
|
|
$payment = $this->_gateway->set_redirection_info( |
435
|
|
|
$payment, |
436
|
|
|
$billing_values, |
437
|
|
|
$return_url, |
438
|
|
|
EE_Config::instance()->core->txn_page_url( |
439
|
|
|
array( |
440
|
|
|
'e_reg_url_link' => $transaction->primary_registration()->reg_url_link(), |
441
|
|
|
'ee_payment_method' => $this->_pm_instance->slug() |
442
|
|
|
) |
443
|
|
|
), |
444
|
|
|
$fail_url |
445
|
|
|
); |
446
|
|
|
$payment->save(); |
447
|
|
|
// Onsite Gateway |
448
|
|
|
} elseif ($this->_gateway instanceof EE_Onsite_Gateway) { |
449
|
|
|
|
450
|
|
|
$payment = $this->_gateway->do_direct_payment($payment, $billing_values); |
451
|
|
|
$payment->save(); |
452
|
|
|
|
453
|
|
|
} else { |
454
|
|
|
throw new EE_Error( |
455
|
|
|
sprintf( |
456
|
|
|
__('Gateway for payment method type "%s" is "%s", not a subclass of either EE_Offsite_Gateway or EE_Onsite_Gateway, or null (to indicate NO gateway)', 'event_espresso'), |
457
|
|
|
get_class($this), |
458
|
|
|
gettype($this->_gateway) |
459
|
|
|
) |
460
|
|
|
); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
} else { |
464
|
|
|
// no gateway provided |
465
|
|
|
// there is no payment. Must be an offline gateway |
466
|
|
|
// create a payment object anyways, but dont save it |
467
|
|
|
$payment = EE_Payment::new_instance( |
468
|
|
|
array( |
469
|
|
|
'STS_ID' => EEM_Payment::status_id_pending, |
470
|
|
|
'TXN_ID' => $transaction->ID(), |
471
|
|
|
'PMD_ID' => $transaction->payment_method_ID(), |
472
|
|
|
'PAY_amount' => 0.00, |
473
|
|
|
'PAY_timestamp' => time(), |
474
|
|
|
) |
475
|
|
|
); |
476
|
|
|
|
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
// if there is billing info, clean it and save it now |
480
|
|
|
if ($billing_info instanceof EE_Billing_Attendee_Info_Form) { |
481
|
|
|
$this->_save_billing_info_to_attendee($billing_info, $transaction); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
return $payment; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Gets the values we want to pass onto the gateway. Normally these |
489
|
|
|
* are just the 'pretty' values, but there may be times the data may need |
490
|
|
|
* a little massaging. Proper subsections will become arrays of inputs |
491
|
|
|
* @param EE_Billing_Info_Form $billing_form |
492
|
|
|
* @return array |
493
|
|
|
*/ |
494
|
|
|
protected function _get_billing_values_from_form($billing_form) |
495
|
|
|
{ |
496
|
|
|
if ($billing_form instanceof EE_Form_Section_Proper) { |
497
|
|
|
return $billing_form->input_pretty_values(true); |
498
|
|
|
} else { |
499
|
|
|
return NULL; |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* Handles an instant payment notification when the transaction is known (by default). |
506
|
|
|
* @param array $req_data |
507
|
|
|
* @param EE_Transaction $transaction |
508
|
|
|
* @return EE_Payment |
509
|
|
|
* @throws InvalidArgumentException |
510
|
|
|
* @throws InvalidInterfaceException |
511
|
|
|
* @throws InvalidDataTypeException |
512
|
|
|
* @throws EE_Error |
513
|
|
|
*/ |
514
|
|
|
public function handle_ipn($req_data, $transaction) |
515
|
|
|
{ |
516
|
|
|
$transaction = EEM_Transaction::instance()->ensure_is_obj($transaction); |
517
|
|
|
if (!$this->_gateway instanceof EE_Offsite_Gateway) { |
518
|
|
|
throw new EE_Error(sprintf(__("Could not handle IPN because '%s' is not an offsite gateway", "event_espresso"), print_r($this->_gateway, TRUE))); |
519
|
|
|
|
520
|
|
|
} |
521
|
|
|
$payment = $this->_gateway->handle_payment_update($req_data, $transaction); |
522
|
|
|
return $payment; |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Saves the billing info onto the attendee of the primary registrant on this transaction, and |
528
|
|
|
* cleans it first. |
529
|
|
|
* @param EE_Billing_Attendee_Info_Form $billing_form |
530
|
|
|
* @param EE_Transaction $transaction |
531
|
|
|
* @return boolean success |
532
|
|
|
* @throws EE_Error |
533
|
|
|
*/ |
534
|
|
|
protected function _save_billing_info_to_attendee($billing_form, $transaction) |
535
|
|
|
{ |
536
|
|
|
if (!$transaction || !$transaction instanceof EE_Transaction) { |
537
|
|
|
EE_Error::add_error(__("Cannot save billing info because no transaction was specified", "event_espresso"), __FILE__, __FUNCTION__, __LINE__); |
538
|
|
|
return false; |
539
|
|
|
} |
540
|
|
|
$primary_reg = $transaction->primary_registration(); |
541
|
|
|
if (!$primary_reg) { |
542
|
|
|
EE_Error::add_error(__("Cannot save billing info because the transaction has no primary registration", "event_espresso"), __FILE__, __FUNCTION__, __LINE__); |
543
|
|
|
return false; |
544
|
|
|
} |
545
|
|
|
$attendee = $primary_reg->attendee(); |
546
|
|
|
if (!$attendee) { |
547
|
|
|
EE_Error::add_error(__("Cannot save billing info because the transaction's primary registration has no attendee!", "event_espresso"), __FILE__, __FUNCTION__, __LINE__); |
548
|
|
|
return false; |
549
|
|
|
} |
550
|
|
|
return $attendee->save_and_clean_billing_info_for_payment_method($billing_form, $transaction->payment_method()); |
551
|
|
|
|
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* Gets the payment this IPN is for. Children may often want to |
557
|
|
|
* override this to inspect the request |
558
|
|
|
* @param EE_Transaction $transaction |
559
|
|
|
* @param array $req_data |
560
|
|
|
* @return EE_Payment |
561
|
|
|
* @throws EE_Error |
562
|
|
|
*/ |
563
|
|
|
protected function find_payment_for_ipn(EE_Transaction $transaction, $req_data = array()) |
564
|
|
|
{ |
565
|
|
|
return $transaction->last_payment(); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* In case generic code cannot provide the payment processor with a specific payment method |
571
|
|
|
* and transaction, it will try calling this method on each activate payment method. |
572
|
|
|
* If the payment method is able to identify the request as being for it, it should fetch |
573
|
|
|
* the payment its for and return it. If not, it should throw an EE_Error to indicate it cannot |
574
|
|
|
* handle the IPN |
575
|
|
|
* @param array $req_data |
576
|
|
|
* @return EE_Payment only if this payment method can find the info its needs from $req_data |
577
|
|
|
* and identifies the IPN as being for this payment method (not just fo ra payment method of this type) |
578
|
|
|
* @throws EE_Error |
579
|
|
|
*/ |
580
|
|
|
public function handle_unclaimed_ipn($req_data = array()) |
581
|
|
|
{ |
582
|
|
|
throw new EE_Error(sprintf(__("Payment Method '%s' cannot handle unclaimed IPNs", "event_espresso"), get_class($this))); |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Logic to be accomplished when the payment attempt is complete. |
588
|
|
|
* Most payment methods don't need to do anything at this point; but some, like Mijireh, do. |
589
|
|
|
* (Mijireh is an offsite gateway which doesn't send an IPN. So when the user returns to EE from |
590
|
|
|
* mijireh, this method needs to be called so the Mijireh PM can ping Mijireh to know the status |
591
|
|
|
* of the payment). Fed a transaction because it's always assumed to be the last payment that |
592
|
|
|
* we're dealing with. Returns that last payment (if there is one) |
593
|
|
|
* |
594
|
|
|
* @param EE_Transaction $transaction |
595
|
|
|
* @return EE_Payment |
596
|
|
|
* @throws EE_Error |
597
|
|
|
*/ |
598
|
|
|
public function finalize_payment_for($transaction) |
599
|
|
|
{ |
600
|
|
|
return $transaction->last_payment(); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Whether or not this payment method's gateway supports sending refund requests |
606
|
|
|
* @return boolean |
607
|
|
|
*/ |
608
|
|
|
public function supports_sending_refunds() |
609
|
|
|
{ |
610
|
|
|
if ($this->_gateway && $this->_gateway instanceof EE_Gateway) { |
611
|
|
|
return $this->_gateway->supports_sending_refunds(); |
612
|
|
|
} else { |
613
|
|
|
return false; |
614
|
|
|
} |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
|
618
|
|
|
/** |
619
|
|
|
* |
620
|
|
|
* @param EE_Payment $payment |
621
|
|
|
* @param array $refund_info |
622
|
|
|
* @throws EE_Error |
623
|
|
|
* @return EE_Payment |
624
|
|
|
*/ |
625
|
|
|
public function process_refund(EE_Payment $payment, $refund_info = array()) |
626
|
|
|
{ |
627
|
|
|
if ($this->_gateway && $this->_gateway instanceof EE_Gateway) { |
628
|
|
|
return $this->_gateway->do_direct_refund($payment, $refund_info); |
629
|
|
|
} else { |
630
|
|
|
throw new EE_Error( |
631
|
|
|
sprintf( |
632
|
|
|
__('Payment Method Type "%s" does not support sending refund requests', 'event_espresso'), |
633
|
|
|
get_class($this) |
634
|
|
|
) |
635
|
|
|
); |
636
|
|
|
} |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Returns one the class's constants onsite,offsite, or offline, depending on this |
642
|
|
|
* payment method's gateway. |
643
|
|
|
* @return string |
644
|
|
|
* @throws EE_Error |
645
|
|
|
*/ |
646
|
|
|
public function payment_occurs() |
647
|
|
|
{ |
648
|
|
|
if (!$this->_gateway) { |
649
|
|
|
return EE_PMT_Base::offline; |
650
|
|
|
} elseif ($this->_gateway instanceof EE_Onsite_Gateway) { |
651
|
|
|
return EE_PMT_Base::onsite; |
652
|
|
|
} elseif ($this->_gateway instanceof EE_Offsite_Gateway) { |
653
|
|
|
return EE_PMT_Base::offsite; |
654
|
|
|
} else { |
655
|
|
|
throw new EE_Error(sprintf(__("Payment method type '%s's gateway isn't an instance of EE_Onsite_Gateway, EE_Offsite_Gateway, or null. It must be one of those", "event_espresso"), get_class($this))); |
656
|
|
|
} |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* For adding any html output ab ove the payment overview. |
662
|
|
|
* Many gateways won't want ot display anything, so this function just returns an empty string. |
663
|
|
|
* Other gateways may want to override this, such as offline gateways. |
664
|
|
|
* @param EE_Payment $payment |
665
|
|
|
* @return string |
666
|
|
|
* @throws \DomainException |
667
|
|
|
*/ |
668
|
|
|
public function payment_overview_content(EE_Payment $payment) |
669
|
|
|
{ |
670
|
|
|
return EEH_Template::display_template(EE_LIBRARIES . 'payment_methods' . DS . 'templates' . DS . 'payment_details_content.template.php', array('payment_method' => $this->_pm_instance, 'payment' => $payment), true); |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* @return array where keys are the help tab name, |
676
|
|
|
* values are: array { |
677
|
|
|
* @type string $title i18n name for the help tab |
678
|
|
|
* @type string $filename name of the file located in ./help_tabs/ (ie, in a folder next to this file) |
679
|
|
|
* @type array $template_args any arguments you want passed to the template file while rendering. |
680
|
|
|
* Keys will be variable names and values with be their values. |
681
|
|
|
*/ |
682
|
|
|
public function help_tabs_config() |
683
|
|
|
{ |
684
|
|
|
return array(); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* The system name for this PMT (eg AIM, Paypal_Pro, Invoice... what gets put into |
690
|
|
|
* the payment method's table's PMT_type column) |
691
|
|
|
* @return string |
692
|
|
|
*/ |
693
|
|
|
public function system_name() |
694
|
|
|
{ |
695
|
|
|
$classname = get_class($this); |
696
|
|
|
return str_replace("EE_PMT_", '', $classname); |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* A pretty i18n version of the PMT name |
702
|
|
|
* @return string |
703
|
|
|
*/ |
704
|
|
|
public function pretty_name() |
705
|
|
|
{ |
706
|
|
|
return $this->_pretty_name; |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
|
710
|
|
|
/** |
711
|
|
|
* Gets the default absolute URL to the payment method type's button |
712
|
|
|
* @return string |
713
|
|
|
*/ |
714
|
|
|
public function default_button_url() |
715
|
|
|
{ |
716
|
|
|
return $this->_default_button_url; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
|
720
|
|
|
/** |
721
|
|
|
* Gets the gateway used by this payment method (if any) |
722
|
|
|
* @return EE_Gateway |
723
|
|
|
*/ |
724
|
|
|
public function get_gateway() |
725
|
|
|
{ |
726
|
|
|
return $this->_gateway; |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* @return string html for the link to a help tab |
732
|
|
|
*/ |
733
|
|
|
public function get_help_tab_link() |
734
|
|
|
{ |
735
|
|
|
return EEH_Template::get_help_tab_link($this->get_help_tab_name()); |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
|
739
|
|
|
/** |
740
|
|
|
* Returns the name of the help tab for this PMT |
741
|
|
|
* @return string |
742
|
|
|
*/ |
743
|
|
|
public function get_help_tab_name() |
744
|
|
|
{ |
745
|
|
|
return 'ee_' . strtolower($this->system_name()) . '_help_tab'; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
/** |
749
|
|
|
* The name of the wp capability that should be associated with the usage of |
750
|
|
|
* this PMT by an admin |
751
|
|
|
* @return string |
752
|
|
|
*/ |
753
|
|
|
public function cap_name() |
754
|
|
|
{ |
755
|
|
|
return 'ee_payment_method_' . strtolower($this->system_name()); |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
/** |
759
|
|
|
* Called by client code to tell the gateway that if it wants to change |
760
|
|
|
* the transaction or line items or registrations related to teh payment it already |
761
|
|
|
* processed (we think, but possibly not) that now's the time to do it. |
762
|
|
|
* It is expected that gateways will store any info they need for this on the PAY_details, |
763
|
|
|
* or maybe an extra meta value |
764
|
|
|
* @param EE_Payment $payment |
765
|
|
|
* @return void |
766
|
|
|
*/ |
767
|
|
|
public function update_txn_based_on_payment($payment) |
768
|
|
|
{ |
769
|
|
|
if ($this->_gateway instanceof EE_Gateway) { |
770
|
|
|
$this->_gateway->update_txn_based_on_payment($payment); |
771
|
|
|
} |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
/** |
775
|
|
|
* Returns a string of HTML describing this payment method type for an admin, |
776
|
|
|
* primarily intended for them to read before activating it. |
777
|
|
|
* The easiest way to set this is to create a folder 'templates' alongside |
778
|
|
|
* your EE_PMT_{System_Name} file, and in it create a file named "{system_name}_intro.template.php". |
779
|
|
|
* Eg, if your payment method file is named "EE_PMT_Foo_Bar.pm.php", |
780
|
|
|
* then you'd create a file named "templates" in the same folder as it, and name the file |
781
|
|
|
* "foo_bar_intro.template.php", and its content will be returned by this method |
782
|
|
|
* @return string |
783
|
|
|
*/ |
784
|
|
|
public function introductory_html() |
785
|
|
|
{ |
786
|
|
|
return EEH_Template::locate_template($this->file_folder() . 'templates' . DS . strtolower($this->system_name()) . '_intro.template.php', array('pmt_obj' => $this, 'pm_instance' => $this->_pm_instance)); |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
|
790
|
|
|
} |
791
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: