1 <?php
2 /**
3 * Copyright 2015 Klarna AB
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * File containing the Klarna_Checkout_Order class
18 *
19 * PHP version 5.3
20 *
21 * @category Payment
22 * @package Klarna_Checkout
23 * @author Klarna <support@klarna.com>
24 * @copyright 2015 Klarna AB
25 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
26 * @link http://developers.klarna.com/
27 */
28
29 /**
30 * Implementation of the order resource
31 *
32 * @category Payment
33 * @package Klarna_Checkout
34 * @author Majid G. <majid.garmaroudi@klarna.com>
35 * @author David K. <david.keijser@klarna.com>
36 * @author Matthias Feist <matthias.feist@klarna.com>
37 * @copyright 2015 Klarna AB
38 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
39 * @link http://developers.klarna.com/
40 */
41 class Klarna_Checkout_Order extends Klarna_Checkout_Resource implements
42 Klarna_Checkout_ResourceCreateableInterface,
43 Klarna_Checkout_ResourceFetchableInterface,
44 Klarna_Checkout_ResourceUpdateableInterface
45 {
46 /**
47 * Path that is used to create resources
48 *
49 * @var string
50 */
51 protected $relativePath = '/checkout/orders';
52
53 /**
54 * Content Type to use
55 *
56 * @var string
57 */
58 protected $contentType
59 = "application/vnd.klarna.checkout.aggregated-order-v2+json";
60
61 /**
62 * Create a new order.
63 *
64 * @param Klarna_Checkout_ConnectorInterface $connector connector to use
65 * @param string $id Order id
66 */
67 public function __construct(
68 Klarna_Checkout_ConnectorInterface $connector,
69 $id = null
70 ) {
71 parent::__construct($connector);
72
73 if ($id !== null) {
74 $uri = $this->connector->getDomain() . "{$this->relativePath}/{$id}";
75 $this->setLocation($uri);
76 }
77 }
78
79 /**
80 * Create a new order
81 *
82 * @param array $data data to initialise order resource with
83 *
84 * @return void
85 */
86 public function create(array $data)
87 {
88 $options = array(
89 'url' => $this->connector->getDomain() . $this->relativePath,
90 'data' => $data
91 );
92
93 $this->connector->apply('POST', $this, $options);
94 }
95
96 /**
97 * Fetch order data
98 *
99 * @return void
100 */
101 public function fetch()
102 {
103 $options = array(
104 'url' => $this->location
105 );
106 $this->connector->apply('GET', $this, $options);
107 }
108
109 /**
110 * Update order data
111 *
112 * @param array $data data to update order resource with
113 *
114 * @return void
115 */
116 public function update(
117 array $data
118 ) {
119 $options = array(
120 'url' => $this->location,
121 'data' => $data
122 );
123 $this->connector->apply('POST', $this, $options);
124 }
125 }
126