Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Commit

Permalink
Examples: Add example "How to set discount".
Browse files Browse the repository at this point in the history
  • Loading branch information
alexions committed Apr 11, 2019
1 parent 0d6b090 commit 634b230
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions docs/examples/CheckoutAPI/discounts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Copyright 2019 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Create a checkout order with discounts.
*/

require_once dirname(__DIR__) . '/../../vendor/autoload.php';

/**
* Follow the link to get your credentials: https://github.com/klarna/kco_rest_php/#api-credentials
*
* Make sure that your credentials belong to the right endpoint. If you have credentials for the US Playground,
* such credentials will not work for the EU Playground and you will get 401 Unauthorized exception.
*/
$merchantId = getenv('USERNAME') ?: 'K123456_abcd12345';
$sharedSecret = getenv('PASSWORD') ?: 'sharedSecret';

/*
EU_BASE_URL = 'https://api.klarna.com'
EU_TEST_BASE_URL = 'https://api.playground.klarna.com'
NA_BASE_URL = 'https://api-na.klarna.com'
NA_TEST_BASE_URL = 'https://api-na.playground.klarna.com'
*/
$apiEndpoint = Klarna\Rest\Transport\ConnectorInterface::EU_TEST_BASE_URL;

$connector = Klarna\Rest\Transport\Connector::create(
$merchantId,
$sharedSecret,
$apiEndpoint
);

$order = [
"purchase_country" => "gb",
"purchase_currency" => "gbp",
"locale" => "en-gb",
"order_amount" => 9000,
"order_tax_amount" => 818,
"order_lines" => [
[
"type" => "physical",
"reference" => "19-402-USA",
"name" => "Red T-Shirt",
"quantity" => 1,
"quantity_unit" => "pcs",
"unit_price" => 10000,
"tax_rate" => 1000,
"total_amount" => 10000,
"total_tax_amount" => 909
],

// Add discount as an order line
[
"type" => "discount",
"reference" => "10-gbp-order-discount",
"name" => "Discount",
"quantity" => 1,
"unit_price" => -1000,
"tax_rate" => 1000,
"total_amount" => -1000,
"total_tax_amount" => -91
]
],
"merchant_urls" => [
"terms" => "https://example.com/toc",
"checkout" => "https://example.com/checkout?klarna_order_id={checkout.order.id}",
"confirmation" => "https://example.com/thank-you?klarna_order_id={checkout.order.id}",
"push" => "https://example.com/create_order?klarna_order_id={checkout.order.id}"
]
];

try {
$checkout = new Klarna\Rest\Checkout\Order($connector);
$checkout->create($order);

echo $checkout['html_snippet'];
} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage() . "\n";
}

0 comments on commit 634b230

Please sign in to comment.