-
Notifications
You must be signed in to change notification settings - Fork 0
/
klarna-instant-shopping-button.php
411 lines (398 loc) · 18.3 KB
/
klarna-instant-shopping-button.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/*
Plugin Name: Woo Klarna Instant Shopping
Plugin URI: https://github.com/mnording/Woo-Klarna-Instant-Shopping
Description: Adds Klarna Instant shopping button to your product pages
Version: 0.1.0
Author: mnording10
Author URI: https://mnording.com
Text Domain: woo-klarna-instant-shopping
Domain Path: /languages
*/
require 'vendor/autoload.php';
require 'klarna-woo-translator.php';
require 'woo-settings-page.php';
require 'klarna-instant-shopping-logger.php';
class KlarnaShoppingButton
{
private $textdomain = "woo-klarna-instant-shopping";
private $baseUrl;
private $wooTranslate;
private $logContext;
private $logger;
private $username;
private $pass;
private $settingspage;
function __construct()
{
add_action('woocommerce_init', array($this, 'init'));
add_action("woocommerce_before_add_to_cart_form", array($this, 'InitAndRender'));
add_action('rest_api_init', function () {
register_rest_route('klarna-instant-shopping', '/place-order/', array(
'methods' => 'POST',
'callback' => array($this, 'HandleOrderPostBack'),
));
});
add_action('admin_menu', array($this, 'CreateOptionsPage'));
}
function CreateOptionsPage()
{
add_options_page('Klarna Instant Shopping', 'Klarna Instant Shopping', 'manage_options', 'woo-klarna-instant-shopping', array($this->settingspage, 'RenderKlarnaSettingsPage'));
}
function init()
{
$this->wooTranslate = new KlarnaWooTranslator();
$this->settingspage = new WooKlarnaInstantShoppingSettingsPage();
$this->username = $this->settingspage->getmid();
$this->pass = $this->settingspage->getpass();
$this->baseUrl = $this->settingspage->getBaseUrl();
$this->logger = new KlarnaInstantShoppingLogger($this->settingspage->shouldLogDebug(), wc_get_logger());
}
function enqueScripts()
{
wp_enqueue_script("woo_klarna_instant-shopping", "https://x.klarnacdn.net/instantshopping/lib/v1/lib.js");
}
function renderButton($buttonID)
{
$testmode = $this->settingspage->getTestmode();
$enviournment = $testmode ? "playground" : "production";
echo '<klarna-instant-shopping data-key="' . $buttonID . '" data-environment="' . $enviournment . '" data-region="eu"></klarna-instant-shopping>';
}
function InitiateButton()
{
global $product;
$id = $product->get_id();
$image = wp_get_attachment_image_src(get_post_thumbnail_id($id), 'woocommerce_thumbnail');
$imageUlr = ($image[0]);
$productPrice = $product->get_price();
WC()->cart->empty_cart();
WC()->cart->add_to_cart($id);
foreach (WC()->cart->get_cart() as $cartitem) {
$klarnaTaxAmount = ($cartitem["line_tax_data"]["subtotal"][1]);
}
$vat = $klarnaTaxAmount / ($productPrice - $klarnaTaxAmount);
$shippingMethods = $this->GetShippingMethodsForKlarna($productPrice, $vat);
if ($product->get_type() == "simple") {
$this->LoadJSForSimple($product, $klarnaTaxAmount, $imageUlr, $vat, $shippingMethods);
}
if ($product->get_type() == "variable") {
$this->LoadJsForVariable($product, $shippingMethods, $vat);
}
}
function LoadJSForSimple($product, $klarnaTaxAmount, $imageUlr, $vat, $shippingMethods)
{
$location = WC_Geolocation::geolocate_ip();
$country = $location['country'];
wp_add_inline_script('woo_klarna_instant-shopping', 'window.klarnaAsyncCallback = function () {
Klarna.InstantShopping.load({
"purchase_country": "' . $country . '",
"purchase_currency": "' . get_woocommerce_currency() . '",
"locale": "' . str_replace('_', '-', get_locale()) . '",
"merchant_urls": {
"terms": "' . get_permalink(woocommerce_get_page_id("terms")).'",
},
"order_lines": [{
"type": "physical",
"reference": "' . $product->get_sku() . '",
"name": "' . $product->get_name() . '",
"quantity": 1,
"merchant_data": "{\"prod_id\":' . $product->get_id() . '}",
"unit_price": ' . intval($product->get_price() * 100) . ',
"tax_rate": ' . intval($vat * 10000) . ',
"total_amount": ' . intval($product->get_price() * 100) . ',
"total_discount_amount": 0,
"total_tax_amount": ' . intval($klarnaTaxAmount * 100) . ',
"image_url": "' . $imageUlr . '"
}],
"shipping_options":
' . json_encode($shippingMethods) . '
}, function (response) {
console.log("Klarna.InstantShopping.load callback with data:" + JSON.stringify(response))
})
};', 'before');
}
function LoadJsForVariable($product, $shippingMethods, $vat)
{
$location = WC_Geolocation::geolocate_ip();
$country = $location['country'];
wp_add_inline_script('woo_klarna_instant-shopping', 'jQuery( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
if(variation.is_in_stock) {
var priceEx = variation.display_price*100/(1+' . floatval($vat) . ');
var taxamount = variation.display_price*100 - priceEx;
var extraprodname = "";
for (var key in variation.attributes) {
extraprodname += variation.attributes[key]+" ";
}
Klarna.InstantShopping.load({
"purchase_country": "' . $country . '",
"purchase_currency": "' . get_woocommerce_currency() . '",
"locale": "' . str_replace('_', '-', get_locale()) . '",
"merchant_urls": {
"terms": "' . rtrim(get_permalink(woocommerce_get_page_id("terms")), '/') /* TODO: rtrim pending klarna fix*/. '",
},
"order_lines": [{
"type": "physical",
"reference": variation.sku,
"name": "' . $product->get_name() . ' "+extraprodname,
"quantity": 1,
"merchant_data": "{\"prod_id\":' . $product->get_id() . ',\"variation_id\":"+variation.variation_id+"}",
"unit_price": variation.display_price*100,
"tax_rate": ' . intval($vat * 10000) . ',
"total_amount": variation.display_price*100,
"total_discount_amount": 0,
"total_tax_amount": taxamount,
"image_url": variation.image.src
}],
"shipping_options":
' . json_encode($shippingMethods) . '
}, function (response) {
console.log("Klarna.InstantShopping.load callback with data:" + JSON.stringify(response))
});
}
else {
var getbbdom = document.getElementsByTagName("klarna-instant-shopping")[0];
getbbdom.innerHTML ="";
}
} );', 'after');
}
function GetShippingOptionsForProduct($prod)
{
$prod->get_shipping_class_id();
}
function InitAndRender()
{
$button = get_option("woo-klarna-instant-shopping-buttonid"); // $this->generateButtonKey();
$this->logger->logDebug('Rendering button with buttonId ' . $button);
global $product;
if ($product->get_stock_status() == "instock") {
$this->enqueScripts();
$this->renderButton($button);
$this->InitiateButton();
} else {
$this->logger->logDebug("Product Not in stock");
}
}
function GetOrderDetailsFromKlarna($authToken)
{
$client = new GuzzleHttp\Client();
$res = $client->get($this->baseUrl . '/instantshopping/v1/authorizations/' . $authToken, ['auth' => [$this->username, $this->pass], 'headers' => [
'User-Agent' => 'Mnording Instant Shopping WP-Plugin',
]]);
$this->logger->logDebug('Got order details from klarna ');
$this->logger->logDebug($res->getBody());
return json_decode($res->getBody());
}
function HandleOrderPostBack($request_data)
{
$this->logger->logDebug('Got postback with successfull auth ');
$this->logger->logDebug($request_data->get_body());
$req = json_decode($request_data->get_body());
$klarnaorder = $this->GetOrderDetailsFromKlarna($req->authorization_token);
$this->logger->logDebug('Got Klarna Order Details');
if ($this->VerifyOrder($klarnaorder)) {
$WCOrder = $this->CreateWcOrder($klarnaorder);
$WCOrderId = $WCOrder->get_id();
$this->logger->logDebug('Created WC order ' . $WCOrderId);
$klarnaorderID = $this->PlaceOrder($req->authorization_token, $klarnaorder, $WCOrder);
$this->logger->logDebug('Created Klarna order ' . $klarnaorderID);
$this->UpdateWCOrder($WCOrderId, $klarnaorderID);
$this->logger->logDebug('Updated WC Order ' . $WCOrderId . ' with klarna order id ' . $klarnaorderID);
} else {
$this->DenyOrder($req->authorization_token, "other", __("Could not place order", $this->textdomain));
}
}
/*
- address_error when there are problems with the provided address,
- item_out_of_stock when the item has gone out of stock,
- consumer_underaged when the product has limitations based on the age of the consumer,
- unsupported_shipping_address when there are problems with the shipping address. You don’t need to specify a deny_message for the above codes.
- other for which you may specify a deny_message which will be shown to the consumer. It is important that the language of the message matches the locale of the Instant Shopping flow
*/
function DenyOrder($auth, $code, $message)
{
$client = new GuzzleHttp\Client();
$res = $client->delete(
$this->baseUrl . '/instantshopping/v1/authorizations/' . $auth,
['json' => [
"deny_code" => $code,
"deny_message" => $message
], 'headers' => [
'User-Agent' => 'Mnording Instant Shopping WP-Plugin',
], 'auth' => [$this->username, $this->pass]]
);
$this->logger->logDebug("Deleted Klarna AuthToken " . $auth);
}
function PlaceOrder($auth, $order, $wcorder)
{
$client = new GuzzleHttp\Client();
$wcorderUrl = $wcorder->get_checkout_order_received_url();
$order->merchant_urls->confirmation = $wcorderUrl;
$res = $client->post(
$this->baseUrl . '/instantshopping/v1/authorizations/' . $auth . '/orders/',
['json' => $order, 'headers' => [
'User-Agent' => 'Mnording Instant Shopping WP-Plugin',
], 'auth' => [$this->username, $this->pass]]
);
$order = json_decode($res->getBody());
return $order->order_id;
}
function VerifyOrder($klarnaOrder)
{
if (!$this->verifyStockLevels($klarnaOrder)) {
return false;
}
$this->verifyShipping($klarnaOrder);
return true;
}
function verifyStockLevels($klarnaOrder)
{
foreach ($klarnaOrder->order_lines as $orderline) {
if ($orderline->type != "shipping_fee") {
$merchdata = json_decode($orderline->merchant_data);
$prodid = $merchdata->prod_id;
$prod = wc_get_product($prodid);
if ($merchdata->variation_id) {
$prod = wc_get_product($merchdata->variation_id);
}
if (!$prod->is_in_stock()) {
return false;
}
}
return true;
}
}
function verifyShipping($klarnaOrder)
{ //Verify that selected shipping is applicable
}
function GetShippingMethodsForKlarna($productPrice, $vat)
{
$lowestCost = 999999999;
$selectedIndex = 0;
$shippingMethods = array();
$location = WC_Geolocation::geolocate_ip();
$country = $location['country'];
foreach ($this->GetShippingMethodsForAmount($productPrice, $country) as $key => $methods) {
$this->logger->logDebug("Shipping price is " . $methods["price"]);
$shippingPriceInCents = $methods["price"] * 100;
$this->logger->logDebug("Shipping in cents price is " . $shippingPriceInCents);
if ($shippingPriceInCents < $lowestCost) {
$lowestCost = $shippingPriceInCents;
$selectedIndex = $key;
}
$shippingMethods[] = array(
"id" => $methods["id"],
"name" => $methods["name"],
"description" => "",
"price" => $shippingPriceInCents + intval($shippingPriceInCents * $vat),
"tax_amount" => intval($shippingPriceInCents * $vat),
"tax_rate" => intval($vat * 10000),
"preselected" => false,
"shipping_method" => "PickUpPoint"
);
};
$this->logger->logDebug("Lowest shipping was " . $lowestCost . " for index " . $selectedIndex);
$shippingMethods[$selectedIndex]["preselected"] = true;
$this->logger->logDebug(json_encode($shippingMethods));
return $shippingMethods;
}
function GetShippingMethodsForAmount($amount, $country)
{
$active_methods = array();
$values = array(
'country' => $country,
'amount' => $amount
);
// Fake product number to get a filled card....
WC()->cart->add_to_cart('1');
WC()->shipping->calculate_shipping($this->get_shipping_packages($values));
$shipping_methods = WC()->shipping->packages;
foreach ($shipping_methods[0]['rates'] as $id => $shipping_method) {
$active_methods[] = array(
'id' => $shipping_method->get_instance_id(),
'type' => $shipping_method->method_id,
'provider' => $shipping_method->method_id,
'name' => $shipping_method->label,
'price' => number_format($shipping_method->cost, 2, '.', '')
);
}
return $active_methods;
}
function get_shipping_packages($value)
{
// Packages array for storing 'carts'
$packages = array();
$packages[0]['contents'] = WC()->cart->cart_contents;
$packages[0]['contents_cost'] = $value['amount'];
$packages[0]['applied_coupons'] = WC()->session->applied_coupon;
$packages[0]['destination']['country'] = $value['country'];
$packages[0]['destination']['state'] = '';
$packages[0]['destination']['postcode'] = '';
$packages[0]['destination']['city'] = '';
$packages[0]['destination']['address'] = '';
$packages[0]['destination']['address_2'] = '';
return apply_filters('woocommerce_cart_shipping_packages', $packages);
}
function UpdateWCOrder($orderid, $klarnaId)
{
$order = wc_get_order($orderid);
$order->payment_complete($klarnaId);
$order->set_payment_method("kco");
$order->set_payment_method_title("Klarna");
$order->update_status("processing", 'Got Klarna OK', true);
$order->save();
}
function CreateWcOrder($klarnaOrderObject)
{
$address = $this->wooTranslate->GetWooAdressFromKlarnaOrder($klarnaOrderObject);
$this->logger->logDebug('Got address from klarna object ');
$this->logger->logDebug(json_encode($address), $this->logContext);
$orderlines = $this->wooTranslate->GetWCLineItemsFromKlarnaOrder($klarnaOrderObject);
$this->logger->logDebug('Got line items from klarna object ');
$this->logger->logDebug(json_encode($orderlines), $this->logContext);
$shippinglines = $this->wooTranslate->GetWCShippingLinesFromKlarnaOrder($klarnaOrderObject);
$this->logger->logDebug('Got shipping lines from klarna object ');
$this->logger->logDebug(json_encode($shippinglines));
// Now we create the order
try {
$order = wc_create_order();
$this->logger->logDebug("Created WC order");
$item = new WC_Order_Item_Shipping();
$this->logger->logDebug("Adding shipping");
$item->set_method_title($shippinglines["name"]);
$item->set_method_id($shippinglines["id"]);
$item->set_total($shippinglines["price"]);
$order->add_item($item);
foreach ($orderlines as $line) {
if ($line["variation_id"]) {
$this->logger->logDebug("Creating variation order");
$membershipProduct = new WC_Product_Variable($line["product_id"]);
$theMemberships = $membershipProduct->get_available_variations();
$variationsArray = array();
foreach ($theMemberships as $membership) {
if ($membership['variation_id'] == $line["variation_id"]) {
$variationID = $membership['variation_id'];
$variationsArray['variation'] = $membership['attributes'];
}
}
$this->logger->logDebug("Looking for variation id " . $line["variation_id"]);
if ($variationID) {
$this->logger->logDebug("Found variation with id " . $variationID);
$varProduct = new WC_Product_Variation($variationID);
$order->add_product($varProduct, 1, $variationsArray);
}
} else {
$order->add_product(get_product($line["product_id"]), $line["quantity"]);
}
}
$order->set_address($address, 'billing');
$order->set_address($address, 'shipping'); // TODO: Seperate shipping/billing?
$order->calculate_totals();
$order->update_status("pending", 'Imported order', true);
} catch (Exception $e) {
$this->logger->logError('Unable to create order');
$this->logger->logError('Caught exception: ', $e->getMessage());
}
return $order;
}
}
$t = new KlarnaShoppingButton();