Passed
Push — master ( 4cac9e...052e2f )
by Chris
02:15
created

class-edu-klarnacheckout.php (12 issues)

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
defined( 'ABSPATH' ) || die( 'This plugin must be run within the scope of WordPress.' );
3
4
if ( ! class_exists( 'EDU_KlarnaCheckout' ) ) {
5
	class EDU_KlarnaCheckout extends EDU_Integration {
0 ignored issues
show
The type EDU_Integration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
		public function __construct() {
7
			$this->id          = 'eduadmin-klarnacheckout';
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
8
			$this->displayName = __( 'Klarna Checkout', 'eduadmin-wp-klarna-checkout' );
0 ignored issues
show
Bug Best Practice introduced by
The property displayName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
9
			$this->description = '';
0 ignored issues
show
Bug Best Practice introduced by
The property description does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
10
11
			$this->init_form_fields();
12
			$this->init_settings();
13
14
			add_action( 'eduadmin-checkpaymentplugins', array( $this, 'intercept_booking' ) );
15
			add_action( 'eduadmin-processbooking', array( $this, 'process_booking' ) );
16
			add_action( 'wp_loaded', array( $this, 'process_klarnaresponse' ) );
17
18
			add_shortcode( 'eduadmin-klarna-testpage', array( $this, 'test_page' ) );
19
		}
20
21
		/**
22
		 * @param int $booking_id
23
		 */
24
		public function test_page( $attributes ) {
25
			$attributes = shortcode_atts(
26
				array(
27
					'bookingid' => 0,
28
				),
29
				normalize_empty_atts( $attributes ),
0 ignored issues
show
The function normalize_empty_atts was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
				/** @scrutinizer ignore-call */ 
30
    normalize_empty_atts( $attributes ),
Loading history...
30
				'test_page'
31
			);
32
33
			$event_booking = EDUAPI()->OData->Bookings->GetItem(
0 ignored issues
show
The function EDUAPI was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
			$event_booking = /** @scrutinizer ignore-call */ EDUAPI()->OData->Bookings->GetItem(
Loading history...
34
				intval( $attributes['bookingid'] ),
35
				null,
36
				'Customer($select=CustomerId;),ContactPerson($select=PersonId;)'
37
			);
38
			$_customer     = EDUAPI()->OData->Customers->GetItem( $event_booking['Customer']['CustomerId'] );
39
			$_contact      = EDUAPI()->OData->Persons->GetItem( $event_booking['ContactPerson']['PersonId'] );
40
41
			$ebi = new EduAdmin_BookingInfo( $event_booking, $_customer, $_contact );
0 ignored issues
show
The type EduAdmin_BookingInfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
43
			do_action( 'eduadmin-processbooking', $ebi );
44
		}
45
46
		/**
47
		 * @param EduAdmin_BookingInfo|null $ebi
48
		 */
49
		public function intercept_booking( $ebi = null ) {
50
			if ( 'no' === $this->get_option( 'enabled', 'no' ) ) {
51
				return;
52
			}
53
54
			if ( ! empty( $_POST['act'] ) && 'bookCourse' === $_POST['act'] ) {
55
				$ebi->NoRedirect = true;
56
			}
57
		}
58
59
		/**
60
		 * @param EduAdmin_BookingInfo|null $ebi
61
		 */
62
		public function process_booking( $ebi = null ) {
63
			if ( 'no' === $this->get_option( 'enabled', 'no' ) ) {
64
				return;
65
			}
66
67
			$ebi->NoRedirect = true;
68
69
			$checkout = $this->create_checkout( $ebi );
0 ignored issues
show
The assignment to $checkout is dead and can be removed.
Loading history...
70
			include_once 'checkout-page.php';
71
		}
72
73
		public function process_klarnaresponse() {
74
			if ( 'no' === $this->get_option( 'enabled', 'no' ) ) {
75
				return;
76
			}
77
		}
78
79
		public function init_form_fields() {
80
			$this->setting_fields = array(
0 ignored issues
show
Bug Best Practice introduced by
The property setting_fields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
				'enabled'  => array(
82
					'title'       => __( 'Enabled', 'edauadmin-wp-klarna-checkout' ),
83
					'type'        => 'checkbox',
84
					'description' => __( 'Enables/Disabled the integration with Klarna Checkout', 'eduadmin-wp-klarna-checkout' ),
85
					'default'     => 'no',
86
				),
87
				'username' => array(
88
					'title'       => __( 'Username (UID)', 'eduadmin-wp-klarna-checkout' ),
89
					'type'        => 'text',
90
					'description' => __( 'The API credential username from Klarna', 'eduadmin-wp-klarna-checkout' ),
91
					'default'     => '',
92
				),
93
				'password' => array(
94
					'title'       => __( 'Password', 'eduadmin-wp-klarna-checkout' ),
95
					'type'        => 'password',
96
					'description' => __( 'Password for the API credentials from Klarna', 'eduadmin-wp-klarna-checkout' ),
97
					'default'     => '',
98
				),
99
				'termsurl' => array(
100
					'title'       => __( 'Terms and Conditions URL', 'eduadmin-wp-klarna-checkout' ),
101
					'type'        => 'text',
102
					'description' => __( 'This URL is required for Klarna Checkout', 'eduadmin-wp-klarna-checkout' ),
103
					'default'     => '',
104
				),
105
			);
106
		}
107
108
		/**
109
		 * @param EduAdmin_BookingInfo|null $ebi
110
		 *
111
		 * @return Klarna_Checkout_Order
112
		 */
113
		public function create_checkout( $ebi = null ) {
114
			$create                  = array();
115
			$create['locale']        = strtolower( str_replace( '_', '-', get_locale() ) );
116
			$create['cart']          = array();
117
			$create['cart']['items'] = array();
118
119
			try {
120
				$connector = Klarna_Checkout_Connector::create(
121
					'',
122
					Klarna_Checkout_Connector::BASE_TEST_URL
123
				);
124
125
				$order = new Klarna_Checkout_Order( $connector );
126
				$order->create( $create );
127
128
				return $order;
129
			} catch ( Klarna_Checkout_ApiErrorException $ex ) {
130
				EDU()->write_debug( $ex->getMessage() );
0 ignored issues
show
The function EDU was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
				/** @scrutinizer ignore-call */ 
131
    EDU()->write_debug( $ex->getMessage() );
Loading history...
131
				EDU()->write_debug( $ex->getPayload() );
132
				EDU()->write_debug( $create );
133
			}
134
		}
135
	}
136
}
137