From ad444c20849a39ad7d477309bfffea5dfc5a5af9 Mon Sep 17 00:00:00 2001 From: Aleksandr Manichev Date: Fri, 15 Feb 2019 19:34:33 +0500 Subject: [PATCH 1/5] * add support for getReceiptStatus.do method --- src/Gateway.php | 22 ++ src/Message/OrderReceiptStatusRequest.php | 69 +++++ src/Message/OrderReceiptStatusResponse.php | 278 +++++++++++++++++++++ 3 files changed, 369 insertions(+) create mode 100644 src/Message/OrderReceiptStatusRequest.php create mode 100644 src/Message/OrderReceiptStatusResponse.php diff --git a/src/Gateway.php b/src/Gateway.php index 7ea6525..aed3f95 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -13,6 +13,7 @@ use Omnipay\Sberbank\Message\GetLastOrdersForMerchantsRequest; use Omnipay\Sberbank\Message\OrderStatusRequest; use Omnipay\Sberbank\Message\PurchaseRequest; +use Omnipay\Sberbank\Message\OrderReceiptStatusRequest; use Omnipay\Sberbank\Message\RefundRequest; use Omnipay\Sberbank\Message\UnBindCardRequest; use Omnipay\Sberbank\Message\UpdateSSLCardListRequest; @@ -310,6 +311,27 @@ public function supportsExtendedOrderStatus() return method_exists($this, 'extendedOrderStatus'); } + /** + * Receipt status request + * + * @param array $options + * @return RequestInterface + */ + public function orderReceiptStatus(array $options = []): RequestInterface + { + return $this->createRequest(OrderReceiptStatusRequest::class, $options); + } + + /** + * Supports receiptStatus + * + * @return boolean True if this gateway supports the orderReceiptStatus() method + */ + public function supportsOrderReceiptStatus() + { + return method_exists($this, 'orderReceiptStatus'); + } + /** * Request to verify the involvement of the card in 3DS * diff --git a/src/Message/OrderReceiptStatusRequest.php b/src/Message/OrderReceiptStatusRequest.php new file mode 100644 index 0000000..4577c1f --- /dev/null +++ b/src/Message/OrderReceiptStatusRequest.php @@ -0,0 +1,69 @@ +getOrderId() && !$this->getOrderNumber()) { + throw new InvalidRequestException('You must specify one of the parameters - orderId or orderNumber'); + } + + return $this->specifyAdditionalParameters([], ['orderId', 'orderNumber', 'language', 'uuid']); + } + + /** + * @return string + */ + public function getMethod() + { + return 'getReceiptStatus.do'; + } + + /** + * @return mixed + */ + public function getOrderNumber() + { + return $this->getParameter('orderNumber'); + } + + /** + * @param $value + * @return \Omnipay\Common\Message\AbstractRequest + */ + public function setOrderNumber($value) + { + return $this->setParameter('orderNumber', $value); + } + + /** + * Get receipt ID in the fiscalizer + * + * @return string + */ + public function getUuid() + { + return $this->getParameter('uuid'); + } + + /** + * Set receipt ID in the fiscalizer + * + * @param string $value + * + * @return OrderReceiptStatusRequest + */ + public function setUuid($value) + { + return $this->setParameter('uuid', $value); + } +} \ No newline at end of file diff --git a/src/Message/OrderReceiptStatusResponse.php b/src/Message/OrderReceiptStatusResponse.php new file mode 100644 index 0000000..1cda739 --- /dev/null +++ b/src/Message/OrderReceiptStatusResponse.php @@ -0,0 +1,278 @@ +data) ? $this->data['orderNumber'] : null; + } + + /** + * Get the order number in the payment system. Unique within the system. + * + * @return string|null + */ + public function getOrderId() + { + return array_key_exists('orderId', $this->data) ? $this->data['orderId'] : null; + } + + /** + * Server name + * + * @return string|null + */ + public function getDaemonCode() + { + return array_key_exists('daemonŠ”ode', $this->data) ? $this->data['daemonŠ”ode'] : null; + } + + /** + * Checkout equipment code + * + * @return string|null + */ + public function getDeviceCode() + { + return array_key_exists('deviceCode', $this->data) ? $this->data['deviceCode'] : null; + } + + /** + * Receipts list + * + * @return array + */ + public function getReceipts() + { + return array_key_exists('receipt', $this->data) ? $this->data['receipt'] : []; + } + + /** + * Receipt + * + * @param int $index + * + * @return array + */ + public function getReceipt($index) + { + $receiptList = $this->getReceipts(); + return array_key_exists('receipt', $receiptList[$index]) ? $receiptList[$index] : []; + } + + /** + * Receipt status + * + * @param int $index + * + * @return int|null + */ + public function getReceiptStatus($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('receiptStatus', $receipt) ? $receipt['receiptStatus'] : null; + } + + /** + * Receipt ID in the fiscalizer + * + * @param int $index + * + * @return mixed|null + */ + public function getReceiptUuid($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('uuid', $receipt) ? $receipt['uuid'] : null; + } + + /** + * Receipt shift number + * + * @param int $index + * + * @return int|null + */ + public function getReceiptShiftNumber($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('shift_number', $receipt) ? $receipt['shift_number'] : null; + } + + /** + * Receipt number + * + * @param int $index + * + * @return int|null + */ + public function getReceiptNumber($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('receipt_number', $receipt) ? $receipt['receipt_number'] : null; + } + + /** + * Receipt date time in the fiscalizer + * + * @param int $index + * + * @return string|null + */ + public function getReceiptDatetime($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('receipt_datetime', $receipt) ? $receipt['receipt_datetime'] : null; + } + + /** + * Fiscalizer number + * + * @param int $index + * + * @return string|null + */ + public function getReceiptFnNumber($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('fn_number', $receipt) ? $receipt['fn_number'] : null; + } + + /** + * Checkout equipment registration number + * + * @param int $index + * + * @return string|null + */ + public function getReceiptDeviceNumber($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('device_number', $receipt) ? $receipt['device_number'] : null; + } + + /** + * Fiscal document number + * + * @param int $index + * + * @return int|null + */ + public function getReceiptFiscalDocumentNumber($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('fiscal_document_number', $receipt) ? $receipt['fiscal_document_number'] : null; + } + + /** + * Fiscal document attribute + * + * @param int $index + * + * @return string|null + */ + public function getReceiptFiscalDocumentAttribute($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('fiscal_document_attribute', $receipt) ? $receipt['fiscal_document_attribute'] : null; + } + + /** + * Amount total + * + * @param int $index + * + * @return string|null + */ + public function getReceiptAmountTotal($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('amount_total', $receipt) ? $receipt['amount_total'] : null; + } + + /** + * Checkout equipment serial number + * + * @param int $index + * + * @return string|null + */ + public function getReceiptDeviceSerialNumber($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('serial_number', $receipt) ? $receipt['serial_number'] : null; + } + + /** + * The Federal Tax Service site url + * + * @param int $index + * + * @return string|null + */ + public function getReceiptFTSSite($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('fnsSite', $receipt) ? $receipt['fnsSite'] : null; + } + + /** + * OFD data + * + * @param int $index + * + * @return array + */ + public function getReceiptOFD($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('OFD', $receipt) ? $receipt['OFD'] : []; + } + + /** + * OFD operator name + * + * @param int $index + * + * @return string|null + */ + public function getReceiptOFDName($index) + { + $receiptOFD = $this->getReceiptOFD($index); + return array_key_exists('name', $receiptOFD) ? $receiptOFD['name'] : null; + } + + /** + * OFD operator site url + * + * @param int $index + * + * @return string|null + */ + public function getReceiptOFDWebsite($index) + { + $receiptOFD = $this->getReceiptOFD($index); + return array_key_exists('website', $receiptOFD) ? $receiptOFD['website'] : null; + } + + /** + * OFD operator INN + * + * @param int $index + * + * @return string|null + */ + public function getReceiptOFDINN($index) + { + $receiptOFD = $this->getReceiptOFD($index); + return array_key_exists('INN', $receiptOFD) ? $receiptOFD['INN'] : null; + } +} \ No newline at end of file From d7ef21039626a32e683c769b6f24122a086d6664 Mon Sep 17 00:00:00 2001 From: Aleksandr Manichev Date: Tue, 19 Feb 2019 15:26:59 +0500 Subject: [PATCH 2/5] * correctly add json serializable parameters * add orderBundle and taxSystem parameters * add FiscalOFDAdapter * fix getting receipt from list --- composer.json | 1 + src/Message/AbstractRequest.php | 49 +++++++- src/Message/AuthorizeRequest.php | 56 ++++++++- src/Message/OrderReceiptStatusResponse.php | 17 ++- src/Util/FiscalOFDReceiptAdapter.php | 127 +++++++++++++++++++++ 5 files changed, 239 insertions(+), 11 deletions(-) create mode 100644 src/Util/FiscalOFDReceiptAdapter.php diff --git a/composer.json b/composer.json index 5995210..0dd8862 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ ], "require": { "php": "^7.1", + "ext-json": "*", "omnipay/common": "v3.0.2" }, "require-dev": { diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 415ab03..8aaf9b5 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -193,14 +193,51 @@ public function sendData($data) public function specifyAdditionalParameters(array $data, array $additionalParams): array { foreach ($additionalParams as $param) { - $method = 'get' . ucfirst($param); - if (method_exists($this, $method)) { - $value = $this->{$method}(); - if ($value) { - $data[$param] = $value; - } + $value = $this->getParameterValue($param); + if ($value !== null) { + $data[$param] = $value; } } + return $data; } + + /** + * Add additional json encoded params to data + * + * @param array $data + * @param array $additionalParams + * @return array + */ + public function specifyAdditionalJsonParameters(array $data, array $additionalParams): array + { + foreach ($additionalParams as $param) { + $value = $this->getParameterValue($param); + if ($value !== null) { + $data[$param] = json_encode($value); + } + } + + return $data; + } + + /** + * Get parameter value by name + * + * @param string $param + * + * @return mixed|null + */ + protected function getParameterValue($param) + { + $method = 'get' . ucfirst($param); + if (method_exists($this, $method)) { + $value = $this->{$method}(); + if ($value) { + return $value; + } + } + + return null; + } } diff --git a/src/Message/AuthorizeRequest.php b/src/Message/AuthorizeRequest.php index 0f7e5c0..8921372 100644 --- a/src/Message/AuthorizeRequest.php +++ b/src/Message/AuthorizeRequest.php @@ -2,7 +2,9 @@ namespace Omnipay\Sberbank\Message; +use Fiscal\OFD\OrderInterface; use Omnipay\Common\Exception\RuntimeException; +use Omnipay\Sberbank\Util\FiscalOFDReceiptAdapter; /** * Class AuthorizeRequest @@ -32,13 +34,21 @@ public function getData() 'pageView', 'clientId', 'merchantLogin', - 'jsonParams', 'sessionTimeoutSecs', 'expirationDate', - 'bindingId' + 'bindingId', + 'taxSystem' ]; - return $this->specifyAdditionalParameters($data, $additionalParams); + $additionalJsonParams = [ + 'jsonParams', + 'orderBundle', + ]; + + return array_merge( + $this->specifyAdditionalParameters($data, $additionalParams), + $this->specifyAdditionalJsonParameters($data, $additionalJsonParams) + ); } /** @@ -229,6 +239,46 @@ public function setBindingId($value) return $this->setParameter('bindingId', $value); } + /** + * @return mixed + */ + public function getOrderBundle() + { + return $this->getParameter('orderBundle'); + } + + /** + * @param $value + * @return \Omnipay\Common\Message\AbstractRequest + * @throws \Omnipay\Common\Exception\RuntimeException + */ + public function setOrderBundle($value) + { + if(interface_exists('Fiscal\\OFD\\OrderInterface') && $value instanceof OrderInterface) { + $adapter = new FiscalOFDReceiptAdapter($value); + $value = $adapter->getOrderBundle(); + } + return $this->setParameter('orderBundle', $value); + } + + /** + * @return mixed + */ + public function getTaxSystem() + { + return $this->getParameter('taxSystem'); + } + + /** + * @param int $value + * @return \Omnipay\Common\Message\AbstractRequest + * @throws \Omnipay\Common\Exception\RuntimeException + */ + public function setTaxSystem($value) + { + return $this->setParameter('taxSystem', $value); + } + /** * @param mixed $data * @return object|\Omnipay\Common\Message\ResponseInterface diff --git a/src/Message/OrderReceiptStatusResponse.php b/src/Message/OrderReceiptStatusResponse.php index 1cda739..a811210 100644 --- a/src/Message/OrderReceiptStatusResponse.php +++ b/src/Message/OrderReceiptStatusResponse.php @@ -65,7 +65,7 @@ public function getReceipts() public function getReceipt($index) { $receiptList = $this->getReceipts(); - return array_key_exists('receipt', $receiptList[$index]) ? $receiptList[$index] : []; + return array_key_exists($index, $receiptList) ? $receiptList[$index] : []; } /** @@ -130,7 +130,7 @@ public function getReceiptNumber($index) public function getReceiptDatetime($index) { $receipt = $this->getReceipt($index); - return array_key_exists('receipt_datetime', $receipt) ? $receipt['receipt_datetime'] : null; + return array_key_exists('receipt_date_time', $receipt) ? $receipt['receipt_date_time'] : null; } /** @@ -211,6 +211,19 @@ public function getReceiptDeviceSerialNumber($index) return array_key_exists('serial_number', $receipt) ? $receipt['serial_number'] : null; } + /** + * ECR registration number + * + * @param int $index + * + * @return string|null + */ + public function getReceiptEcrRegistrationNumber($index) + { + $receipt = $this->getReceipt($index); + return array_key_exists('ecr_registration_number', $receipt) ? $receipt['ecr_registration_number'] : null; + } + /** * The Federal Tax Service site url * diff --git a/src/Util/FiscalOFDReceiptAdapter.php b/src/Util/FiscalOFDReceiptAdapter.php new file mode 100644 index 0000000..9188671 --- /dev/null +++ b/src/Util/FiscalOFDReceiptAdapter.php @@ -0,0 +1,127 @@ +order = $order; + } + + /** + * @return array + */ + public function getOrderBundle(): array + { + $array = [ + 'orderCreationDate' => $this->order->getCreationDate() !== null ? $this->order->getCreationDate()->format('U') : time(), + 'cartItems' => [], + 'customerDetails' => $this->customerToArray() ?: [], + ]; + + $this->position = 1; + $array['cartItems']['items'] = array_map([$this, 'cartItemToArray'], $this->order->getItems()); + + if ($this->order instanceof OrderDeliverableInterface) { + $deliveryInfo = []; + + foreach (['deliveryType', 'country', 'city', 'postAddress'] as $field) { + $method = 'get' . ucfirst($field); + $deliveryInfo[$field] = $this->order->{$method}(); + } + + if ($deliveryInfo = array_filter($deliveryInfo)) { + $array['customerDetails']['deliveryInfo'] = $deliveryInfo; + } + + if($this->order->getDeliveryOrderItem() !== null) { + $array['cartItems']['items'][] = $this->cartItemToArray($this->order->getDeliveryOrderItem()); + } + } + + return $array; + } + + /** + * Transform cart item to array. + * + * @param OrderItemInterface $cartItem + * + * @return array + */ + protected function cartItemToArray(OrderItemInterface $cartItem): array + { + $array = [ + 'positionId' => $this->position, + 'name' => $cartItem->getName(), + 'quantity' => [ + 'value' => $cartItem->getQuantity(), + 'measure' => $cartItem->getMeasure(), + ], + 'itemAmount' => $cartItem->getAmount() * 100, + 'itemCode' => $cartItem->getCode(), + 'itemPrice' => $cartItem->getPrice() * 100, + 'itemDetails' => $cartItem->getDetailParams(), + 'itemCurrency' => $cartItem->getCurrency(), + ]; + + if ($discountValue = $cartItem->getDiscountValue()) { + $array['discount']['discountValue'] = $discountValue; + $array['discount']['discountType'] = $cartItem->getDiscountType(); + } + + if ($cartItem instanceof OrderItemTaxableInterface) { + $array['tax'] = array_filter( + [ + 'taxSum' => $cartItem->getTaxSum(), + 'taxType' => $cartItem->getTaxType(), + ] + ); + } + + $this->position++; + + return array_filter($array); + } + + /** + * Transform customer to array. + * + * @return array + */ + protected function customerToArray(): array + { + $customer = $this->order->getCustomer(); + $array = []; + + if ($customer !== null) { + foreach (['email', 'phone', 'contact'] as $field) { + $method = 'get' . ucfirst($field); + $array[$field] = $customer->{$method}(); + } + } + + return array_filter($array); + } +} \ No newline at end of file From cf32922f774a78ddd0255f3b7cb468495d7af3ee Mon Sep 17 00:00:00 2001 From: Aleksandr Manichev Date: Tue, 19 Feb 2019 18:34:36 +0500 Subject: [PATCH 3/5] * fix empty customer details --- src/Util/FiscalOFDReceiptAdapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Util/FiscalOFDReceiptAdapter.php b/src/Util/FiscalOFDReceiptAdapter.php index 9188671..5c208f8 100644 --- a/src/Util/FiscalOFDReceiptAdapter.php +++ b/src/Util/FiscalOFDReceiptAdapter.php @@ -37,7 +37,7 @@ public function getOrderBundle(): array $array = [ 'orderCreationDate' => $this->order->getCreationDate() !== null ? $this->order->getCreationDate()->format('U') : time(), 'cartItems' => [], - 'customerDetails' => $this->customerToArray() ?: [], + 'customerDetails' => $this->customerToArray() ?: null, ]; $this->position = 1; From 7b346659113afd07ba878b0f0a0d0147b8fafb9b Mon Sep 17 00:00:00 2001 From: Aleksandr Manichev Date: Fri, 1 Mar 2019 20:39:02 +0500 Subject: [PATCH 4/5] * add captureItems * add refundItems * refactor FiscalOFDAdapter --- src/Message/AuthorizeRequest.php | 9 +- src/Message/CaptureRequest.php | 34 +++++- src/Message/RefundRequest.php | 33 +++++- src/Util/FiscalOFDAdapter.php | 154 +++++++++++++++++++++++++++ src/Util/FiscalOFDReceiptAdapter.php | 127 ---------------------- 5 files changed, 224 insertions(+), 133 deletions(-) create mode 100644 src/Util/FiscalOFDAdapter.php delete mode 100644 src/Util/FiscalOFDReceiptAdapter.php diff --git a/src/Message/AuthorizeRequest.php b/src/Message/AuthorizeRequest.php index 8921372..42c6527 100644 --- a/src/Message/AuthorizeRequest.php +++ b/src/Message/AuthorizeRequest.php @@ -4,7 +4,7 @@ use Fiscal\OFD\OrderInterface; use Omnipay\Common\Exception\RuntimeException; -use Omnipay\Sberbank\Util\FiscalOFDReceiptAdapter; +use Omnipay\Sberbank\Util\FiscalOFDAdapter; /** * Class AuthorizeRequest @@ -240,7 +240,7 @@ public function setBindingId($value) } /** - * @return mixed + * @return array|null */ public function getOrderBundle() { @@ -248,14 +248,15 @@ public function getOrderBundle() } /** - * @param $value + * @param OrderInterface|array|null $value + * * @return \Omnipay\Common\Message\AbstractRequest * @throws \Omnipay\Common\Exception\RuntimeException */ public function setOrderBundle($value) { if(interface_exists('Fiscal\\OFD\\OrderInterface') && $value instanceof OrderInterface) { - $adapter = new FiscalOFDReceiptAdapter($value); + $adapter = new FiscalOFDAdapter($value); $value = $adapter->getOrderBundle(); } return $this->setParameter('orderBundle', $value); diff --git a/src/Message/CaptureRequest.php b/src/Message/CaptureRequest.php index 60bbb6d..19266e2 100644 --- a/src/Message/CaptureRequest.php +++ b/src/Message/CaptureRequest.php @@ -2,8 +2,13 @@ namespace Omnipay\Sberbank\Message; +use Fiscal\OFD\OrderInterface; +use Fiscal\OFD\OrderItemInterface; +use Omnipay\Sberbank\Util\FiscalOFDAdapter; + /** * Class CaptureRequest + * * @package Omnipay\Sberbank\Message */ class CaptureRequest extends AbstractRequest @@ -21,7 +26,11 @@ public function getData() 'amount' => $this->getAmountInteger(), ]; - return $data; + $additionalJsonParams = [ + 'depositItems', + ]; + + return $this->specifyAdditionalJsonParameters($data, $additionalJsonParams); } /** @@ -31,4 +40,27 @@ public function getMethod() { return 'deposit.do'; } + + /** + * @return array|null + */ + public function getDepositItems() + { + return $this->getParameter('depositItems'); + } + + /** + * @param OrderInterface|array|null $value + * + * @return \Omnipay\Common\Message\AbstractRequest + * @throws \Omnipay\Common\Exception\RuntimeException + */ + public function setDepositItems($value) + { + if(interface_exists('Fiscal\\OFD\\OrderInterface') && $value instanceof OrderInterface) { + $adapter = new FiscalOFDAdapter($value); + $value = $adapter->getDepositItems(); + } + return $this->setParameter('depositItems', $value); + } } diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php index 1eddf09..3b8c658 100644 --- a/src/Message/RefundRequest.php +++ b/src/Message/RefundRequest.php @@ -2,8 +2,13 @@ namespace Omnipay\Sberbank\Message; +use Fiscal\OFD\OrderInterface; +use Fiscal\OFD\OrderItemInterface; +use Omnipay\Sberbank\Util\FiscalOFDAdapter; + /** * Class RefundRequest + * * @package Omnipay\Sberbank\Message */ class RefundRequest extends AbstractRequest @@ -21,7 +26,11 @@ public function getData() 'amount' => $this->getAmountInteger(), ]; - return $data; + $additionalJsonParams = [ + 'refundItems', + ]; + + return $this->specifyAdditionalJsonParameters($data, $additionalJsonParams); } /** @@ -31,4 +40,26 @@ public function getMethod() { return 'refund.do'; } + + /** + * @return mixed + */ + public function getRefundItems() + { + return $this->getParameter('refundItems'); + } + + /** + * @param OrderInterface|array $value + * @return \Omnipay\Common\Message\AbstractRequest + * @throws \Omnipay\Common\Exception\RuntimeException + */ + public function setRefundItems($value) + { + if(interface_exists('Fiscal\\OFD\\OrderInterface') && $value instanceof OrderInterface) { + $adapter = new FiscalOFDAdapter($value); + $value = $adapter->getRefundItems(); + } + return $this->setParameter('refundItems', $value); + } } diff --git a/src/Util/FiscalOFDAdapter.php b/src/Util/FiscalOFDAdapter.php new file mode 100644 index 0000000..633efe4 --- /dev/null +++ b/src/Util/FiscalOFDAdapter.php @@ -0,0 +1,154 @@ +order = $order; + } + + /** + * Return orderBundle + * + * @return array + */ + public function getOrderBundle(): array + { + $orderBundle = [ + 'orderCreationDate' => $this->formatOrderCreationDate(), + 'cartItems' => [], + 'customerDetails' => $this->customerToArray() ?: null, + ]; + + $orderBundle['cartItems']['items'] = array_map([$this, 'cartItemToArray'], $this->order->getItems()); + + if ($this->order instanceof OrderDeliverableInterface) { + $deliveryInfo = [ + 'deliveryType' => $this->order->getDeliveryType(), + 'country' => $this->order->getCountry(), + 'city' => $this->order->getCity(), + 'postAddress' => $this->order->getPostAddress(), + ]; + + if ($deliveryInfo = array_filter($deliveryInfo)) { + $orderBundle['customerDetails']['deliveryInfo'] = $deliveryInfo; + } + + if ($this->order->getDeliveryOrderItem() !== null) { + $orderBundle['cartItems']['items'][] = $this->cartItemToArray($this->order->getDeliveryOrderItem()); + } + } + + return $orderBundle; + } + + /** + * Return refundItems + * + * @return array + */ + public function getRefundItems() :array + { + return ['items' => array_map([$this, 'cartItemToArray'], $this->order->getItems())]; + } + + /** + * Return refundItems + * + * @return array + */ + public function getDepositItems() :array + { + return ['items' => array_map([$this, 'cartItemToArray'], $this->order->getItems())]; + } + + /** + * Transform Cart item to array. + * + * @param OrderItemInterface $cartItem + * + * @return array + */ + protected function cartItemToArray(OrderItemInterface $cartItem): array + { + $cartItemAsArray = [ + 'positionId' => $cartItem->getPositionId(), + 'name' => $cartItem->getName(), + 'quantity' => [ + 'value' => $cartItem->getQuantity(), + 'measure' => $cartItem->getMeasure(), + ], + 'itemAmount' => $cartItem->getAmount() * 100, + 'itemCode' => $cartItem->getCode(), + 'itemPrice' => $cartItem->getPrice() * 100, + 'itemCurrency' => $cartItem->getCurrency(), + ]; + + if ($cartItem instanceof OrderItemDetailsInterface) { + $cartItemAsArray['itemDetails'] = $cartItem->getDetailParams(); + } + + if ($cartItem instanceof OrderItemDiscountInterface) { + $cartItemAsArray['discount']['discountValue'] = $cartItem->getDiscountValue(); + $cartItemAsArray['discount']['discountType'] = $cartItem->getDiscountType(); + } + + if ($cartItem instanceof OrderItemTaxableInterface) { + $cartItemAsArray['tax'] = array_filter( + [ + 'taxSum' => $cartItem->getTaxSum(), + 'taxType' => $cartItem->getTaxType(), + ] + ); + } + + return array_filter($cartItemAsArray); + } + + /** + * Transform Customer to array. + * + * @return array + */ + private function customerToArray(): array + { + $customerData = []; + + if ($this->order->getCustomer() !== null) { + $customerData = [ + 'email' => $this->order->getCustomer()->getEmail(), + 'phone' => $this->order->getCustomer()->getPhone(), + 'contact' => $this->order->getCustomer()->getContact(), + ]; + } + + return array_filter($customerData); + } + + /** + * Format order creation date + * + * @return int|string + */ + private function formatOrderCreationDate() : string + { + return $this->order->getCreationDate() !== null ? $this->order->getCreationDate()->format('U') : (string) time(); + } +} \ No newline at end of file diff --git a/src/Util/FiscalOFDReceiptAdapter.php b/src/Util/FiscalOFDReceiptAdapter.php deleted file mode 100644 index 5c208f8..0000000 --- a/src/Util/FiscalOFDReceiptAdapter.php +++ /dev/null @@ -1,127 +0,0 @@ -order = $order; - } - - /** - * @return array - */ - public function getOrderBundle(): array - { - $array = [ - 'orderCreationDate' => $this->order->getCreationDate() !== null ? $this->order->getCreationDate()->format('U') : time(), - 'cartItems' => [], - 'customerDetails' => $this->customerToArray() ?: null, - ]; - - $this->position = 1; - $array['cartItems']['items'] = array_map([$this, 'cartItemToArray'], $this->order->getItems()); - - if ($this->order instanceof OrderDeliverableInterface) { - $deliveryInfo = []; - - foreach (['deliveryType', 'country', 'city', 'postAddress'] as $field) { - $method = 'get' . ucfirst($field); - $deliveryInfo[$field] = $this->order->{$method}(); - } - - if ($deliveryInfo = array_filter($deliveryInfo)) { - $array['customerDetails']['deliveryInfo'] = $deliveryInfo; - } - - if($this->order->getDeliveryOrderItem() !== null) { - $array['cartItems']['items'][] = $this->cartItemToArray($this->order->getDeliveryOrderItem()); - } - } - - return $array; - } - - /** - * Transform cart item to array. - * - * @param OrderItemInterface $cartItem - * - * @return array - */ - protected function cartItemToArray(OrderItemInterface $cartItem): array - { - $array = [ - 'positionId' => $this->position, - 'name' => $cartItem->getName(), - 'quantity' => [ - 'value' => $cartItem->getQuantity(), - 'measure' => $cartItem->getMeasure(), - ], - 'itemAmount' => $cartItem->getAmount() * 100, - 'itemCode' => $cartItem->getCode(), - 'itemPrice' => $cartItem->getPrice() * 100, - 'itemDetails' => $cartItem->getDetailParams(), - 'itemCurrency' => $cartItem->getCurrency(), - ]; - - if ($discountValue = $cartItem->getDiscountValue()) { - $array['discount']['discountValue'] = $discountValue; - $array['discount']['discountType'] = $cartItem->getDiscountType(); - } - - if ($cartItem instanceof OrderItemTaxableInterface) { - $array['tax'] = array_filter( - [ - 'taxSum' => $cartItem->getTaxSum(), - 'taxType' => $cartItem->getTaxType(), - ] - ); - } - - $this->position++; - - return array_filter($array); - } - - /** - * Transform customer to array. - * - * @return array - */ - protected function customerToArray(): array - { - $customer = $this->order->getCustomer(); - $array = []; - - if ($customer !== null) { - foreach (['email', 'phone', 'contact'] as $field) { - $method = 'get' . ucfirst($field); - $array[$field] = $customer->{$method}(); - } - } - - return array_filter($array); - } -} \ No newline at end of file From ea9210e87c68b22e5595a5145250e4197e43c8ea Mon Sep 17 00:00:00 2001 From: Aleksandr Manichev Date: Wed, 27 Mar 2019 16:11:24 +0500 Subject: [PATCH 5/5] * add delivery order item to refund items * add delivery order item to capture items --- src/Util/FiscalOFDAdapter.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Util/FiscalOFDAdapter.php b/src/Util/FiscalOFDAdapter.php index 633efe4..72db1b0 100644 --- a/src/Util/FiscalOFDAdapter.php +++ b/src/Util/FiscalOFDAdapter.php @@ -64,9 +64,14 @@ public function getOrderBundle(): array * * @return array */ - public function getRefundItems() :array + public function getRefundItems(): array { - return ['items' => array_map([$this, 'cartItemToArray'], $this->order->getItems())]; + $refundItems = ['items' => array_map([$this, 'cartItemToArray'], $this->order->getItems())]; + if ($this->order instanceof OrderDeliverableInterface && $this->order->getDeliveryOrderItem() !== null) { + $refundItems['items'][] = $this->cartItemToArray($this->order->getDeliveryOrderItem()); + } + + return $refundItems; } /** @@ -74,9 +79,14 @@ public function getRefundItems() :array * * @return array */ - public function getDepositItems() :array + public function getDepositItems(): array { - return ['items' => array_map([$this, 'cartItemToArray'], $this->order->getItems())]; + $depositItems = ['items' => array_map([$this, 'cartItemToArray'], $this->order->getItems())]; + if ($this->order instanceof OrderDeliverableInterface && $this->order->getDeliveryOrderItem() !== null) { + $depositItems['items'][] = $this->cartItemToArray($this->order->getDeliveryOrderItem()); + } + + return $depositItems; } /** @@ -133,8 +143,8 @@ private function customerToArray(): array if ($this->order->getCustomer() !== null) { $customerData = [ - 'email' => $this->order->getCustomer()->getEmail(), - 'phone' => $this->order->getCustomer()->getPhone(), + 'email' => $this->order->getCustomer()->getEmail(), + 'phone' => $this->order->getCustomer()->getPhone(), 'contact' => $this->order->getCustomer()->getContact(), ]; } @@ -147,7 +157,7 @@ private function customerToArray(): array * * @return int|string */ - private function formatOrderCreationDate() : string + private function formatOrderCreationDate(): string { return $this->order->getCreationDate() !== null ? $this->order->getCreationDate()->format('U') : (string) time(); }