1
|
|
|
<?php |
|
|
|
|
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 { |
|
|
|
|
6
|
|
|
public function __construct() { |
7
|
|
|
$this->id = 'eduadmin-klarnacheckout'; |
|
|
|
|
8
|
|
|
$this->displayName = __( 'Klarna Checkout', 'eduadmin-wp-klarna-checkout' ); |
|
|
|
|
9
|
|
|
$this->description = ''; |
|
|
|
|
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 ), |
|
|
|
|
30
|
|
|
'test_page' |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$event_booking = EDUAPI()->OData->Bookings->GetItem( |
|
|
|
|
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 ); |
|
|
|
|
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 ); |
|
|
|
|
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( |
|
|
|
|
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() ); |
|
|
|
|
131
|
|
|
EDU()->write_debug( $ex->getPayload() ); |
132
|
|
|
EDU()->write_debug( $create ); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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.