Total Complexity | 93 |
Total Lines | 722 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Plugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Plugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Plugin { |
||
30 | /** |
||
31 | * Version. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $version = '5.0.0'; |
||
36 | |||
37 | /** |
||
38 | * The root file of this WordPress plugin |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | public static $file; |
||
43 | |||
44 | /** |
||
45 | * The plugin dirname |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | public static $dirname; |
||
50 | |||
51 | /** |
||
52 | * The timezone |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | const TIMEZONE = 'UTC'; |
||
57 | |||
58 | /** |
||
59 | * Instance. |
||
60 | * |
||
61 | * @var Plugin |
||
62 | */ |
||
63 | protected static $instance = null; |
||
64 | |||
65 | /** |
||
66 | * Instance. |
||
67 | * |
||
68 | * @param string $file The plugin file. |
||
69 | */ |
||
70 | public static function instance( $file = null ) { |
||
71 | if ( is_null( self::$instance ) ) { |
||
72 | self::$instance = new self(); |
||
73 | |||
74 | // Backward compatibility. |
||
75 | self::$file = $file; |
||
76 | self::$dirname = dirname( $file ); |
||
77 | } |
||
78 | |||
79 | return self::$instance; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Construct and initialize an Pronamic Pay plugin object |
||
84 | */ |
||
85 | public function __construct() { |
||
86 | // Bootstrap the add-ons. |
||
87 | Extensions\Charitable\Extension::bootstrap(); |
||
88 | Extensions\Give\Extension::bootstrap(); |
||
89 | Extensions\WooCommerce\Extension::bootstrap(); |
||
90 | Extensions\GravityForms\Extension::bootstrap(); |
||
91 | Extensions\Shopp\Extension::bootstrap(); |
||
92 | Extensions\Jigoshop\Extension::bootstrap(); |
||
93 | Extensions\WPeCommerce\Extension::bootstrap(); |
||
94 | Extensions\ClassiPress\Extension::bootstrap(); |
||
95 | Extensions\EventEspressoLegacy\Extension::bootstrap(); |
||
96 | Extensions\EventEspresso\Extension::bootstrap(); |
||
97 | Extensions\AppThemes\Extension::bootstrap(); |
||
98 | Extensions\S2Member\Extension::bootstrap(); |
||
99 | Extensions\Membership\Extension::bootstrap(); |
||
100 | Extensions\EasyDigitalDownloads\Extension::bootstrap(); |
||
101 | Extensions\IThemesExchange\Extension::bootstrap(); |
||
102 | Extensions\MemberPress\Extension::bootstrap(); |
||
103 | Extensions\FormidableForms\Extension::bootstrap(); |
||
104 | Extensions\RestrictContentPro\Extension::bootstrap(); |
||
105 | |||
106 | // Settings. |
||
107 | $this->settings = new Settings( $this ); |
||
|
|||
108 | |||
109 | // Data Stores. |
||
110 | $this->payments_data_store = new Payments\PaymentsDataStoreCPT(); |
||
111 | $this->subscriptions_data_store = new Subscriptions\SubscriptionsDataStoreCPT(); |
||
112 | |||
113 | // Post Types. |
||
114 | $this->gateway_post_type = new GatewayPostType(); |
||
115 | $this->payment_post_type = new PaymentPostType(); |
||
116 | $this->subscription_post_type = new SubscriptionPostType(); |
||
117 | |||
118 | // License Manager. |
||
119 | $this->license_manager = new LicenseManager( $this ); |
||
120 | |||
121 | // Modules. |
||
122 | $this->forms_module = new Forms\FormsModule( $this ); |
||
123 | $this->payments_module = new Payments\PaymentsModule( $this ); |
||
124 | $this->subscriptions_module = new Subscriptions\SubscriptionsModule( $this ); |
||
125 | |||
126 | // Google Analytics Ecommerce. |
||
127 | $this->google_analytics_ecommerce = new GoogleAnalyticsEcommerce(); |
||
128 | |||
129 | // Admin. |
||
130 | if ( is_admin() ) { |
||
131 | $this->admin = new Admin\AdminModule( $this ); |
||
132 | } |
||
133 | |||
134 | /* |
||
135 | * Plugins loaded. |
||
136 | * |
||
137 | * Priority should be at least lower then 8 to support the "WP eCommerce" plugin. |
||
138 | * |
||
139 | * new WP_eCommerce() |
||
140 | * add_action( 'plugins_loaded' , array( $this, 'init' ), 8 ); |
||
141 | * $this->load(); |
||
142 | * wpsc_core_load_gateways(); |
||
143 | * |
||
144 | * @see https://github.com/wp-e-commerce/WP-e-Commerce/blob/branch-3.11.2/wp-shopping-cart.php#L342-L343 |
||
145 | * @see https://github.com/wp-e-commerce/WP-e-Commerce/blob/branch-3.11.2/wp-shopping-cart.php#L26-L35 |
||
146 | * @see https://github.com/wp-e-commerce/WP-e-Commerce/blob/branch-3.11.2/wp-shopping-cart.php#L54 |
||
147 | * @see https://github.com/wp-e-commerce/WP-e-Commerce/blob/branch-3.11.2/wp-shopping-cart.php#L296-L297 |
||
148 | */ |
||
149 | add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 5 ); |
||
150 | |||
151 | // Plugin locale. |
||
152 | add_filter( 'plugin_locale', array( $this, 'plugin_locale' ), 10, 2 ); |
||
153 | |||
154 | // If WordPress is loaded check on returns and maybe redirect requests. |
||
155 | add_action( 'wp_loaded', array( $this, 'handle_returns' ) ); |
||
156 | add_action( 'wp_loaded', array( $this, 'maybe_redirect' ) ); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get the version number of this plugin. |
||
161 | * |
||
162 | * @return string The version number of this plugin. |
||
163 | */ |
||
164 | public function get_version() { |
||
165 | return $this->version; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get plugin file path. |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function get_file() { |
||
174 | return self::$file; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get the plugin dir path. |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function get_plugin_dir_path() { |
||
183 | return plugin_dir_path( $this->get_file() ); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Update payment. |
||
188 | * |
||
189 | * @param Payment $payment The payment to update. |
||
190 | * @param bool $can_redirect Flag to indicate if redirect is allowed after the payment update. |
||
191 | */ |
||
192 | public static function update_payment( $payment = null, $can_redirect = true ) { |
||
193 | if ( empty( $payment ) ) { |
||
194 | return; |
||
195 | } |
||
196 | |||
197 | $gateway = Plugin::get_gateway( $payment->config_id ); |
||
198 | |||
199 | if ( empty( $gateway ) ) { |
||
200 | return; |
||
201 | } |
||
202 | |||
203 | $amount = $payment->get_amount(); |
||
204 | |||
205 | if ( empty( $amount ) ) { |
||
206 | $payment->set_status( Core\Statuses::SUCCESS ); |
||
207 | } else { |
||
208 | $gateway->update_status( $payment ); |
||
209 | |||
210 | if ( $gateway->has_error() ) { |
||
211 | foreach ( $gateway->error->get_error_codes() as $code ) { |
||
212 | $payment->add_note( sprintf( '%s: %s', $code, $gateway->error->get_error_message( $code ) ) ); |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | // Update payment in data store. |
||
218 | $payment->save(); |
||
219 | |||
220 | // Maybe redirect. |
||
221 | if ( defined( 'DOING_CRON' ) && ( empty( $payment->status ) || Statuses::OPEN === $payment->status ) ) { |
||
222 | $can_redirect = false; |
||
223 | } |
||
224 | |||
225 | if ( $can_redirect ) { |
||
226 | $url = $payment->get_return_redirect_url(); |
||
227 | |||
228 | wp_redirect( $url ); |
||
229 | |||
230 | exit; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Handle returns. |
||
236 | */ |
||
237 | public function handle_returns() { |
||
238 | if ( ! filter_has_var( INPUT_GET, 'payment' ) ) { |
||
239 | return; |
||
240 | } |
||
241 | |||
242 | $payment_id = filter_input( INPUT_GET, 'payment', FILTER_SANITIZE_NUMBER_INT ); |
||
243 | |||
244 | $payment = get_pronamic_payment( $payment_id ); |
||
245 | |||
246 | // Check if payment key is valid. |
||
247 | $valid_key = false; |
||
248 | |||
249 | if ( empty( $payment->key ) ) { |
||
250 | $valid_key = true; |
||
251 | } elseif ( filter_has_var( INPUT_GET, 'key' ) ) { |
||
252 | $key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_STRING ); |
||
253 | |||
254 | $valid_key = ( $key === $payment->key ); |
||
255 | } |
||
256 | |||
257 | if ( ! $valid_key ) { |
||
258 | wp_redirect( home_url() ); |
||
259 | |||
260 | exit; |
||
261 | } |
||
262 | |||
263 | // Check if we should redirect. |
||
264 | $should_redirect = true; |
||
265 | |||
266 | // Check if the request is an callback request. |
||
267 | // Sisow gatway will extend callback requests with querystring "callback=true". |
||
268 | if ( filter_has_var( INPUT_GET, 'callback' ) && filter_input( INPUT_GET, 'callback', FILTER_VALIDATE_BOOLEAN ) ) { |
||
269 | $should_redirect = false; |
||
270 | } |
||
271 | |||
272 | // Check if the request is an notify request. |
||
273 | // Sisow gatway will extend callback requests with querystring "notify=true". |
||
274 | if ( filter_has_var( INPUT_GET, 'notify' ) && filter_input( INPUT_GET, 'notify', FILTER_VALIDATE_BOOLEAN ) ) { |
||
275 | $should_redirect = false; |
||
276 | } |
||
277 | |||
278 | self::update_payment( $payment, $should_redirect ); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Maybe redirect. |
||
283 | */ |
||
284 | public function maybe_redirect() { |
||
285 | if ( ! filter_has_var( INPUT_GET, 'payment_redirect' ) ) { |
||
286 | return; |
||
287 | } |
||
288 | |||
289 | $payment_id = filter_input( INPUT_GET, 'payment_redirect', FILTER_SANITIZE_NUMBER_INT ); |
||
290 | |||
291 | $payment = get_pronamic_payment( $payment_id ); |
||
292 | |||
293 | // HTML Answer. |
||
294 | $html_answer = $payment->get_meta( 'ogone_directlink_html_answer' ); |
||
295 | |||
296 | if ( ! empty( $html_answer ) ) { |
||
297 | echo $html_answer; // WPCS: XSS ok. |
||
298 | |||
299 | exit; |
||
300 | } |
||
301 | |||
302 | $redirect_message = $payment->get_meta( 'payment_redirect_message' ); |
||
303 | |||
304 | if ( ! empty( $redirect_message ) ) { |
||
305 | $key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_STRING ); |
||
306 | |||
307 | if ( $key !== $payment->key ) { |
||
308 | wp_redirect( home_url() ); |
||
309 | |||
310 | exit; |
||
311 | } |
||
312 | |||
313 | // @see https://github.com/woothemes/woocommerce/blob/2.3.11/includes/class-wc-cache-helper.php |
||
314 | // @see https://www.w3-edge.com/products/w3-total-cache/ |
||
315 | if ( ! defined( 'DONOTCACHEPAGE' ) ) { |
||
316 | define( 'DONOTCACHEPAGE', true ); |
||
317 | } |
||
318 | |||
319 | if ( ! defined( 'DONOTCACHEDB' ) ) { |
||
320 | define( 'DONOTCACHEDB', true ); |
||
321 | } |
||
322 | |||
323 | if ( ! defined( 'DONOTMINIFY' ) ) { |
||
324 | define( 'DONOTMINIFY', true ); |
||
325 | } |
||
326 | |||
327 | if ( ! defined( 'DONOTCDN' ) ) { |
||
328 | define( 'DONOTCDN', true ); |
||
329 | } |
||
330 | |||
331 | if ( ! defined( 'DONOTCACHEOBJECT' ) ) { |
||
332 | define( 'DONOTCACHEOBJECT', true ); |
||
333 | } |
||
334 | |||
335 | nocache_headers(); |
||
336 | |||
337 | include Plugin::$dirname . '/views/redirect-message.php'; |
||
338 | |||
339 | exit; |
||
340 | } |
||
341 | |||
342 | $gateway = Plugin::get_gateway( $payment->config_id ); |
||
343 | |||
344 | if ( $gateway && $gateway->is_html_form() ) { |
||
345 | $gateway->start( $payment ); |
||
346 | |||
347 | $error = $gateway->get_error(); |
||
348 | |||
349 | if ( is_wp_error( $error ) ) { |
||
350 | Plugin::render_errors( $error ); |
||
351 | } else { |
||
352 | $gateway->redirect( $payment ); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | if ( ! empty( $payment->action_url ) ) { |
||
357 | wp_redirect( $payment->action_url ); |
||
358 | |||
359 | exit; |
||
360 | } |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Get number payments. |
||
365 | * |
||
366 | * @return int |
||
367 | */ |
||
368 | public static function get_number_payments() { |
||
369 | $number = false; |
||
370 | |||
371 | $count = wp_count_posts( 'pronamic_payment' ); |
||
372 | |||
373 | if ( isset( $count, $count->payment_completed ) ) { |
||
374 | $number = intval( $count->payment_completed ); |
||
375 | } |
||
376 | |||
377 | return $number; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Setup, creates or updates database tables. Will only run when version changes. |
||
382 | */ |
||
383 | public function plugins_loaded() { |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Filter plugin locale. |
||
400 | * |
||
401 | * @param string $locale A WordPress locale identifier. |
||
402 | * @param string $domain A WordPress text domain indentifier. |
||
403 | * |
||
404 | * @return string |
||
405 | */ |
||
406 | public function plugin_locale( $locale, $domain ) { |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Get default error message. |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | public static function get_default_error_message() { |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Get config select options. |
||
433 | * |
||
434 | * @param null|string $payment_method The gateway configuration options for the specified payment method. |
||
435 | * |
||
436 | * @return array |
||
437 | */ |
||
438 | public static function get_config_select_options( $payment_method = null ) { |
||
439 | $args = array( |
||
440 | 'post_type' => 'pronamic_gateway', |
||
441 | 'nopaging' => true, |
||
442 | ); |
||
443 | |||
444 | if ( $payment_method ) { |
||
445 | $args['post__in'] = PaymentMethods::get_config_ids( $payment_method ); |
||
446 | } |
||
447 | |||
448 | $query = new WP_Query( $args ); |
||
449 | |||
450 | $options = array( __( '— Select Configuration —', 'pronamic_ideal' ) ); |
||
451 | |||
452 | foreach ( $query->posts as $post ) { |
||
453 | $id = $post->ID; |
||
454 | |||
455 | $options[ $id ] = sprintf( |
||
456 | '%s (%s)', |
||
457 | get_the_title( $id ), |
||
458 | get_post_meta( $id, '_pronamic_gateway_mode', true ) |
||
459 | ); |
||
460 | } |
||
461 | |||
462 | return $options; |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Maybe set active payment methods option. |
||
467 | * |
||
468 | * @since unreleased |
||
469 | */ |
||
470 | public static function maybe_set_active_payment_methods() { |
||
471 | $active_methods = get_option( 'pronamic_pay_active_payment_methods' ); |
||
472 | |||
473 | if ( is_array( $active_methods ) ) { |
||
474 | return; |
||
475 | } |
||
476 | |||
477 | PaymentMethods::update_active_payment_methods(); |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Render errors. |
||
482 | * |
||
483 | * @param array $errors An array with errors to render. |
||
484 | */ |
||
485 | public static function render_errors( $errors = array() ) { |
||
486 | if ( ! is_array( $errors ) ) { |
||
487 | $errors = array( $errors ); |
||
488 | } |
||
489 | |||
490 | foreach ( $errors as $error ) { |
||
491 | include Plugin::$dirname . '/views/error.php'; |
||
492 | } |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Get gateway. |
||
497 | * |
||
498 | * @param string|integer|boolean $config_id A gateway configuration ID. |
||
499 | * |
||
500 | * @return Gateway|null |
||
501 | */ |
||
502 | public static function get_gateway( $config_id ) { |
||
503 | if ( empty( $config_id ) ) { |
||
504 | return null; |
||
505 | } |
||
506 | |||
507 | $gateway_id = get_post_meta( $config_id, '_pronamic_gateway_id', true ); |
||
508 | $mode = get_post_meta( $config_id, '_pronamic_gateway_mode', true ); |
||
509 | $is_utf8 = strcasecmp( get_bloginfo( 'charset' ), 'UTF-8' ) === 0; |
||
510 | |||
511 | $config = Core\ConfigProvider::get_config( $gateway_id, $config_id ); |
||
512 | |||
513 | switch ( $gateway_id ) { |
||
514 | case 'abnamro-ideal-easy': |
||
515 | case 'abnamro-ideal-only-kassa': |
||
516 | case 'abnamro-internetkassa': |
||
517 | $config->form_action_url = sprintf( |
||
518 | 'https://internetkassa.abnamro.nl/ncol/%s/orderstandard%s.asp', |
||
519 | 'test' === $mode ? 'test' : 'prod', |
||
520 | $is_utf8 ? '_utf8' : '' |
||
521 | ); |
||
522 | |||
523 | break; |
||
524 | case 'abnamro-ideal-zelfbouw-v3': |
||
525 | $config->payment_server_url = 'https://abnamro.ideal-payment.de/ideal/iDEALv3'; |
||
526 | |||
527 | if ( 'test' === $mode ) { |
||
528 | $config->payment_server_url = 'https://abnamro-test.ideal-payment.de/ideal/iDEALv3'; |
||
529 | } |
||
530 | |||
531 | $config->certificates = array(); |
||
532 | |||
533 | break; |
||
534 | case 'deutschebank-ideal-expert-v3': |
||
535 | $config->payment_server_url = 'https://myideal.db.com/ideal/iDealv3'; |
||
536 | |||
537 | $config->certificates = array(); |
||
538 | |||
539 | break; |
||
540 | case 'ideal-simulator-ideal-basic': |
||
541 | $config->url = 'https://www.ideal-simulator.nl/lite/'; |
||
542 | |||
543 | break; |
||
544 | case 'ideal-simulator-ideal-advanced-v3': |
||
545 | $config->payment_server_url = 'https://www.ideal-checkout.nl/simulator/'; |
||
546 | |||
547 | $config->certificates = array(); |
||
548 | |||
549 | break; |
||
550 | case 'ing-ideal-basic': |
||
551 | $config->url = 'https://ideal.secure-ing.com/ideal/mpiPayInitIng.do'; |
||
552 | |||
553 | if ( 'test' === $mode ) { |
||
554 | $config->url = 'https://idealtest.secure-ing.com/ideal/mpiPayInitIng.do'; |
||
555 | } |
||
556 | |||
557 | break; |
||
558 | case 'ing-ideal-advanced-v3': |
||
559 | $config->payment_server_url = 'https://ideal.secure-ing.com/ideal/iDEALv3'; |
||
560 | |||
561 | if ( 'test' === $mode ) { |
||
562 | $config->payment_server_url = 'https://idealtest.secure-ing.com/ideal/iDEALv3'; |
||
563 | } |
||
564 | |||
565 | $config->certificates = array(); |
||
566 | |||
567 | break; |
||
568 | case 'mollie-ideal-basic': |
||
569 | $config->url = 'https://secure.mollie.nl/xml/idealAcquirer/lite/'; |
||
570 | |||
571 | if ( 'test' === $mode ) { |
||
572 | $config->url = 'https://secure.mollie.nl/xml/idealAcquirer/testmode/lite/'; |
||
573 | } |
||
574 | |||
575 | break; |
||
576 | case 'postcode-ideal': |
||
577 | $config->payment_server_url = 'https://ideal.postcode.nl/ideal'; |
||
578 | |||
579 | if ( 'test' === $mode ) { |
||
580 | $config->payment_server_url = 'https://ideal-test.postcode.nl/ideal'; |
||
581 | } |
||
582 | |||
583 | $config->certificates = array(); |
||
584 | |||
585 | break; |
||
586 | case 'rabobank-ideal-professional-v3': |
||
587 | $config->payment_server_url = 'https://ideal.rabobank.nl/ideal/iDEALv3'; |
||
588 | |||
589 | if ( 'test' === $mode ) { |
||
590 | $config->payment_server_url = 'https://idealtest.rabobank.nl/ideal/iDEALv3'; |
||
591 | } |
||
592 | |||
593 | $config->certificates = array(); |
||
594 | |||
595 | break; |
||
596 | case 'sisow-ideal-basic': |
||
597 | $config->url = 'https://www.sisow.nl/Sisow/iDeal/IssuerHandler.ashx'; |
||
598 | |||
599 | if ( 'test' === $mode ) { |
||
600 | $config->url = 'https://www.sisow.nl/Sisow/iDeal/IssuerHandler.ashx/test'; |
||
601 | } |
||
602 | |||
603 | break; |
||
604 | } |
||
605 | |||
606 | $gateway = Core\GatewayFactory::create( $config ); |
||
607 | |||
608 | return $gateway; |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * Start a payment. |
||
613 | * |
||
614 | * @param string $config_id A gateway configuration ID. |
||
615 | * @param Gateway $gateway The gateway to start the payment at. |
||
616 | * @param PaymentDataInterface $data A payment data interface object with all the required payment info. |
||
617 | * @param string|null $payment_method The payment method to use to start the payment. |
||
618 | * |
||
619 | * @return Payment |
||
620 | */ |
||
621 | public static function start( $config_id, Gateway $gateway, PaymentDataInterface $data, $payment_method = null ) { |
||
622 | $payment = new Payments\Payment(); |
||
623 | |||
624 | $payment->title = sprintf( __( 'Payment for %s', 'pronamic_ideal' ), $data->get_title() ); |
||
625 | $payment->user_id = $data->get_user_id(); |
||
626 | $payment->config_id = $config_id; |
||
627 | $payment->key = uniqid( 'pay_' ); |
||
628 | $payment->order_id = $data->get_order_id(); |
||
629 | $payment->currency = $data->get_currency(); |
||
630 | $payment->amount = $data->get_amount(); |
||
631 | $payment->language = $data->get_language(); |
||
632 | $payment->locale = $data->get_language_and_country(); |
||
633 | $payment->entrance_code = $data->get_entrance_code(); |
||
634 | $payment->description = $data->get_description(); |
||
635 | $payment->source = $data->get_source(); |
||
636 | $payment->source_id = $data->get_source_id(); |
||
637 | $payment->email = $data->get_email(); |
||
638 | $payment->status = null; |
||
639 | $payment->method = $payment_method; |
||
640 | $payment->issuer = $data->get_issuer( $payment_method ); |
||
641 | $payment->first_name = $data->get_first_name(); |
||
642 | $payment->last_name = $data->get_last_name(); |
||
643 | $payment->customer_name = $data->get_customer_name(); |
||
644 | $payment->address = $data->get_address(); |
||
645 | $payment->zip = $data->get_zip(); |
||
646 | $payment->city = $data->get_city(); |
||
647 | $payment->country = $data->get_country(); |
||
648 | $payment->telephone_number = $data->get_telephone_number(); |
||
649 | $payment->analytics_client_id = $data->get_analytics_client_id(); |
||
650 | $payment->recurring = $data->get_recurring(); |
||
651 | $payment->subscription = $data->get_subscription(); |
||
652 | $payment->subscription_id = $data->get_subscription_id(); |
||
653 | $payment->set_credit_card( $data->get_credit_card() ); |
||
654 | |||
655 | // User Agent (@see https://github.com/WordPress/WordPress/blob/4.9.4/wp-includes/comment.php#L1962-L1965). |
||
656 | $payment->user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null; |
||
657 | |||
658 | // IP (@see https://github.com/WordPress/WordPress/blob/4.9.4/wp-includes/comment.php#L1957-L1960). |
||
659 | $payment->user_ip = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null; |
||
660 | |||
661 | return self::start_payment( $payment, $gateway ); |
||
662 | } |
||
663 | |||
664 | /** |
||
665 | * Start payment. |
||
666 | * |
||
667 | * @param Payment $payment The payment to start at the specified gateway. |
||
668 | * @param Gateway $gateway The gateway to start the payment at. |
||
669 | * |
||
670 | * @return Payment |
||
671 | */ |
||
672 | public static function start_payment( Payment $payment, Gateway $gateway ) { |
||
673 | global $pronamic_ideal; |
||
674 | |||
675 | $pronamic_ideal->payments_data_store->create( $payment ); |
||
676 | |||
677 | // Prevent payment start at gateway if amount is empty. |
||
678 | $amount = $payment->get_amount(); |
||
679 | |||
680 | if ( empty( $amount ) ) { |
||
681 | // Keep track of free payments to update during shutdown. |
||
682 | pronamic_pay_plugin()->payments_module->free[] = $payment->get_id(); |
||
683 | |||
684 | return $payment; |
||
685 | } |
||
686 | |||
687 | // Start payment at the gateway. |
||
688 | $result = $gateway->start( $payment ); |
||
689 | |||
690 | // If result is false the payment failed to start. |
||
691 | if ( false === $result ) { |
||
692 | // If payment failed to start we directly update the payment status to 'failure'. |
||
693 | $payment->set_status( Core\Statuses::FAILURE ); |
||
694 | |||
695 | // Check if there is a subscription attached to the payment. |
||
696 | $subscription = $payment->get_subscription(); |
||
697 | |||
698 | if ( $subscription ) { |
||
699 | if ( ! $payment->get_recurring() ) { |
||
700 | // First payment. |
||
701 | // Cancel subscription to prevent unwanted recurring payments in the future, |
||
702 | // when a valid customer ID might be set for the user. |
||
703 | $subscription->set_status( Core\Statuses::CANCELLED ); |
||
704 | } else { |
||
705 | $subscription->set_status( Core\Statuses::FAILURE ); |
||
706 | } |
||
707 | |||
708 | $subscription->save(); |
||
709 | } |
||
710 | } |
||
711 | |||
712 | // Check if the gateway has an error. |
||
713 | if ( $gateway->has_error() ) { |
||
714 | foreach ( $gateway->error->get_error_codes() as $code ) { |
||
715 | $payment->add_note( sprintf( '%s: %s', $code, $gateway->error->get_error_message( $code ) ) ); |
||
716 | } |
||
717 | } |
||
718 | |||
719 | $payment->save(); |
||
720 | |||
721 | if ( $gateway->supports( 'payment_status_request' ) ) { |
||
722 | Payments\StatusChecker::schedule_event( $payment ); |
||
723 | } |
||
724 | |||
725 | return $payment; |
||
726 | } |
||
727 | |||
728 | /** |
||
729 | * Get pages. |
||
730 | * |
||
731 | * @return array |
||
732 | */ |
||
733 | public function get_pages() { |
||
751 | } |
||
752 | } |
||
753 |