Completed
Push — master ( ba7de8...b94793 )
by Aimeos
02:24
created

Standard::getUrlUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi\Order;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API standard client
19
 *
20
 * @package Client
21
 * @subpackage JsonApi
22
 */
23
class Standard
24
	extends \Aimeos\Client\JsonApi\Base
25
	implements \Aimeos\Client\JsonApi\Iface
26
{
27
	/**
28
	 * Returns the resource or the resource list
29
	 *
30
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
31
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
32
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
33
	 */
34
	public function get( ServerRequestInterface $request, ResponseInterface $response )
35
	{
36
		$view = $this->getView();
37
38
		try
39
		{
40
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'order' );
41
42
			if( ( $id = $view->param( 'id' ) ) != '' )
43
			{
44
				$view->items = $cntl->getItem( $id );
45
				$view->total = 1;
46
			}
47
			else
48
			{
49
				$total = 0;
50
				$filter = $cntl->createFilter();
51
				$this->initCriteria( $filter, $view->param() );
52
53
				$view->items = $cntl->searchItems( $filter, $total );
54
				$view->total = $total;
55
			}
56
57
			$status = 200;
58
		}
59
		catch( \Aimeos\Controller\Frontend\Exception $e )
60
		{
61
			$status = 403;
62
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
63
		}
64
		catch( \Aimeos\MShop\Exception $e )
65
		{
66
			$status = 404;
67
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
68
		}
69
		catch( \Exception $e )
70
		{
71
			$status = 500;
72
			$view->errors = $this->getErrorDetails( $e );
73
		}
74
75
		return $this->render( $response, $view, $status );
76
	}
77
78
79
	/**
80
	 * Creates or updates the resource or the resource list
81
	 *
82
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
83
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
84
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
85
	 */
86
	public function post( ServerRequestInterface $request, ResponseInterface $response )
87
	{
88
		$view = $this->getView();
89
90
		try
91
		{
92
			$body = (string) $request->getBody();
93
94
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
95
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
96
			}
97
98
			if( !isset( $payload->data->attributes->{'order.baseid'} ) ) {
99
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Required attribute "order.baseid" is missing' ), 400 );
100
			}
101
102
			$basket = $this->getBasket( $payload->data->attributes->{'order.baseid'} );
103
			$item = $this->createOrder( $payload->data->attributes->{'order.baseid'} );
104
105
			$view->form = $this->getPaymentForm( $basket, $item, (array) $payload->data->attributes );
106
			$view->items = $item;
107
			$view->total = 1;
108
109
			$status = 201;
110
		}
111
		catch( \Aimeos\Client\JsonApi\Exception $e )
112
		{
113
			$status = $e->getCode();
114
			$view->errors = $this->getErrorDetails( $e, 'client/jsonapi' );
115
		}
116
		catch( \Aimeos\Controller\Frontend\Exception $e )
117
		{
118
			$status = 403;
119
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
120
		}
121
		catch( \Aimeos\MShop\Exception $e )
122
		{
123
			$status = 404;
124
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
125
		}
126
		catch( \Exception $e )
127
		{
128
			$status = 500;
129
			$view->errors = $this->getErrorDetails( $e );
130
		}
131
132
		return $this->render( $response, $view, $status );
133
	}
134
135
136
	/**
137
	 * Adds and returns a new order item for the given order base ID
138
	 *
139
	 * @param string $baseId Unique order base ID
140
	 * @return \Aimeos\MShop\Order\Item\Iface New order item
141
	 */
142
	protected function createOrder( $baseId )
143
	{
144
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'order' );
145
		$item = $cntl->addItem( $baseId, 'jsonapi' );
146
		$cntl->block( $item );
147
148
		return $item;
149
	}
150
151
152
	/**
153
	 * Returns the basket object for the given ID
154
	 *
155
	 * @param string $basketId Unique order base ID
156
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Basket object including only the services
157
	 * @throws \Aimeos\Client\JsonApi\Exception If basket ID is not the same as stored before in the current session
158
	 */
159
	protected function getBasket( $basketId )
160
	{
161
		$baseId = $this->getContext()->getSession()->get( 'aimeos/order.baseid' );
162
163
		$parts = \Aimeos\MShop\Order\Manager\Base\Base::PARTS_SERVICE;
164
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'basket' );
165
		$basket = $cntl->load( $basketId, $parts, false );
166
167
		if( $baseId != $basket->getId() )
168
		{
169
			$msg = sprintf( 'No basket for the "order.baseid" ("%1$s") found', $baseId );
170
			throw new \Aimeos\Client\JsonApi\Exception( $msg, 403 );
171
		}
172
173
		return $basket;
174
	}
175
176
177
	/**
178
	 * Returns the form helper object for building the payment form in the frontend
179
	 *
180
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Saved basket object including payment service object
181
	 * @param \Aimeos\MShop\Order\Item\Iface $orderItem Saved order item created for the basket object
182
	 * @param array $attributes Associative list of payment data pairs
183
	 * @return \Aimeos\MShop\Common\Item\Helper\Form\Iface|null Form object with URL, parameters, etc.
184
	 * 	or null if no form data is required
185
	 */
186
	protected function getPaymentForm( \Aimeos\MShop\Order\Item\Base\Iface $basket,
187
		\Aimeos\MShop\Order\Item\Iface $orderItem, array $attributes )
188
	{
189
		$view = $this->getView();
190
		$service = $basket->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT );
191
192
		$config = array( 'absoluteUri' => true, 'namespace' => false );
193
		$args = array( 'code' => $service->getCode(), 'orderid' => $orderItem->getId() );
194
		$urls = array(
195
			'payment.url-success' => $this->getUrlConfirm( $view, $args, $config ),
196
			'payment.url-update' => $this->getUrlUpdate( $view, $args, $config ),
197
		);
198
199
		foreach( $service->getAttributes() as $item ) {
200
			$attributes[$item->getCode()] = $item->getValue();
201
		}
202
203
		$serviceCntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'service' );
204
		return $serviceCntl->process( $orderItem, $service->getServiceId(), $urls, $attributes );
205
	}
206
207
208
	/**
209
	 * Returns the URL to the confirm page.
210
	 *
211
	 * @param \Aimeos\MW\View\Iface $view View object
212
	 * @param array $params Parameters that should be part of the URL
213
	 * @param array $config Default URL configuration
214
	 * @return string URL string
215
	 */
216
	protected function getUrlConfirm( \Aimeos\MW\View\Iface $view, array $params, array $config )
217
	{
218
		$target = $view->config( 'client/html/checkout/confirm/url/target' );
219
		$cntl = $view->config( 'client/html/checkout/confirm/url/controller', 'checkout' );
220
		$action = $view->config( 'client/html/checkout/confirm/url/action', 'confirm' );
221
		$config = $view->config( 'client/html/checkout/confirm/url/config', $config );
222
223
		return $view->url( $target, $cntl, $action, $params, [], $config );
224
	}
225
226
227
	/**
228
	 * Returns the URL to the update page.
229
	 *
230
	 * @param \Aimeos\MW\View\Iface $view View object
231
	 * @param array $params Parameters that should be part of the URL
232
	 * @param array $config Default URL configuration
233
	 * @return string URL string
234
	 */
235
	protected function getUrlUpdate( \Aimeos\MW\View\Iface $view, array $params, array $config )
236
	{
237
		$target = $view->config( 'client/html/checkout/update/url/target' );
238
		$cntl = $view->config( 'client/html/checkout/update/url/controller', 'checkout' );
239
		$action = $view->config( 'client/html/checkout/update/url/action', 'update' );
240
		$config = $view->config( 'client/html/checkout/update/url/config', $config );
241
242
		return $view->url( $target, $cntl, $action, $params, [], $config );
243
	}
244
245
246
	/**
247
	 * Returns the response object with the rendered header and body
248
	 *
249
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
250
	 * @param \Aimeos\MW\View\Iface $view View instance
251
	 * @param integer $status HTTP status code
252
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
253
	 */
254
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
255
	{
256
		/** client/jsonapi/order/standard/template
257
		 * Relative path to the order JSON API template
258
		 *
259
		 * The template file contains the code and processing instructions
260
		 * to generate the result shown in the JSON API body. The
261
		 * configuration string is the path to the template file relative
262
		 * to the templates directory (usually in client/jsonapi/templates).
263
		 *
264
		 * You can overwrite the template file configuration in extensions and
265
		 * provide alternative templates. These alternative templates should be
266
		 * named like the default one but with the string "standard" replaced by
267
		 * an unique name. You may use the name of your project for this. If
268
		 * you've implemented an alternative client class as well, "standard"
269
		 * should be replaced by the name of the new class.
270
		 *
271
		 * @param string Relative path to the template creating the body of the JSON API
272
		 * @since 2017.03
273
		 * @category Developer
274
		 */
275
		$tplconf = 'client/jsonapi/order/standard/template';
276
		$default = 'order/standard.php';
277
278
		$body = $view->render( $view->config( $tplconf, $default ) );
279
280
		return $response->withHeader( 'Allow', 'GET,POST' )
281
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
282
			->withBody( $view->response()->createStreamFromString( $body ) )
283
			->withStatus( $status );
284
	}
285
}
286