Completed
Pull Request — dev (#9)
by Andrey
07:05 queued 03:34
created

CaptureCommandPlugin::isPaymentSuccess()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Apache OSL-2
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\AmazonPay\Communication\Plugin\Oms\Command;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\AmazonpayCallTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\AmazonpayCallTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Orm\Zed\Sales\Persistence\SpySalesOrder;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject;
14
use SprykerEco\Shared\AmazonPay\AmazonPayConfig;
15
16
/**
17
 * @method \SprykerEco\Zed\AmazonPay\Business\AmazonPayFacadeInterface getFacade()
18
 */
19
class CaptureCommandPlugin extends AbstractAmazonpayCommandPlugin
20
{
21
    /**
22
     * @param array $salesOrderItems
23
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
24
     * @param \Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject $data
25
     *
26
     * @return array
27
     */
28
    public function run(array $salesOrderItems, SpySalesOrder $orderEntity, ReadOnlyArrayObject $data)
29
    {
30
        $amazonpayCallTransfers = $this->groupSalesOrderItemsByAuthId($salesOrderItems);
31
32
        $wasSuccessful = $this->captureGroupedOrderItems($amazonpayCallTransfers, $orderEntity);
33
34
        if ($wasSuccessful) {
35
            $this->updateOrdersStatus($orderEntity);
36
        }
37
38
        return [];
39
    }
40
41
    /**
42
     * @param array $amazonpayCallTransfers
43
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
44
     *
45
     * @return bool
46
     */
47
    protected function captureGroupedOrderItems(array $amazonpayCallTransfers, SpySalesOrder $orderEntity)
48
    {
49
        $wasSuccessful = false;
50
51
        foreach ($amazonpayCallTransfers as $amazonpayCallTransfer) {
52
            $amazonpayCallTransfer->setRequestedAmount(
53
                $this->getRequestedAmountByOrderAndItems($orderEntity, $amazonpayCallTransfer->getItems())
54
            );
55
            $result = $this->getFacade()->captureOrder($amazonpayCallTransfer);
56
57
            if ($this->isPaymentSuccess($result)) {
58
                $wasSuccessful = true;
59
            }
60
        }
61
62
        return $wasSuccessful;
63
    }
64
65
    /**
66
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
67
     *
68
     * @return void
69
     */
70
    protected function updateOrdersStatus(SpySalesOrder $orderEntity)
71
    {
72
        $items = new ArrayObject();
73
74
        foreach ($orderEntity->getItems() as $salesOrderItem) {
75
            if ($salesOrderItem->getState()->getName() === AmazonPayConfig::OMS_STATUS_AUTH_OPEN) {
76
                $items[] = $salesOrderItem;
77
            }
78
        }
79
80
        $this->setOrderItemsStatus($items, AmazonPayConfig::OMS_STATUS_AUTH_OPEN_WITHOUT_CANCEL);
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    protected function getAffectingRequestedAmountItemsStateFlag()
87
    {
88
        return AmazonPayConfig::OMS_FLAG_NOT_CAPTURED;
89
    }
90
91
    /**
92
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $payment
93
     *
94
     * @return bool
95
     */
96
    protected function isPaymentSuccess(AmazonpayCallTransfer $payment)
97
    {
98
        return $payment->getAmazonpayPayment() &&
99
            $payment->getAmazonpayPayment()->getResponseHeader() &&
100
            $payment->getAmazonpayPayment()->getResponseHeader()->getIsSuccess();
101
    }
102
}
103