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( 'eduadmin-bookingcompleted', array( $this, 'process_klarnaresponse' ) ); |
||
17 | add_action( 'wp_loaded', array( $this, 'process_paymentstatus' ) ); |
||
18 | |||
19 | add_shortcode( 'eduadmin-klarna-testpage', array( $this, 'test_page' ) ); |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * @param $attributes |
||
24 | */ |
||
25 | public function test_page( $attributes ) { |
||
26 | $attributes = shortcode_atts( |
||
27 | array( |
||
28 | 'bookingid' => 0, |
||
29 | 'programmebookingid' => 0, |
||
30 | ), |
||
31 | normalize_empty_atts( $attributes ), |
||
32 | 'test_page' |
||
33 | ); |
||
34 | |||
35 | if ( $attributes['bookingid'] > 0 ) { |
||
36 | $event_booking = EDUAPI()->OData->Bookings->GetItem( |
||
37 | $attributes['bookingid'], |
||
38 | null, |
||
39 | 'Customer($select=CustomerId;),ContactPerson($select=PersonId;),OrderRows', |
||
40 | false |
||
41 | ); |
||
42 | } elseif ( $attributes['programmebookingid'] > 0 ) { |
||
43 | $event_booking = EDUAPI()->OData->ProgrammeBookings->GetItem( |
||
44 | $attributes['programmebookingid'], |
||
45 | null, |
||
46 | 'Customer($select=CustomerId;),ContactPerson($select=PersonId;),OrderRows', |
||
47 | false |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | $_customer = EDUAPI()->OData->Customers->GetItem( |
||
52 | $event_booking['Customer']['CustomerId'], |
||
53 | null, |
||
54 | null, |
||
55 | false |
||
56 | ); |
||
57 | |||
58 | $_contact = EDUAPI()->OData->Persons->GetItem( |
||
59 | $event_booking['ContactPerson']['PersonId'], |
||
60 | null, |
||
61 | null, |
||
62 | false |
||
63 | ); |
||
64 | |||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
65 | |||
66 | $ebi = new EduAdmin_BookingInfo( $event_booking, $_customer, $_contact ); |
||
67 | |||
68 | if ( ! empty( EDU()->session['klarna-order-id'] ) && ! empty( $_GET['klarna_order_id'] ) && EDU()->session['klarna-order-id'] === $_GET['klarna_order_id'] ) { |
||
69 | do_action( 'eduadmin-bookingcompleted', $ebi ); |
||
70 | } else { |
||
71 | do_action( 'eduadmin-processbooking', $ebi ); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param EduAdmin_BookingInfo|null $ebi |
||
77 | */ |
||
78 | public function intercept_booking( $ebi = null ) { |
||
79 | if ( 'no' === $this->get_option( 'enabled', 'no' ) ) { |
||
80 | return; |
||
81 | } |
||
82 | |||
83 | if ( ! empty( $_POST['act'] ) && ( 'bookCourse' === $_POST['act'] || 'bookProgramme' === $_POST['act'] ) ) { |
||
84 | $ebi->NoRedirect = true; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param EduAdmin_BookingInfo|null $ebi |
||
90 | */ |
||
91 | public function process_booking( $ebi = null ) { |
||
92 | if ( 'no' === $this->get_option( 'enabled', 'no' ) ) { |
||
93 | return; |
||
94 | } |
||
95 | |||
96 | $ebi->NoRedirect = true; |
||
97 | |||
98 | if ( empty( $_GET['klarna_order_id'] ) || empty( EDU()->session['klarna-order-id'] ) ) { |
||
99 | $checkout = $this->create_checkout( $ebi ); |
||
100 | |||
101 | $snippet = $checkout['gui']['snippet']; |
||
102 | echo "<div>{$snippet}</div>"; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | public function process_klarnaresponse() { |
||
107 | if ( 'no' === $this->get_option( 'enabled', 'no' ) ) { |
||
108 | return; |
||
109 | } |
||
110 | $checkout_url = ! checked( $this->get_option( 'test_mode', 'no' ), '1', false ) ? Klarna_Checkout_Connector::BASE_URL : Klarna_Checkout_Connector::BASE_TEST_URL; |
||
111 | $shared_secret = $this->get_option( 'shared_secret', '' ); |
||
112 | |||
113 | if ( ! empty( $_GET['klarna_order_id'] ) && ! empty( EDU()->session['klarna-order-id'] ) && EDU()->session['klarna-order-id'] === $_GET['klarna_order_id'] ) { |
||
114 | try { |
||
115 | $connector = Klarna_Checkout_Connector::create( |
||
116 | $shared_secret, |
||
117 | $checkout_url |
||
118 | ); |
||
119 | |||
120 | $order_id = EDU()->session['klarna-order-id']; |
||
121 | |||
122 | $order = new Klarna_Checkout_Order( $connector, $order_id ); |
||
123 | |||
124 | $order->fetch(); |
||
125 | |||
126 | $snippet = $order['gui']['snippet']; |
||
127 | echo "<div>{$snippet}</div>"; |
||
128 | EDU()->session['klarna-order-id'] = null; |
||
129 | |||
130 | } catch ( Klarna_Checkout_ApiErrorException $ex ) { |
||
131 | EDU()->write_debug( $ex->getMessage() ); |
||
132 | EDU()->write_debug( $ex->getPayload() ); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | public function init_form_fields() { |
||
138 | $this->setting_fields = array( |
||
139 | 'enabled' => array( |
||
140 | 'title' => __( 'Enabled', 'edauadmin-wp-klarna-checkout' ), |
||
141 | 'type' => 'checkbox', |
||
142 | 'description' => __( 'Enables/Disabled the integration with Klarna Checkout', 'eduadmin-wp-klarna-checkout' ), |
||
143 | 'default' => 'no', |
||
144 | ), |
||
145 | 'eid' => array( |
||
146 | 'title' => __( 'EID', 'eduadmin-wp-klarna-checkout' ), |
||
147 | 'type' => 'text', |
||
148 | 'description' => __( 'The EID to connect to Klarna Checkout v2', 'eduadmin-wp-klarna-checkout' ), |
||
149 | 'default' => '', |
||
150 | ), |
||
151 | 'shared_secret' => array( |
||
152 | 'title' => __( 'Shared secret', 'eduadmin-wp-klarna-checkout' ), |
||
153 | 'type' => 'password', |
||
154 | 'description' => __( 'The shared secret to connect to Klarna Checkout v2', 'eduadmin-wp-klarna-checkout' ), |
||
155 | 'default' => '', |
||
156 | ), |
||
157 | 'termsurl' => array( |
||
158 | 'title' => __( 'Terms and Conditions URL', 'eduadmin-wp-klarna-checkout' ), |
||
159 | 'type' => 'text', |
||
160 | 'description' => __( 'This URL is required for Klarna Checkout', 'eduadmin-wp-klarna-checkout' ), |
||
161 | 'default' => '', |
||
162 | ), |
||
163 | 'test_mode' => array( |
||
164 | 'title' => __( 'Test mode', 'eduadmin-wp-klarna-checkout' ), |
||
165 | 'type' => 'checkbox', |
||
166 | 'description' => __( 'Enables test mode, so you can test the integration', 'eduadmin-wp-klarna-checkout' ), |
||
167 | 'default' => 'no', |
||
168 | ), |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param EduAdmin_BookingInfo|null $ebi |
||
174 | * |
||
175 | * @return Klarna_Checkout_Order|null |
||
176 | */ |
||
177 | public function create_checkout( $ebi = null ) { |
||
178 | |||
179 | $checkout_url = ! checked( $this->get_option( 'test_mode', 'no' ), '1', false ) ? Klarna_Checkout_Connector::BASE_URL : Klarna_Checkout_Connector::BASE_TEST_URL; |
||
180 | $shared_secret = $this->get_option( 'shared_secret', '' ); |
||
181 | |||
182 | $create = array(); |
||
183 | |||
184 | $create['locale'] = strtolower( str_replace( '_', '-', get_locale() ) ); |
||
185 | $create['purchase_country'] = 'SE'; |
||
186 | $create['purchase_currency'] = get_option( 'eduadmin-currency', 'SEK' ); |
||
187 | |||
188 | $merchant = array(); |
||
189 | $merchant['id'] = $this->get_option( 'eid', '' ); |
||
190 | $merchant['terms_uri'] = $this->get_option( 'termsurl', '' ); |
||
191 | |||
192 | $current_url = esc_url( "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}" ); |
||
193 | |||
194 | $booking_id = 0; |
||
195 | $programme_booking_id = 0; |
||
196 | |||
197 | $reference_id = 0; |
||
198 | |||
199 | if ( ! empty( $ebi->EventBooking['BookingId'] ) ) { |
||
200 | $booking_id = intval( $ebi->EventBooking['BookingId'] ); |
||
201 | $reference_id = $booking_id; |
||
202 | } |
||
203 | |||
204 | if ( ! empty( $ebi->EventBooking['ProgrammeBookingId'] ) ) { |
||
205 | $programme_booking_id = intval( $ebi->EventBooking['ProgrammeBookingId'] ); |
||
206 | $reference_id = $programme_booking_id; |
||
207 | } |
||
208 | |||
209 | $confirmation_url = add_query_arg( |
||
210 | array( |
||
211 | 'klarna_order_id' => '{checkout.order.id}', |
||
212 | 'booking_id' => $booking_id, |
||
213 | 'programme_booking_id' => $programme_booking_id, |
||
214 | 'edu-valid-form' => wp_create_nonce( 'edu-booking-confirm' ), |
||
215 | 'act' => 'paymentCompleted', |
||
216 | ), |
||
217 | $current_url |
||
218 | ); |
||
219 | |||
220 | $push_url = add_query_arg( |
||
221 | array( |
||
222 | 'klarna_order_id' => '{checkout.order.id}', |
||
223 | 'booking_id' => $booking_id, |
||
224 | 'programme_booking_id' => $programme_booking_id, |
||
225 | 'status' => 'push', |
||
226 | ), |
||
227 | $current_url |
||
228 | ); |
||
229 | |||
230 | $merchant['checkout_uri'] = $current_url; |
||
231 | $merchant['confirmation_uri'] = $confirmation_url; |
||
232 | $merchant['push_uri'] = $push_url; |
||
233 | |||
234 | $create['merchant'] = $merchant; |
||
235 | |||
236 | $create['merchant_reference'] = array(); |
||
237 | $create['merchant_reference']['orderid1'] = $reference_id; |
||
238 | $create['merchant_reference']['orderid2'] = $reference_id; |
||
239 | |||
240 | $create['cart'] = array(); |
||
241 | $create['cart']['items'] = array(); |
||
242 | |||
243 | foreach ( $ebi->EventBooking['OrderRows'] as $order_row ) { |
||
244 | $cart_item = array(); |
||
245 | |||
246 | $cart_item['reference'] = $order_row['ItemNumber']; |
||
247 | $cart_item['name'] = $order_row['Description']; |
||
248 | $cart_item['quantity'] = intval( $order_row['Quantity'] ); |
||
249 | |||
250 | if ( ! $order_row['PriceIncVat'] ) { |
||
251 | $price_per_unit = $order_row['PricePerUnit'] * ( 1 + ( $order_row['VatPercent'] / 100 ) ) * 100; |
||
252 | } else { |
||
253 | $price_per_unit = $order_row['PricePerUnit'] * 100; |
||
254 | } |
||
255 | |||
256 | $cart_item['unit_price'] = $price_per_unit; |
||
257 | $cart_item['tax_rate'] = intval( $order_row['VatPercent'] * 100 ); |
||
258 | $cart_item['discount_rate'] = intval( $order_row['DiscountPercent'] * 100 ); |
||
259 | |||
260 | $create['cart']['items'][] = $cart_item; |
||
261 | } |
||
262 | |||
263 | try { |
||
264 | $connector = Klarna_Checkout_Connector::create( |
||
265 | $shared_secret, |
||
266 | $checkout_url |
||
267 | ); |
||
268 | |||
269 | $order = new Klarna_Checkout_Order( $connector ); |
||
270 | $order->create( $create ); |
||
271 | |||
272 | $order->fetch(); |
||
273 | |||
274 | $order_id = $order['id']; |
||
275 | EDU()->session['klarna-order-id'] = $order_id; |
||
276 | |||
277 | return $order; |
||
278 | } catch ( Klarna_Checkout_ApiErrorException $ex ) { |
||
279 | EDU()->write_debug( $ex->getMessage() ); |
||
280 | EDU()->write_debug( $ex->getPayload() ); |
||
281 | |||
282 | return null; |
||
283 | } |
||
284 | } |
||
285 | |||
286 | public function process_paymentstatus() { |
||
287 | if ( ! empty( $_GET['klarna_order_id'] ) && ! empty( $_GET['status'] ) ) { |
||
288 | $checkout_url = ! checked( $this->get_option( 'test_mode', 'no' ), '1', false ) ? Klarna_Checkout_Connector::BASE_URL : Klarna_Checkout_Connector::BASE_TEST_URL; |
||
289 | $shared_secret = $this->get_option( 'shared_secret', '' ); |
||
290 | |||
291 | try { |
||
292 | $connector = Klarna_Checkout_Connector::create( |
||
293 | $shared_secret, |
||
294 | $checkout_url |
||
295 | ); |
||
296 | |||
297 | $order_id = $_GET['klarna_order_id']; |
||
298 | |||
299 | $order = new Klarna_Checkout_Order( $connector, $order_id ); |
||
300 | |||
301 | $order->fetch(); |
||
302 | |||
303 | $booking_id = intval( $_GET['booking_id'] ); |
||
304 | $programme_booking_id = intval( $_GET['programme_booking_id'] ); |
||
305 | |||
306 | |||
307 | if ( 'checkout_complete' === $order['status'] ) { |
||
308 | |||
309 | $patch_booking = new stdClass(); |
||
310 | $patch_booking->Paid = true; |
||
311 | |||
312 | // We're setting this as a Card Payment, so that our service in the background will remove it if it doesn't get paid in time (15 minute slot) |
||
313 | $patch_booking->PaymentMethodId = 2; |
||
314 | |||
315 | if ( $booking_id > 0 ) { |
||
316 | EDUAPI()->REST->Booking->PatchBooking( |
||
317 | $booking_id, |
||
318 | $patch_booking |
||
319 | ); |
||
320 | } |
||
321 | |||
322 | if ( $programme_booking_id > 0 ) { |
||
323 | EDUAPI()->REST->ProgrammeBooking->PatchBooking( |
||
324 | $programme_booking_id, |
||
325 | $patch_booking |
||
326 | ); |
||
327 | } |
||
328 | |||
329 | $update = array(); |
||
330 | $update['status'] = 'created'; |
||
331 | $order->update( $update ); |
||
332 | } |
||
333 | exit( 0 ); |
||
334 | } catch ( Klarna_Checkout_ApiErrorException $ex ) { |
||
335 | EDU()->write_debug( $ex->getMessage() ); |
||
336 | EDU()->write_debug( $ex->getPayload() ); |
||
337 | exit( 1 ); |
||
338 | } |
||
339 | } |
||
340 | } |
||
341 | } |
||
342 | } |
||
343 |