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

Commit

Permalink
Merge pull request #25 from klarna/instant_shopping_api
Browse files Browse the repository at this point in the history
Add support of Instant Shopping API
  • Loading branch information
freethan authored Mar 15, 2019
2 parents 1c71045 + e509e76 commit c3400ad
Show file tree
Hide file tree
Showing 18 changed files with 1,112 additions and 17 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# PSR-1 / PSR-2 Coding Style, https://www.php-fig.org/psr/psr-2/
[*.php]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4

110 changes: 110 additions & 0 deletions docs/examples/InstantShoppingAPI/ButtonKeys/create_button_key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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 button key based on setup options.
*/

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
);

try {
$buttonsApi = new Klarna\Rest\InstantShopping\ButtonKeys($connector);
$data = [
'merchant_name' => 'John Doe',
'merchant_urls' => [
'place_order' => 'https://example.com/place-callback',
'push' => 'https://example.com/push-callback',
'confirmation' => 'https://example.com/confirmation-callback',
'terms' => 'https://example.com/terms-callback',
'notification' => 'https://example.com/notification-callback',
'update' => 'https://example.com/update-callback',
],
'purchase_currency' => 'EUR',
'purchase_country' => 'DE',
'billing_countries' => ["UK", "DE", "SE"],
'shipping_countries' => ["UK", "DE", "SE"],
'locale' => 'en-US',
'order_amount' => 50000,
'order_tax_amount' => 0,
'order_lines' => [
[
'name' => 'Red T-Shirt',
'type' => 'physical',
'reference' => '19-402-USA',
'quantity' => 5,
'quantity_unit' => 'pcs',
'tax_rate' => 0,
'total_amount' => 50000,
'total_discount_amount' => 0,
'total_tax_amount' => 0,
'unit_price' => 10000,
'product_url' => 'https://www.estore.com/products/f2a8d7e34',
'image_url' => 'https://www.exampleobjects.com/logo.png',
'product_identifiers' =>
[
'category_path' => 'Electronics Store > Computers & Tablets > Desktops',
'global_trade_item_number' => '735858293167',
'manufacturer_part_number' => 'BOXNUC5CPYH',
'brand' => 'Intel',
],
],
],
'shipping_options' => [
[
'id' => 'my-shipping-id',
'name' => 'Pickup Store',
'description' => 'My custom description',
'promo' => 'string',
'price' => 10,
'tax_amount' => 0,
'tax_rate' => 0,
'preselected' => true,
'shipping_method' => 'PICKUPSTORE',
],
],
];
$button = $buttonsApi->create($data);

echo 'Button has been successfully created' . PHP_EOL;
print_r($button);

} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage() . "\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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.
*/

/**
* See the setup options for a specific button key.
*/

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';
$buttonKey = getenv('BUTTON_KEY') ?: 'ab12345c-1234-abcd-1234-1234abcd';

/*
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
);

try {
$buttonsApi = new Klarna\Rest\InstantShopping\ButtonKeys($connector, $buttonKey);
$button = $buttonsApi->retrieve();

print_r($button->getArrayCopy());

} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage() . "\n";
}
80 changes: 80 additions & 0 deletions docs/examples/InstantShoppingAPI/ButtonKeys/update_button_key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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.
*/

/**
* Update the setup options for a specific button key.
*/

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';
$buttonKey = getenv('BUTTON_KEY') ?: 'ab12345c-1234-abcd-1234-1234abcd';

/*
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
);

try {
$buttonsApi = new Klarna\Rest\InstantShopping\ButtonKeys($connector, $buttonKey);
$data = [
'merchant_name' => 'New name',
'merchant_urls' => [
'place_order' => 'https://example.com/place-callback',
'push' => 'https://example.com/push-callback',
'confirmation' => 'https://example.com/confirmation-callback',
'terms' => 'https://example.com/terms-callback',
'notification' => 'https://example.com/notification-callback',
'update' => 'https://example.com/update-callback',
],
'shipping_options' => [
[
'id' => 'my-new-shipping-id',
'name' => 'Priority delivery',
'description' => '',
'price' => 300,
'tax_amount' => 0,
'tax_rate' => 0,
'preselected' => false,
'shipping_method' => 'PRIME_DELIVERY',
],
],
];
$button = $buttonsApi->update($data);

echo 'Button has been successfully updated' . PHP_EOL;
print_r($button);

} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage() . "\n";
}
121 changes: 121 additions & 0 deletions docs/examples/InstantShoppingAPI/Orders/approve_order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?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.
*/

/**
* Approve the authorized order and place an order identified by the authorization token.
*/

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';
$authToken = getenv('AUTH_TOKEN') ?: 'authorization_token';

/*
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 = [
"order_id" => "f3392f8b-6116-4073-ab96-e330819e2c07",
"purchase_country" => "gb",
"purchase_currency" => "gbp",
"locale" => "en-gb",
"order_amount" => 10000,
"order_tax_amount" => 2000,
"billing_address" => [
"given_name" => "Jane",
"family_name"=> "Doe",
"email"=> "[email protected]",
"title"=> "Ms",
"street_address"=> "Lombard St 10",
"street_address2"=> "Apt 214",
"postal_code"=> "90210",
"city"=> "Beverly Hills",
"region"=> "CA",
"phone"=> "333444555",
"country"=> "US"
],
"order_lines" => [
[
"type" => "physical",
"reference" => "123050",
"name" => "Tomatoes",
"quantity" => 10,
"quantity_unit" => "kg",
"unit_price" => 600,
"tax_rate" => 2500,
"total_amount" => 6000,
"total_tax_amount" => 1200
],
[
"type" => "physical",
"reference" => "543670",
"name" => "Bananas",
"quantity" => 1,
"quantity_unit" => "bag",
"unit_price" => 5000,
"tax_rate" => 2500,
"total_amount" => 4000,
"total_discount_amount" => 1000,
"total_tax_amount" => 800
]
],
"merchant_urls" => [
"terms" => "http://www.merchant.com/toc",
"checkout" => "http://www.merchant.com/checkout?klarna_order_id={checkout.order.id}",
"confirmation" => "http://www.merchant.com/thank-you?klarna_order_id={checkout.order.id}",
"push" => "http://www.merchant.com/create_order?klarna_order_id={checkout.order.id}"
],
"customer" => [
"date_of_birth" => "1995-10-20",
"title" => "Mr",
"gender" => "male",
"last_four_ssn" => "0512",
"national_identification_number" => "3108971100",
"type" => "person",
"vat_id" => "string",
"organization_registration_id" => "556737-0431",
"organization_entity_type" => "LIMITED_COMPANY"
]
];

try {
$orderApi = new Klarna\Rest\InstantShopping\Orders($connector, $authToken);
$status = $orderApi->approve($order);

echo 'The order has been approved' . PHP_EOL;
print_r($status);

} catch (Exception $e) {
echo 'Caught exception => ' . $e->getMessage() . "\n";
}
Loading

0 comments on commit c3400ad

Please sign in to comment.