Completed
Branch FET-10486-add-timestamp-checki... (611b15)
by
unknown
105:07 queued 90:18
created

EE_Gateway::set_settings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use \EventEspresso\core\services\payment_methods\gateways\GatewayDataFormatterInterface;
4
use \EventEspresso\core\exceptions\InvalidEntityException;
5
use \EventEspresso\core\services\formatters\FormatterInterface;
6
7
/**
8
 * EE_Gateway
9
 * Abstract base class for all gateways for processing payments.
10
 * This has been designed in a way so that other WP Plugins
11
 * can use this class for processing payments, and theoretically any of its children,
12
 * provided they implement the interfaces it uses.
13
 * The necessary interfaces to be implemented are contained in:
14
 * core/libraries/payment_methods/EEI_Payment_Method_Interfaces.php and EEI_Interfaces.
15
 * After constructing a gateway object, you need to set all the properties which reference many of the
16
 * needed helpers and models (see all the methods starting with "set_",
17
 * eg seg_line_item_helper which should be passed an object which implements EEHI_Line_Item_Helper; etc).
18
 *
19
 * @package            Event Espresso
20
 * @subpackage		core/libraries/payment_methods
21
 * @author			Mike Nelson
22
 */
23
abstract class EE_Gateway{
24
	/**
25
	 * a constant used as a possible value for $_currencies_supported to indicate
26
	 * that ALL currencies are supported by this gateway
27
	 */
28
	const all_currencies_supported = 'all_currencies_supported';
29
	/**
30
	 * Where values are 3-letter currency codes
31
	 * @var array
32
	 */
33
	protected $_currencies_supported = array();
34
	/**
35
	 * Whether or not this gateway can support SENDING a refund request (ie, initiated by
36
	 * admin in EE's wp-admin page)
37
	 * @var boolean
38
	 */
39
	protected $_supports_sending_refunds = false;
40
41
	/**
42
	 * Whether or not this gateway can support RECEIVING a refund request from the payment
43
	 * provider (ie, initiated by admin on the payment prover's website who sends an IPN to EE)
44
	 * @var boolean
45
	 */
46
	protected $_supports_receiving_refunds = false;
47
	/**
48
	 * Model for querying for existing payments
49
	 * @var EEMI_Payment
50
	 */
51
	protected $_pay_model;
52
53
	/**
54
	 * Model used for adding to the payments log
55
	 * @var EEMI_Payment_Log
56
	 */
57
	protected $_pay_log;
58
59
	/**
60
	 * Used for formatting some input to gateways
61
	 * @var EEHI_Template
62
	 */
63
	protected $_template;
64
65
	/**
66
	 * Concrete class that implements EEHI_Money, used by most gateways
67
	 * @var EEHI_Money
68
	 */
69
	protected $_money;
70
71
	/**
72
	 * Concrete class that implements EEHI_Line_Item, used for manipulating the line item tree
73
	 * @var EEHI_Line_Item
74
	 */
75
	protected $_line_item;
76
77
    /**
78
     * @var GatewayDataFormatterInterface
79
     */
80
    protected $_gateway_data_formatter;
81
82
    /**
83
     * @var FormatterInterface
84
     */
85
    protected $_unsupported_character_remover;
86
87
	/**
88
	 * The ID of the payment method using this gateway
89
	 * @var int
90
	 */
91
	protected $_ID;
92
93
	/**
94
	 * @var $_debug_mode boolean whether to send requests to teh sandbox site or not
95
	 */
96
	protected $_debug_mode;
97
	/**
98
	 *
99
	 * @var string $_name name to show for this payment method
100
	 */
101
	protected $_name;
102
	/**
103
	 *
104
	 * @var string name to show fir this payment method to admin-type users
105
	 */
106
	protected $_admin_name;
107
108
	/**
109
	 * @return EE_Gateway
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
110
	 */
111
	public function __construct(){
112
	}
113
114
	/**
115
	 * We don't want to serialize models as they often have circular structures
116
	 * (eg a payment model has a reference to each payment model object; and most
117
	 * payments have a transaction, most transactions have a payment method;
118
	 * most payment methods have a payment method type; most payment method types
119
	 * have a gateway. And if a gateway serializes its models, we start at the
120
	 * beginning again)
121
	 * @return array
122
	 */
123
	public function __sleep(){
124
		$properties = get_object_vars($this);
125
		unset( $properties[ '_pay_model' ], $properties[ '_pay_log' ] );
126
		return array_keys($properties);
127
	}
128
	/**
129
	 * Returns whether or not this gateway should support SENDING refunds
130
	 * see $_supports_sending_refunds
131
	 * @return boolean
132
	 */
133
	public function supports_sending_refunds(){
134
		return $this->_supports_sending_refunds;
135
	}
136
	/**
137
	 * Returns whether or not this gateway should support RECEIVING refunds
138
	 * see $_supports_receiving_refunds
139
	 * @return boolean
140
	 */
141
	public function supports_receiving_refunds(){
142
		return $this->_supports_receiving_refunds;
143
	}
144
145
146
147
	/**
148
	 * Tries to refund the payment specified, taking into account the extra
149
	 * refund info. Note that if the gateway's _supports_sending_refunds is false,
150
	 * this should just throw an exception.
151
	 * @param EE_Payment $payment
152
	 * @param array $refund_info
153
	 * @return EE_Payment for the refund
154
	 * @throws EE_Error
155
	 */
156
	public function do_direct_refund( EE_Payment $payment, $refund_info = null ) {
157
		return NULL;
158
	}
159
160
161
162
	/**
163
	 * Sets the payment method's settings so the gateway knows where to send the request
164
	 * etc
165
	 * @param array $settings_array
166
	 */
167
	public function set_settings($settings_array){
168
		foreach($settings_array as $name => $value){
169
			$property_name = "_".$name;
170
			$this->{$property_name} = $value;
171
		}
172
	}
173
	/**
174
	 * See this class description
175
	 * @param EEMI_Payment $payment_model
176
	 */
177
	public function set_payment_model($payment_model){
178
		$this->_pay_model = $payment_model;
179
	}
180
	/**
181
	 * See this class description
182
	 * @param EEMI_Payment_Log $payment_log_model
183
	 */
184
	public function set_payment_log($payment_log_model){
185
		$this->_pay_log = $payment_log_model;
186
	}
187
188
	/**
189
	 * See this class description
190
	 * @param EEHI_Template $template_helper
191
	 */
192
	public function set_template_helper($template_helper){
193
		$this->_template = $template_helper;
194
	}
195
196
	/**
197
	 * See this class description
198
	 * @param EEHI_Line_Item $line_item_helper
199
	 */
200
	public function set_line_item_helper( $line_item_helper ){
201
		$this->_line_item = $line_item_helper;
202
	}
203
204
	/**
205
	 * See this class description
206
	 * @param EEHI_Money $money_helper
207
	 */
208
	public function set_money_helper( $money_helper ){
209
		$this->_money = $money_helper;
210
	}
211
212
213
214
    /**
215
     * Sets the gateway data formatter helper
216
     * @param GatewayDataFormatterInterface $gateway_data_formatter
217
     * @throws InvalidEntityException if it's not set properly
218
     */
219 View Code Duplication
	public function set_gateway_data_formatter( GatewayDataFormatterInterface $gateway_data_formatter){
220
        if( ! $gateway_data_formatter instanceof GatewayDataFormatterInterface){
221
            throw new InvalidEntityException(
222
                is_object($gateway_data_formatter)
223
                    ? get_class($gateway_data_formatter)
224
                    : esc_html__('Not an object','event_espresso'),
225
                '\\EventEspresso\\core\\services\\payment_methods\\gateways\\GatewayDataFormatterInterface'
226
            );
227
        }
228
        $this->_gateway_data_formatter = $gateway_data_formatter;
229
    }
230
231
    /**
232
     * Gets the gateway data formatter
233
     * @return GatewayDataFormatterInterface
234
     * @throws InvalidEntityException if it's not set properly
235
     */
236 View Code Duplication
    protected function _get_gateway_formatter(){
237
        if( ! $this->_gateway_data_formatter instanceof GatewayDataFormatterInterface){
238
            throw new InvalidEntityException(
239
                is_object($this->_gateway_data_formatter)
240
                    ? get_class($this->_gateway_data_formatter)
241
                    : esc_html__('Not an object','event_espresso'),
242
                '\\EventEspresso\\core\\services\\payment_methods\\gateways\\GatewayDataFormatterInterface'
243
            );
244
        }
245
        return $this->_gateway_data_formatter;
246
    }
247
248
249
250
    /**
251
     * Sets the helper which will remove unsupported characters for most gateways
252
     * @param FormatterInterface $formatter
253
     * @return FormatterInterface
254
     * @throws InvalidEntityException
255
     */
256 View Code Duplication
    public function set_unsupported_character_remover( FormatterInterface $formatter){
257
        if( ! $formatter instanceof FormatterInterface){
258
            throw new InvalidEntityException(
259
                is_object($formatter)
260
                    ? get_class($formatter)
261
                    : esc_html__('Not an object','event_espresso'),
262
                '\\EventEspresso\\core\\services\\formatters\\FormatterInterface'
263
            );
264
        }
265
       $this->_unsupported_character_remover = $formatter;
266
    }
267
    /**
268
     * Gets the helper which removes characters which gateways might not support, like emojis etc.
269
     * @return FormatterInterface
270
     * @throws InvalidEntityException
271
     */
272 View Code Duplication
    protected function _get_unsupported_character_remover(){
273
        if( ! $this->_unsupported_character_remover instanceof FormatterInterface){
274
            throw new InvalidEntityException(
275
                is_object($this->_unsupported_character_remover)
276
                    ? get_class($this->_unsupported_character_remover)
277
                    : esc_html__('Not an object','event_espresso'),
278
                '\\EventEspresso\\core\\services\\formatters\\FormatterInterface'
279
            );
280
        }
281
        return $this->_unsupported_character_remover;
282
    }
283
284
285
	/**
286
	 * @param $message
287
	 * @param $payment
288
	 */
289
	public function log($message,$payment){
290
		if($payment instanceof EEI_Payment){
291
			$type='Payment';
292
			$id = $payment->ID();
293
		}else{
294
			$type = 'Payment_Method';
295
			$id = $this->_ID;
296
		}
297
		$this->_pay_log->gateway_log($message,$id,$type);
298
	}
299
	/**
300
	 * Formats the amount so it can generally be sent to gateways
301
	 * @param float $amount
302
	 * @return string
303
     * @deprecated since 4.9.31 insetad use EventEspresso\core\services\payment_methods\gateways\GatewayDataFormatter::format_currency()
304
	 */
305
	public function format_currency($amount){
306
		return $this->_get_gateway_formatter()->formatCurrency($amount);
307
	}
308
309
	/**
310
	 * Returns either an array of all the currency codes supported,
311
	 * or a string indicating they're all supported (EE_gateway::all_currencies_supported)
312
	 * @return mixed array or string
313
	 */
314
	public function currencies_supported(){
315
		return $this->_currencies_supported;
316
	}
317
318
	/**
319
	 * Returns what a simple summing of items and taxes for this transaction. This
320
	 * can be used to determine if some more complex line items, like promotions,
321
	 * surcharges, or cancellations occurred (in which case we might want to forget
322
	 * about creating an itemized list of purchases and instead only send the total due)
323
	 * @param EE_Transaction  $transaction
324
	 * @return float
325
	 */
326
	protected function _sum_items_and_taxes( EE_Transaction  $transaction){
327
		$total_line_item = $transaction->total_line_item();
328
		$total = 0;
329
		foreach($total_line_item->get_items() as $item_line_item ){
330
			$total += max( $item_line_item->total(), 0 );
331
		}
332
		foreach($total_line_item->tax_descendants() as $tax_line_item ){
333
			$total += max( $tax_line_item->total(), 0 );
334
		}
335
		return $total;
336
	}
337
338
	/**
339
	 * Determines whether or not we can easily itemize the transaction using only
340
	 * items and taxes (ie, no promotions or surcharges or cancellations needed)
341
	 * @param EEI_Payment $payment
342
	 * @return boolean
343
	 */
344
	protected function _can_easily_itemize_transaction_for( EEI_Payment $payment ){
345
		return  $this->_money->compare_floats(
346
					$this->_sum_items_and_taxes( $payment->transaction() ),
347
					$payment->transaction()->total() ) &&
348
				$this->_money->compare_floats(
349
					$payment->amount(),
350
					$payment->transaction()->total() );
351
	}
352
353
	/**
354
	 * Handles updating the transaction and any other related data based on the payment.
355
	 * You may be tempted to do this as part of do_direct_payment or handle_payment_update,
356
	 * but doing so on those functions might be too early. It's possible that the changes
357
	 * you make to teh transaction or registration or line items may just get overwritten
358
	 * at that point. Instead, you should store any info you need on the payment during those
359
	 * functions, and use that information at this step, which client code will decide
360
	 * for you when it should be called.
361
	 * @param EE_Payment $payment
362
	 * @return void
363
	 */
364
	public function update_txn_based_on_payment( $payment ){
365
		//maybe update the transaction or line items or registrations
366
		//but most gateways don't need to do this, because they only update the payment
367
	}
368
	
369
	/**
370
	 * Gets the first event for this payment (it's possible that it could be for multiple)
371
	 * @param EEI_Payment $payment
372
	 * @return EEI_Event|null
373
     * @deprecated since 4.9.31 instead use EEI_Payment::get_first_event()
374
	 */
375
	protected function _get_first_event_for_payment( EEI_Payment $payment ) {
376
		return $payment->get_first_event();
377
	}
378
	
379
	/**
380
	 * Gets the name of the first event for which is being paid
381
	 * @param EEI_Payment $payment
382
	 * @return string
383
     * @deprecated since 4.9.31 instead use EEI_Payment::get_first_event_name()
384
	 */
385
	protected function _get_first_event_name_for_payment( EEI_Payment $payment ) {
386
		return $payment->get_first_event_name();
387
	}
388
	/**
389
	 * Gets the text to use for a gateway's line item name when this is a partial payment
390
     * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatPartialPaymentLineItemName($payment)
391
	 * @param EE_Payment $payment
392
	 * @return string
393
	 */
394
	protected function _format_partial_payment_line_item_name( EEI_Payment $payment ){
395
		return $this->_get_gateway_formatter()->formatPartialPaymentLineItemName($payment);
396
	}
397
	/**
398
	 * Gets the text to use for a gateway's line item description when this is a partial payment
399
     * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatPartialPaymentLineItemDesc()
400
	 * @param EEI_Payment $payment
401
	 * @return string
402
	 */
403
	protected function _format_partial_payment_line_item_desc( EEI_Payment $payment ) {
404
		return $this->_get_gateway_formatter()->formatPartialPaymentLineItemDesc($payment);
405
	}
406
	
407
	/**
408
     * Gets the name to use for a line item when sending line items to the gateway
409
     * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatLineItemName($line_item,$payment)
410
     * @param EEI_Line_Item $line_item
411
	 * @param EEI_Payment $payment
412
	 * @return string
413
	 */
414
	protected function _format_line_item_name( EEI_Line_Item $line_item, EEI_Payment $payment ) {
415
		return $this->_get_gateway_formatter()->formatLineItemName($line_item,$payment);
416
	}
417
	
418
	/**
419
	 * Gets the description to use for a line item when sending line items to the gateway
420
     * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatLineItemDesc($line_item, $payment))
421
	 * @param EEI_Line_Item $line_item
422
	 * @param EEI_Payment $payment
423
	 * @return string
424
	 */
425
	protected function _format_line_item_desc( EEI_Line_Item $line_item, EEI_Payment $payment ) {
426
		return $this->_get_gateway_formatter()->formatLineItemDesc($line_item, $payment);
427
	}
428
	
429
	/**
430
	 * Gets the order description that should generlly be sent to gateways
431
     * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatOrderDescription($payment)
432
	 * @param EEI_Payment $payment
433
	 * @return type
434
	 */
435
	protected function _format_order_description( EEI_Payment $payment ) {
436
		return $this->_get_gateway_formatter()->formatOrderDescription($payment);
437
	}
438
}