| | <?php |
| | +require_once 'Currency.php'; |
| | +require_once 'ExchangeRate.php'; |
| | +require_once 'Money.php'; |
| | + |
| | class Order { |
| | private $summary = []; |
 |
| | public function process( array $response, int $statusCode ) { |
| | if( $statusCode === 201 ) { |
| | - $this->processSuccess( $response ); |
| | + $this->processResponse( $response ); |
| | } else { |
| | $this->processError( $response, $statusCode ); |
| | } |
| | } |
| | |
| | - protected function processSuccess( array $response ): void { |
| | - $this->summary = $response; |
| | - $this->renderMethod = 'renderSummary'; |
| | - $this->success = true; |
| | + protected function processResponse( array $response ): void { |
| | + $errorDetail = ''; |
| | + $lineItem = $response['line_item_costs'][0] ?? null; |
| | + |
| | + if( !isset( $lineItem['total_cost_excl_tax'] ) || |
| | + !isset( $response['total_cost_incl_tax'] ) || |
| | + !isset( $response['shipping_address'] ) ) { |
| | + $errorDetail = 'Invalid order data received from server.'; |
| | + } else { |
| | + $bookPrice = (float)( $lineItem['total_cost_excl_tax'] ); |
| | + |
| | + if( $bookPrice <= 0 ) { |
| | + $errorDetail = 'Invalid book price received.'; |
| | + } |
| | + } |
| | + |
| | + if( empty( $errorDetail ) ) { |
| | + $this->summary = $response; |
| | + $this->renderMethod = 'renderSummary'; |
| | + $this->success = true; |
| | + } else { |
| | + $this->processError( ['detail' => $errorDetail], 500 ); |
| | + } |
| | } |
| | |
 |
| | |
| | private function renderSummary() { |
| | + $EXCLUDING = "total_cost_excl_tax"; |
| | + $config = new Configuration(); |
| | + $forex = $config->createExchangeRate( Currency::CAD ); |
| | + |
| | $address = $this->summary[ 'shipping_address' ]; |
| | - $currency = $this->escape( $this->summary[ 'currency' ] ); |
| | + $target = Currency::fromCountry( $address['country'] ?? 'CA' ); |
| | + |
| | + $price = new Money( $this->summary['line_item_costs'][0][$EXCLUDING] ); |
| | + $shipping = new Money( $this->summary['shipping_cost'][$EXCLUDING] ); |
| | + $fulfillment = new Money( $this->summary['fulfillment_cost'][$EXCLUDING] ); |
| | + $taxes = new Money( $this->summary['total_tax'] ); |
| | + $total = new Money( $this->summary['total_cost_incl_tax'] ); |
| | + |
| | + $price = $forex->convert( $price, $target ); |
| | + $shipping = $forex->convert( $shipping, $target ); |
| | + $fulfillment = $forex->convert( $fulfillment, $target ); |
| | + $taxes = $forex->convert( $taxes, $target ); |
| | + $total = $forex->convert( $total, $target ); |
| | ?> |
| | <section> |
 |
| | <tr> |
| | <td>Book price</td> |
| | - <td><?= $this->escape( $this->summary[ 'line_item_costs' ][ 0 ] |
| | - [ 'total_cost_excl_tax' ] ) ?> <?= $currency ?></td> |
| | + <td><?= $price->render(); ?></td> |
| | </tr> |
| | <tr> |
| | <td>Shipping</td> |
| | - <td><?= $this->escape( $this->summary[ 'shipping_cost' ] |
| | - [ 'total_cost_excl_tax' ] ) ?> <?= $currency ?></td> |
| | + <td><?= $shipping->render(); ?></td> |
| | + </tr> |
| | + <tr> |
| | + <td>Fulfillment</td> |
| | + <td><?= $fulfillment->render(); ?></td> |
| | </tr> |
| | <tr> |
| | <td>Taxes</td> |
| | - <td><?= $this->escape( $this->summary[ 'total_tax' ] ) ?> |
| | - <?= $currency ?></td> |
| | + <td><?= $taxes->render(); ?></td> |
| | </tr> |
| | <tr> |
| | <td><strong>Amount due</strong></td> |
| | - <td><strong><?= $this->escape( |
| | - $this->summary[ 'total_cost_incl_tax' ] ) ?> |
| | - <?= $currency ?></strong></td> |
| | + <td><strong><?= $total->render(); ?></strong></td> |
| | </tr> |
| | </table> |