| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- <?php
- /**
- * This file is part of the Klarna Core 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\Core\Model\Api;
- use Klarna\Core\Api\BuilderInterface;
- use Klarna\Core\Helper\ConfigHelper;
- use Klarna\Core\Helper\KlarnaConfig;
- use Klarna\Core\Model\Checkout\Orderline\Collector;
- use Magento\Customer\Api\Data\AddressInterface;
- use Magento\Customer\Model\Data\Address as CustomerAddress;
- use Magento\Directory\Helper\Data as DirectoryHelper;
- use Magento\Framework\DataObject;
- use Magento\Framework\DataObjectFactory;
- use Magento\Framework\Event\ManagerInterface as EventManager;
- use Magento\Framework\Stdlib\DateTime;
- use Magento\Framework\Url;
- use Magento\Quote\Model\Quote\Address;
- use Magento\Store\Api\Data\StoreInterface;
- /**
- * Base class to generate API configuration
- *
- * @method Builder setShippingUnitPrice($integer)
- * @method int getShippingUnitPrice()
- * @method Builder setShippingTaxRate($integer)
- * @method int getShippingTaxRate()
- * @method Builder setShippingTotalAmount($integer)
- * @method int getShippingTotalAmount()
- * @method Builder setShippingTaxAmount($integer)
- * @method int getShippingTaxAmount()
- * @method Builder setShippingTitle($string)
- * @method string getShippingTitle()
- * @method Builder setShippingReference($integer)
- * @method int getShippingReference()
- * @method Builder setDiscountUnitPrice($integer)
- * @method int getDiscountUnitPrice()
- * @method Builder setDiscountTaxRate($integer)
- * @method int getDiscountTaxRate()
- * @method Builder setDiscountTotalAmount($integer)
- * @method int getDiscountTotalAmount()
- * @method Builder setDiscountTaxAmount($integer)
- * @method int getDiscountTaxAmount()
- * @method Builder setDiscountTitle($integer)
- * @method int getDiscountTitle()
- * @method Builder setDiscountReference($integer)
- * @method int getDiscountReference()
- * @method Builder setTaxUnitPrice($integer)
- * @method int getTaxUnitPrice()
- * @method Builder setTaxTotalAmount($integer)
- * @method int getTaxTotalAmount()
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- abstract class Builder extends DataObject implements BuilderInterface
- {
- /**
- * @var string
- */
- public $prefix = '';
- /**
- * @var Collector
- */
- protected $orderLineCollector = null;
- /**
- * @var EventManager
- */
- protected $eventManager;
- /**
- * @var array
- */
- protected $orderLines = [];
- /**
- * @var \Magento\Sales\Model\AbstractModel|\Magento\Quote\Model\Quote
- */
- protected $object = null;
- /**
- * @var array
- */
- protected $request = [];
- /**
- * @var bool
- */
- protected $inRequestSet = false;
- /**
- * @var ConfigHelper
- */
- protected $configHelper;
- /**
- * @var Url
- */
- protected $url;
- /**
- * @var DirectoryHelper
- */
- protected $directoryHelper;
- /**
- * @var \Magento\Framework\Stdlib\DateTime\DateTime
- */
- protected $coreDate;
- /**
- * @var KlarnaConfig
- */
- protected $klarnaConfig;
- /**
- * @var DataObjectFactory
- */
- protected $dataObjectFactory;
- /**
- * @var DataObject\Copy
- */
- private $objCopyService;
- /**
- * @var \Magento\Customer\Model\AddressRegistry
- */
- private $addressRegistry;
- /**
- * Init
- *
- * @param EventManager $eventManager
- * @param Collector $collector
- * @param Url $url
- * @param ConfigHelper $configHelper
- * @param DirectoryHelper $directoryHelper
- * @param DateTime\DateTime $coreDate
- * @param DataObject\Copy $objCopyService
- * @param \Magento\Customer\Model\AddressRegistry $addressRegistry
- * @param KlarnaConfig $klarnaConfig
- * @param DataObjectFactory $dataObjectFactory
- * @param array $data
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- EventManager $eventManager,
- Collector $collector,
- Url $url,
- ConfigHelper $configHelper,
- DirectoryHelper $directoryHelper,
- \Magento\Framework\Stdlib\DateTime\DateTime $coreDate,
- \Magento\Framework\DataObject\Copy $objCopyService,
- \Magento\Customer\Model\AddressRegistry $addressRegistry,
- KlarnaConfig $klarnaConfig,
- DataObjectFactory $dataObjectFactory,
- array $data = []
- ) {
- parent::__construct($data);
- $this->eventManager = $eventManager;
- $this->orderLineCollector = $collector;
- $this->url = $url;
- $this->configHelper = $configHelper;
- $this->directoryHelper = $directoryHelper;
- $this->coreDate = $coreDate;
- $this->objCopyService = $objCopyService;
- $this->addressRegistry = $addressRegistry;
- $this->klarnaConfig = $klarnaConfig;
- $this->dataObjectFactory = $dataObjectFactory;
- }
- /**
- * Generate order body
- *
- * @param string $type
- * @return $this
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function generateRequest($type = self::GENERATE_TYPE_CREATE)
- {
- $this->collectOrderLines($this->getObject()->getStore());
- return $this;
- }
- /**
- * Collect order lines
- *
- * @param StoreInterface $store
- * @return $this
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function collectOrderLines(StoreInterface $store)
- {
- /** @var \Klarna\Core\Model\Checkout\Orderline\AbstractLine $model */
- foreach ($this->getOrderLinesCollector()->getCollectors($store) as $model) {
- $model->collect($this);
- }
- return $this;
- }
- /**
- * Get totals collector model
- *
- * @return \Klarna\Core\Model\Checkout\Orderline\Collector
- */
- public function getOrderLinesCollector()
- {
- return $this->orderLineCollector;
- }
- /**
- * Get the object used to generate request
- *
- * @return \Magento\Sales\Model\AbstractModel|\Magento\Quote\Model\Quote
- */
- public function getObject()
- {
- return $this->object;
- }
- /**
- * Set the object used to generate request
- *
- * @param \Magento\Sales\Model\AbstractModel|\Magento\Quote\Model\Quote $object
- *
- * @return $this
- */
- public function setObject($object)
- {
- $this->object = $object;
- return $this;
- }
- /**
- * Get request
- *
- * @return array
- */
- abstract public function getRequest();
- /**
- * Set generated request
- *
- * @param array $request
- * @param string $type
- *
- * @return $this
- */
- public function setRequest(array $request, $type = self::GENERATE_TYPE_CREATE)
- {
- $this->request = $this->cleanNulls($request);
- if (!$this->inRequestSet) {
- $this->inRequestSet = true;
- $this->eventManager->dispatch(
- $this->prefix . "_builder_set_request_{$type}",
- [
- 'builder' => $this
- ]
- );
- $this->eventManager->dispatch(
- $this->prefix . '_builder_set_request',
- [
- 'builder' => $this
- ]
- );
- $this->inRequestSet = false;
- }
- return $this;
- }
- /**
- * Remove items that are not allowed to be null
- *
- * @param array $request
- * @return array
- */
- protected function cleanNulls(array $request)
- {
- $disallowNulls = [
- 'customer',
- 'billing_address',
- 'shipping_address',
- 'external_payment_methods'
- ];
- foreach ($disallowNulls as $key) {
- if (empty($request[$key])) {
- unset($request[$key]);
- }
- }
- return $request;
- }
- /**
- * Get order lines as array
- *
- * @param StoreInterface $store
- * @param bool $orderItemsOnly
- *
- * @return array
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getOrderLines(StoreInterface $store, $orderItemsOnly = false)
- {
- /** @var \Klarna\Core\Model\Checkout\Orderline\AbstractLine $model */
- foreach ($this->getOrderLinesCollector()->getCollectors($store) as $model) {
- if ($model->isIsTotalCollector() && $orderItemsOnly) {
- continue;
- }
- $model->fetch($this);
- }
- return $this->orderLines;
- }
- /**
- * Add an order line
- *
- * @param array $orderLine
- *
- * @return $this
- */
- public function addOrderLine(array $orderLine)
- {
- $this->orderLines[] = $orderLine;
- return $this;
- }
- /**
- * Remove all order lines
- *
- * @return $this
- */
- public function resetOrderLines()
- {
- $this->orderLines = [];
- return $this;
- }
- /**
- * Get merchant references
- *
- * @param $quote
- * @return DataObject
- */
- public function getMerchantReferences($quote)
- {
- $merchantReferences = $this->dataObjectFactory->create([
- 'data' => [
- 'merchant_reference_1' => $quote->getReservedOrderId(),
- 'merchant_reference_2' => ''
- ]
- ]);
- $this->eventManager->dispatch(
- $this->prefix . '_merchant_reference_update',
- [
- 'quote' => $quote,
- 'merchant_reference_object' => $merchantReferences
- ]
- );
- return $merchantReferences;
- }
- /**
- * Get Terms URL
- *
- * @param $store
- * @param $configPath
- * @return mixed|string
- */
- public function getTermsUrl($store, $configPath = 'terms_url')
- {
- $termsUrl = $this->configHelper->getCheckoutConfig($configPath, $store);
- if (!empty($termsUrl) && !parse_url($termsUrl, PHP_URL_SCHEME)) {
- $termsUrl = $this->url->getDirectUrl($termsUrl, ['_nosid' => true]);
- return $termsUrl;
- }
- return $termsUrl;
- }
- /**
- * Populate prefill values
- *
- * @param $create
- * @param $quote
- * @param $store
- * @return mixed
- */
- public function prefill($create, $quote, $store)
- {
- /**
- * Customer
- */
- $create['customer'] = $this->getCustomerData($quote);
- /**
- * Billing Address
- */
- $create['billing_address'] = $this->getAddressData($quote, Address::TYPE_BILLING);
- /**
- * Shipping Address
- */
- if (isset($create['billing_address'])
- && $this->configHelper->isCheckoutConfigFlag('separate_address', $store)
- ) {
- $create['shipping_address'] = $this->getAddressData($quote, Address::TYPE_SHIPPING);
- }
- return $create;
- }
- /**
- * Get customer details
- *
- * @param $quote
- * @return array|null
- */
- public function getCustomerData($quote)
- {
- if (!$quote->getCustomerIsGuest() && $quote->getCustomerDob()) {
- return [
- 'date_of_birth' => $this->coreDate->date('Y-m-d', $quote->getCustomerDob())
- ];
- }
- return null;
- }
- /**
- * Auto fill user address details
- *
- * @param \Magento\Quote\Api\Data\CartInterface $quote
- * @param string $type
- *
- * @return array
- */
- protected function getAddressData($quote, $type = null)
- {
- $result = [];
- if ($quote->getCustomerEmail()) {
- $result['email'] = $quote->getCustomerEmail();
- }
- $customer = $quote->getCustomer();
- if ($quote->isVirtual() || $type === Address::TYPE_BILLING) {
- $address = $quote->getBillingAddress();
- if ($customer->getId() && !$address->getPostcode()) {
- $address = $this->getCustomerAddress($customer->getDefaultBilling());
- }
- } else {
- $address = $quote->getShippingAddress();
- if ($customer->getId() && !$address->getPostcode()) {
- $address = $this->getCustomerAddress($customer->getDefaultShipping());
- }
- }
- return $this->processAddress($result, $address);
- }
- /**
- * Retrieve customer address
- *
- * @param AddressInterface|string $address_id
- * @return CustomerAddress|AddressInterface
- */
- private function getCustomerAddress($address_id)
- {
- if (!$address_id) {
- return null;
- }
- if ($address_id instanceof AddressInterface) {
- return $address_id;
- }
- try {
- return $this->addressRegistry->retrieve($address_id);
- } catch (\Exception $e) {
- return null;
- }
- }
- /**
- * @param $result
- * @param $address
- * @return array
- */
- private function processAddress($result, $address = null)
- {
- $resultObject = $this->dataObjectFactory->create(['data' => $result]);
- if ($address) {
- $address->explodeStreetAddress();
- $this->objCopyService->copyFieldsetToTarget(
- 'sales_convert_quote_address',
- 'to_klarna',
- $address,
- $resultObject
- );
- if ($address->getCountryId() === 'US') {
- $resultObject->setRegion($address->getRegionCode());
- }
- }
- $street_address = $this->prepareStreetAddressArray($resultObject);
- $resultObject->setStreetAddress($street_address[0]);
- $resultObject->setData('street_address2', $street_address[1]);
- if (isset($result['email'])) {
- $resultObject->setEmail($result['email']);
- }
- return array_filter($resultObject->toArray());
- }
- /**
- * @param $resultObject
- * @return array
- */
- private function prepareStreetAddressArray($resultObject)
- {
- $street_address = $resultObject->getStreetAddress();
- if (!is_array($street_address)) {
- $street_address = [$street_address];
- }
- if (count($street_address) === 1) {
- $street_address[] = '';
- }
- return $street_address;
- }
- /**
- * @param $items
- * @return $this
- */
- public function setItems($items)
- {
- $this->setData('items', $items);
- return $this;
- }
- /**
- * @return array
- */
- public function getItems()
- {
- return $this->getData('items');
- }
- }
|