| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 | 
							- <?php
 
- /**
 
-  * This file is part of the Klarna KP module
 
-  *
 
-  * (c) Klarna Bank AB (publ)
 
-  *
 
-  * For the full copyright and license information, please view the NOTICE
 
-  * and LICENSE files that were distributed with this source code.
 
-  */
 
- namespace Klarna\Kp\Model\Api\Request;
 
- use Klarna\Core\Model\Api\Exception as KlarnaApiException;
 
- use Klarna\Kp\Api\Data\AddressInterface;
 
- use Klarna\Kp\Api\Data\AttachmentInterface;
 
- use Klarna\Kp\Api\Data\CustomerInterface;
 
- use Klarna\Kp\Api\Data\OptionsInterface;
 
- use Klarna\Kp\Api\Data\OrderlineInterface;
 
- use Klarna\Kp\Api\Data\RequestInterface;
 
- use Klarna\Kp\Api\Data\UrlsInterface;
 
- use Klarna\Kp\Model\Api\RequestFactory;
 
- use Magento\Framework\DataObject;
 
- /**
 
-  * Class Builder
 
-  *
 
-  * @package Klarna\Kp\Model\Api\Request
 
-  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 
-  * @SuppressWarnings(PHPMD.TooManyFields)
 
-  */
 
- class Builder
 
- {
 
-     /**
 
-      * @var string
 
-      */
 
-     private $merchant_reference1;
 
-     /**
 
-      * @var string
 
-      */
 
-     private $merchant_reference2;
 
-     /**
 
-      * @var string
 
-      */
 
-     private $purchase_country;
 
-     /**
 
-      * @var string
 
-      */
 
-     private $purchase_currency;
 
-     /**
 
-      * @var string
 
-      */
 
-     private $locale;
 
-     /**
 
-      * @var int
 
-      */
 
-     private $order_tax_amount = 0;
 
-     /**
 
-      * @var int
 
-      */
 
-     private $order_amount = 0;
 
-     /**
 
-      * @var CustomerInterface
 
-      */
 
-     private $customer;
 
-     /**
 
-      * @var AttachmentInterface
 
-      */
 
-     private $attachment;
 
-     /**
 
-      * @var AddressInterface
 
-      */
 
-     private $billing_address;
 
-     /**
 
-      * @var AddressInterface
 
-      */
 
-     private $shipping_address;
 
-     /**
 
-      * @var UrlsInterface
 
-      */
 
-     private $merchant_urls;
 
-     /**
 
-      * @var OptionsInterface
 
-      */
 
-     private $options;
 
-     /**
 
-      * @var OrderlineInterface[]
 
-      */
 
-     private $orderlines = [];
 
-     /**
 
-      * @var RequestFactory
 
-      */
 
-     private $requestFactory;
 
-     /**
 
-      * @var AddressFactory
 
-      */
 
-     private $addressFactory;
 
-     /**
 
-      * @var AttachmentFactory
 
-      */
 
-     private $attachmentFactory;
 
-     /**
 
-      * @var CustomerFactory
 
-      */
 
-     private $customerFactory;
 
-     /**
 
-      * @var MerchantUrlsFactory
 
-      */
 
-     private $urlFactory;
 
-     /**
 
-      * @var OptionsFactory
 
-      */
 
-     private $optionsFactory;
 
-     /**
 
-      * @var OrderlineFactory
 
-      */
 
-     private $orderlineFactory;
 
-     /**
 
-      * Builder constructor.
 
-      *
 
-      * @param RequestFactory     $requestFactory
 
-      * @param AddressFactory     $addressFactory
 
-      * @param AttachmentFactory  $attachmentFactory
 
-      * @param CustomerFactory    $customerFactory
 
-      * @param MerchantUrlFactory $merchantUrlFactory
 
-      * @param OptionsFactory     $optionsFactory
 
-      * @param OrderlineFactory   $orderlineFactory
 
-      */
 
-     public function __construct(
 
-         RequestFactory $requestFactory,
 
-         AddressFactory $addressFactory,
 
-         AttachmentFactory $attachmentFactory,
 
-         CustomerFactory $customerFactory,
 
-         MerchantUrlsFactory $urlFactory,
 
-         OptionsFactory $optionsFactory,
 
-         OrderlineFactory $orderlineFactory
 
-     ) {
 
-         $this->requestFactory = $requestFactory;
 
-         $this->addressFactory = $addressFactory;
 
-         $this->attachmentFactory = $attachmentFactory;
 
-         $this->customerFactory = $customerFactory;
 
-         $this->urlFactory = $urlFactory;
 
-         $this->optionsFactory = $optionsFactory;
 
-         $this->orderlineFactory = $orderlineFactory;
 
-     }
 
-     /**
 
-      * @param $data
 
-      * @return $this
 
-      */
 
-     public function setAttachment($data)
 
-     {
 
-         $this->attachment = $this->attachmentFactory->create(['data' => $data]);
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param $data
 
-      * @return $this
 
-      */
 
-     public function setBillingAddress($data)
 
-     {
 
-         $this->billing_address = $this->addressFactory->create(['data' => $data]);
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param $data
 
-      * @return $this
 
-      */
 
-     public function setShippingAddress($data)
 
-     {
 
-         $this->shipping_address = $this->addressFactory->create(['data' => $data]);
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param $data
 
-      * @return $this
 
-      */
 
-     public function setMerchantUrls($data)
 
-     {
 
-         $this->merchant_urls = $this->urlFactory->create(['data' => $data]);
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param $data
 
-      * @return $this
 
-      */
 
-     public function setOptions($data)
 
-     {
 
-         $this->options = $this->optionsFactory->create(['data' => $data]);
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param $data
 
-      * @return $this
 
-      */
 
-     public function addOrderlines($data)
 
-     {
 
-         foreach ($data as $key => $orderLine) {
 
-             $this->orderlines[$key] = $this->orderlineFactory->create(['data' => $orderLine]);
 
-         }
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param DataObject $references
 
-      * @return $this
 
-      */
 
-     public function setMerchantReferences($references)
 
-     {
 
-         $this->merchant_reference1 = $references->getData('merchant_reference_1');
 
-         $this->merchant_reference2 = $references->getData('merchant_reference_2');
 
-         return $this;
 
-     }
 
-     /**
 
-      * @return RequestInterface
 
-      */
 
-     public function getRequest()
 
-     {
 
-         return $this->requestFactory->create([
 
-             'data' => [
 
-                 'purchase_country'    => $this->purchase_country,
 
-                 'purchase_currency'   => $this->purchase_currency,
 
-                 'locale'              => $this->locale,
 
-                 'customer'            => $this->customer,
 
-                 'options'             => $this->options,
 
-                 'order_amount'        => $this->order_amount,
 
-                 'order_tax_amount'    => $this->order_tax_amount,
 
-                 'order_lines'         => $this->orderlines,
 
-                 'urls'                => $this->merchant_urls,
 
-                 'attachment'          => $this->attachment,
 
-                 'billing_address'     => $this->billing_address,
 
-                 'shipping_address'    => $this->shipping_address,
 
-                 'merchant_reference1' => $this->merchant_reference1,
 
-                 'merchant_reference2' => $this->merchant_reference2
 
-             ]
 
-         ]);
 
-     }
 
-     /**
 
-      * @param $data
 
-      * @return $this
 
-      */
 
-     public function setCustomer($data)
 
-     {
 
-         $this->customer = $this->customerFactory->create(['data' => $data]);
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param int $amount
 
-      * @return $this
 
-      */
 
-     public function setOrderAmount($amount)
 
-     {
 
-         $this->order_amount = $amount;
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param int $amount
 
-      * @return $this
 
-      */
 
-     public function setOrderTaxAmount($amount)
 
-     {
 
-         $this->order_tax_amount = $amount;
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param array  $requiredAttributes
 
-      * @param string $type
 
-      * @return $this
 
-      * @throws KlarnaApiException
 
-      */
 
-     public function validate($requiredAttributes, $type)
 
-     {
 
-         $missingAttributes = [];
 
-         foreach ($requiredAttributes as $requiredAttribute) {
 
-             if (null === $this->$requiredAttribute) {
 
-                 $missingAttributes[] = $requiredAttribute;
 
-             }
 
-             if (is_array($this->$requiredAttribute) && count($this->$requiredAttribute) === 0) {
 
-                 $missingAttributes[] = $requiredAttribute;
 
-             }
 
-         }
 
-         if (!empty($missingAttributes)) {
 
-             throw new KlarnaApiException(
 
-                 __(
 
-                     'Missing required attribute(s) on %1: "%2".',
 
-                     $type,
 
-                     implode(', ', $missingAttributes)
 
-                 )
 
-             );
 
-         }
 
-         $total = 0;
 
-         foreach ($this->orderlines as $orderLine) {
 
-             $total += (int)$orderLine->getTotal();
 
-         }
 
-         if ($total !== $this->order_amount) {
 
-             throw new KlarnaApiException(
 
-                 __('Order line totals do not total order_amount - %1 != %2', $total, $this->order_amount)
 
-             );
 
-         }
 
-         return $this;
 
-     }
 
-     /**
 
-      * @return string
 
-      */
 
-     public function getPurchaseCountry()
 
-     {
 
-         return $this->purchase_country;
 
-     }
 
-     /**
 
-      * @param string $purchase_country
 
-      * @return Builder
 
-      */
 
-     public function setPurchaseCountry($purchase_country)
 
-     {
 
-         $this->purchase_country = $purchase_country;
 
-         return $this;
 
-     }
 
-     /**
 
-      * @return string
 
-      */
 
-     public function getLocale()
 
-     {
 
-         return $this->locale;
 
-     }
 
-     /**
 
-      * @param string $locale
 
-      * @return Builder
 
-      */
 
-     public function setLocale($locale)
 
-     {
 
-         $this->locale = $locale;
 
-         return $this;
 
-     }
 
-     /**
 
-      * @return string
 
-      */
 
-     public function getPurchaseCurrency()
 
-     {
 
-         return $this->purchase_currency;
 
-     }
 
-     /**
 
-      * @param string $purchase_currency
 
-      * @return Builder
 
-      */
 
-     public function setPurchaseCurrency($purchase_currency)
 
-     {
 
-         $this->purchase_currency = $purchase_currency;
 
-         return $this;
 
-     }
 
- }
 
 
  |