1 <?php
2 /**
3 * Copyright 2016 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 namespace Klarna\XMLRPC;
18
19 /**
20 * Checkout service request class.
21 *
22 * @example docs/examples/checkoutService.php How to use the checkout service.
23 */
24 class CheckoutServiceRequest
25 {
26 /**
27 * Configuration.
28 *
29 * @var ArrayAccess
30 */
31 protected $config;
32
33 /**
34 * Request parameters.
35 *
36 * @var array
37 */
38 protected $params;
39
40 /**
41 * Request URI.
42 *
43 * @var string
44 */
45 protected $uri = 'https://api.klarna.com/touchpoint/checkout/';
46
47 /**
48 * Media type of the response.
49 *
50 * @var string
51 */
52 protected $accept
53 = 'application/vnd.klarna.touchpoint-checkout.payment-methods-v1+json';
54
55 /**
56 * Constructor.
57 *
58 * @param ArrayAccess $config Configuration
59 * @param array $params Parameters used to build query.
60 */
61 public function __construct($config, $params)
62 {
63 $this->config = $config;
64 $this->params = array_filter($params);
65
66 if (isset($config['mode']) && $config['mode'] === Klarna::BETA) {
67 $this->uri = 'https://api-test.klarna.com/touchpoint/checkout/';
68 }
69
70 if (isset($config['checkout_service_uri'])) {
71 $this->uri = $config['checkout_service_uri'];
72 }
73 }
74
75 /**
76 * Create a checkout service response.
77 *
78 * @param int $code HTTP status code
79 * @param string $body HTTP body
80 *
81 * @return CheckoutServiceResponse Checkout service response
82 */
83 public function createResponse($code, $body)
84 {
85 return new CheckoutServiceResponse($this, $code, $body);
86 }
87
88 /**
89 * Get the url associated to this request.
90 *
91 * @return string URL with query
92 */
93 public function getURL()
94 {
95 return $this->uri.'?'.http_build_query($this->params);
96 }
97
98 /**
99 * Get the headers associated to this request.
100 *
101 * @return array Array of headers strings
102 */
103 public function getHeaders()
104 {
105 $digest = Klarna::digest(
106 Klarna::colon(
107 $this->config['eid'],
108 $this->params['currency'],
109 $this->config['secret']
110 )
111 );
112
113 return array(
114 "Accept: {$this->accept}",
115 "Authorization: xmlrpc-4.2 {$digest}",
116 );
117 }
118 }
119