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 #18 from klarna/hpp_plus_card_service
Browse files Browse the repository at this point in the history
Fix Hosted Payment Page API
Add support of Merchant Card Service API
  • Loading branch information
alexions committed Oct 31, 2018
2 parents e5865ae + a380bb6 commit 0388dcd
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 63 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,10 @@
## v4.1.0 - 2018-10-31
- HPP: HPP service changed API completely without backward compatibility. Adopt SDK to the new changes.
Mark getSessionStatus as @deprecated. Replaced by fetch function.
Return data was changed by HPP API service.
**[partial-backward-compatibility]**
- Add support of Merchant Card Service API

## v4.0.0 - 2018-08-27 (Major release)
- OrderManagement:
* Add ability to fetch Captures;
Expand Down
6 changes: 3 additions & 3 deletions README.md
@@ -1,7 +1,7 @@
# Klarna REST PHP SDK
[![Packagist Version][packagist-image]](https://packagist.org/packages/klarna/kco_rest)
[![Build Status][travis-image]](https://travis-ci.org/klarna/kco_rest_php)
[![Coverage Status][coveralls-image]](https://coveralls.io/r/klarna/kco_rest_php?branch=v4.0)
[![Coverage Status][coveralls-image]](https://coveralls.io/r/klarna/kco_rest_php?branch=v4.1)

## Welcome to Klarna. Smoooth payments.

Expand Down Expand Up @@ -157,5 +157,5 @@ Klarna Checkout REST PHP SDK is licensed under
[Apache License, Version 2.0](http://www.apache.org/LICENSE-2.0)

[packagist-image]: https://img.shields.io/packagist/v/klarna/kco_rest.svg?style=flat
[travis-image]: https://img.shields.io/travis/klarna/kco_rest_php/v4.0.svg?style=flat
[coveralls-image]: https://img.shields.io/coveralls/klarna/kco_rest_php/v4.0.svg?style=flat
[travis-image]: https://img.shields.io/travis/klarna/kco_rest_php/v4.1.svg?style=flat
[coveralls-image]: https://img.shields.io/coveralls/klarna/kco_rest_php/v4.1.svg?style=flat
38 changes: 5 additions & 33 deletions src/Klarna/Rest/HostedPaymentPage/Sessions.php
Expand Up @@ -25,7 +25,6 @@
use Klarna\Rest\Transport\Exception\ConnectorException;
use Klarna\Exceptions\NotApplicableException;


/**
* HPP session resource.
*/
Expand Down Expand Up @@ -61,7 +60,7 @@ public function __construct(Connector $connector, $sessionId = null)
* Creates the resource.
*
* @param array $data Creation data
*
*
* @see https://developers.klarna.com/api/#hosted-payment-page-api-create-a-new-hpp-session
*
* @throws ConnectorException When the API replies with an error response
Expand All @@ -82,19 +81,9 @@ public function create(array $data)
return $data;
}

/**
* Not applicable.
*
* @throws NotApplicableException
*/
public function fetch()
{
throw new NotApplicableException('Not applicable');
}

/**
* Distributes link to the HPP session.
*
*
* @see https://developers.klarna.com/api/#hosted-payment-page-api-distribute-link-to-the-hpp-session
*
* @param array $data Distribute data
Expand All @@ -112,33 +101,16 @@ public function distributeLink(array $data)
{
$this->post($this->getLocation() . '/distribution', $data)
->status('200');
// ->contentType('application/json');
// TODO: We cannot check the Content-type here because of an inconsistency
// between service and documentation. The real Content-Type is
// "application/octet-stream but not the "application/json" as in the docs.

return $this;
}

/**
* Gets HPP session status.
*
* @param array $params Additional query params to filter transactions.
*
* @throws ConnectorException When the API replies with an error response
* @throws RequestException When an error is encountered
* @throws \RuntimeException On an unexpected API response
* @throws \RuntimeException If the response content type is not JSON
* @throws \InvalidArgumentException If the JSON cannot be parsed
* @throws \LogicException When Guzzle cannot populate the response
*
* @return array Session status
* @deprecated HPP API no longer suppors getting the status. Use fetch (getSession) to fetch data;
* @deprecated This method will be removed in the future versions of SDK.
*/
public function getSessionStatus()
{
return $this->get($this->getLocation() . '/status')
->status('200')
->contentType('application/json')
->getJson();
return $this->fetch();
}
}
146 changes: 146 additions & 0 deletions src/Klarna/Rest/MerchantCardService/VCCSettlements.php
@@ -0,0 +1,146 @@
<?php
/**
* Copyright 2014 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.
*
* File containing the Virtual Credit Card Settlements class.
*/

namespace Klarna\Rest\MerchantCardService;

use GuzzleHttp\Exception\RequestException;
use Klarna\Rest\Resource;
use Klarna\Rest\Transport\Connector;
use Klarna\Rest\Transport\Exception\ConnectorException;
use Klarna\Exceptions\NotApplicableException;

/**
* Virtual Credit Card Settlements resource.
*/
class VCCSettlements extends Resource
{
/**
* {@inheritDoc}
*/
const ID_FIELD = 'settlement_id';

/**
* {@inheritDoc}
*/
public static $path = '/merchantcard/v3/settlements';

/**
* Constructs a session instance.
*
* @param Connector $connector HTTP transport connector
* @param string $sessionId Session ID
*/
public function __construct(Connector $connector)
{
parent::__construct($connector);
}

/**
* Not applicable.
*
* @throws NotApplicableException
*/
public function fetch()
{
throw new NotApplicableException('Not applicable');
}

/**
* Creates a new settlement.
*
* @param array $data Creation data
*
* @see https://developers.klarna.com/api/#merchant-card-service-api-create-a-new-settlement
*
* @throws ConnectorException When the API replies with an error response
* @throws RequestException When an error is encountered
* @throws \RuntimeException If the location header is missing
* @throws \RuntimeException If the API replies with an unexpected response
* @throws \LogicException When Guzzle cannot populate the response
*
* @return array Settlement data
*/
public function create(array $data)
{
$data = $this->post(self::$path, $data)
->status('201')
->contentType('application/json')
->getJson();

return $data;
}

/**
* Retrieve an existing settlement.
*
* @see https://developers.klarna.com/api/#hosted-payment-page-api-distribute-link-to-the-hpp-session
*
* @param array $data Distribute data
*
* @throws ConnectorException When the API replies with an error response
* @throws RequestException When an error is encountered
* @throws \RuntimeException On an unexpected API response
* @throws \RuntimeException If the response content type is not JSON
* @throws \InvalidArgumentException If the JSON cannot be parsed
* @throws \LogicException When Guzzle cannot populate the response
*
* @return array Settlement data
*/
public function retrieveSettlement($settlementId, $keyId)
{
$data = $this->request(
'GET',
self::$path . "/$settlementId",
['KeyId' => $keyId]
)->status('200')
->contentType('application/json')
->getJson();

return $data;
}

/**
* Retrieves a settled order's settlement.
*
* @see https://developers.klarna.com/api/#hosted-payment-page-api-distribute-link-to-the-hpp-session
*
* @param array $data Distribute data
*
* @throws ConnectorException When the API replies with an error response
* @throws RequestException When an error is encountered
* @throws \RuntimeException On an unexpected API response
* @throws \RuntimeException If the response content type is not JSON
* @throws \InvalidArgumentException If the JSON cannot be parsed
* @throws \LogicException When Guzzle cannot populate the response
*
* @return array Order's settlement data
*/
public function retrieveOrderSettlement($orderId, $keyId)
{
$data = $this->request(
'GET',
self::$path . "/order/$orderId",
['KeyId' => $keyId]
)->status('200')
->contentType('application/json')
->getJson();

return $data;
}
}
8 changes: 4 additions & 4 deletions src/Klarna/Rest/Payments/Orders.php
Expand Up @@ -105,10 +105,10 @@ public function cancelAuthorization()
{
$this->delete($this->getLocation())
->status('204');
// ->contentType('application/json');
// TODO: We cannot check the Content-type here because of an inconsistency
// between service and documentation. The real Content-Type is
// "application/octet-stream but not the "application/json" as in the docs.
// ->contentType('application/json');
// TODO: We cannot check the Content-type here because of an inconsistency
// between service and documentation. The real Content-Type is
// "application/octet-stream but not the "application/json" as in the docs.

return $this;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Klarna/Rest/Payments/Sessions.php
Expand Up @@ -105,10 +105,10 @@ public function update(array $data)
{
$this->post($this->getLocation(), $data)
->status('204');
// ->contentType('application/json');
// TODO: We cannot check the Content-type here because of an inconsistency
// between service and documentation. The real Content-Type is
// "application/octet-stream but not the "application/json" as in the docs.
// ->contentType('application/json');
// TODO: We cannot check the Content-type here because of an inconsistency
// between service and documentation. The real Content-Type is
// "application/octet-stream but not the "application/json" as in the docs.

return $this;
}
Expand Down
24 changes: 5 additions & 19 deletions tests/Component/HosterPaymentPage/SessionsTest.php
Expand Up @@ -92,11 +92,11 @@ public function testDistributeLink()
*
* @return void
*/
public function testGetSessionStatus()
public function testFetch()
{
$json =<<<JSON
{
"auth_token": "b4bd3423-24e3",
"authorization_token": "b4bd3423-24e3",
"status": "COMPLETED",
"updated_at": "2038-01-19T03:14:07.000Z"
}
Expand All @@ -108,30 +108,16 @@ public function testGetSessionStatus()
);

$session = new Sessions($this->connector, 'session-id-123');
$data = $session->getSessionStatus();
$data = $session->fetch();

$this->assertEquals('b4bd3423-24e3', $data['auth_token']);
$this->assertEquals('b4bd3423-24e3', $data['authorization_token']);
$this->assertEquals('COMPLETED', $data['status']);
$this->assertEquals('session-id-123', $session->getId());

$request = $this->mock->getLastRequest();
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('/hpp/v1/sessions/session-id-123/status', $request->getUri()->getPath());
$this->assertEquals('/hpp/v1/sessions/session-id-123', $request->getUri()->getPath());
$this->assertEquals('', strval($request->getBody()));

$this->assertAuthorization($request);
}

/**
* Make sure that customer cannot fetch the data.
*
* @return void
*/
public function testFetch()
{
$this->setExpectedException('Klarna\Exceptions\NotApplicableException');

$session = new Sessions($this->connector, 'test');
$session->fetch();
}
}

0 comments on commit 0388dcd

Please sign in to comment.