Skip to content

Commit 2354856

Browse files
author
Chris Gårdenberg
committed
Release 1.0
1 parent 052e2f0 commit 2354856

3 files changed

Lines changed: 171 additions & 25 deletions

File tree

checkout-page.php

Lines changed: 0 additions & 2 deletions
This file was deleted.

class-edu-klarnacheckout.php

Lines changed: 170 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ public function __construct() {
1313

1414
add_action( 'eduadmin-checkpaymentplugins', array( $this, 'intercept_booking' ) );
1515
add_action( 'eduadmin-processbooking', array( $this, 'process_booking' ) );
16-
add_action( 'wp_loaded', array( $this, 'process_klarnaresponse' ) );
16+
add_action( 'eduadmin-bookingcompleted', array( $this, 'process_klarnaresponse' ) );
17+
add_action( 'wp_loaded', array( $this, 'process_paymentstatus' ) );
1718

1819
add_shortcode( 'eduadmin-klarna-testpage', array( $this, 'test_page' ) );
1920
}
2021

2122
/**
22-
* @param int $booking_id
23+
* @param $attributes
2324
*/
2425
public function test_page( $attributes ) {
2526
$attributes = shortcode_atts(
@@ -33,14 +34,29 @@ public function test_page( $attributes ) {
3334
$event_booking = EDUAPI()->OData->Bookings->GetItem(
3435
intval( $attributes['bookingid'] ),
3536
null,
36-
'Customer($select=CustomerId;),ContactPerson($select=PersonId;)'
37+
'Customer($select=CustomerId;),ContactPerson($select=PersonId;),OrderRows',
38+
false
39+
);
40+
$_customer = EDUAPI()->OData->Customers->GetItem(
41+
$event_booking['Customer']['CustomerId'],
42+
null,
43+
null,
44+
false
45+
);
46+
$_contact = EDUAPI()->OData->Persons->GetItem(
47+
$event_booking['ContactPerson']['PersonId'],
48+
null,
49+
null,
50+
false
3751
);
38-
$_customer = EDUAPI()->OData->Customers->GetItem( $event_booking['Customer']['CustomerId'] );
39-
$_contact = EDUAPI()->OData->Persons->GetItem( $event_booking['ContactPerson']['PersonId'] );
4052

4153
$ebi = new EduAdmin_BookingInfo( $event_booking, $_customer, $_contact );
4254

43-
do_action( 'eduadmin-processbooking', $ebi );
55+
if ( ! empty( EDU()->session['klarna-order-id'] ) && ! empty( $_GET['klarna_order_id'] ) && EDU()->session['klarna-order-id'] === $_GET['klarna_order_id'] ) {
56+
do_action( 'eduadmin-bookingcompleted', $ebi );
57+
} else {
58+
do_action( 'eduadmin-processbooking', $ebi );
59+
}
4460
}
4561

4662
/**
@@ -66,70 +82,202 @@ public function process_booking( $ebi = null ) {
6682

6783
$ebi->NoRedirect = true;
6884

69-
$checkout = $this->create_checkout( $ebi );
70-
include_once 'checkout-page.php';
85+
if ( empty( EDU()->session['klarna-order-id'] ) && empty( $_GET['klarna_order_id'] ) ) {
86+
$checkout = $this->create_checkout( $ebi );
87+
88+
$snippet = $checkout['gui']['snippet'];
89+
echo "<div>{$snippet}</div>";
90+
}
7191
}
7292

7393
public function process_klarnaresponse() {
7494
if ( 'no' === $this->get_option( 'enabled', 'no' ) ) {
7595
return;
7696
}
97+
$checkout_url = ! checked( $this->get_option( 'test_mode', 'no' ), '1', false ) ? Klarna_Checkout_Connector::BASE_URL : Klarna_Checkout_Connector::BASE_TEST_URL;
98+
$shared_secret = $this->get_option( 'shared_secret', '' );
99+
100+
if ( ! empty( $_GET['klarna_order_id'] ) && ! empty( EDU()->session['klarna-order-id'] ) && EDU()->session['klarna-order-id'] === $_GET['klarna_order_id'] ) {
101+
try {
102+
$connector = Klarna_Checkout_Connector::create(
103+
$shared_secret,
104+
$checkout_url
105+
);
106+
107+
$order_id = EDU()->session['klarna-order-id'];
108+
109+
$order = new Klarna_Checkout_Order( $connector, $order_id );
110+
111+
$order->fetch();
112+
113+
$snippet = $order['gui']['snippet'];
114+
echo "<div>{$snippet}</div>";
115+
EDU()->session['klarna-order-id'] = null;
116+
117+
} catch ( Klarna_Checkout_ApiErrorException $ex ) {
118+
EDU()->write_debug( $ex->getMessage() );
119+
EDU()->write_debug( $ex->getPayload() );
120+
}
121+
}
77122
}
78123

79124
public function init_form_fields() {
80125
$this->setting_fields = array(
81-
'enabled' => array(
126+
'enabled' => array(
82127
'title' => __( 'Enabled', 'edauadmin-wp-klarna-checkout' ),
83128
'type' => 'checkbox',
84129
'description' => __( 'Enables/Disabled the integration with Klarna Checkout', 'eduadmin-wp-klarna-checkout' ),
85130
'default' => 'no',
86131
),
87-
'username' => array(
88-
'title' => __( 'Username (UID)', 'eduadmin-wp-klarna-checkout' ),
132+
'eid' => array(
133+
'title' => __( 'EID', 'eduadmin-wp-klarna-checkout' ),
89134
'type' => 'text',
90-
'description' => __( 'The API credential username from Klarna', 'eduadmin-wp-klarna-checkout' ),
135+
'description' => __( 'The EID to connect to Klarna Checkout v2', 'eduadmin-wp-klarna-checkout' ),
91136
'default' => '',
92137
),
93-
'password' => array(
94-
'title' => __( 'Password', 'eduadmin-wp-klarna-checkout' ),
138+
'shared_secret' => array(
139+
'title' => __( 'Shared secret', 'eduadmin-wp-klarna-checkout' ),
95140
'type' => 'password',
96-
'description' => __( 'Password for the API credentials from Klarna', 'eduadmin-wp-klarna-checkout' ),
141+
'description' => __( 'The shared secret to connect to Klarna Checkout v2', 'eduadmin-wp-klarna-checkout' ),
97142
'default' => '',
98143
),
99-
'termsurl' => array(
144+
'termsurl' => array(
100145
'title' => __( 'Terms and Conditions URL', 'eduadmin-wp-klarna-checkout' ),
101146
'type' => 'text',
102147
'description' => __( 'This URL is required for Klarna Checkout', 'eduadmin-wp-klarna-checkout' ),
103148
'default' => '',
104149
),
150+
'test_mode' => array(
151+
'title' => __( 'Test mode', 'eduadmin-wp-klarna-checkout' ),
152+
'type' => 'checkbox',
153+
'description' => __( 'Enables test mode, so you can test the integration', 'eduadmin-wp-klarna-checkout' ),
154+
'default' => 'no',
155+
),
105156
);
106157
}
107158

108159
/**
109160
* @param EduAdmin_BookingInfo|null $ebi
110161
*
111-
* @return Klarna_Checkout_Order
162+
* @return Klarna_Checkout_Order|null
112163
*/
113164
public function create_checkout( $ebi = null ) {
114-
$create = array();
115-
$create['locale'] = strtolower( str_replace( '_', '-', get_locale() ) );
165+
166+
$checkout_url = ! checked( $this->get_option( 'test_mode', 'no' ), '1', false ) ? Klarna_Checkout_Connector::BASE_URL : Klarna_Checkout_Connector::BASE_TEST_URL;
167+
$shared_secret = $this->get_option( 'shared_secret', '' );
168+
169+
$create = array();
170+
171+
$create['locale'] = strtolower( str_replace( '_', '-', get_locale() ) );
172+
$create['purchase_country'] = 'SE';
173+
$create['purchase_currency'] = get_option( 'eduadmin-currency', 'SEK' );
174+
175+
$merchant = array();
176+
$merchant['id'] = $this->get_option( 'eid', '' );
177+
$merchant['terms_uri'] = $this->get_option( 'termsurl', '' );
178+
179+
$current_url = esc_url( "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}" );
180+
181+
182+
$confirmation_url = add_query_arg(
183+
array(
184+
'klarna_order_id' => '{checkout.order.id}',
185+
'booking_id' => $ebi->EventBooking['BookingId'],
186+
'edu-valid-form' => wp_create_nonce( 'edu-booking-confirm' ),
187+
'act' => 'paymentCompleted'
188+
),
189+
$current_url
190+
);
191+
192+
$push_url = add_query_arg(
193+
array(
194+
'klarna_order_id' => '{checkout.order.id}',
195+
'booking_id' => $ebi->EventBooking['BookingId'],
196+
'status' => 'push',
197+
),
198+
$current_url
199+
);
200+
201+
$merchant['checkout_uri'] = $current_url;
202+
$merchant['confirmation_uri'] = $confirmation_url;
203+
$merchant['push_uri'] = $push_url;
204+
205+
$create['merchant'] = $merchant;
206+
116207
$create['cart'] = array();
117208
$create['cart']['items'] = array();
118209

210+
foreach ( $ebi->EventBooking['OrderRows'] as $order_row ) {
211+
$cart_item = array();
212+
213+
$cart_item['reference'] = $order_row['ItemNumber'];
214+
$cart_item['name'] = $order_row['Description'];
215+
$cart_item['quantity'] = intval( $order_row['Quantity'] );
216+
217+
if ( ! $order_row['PriceIncVat'] ) {
218+
$price_per_unit = $order_row['PricePerUnit'] * ( 1 + ( $order_row['VatPercent'] / 100 ) ) * 100;
219+
} else {
220+
$price_per_unit = $order_row['PricePerUnit'] * 100;
221+
}
222+
223+
$cart_item['unit_price'] = $price_per_unit;
224+
$cart_item['tax_rate'] = intval( $order_row['VatPercent'] * 100 );
225+
$cart_item['discount_rate'] = intval( $order_row['DiscountPercent'] * 100 );
226+
227+
$create['cart']['items'][] = $cart_item;
228+
}
229+
119230
try {
120231
$connector = Klarna_Checkout_Connector::create(
121-
'',
122-
Klarna_Checkout_Connector::BASE_TEST_URL
232+
$shared_secret,
233+
$checkout_url
123234
);
124235

125236
$order = new Klarna_Checkout_Order( $connector );
126237
$order->create( $create );
127238

239+
$order->fetch();
240+
241+
$order_id = $order['id'];
242+
EDU()->session['klarna-order-id'] = $order_id;
243+
128244
return $order;
129245
} catch ( Klarna_Checkout_ApiErrorException $ex ) {
130246
EDU()->write_debug( $ex->getMessage() );
131247
EDU()->write_debug( $ex->getPayload() );
132-
EDU()->write_debug( $create );
248+
249+
return null;
250+
}
251+
}
252+
253+
public function process_paymentstatus() {
254+
if ( ! empty( $_GET['klarna_order_id'] ) && ! empty( $_GET['status'] ) ) {
255+
$checkout_url = ! checked( $this->get_option( 'test_mode', 'no' ), '1', false ) ? Klarna_Checkout_Connector::BASE_URL : Klarna_Checkout_Connector::BASE_TEST_URL;
256+
$shared_secret = $this->get_option( 'shared_secret', '' );
257+
258+
try {
259+
$connector = Klarna_Checkout_Connector::create(
260+
$shared_secret,
261+
$checkout_url
262+
);
263+
264+
$order_id = $_GET['klarna_order_id'];
265+
266+
$order = new Klarna_Checkout_Order( $connector, $order_id );
267+
268+
$order->fetch();
269+
270+
if ( 'checkout_complete' === $order['status'] ) {
271+
$update = array();
272+
$update['status'] = 'created';
273+
$order->update( $update );
274+
}
275+
exit( 0 );
276+
} catch ( Klarna_Checkout_ApiErrorException $ex ) {
277+
EDU()->write_debug( $ex->getMessage() );
278+
EDU()->write_debug( $ex->getPayload() );
279+
exit( 1 );
280+
}
133281
}
134282
}
135283
}

eduadmin-wp-klarna-checkout.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
/*
1818
EduAdmin Booking plugin
19-
Copyright (C) 2015-2017 Chris Gårdenberg, MultiNet Interactive AB
19+
Copyright (C) 2015-2018 Chris Gårdenberg, MultiNet Interactive AB
2020
This program is free software: you can redistribute it and/or modify
2121
it under the terms of the GNU General Public License as published by
2222
the Free Software Foundation, either version 3 of the License, or

0 commit comments

Comments
 (0)