SandboxSimulation.php 5.37 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
<?php
/**
 * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
namespace Amazon\Payment\Observer;

use Amazon\Core\Helper\Data;
use Amazon\Payment\Api\Data\QuoteLinkInterfaceFactory;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Payment\Model\InfoInterface;

class SandboxSimulation implements ObserverInterface
{
    /**
     * @var Data
     */
    private $coreHelper;

    /**
     * @var QuoteLinkInterfaceFactory
     */
    private $quoteLinkFactory;

    /**
     * @param Data $coreHelper
     * @param QuoteLinkInterfaceFactory $quoteLinkFactory
     */
    public function __construct(
        Data $coreHelper,
        QuoteLinkInterfaceFactory $quoteLinkFactory
    ) {
        $this->coreHelper = $coreHelper;
        $this->quoteLinkFactory = $quoteLinkFactory;
    }

    public function execute(Observer $observer)
    {
        if ($this->coreHelper->isSandboxEnabled()) {
            $context = $observer->getEvent()->getContext();
            $payment = $observer->getEvent()->getPayment();

            $simulationReference = $this->getSimulationReference($payment);

            if (! empty($simulationReference)) {
                $simulationString = $this->getSimulationString($simulationReference, $context);
                if (! empty($simulationString)) {
                    $requestParameter = $this->getRequestParameter($context);
                    $observer->getTransport()->addData([$requestParameter => $simulationString]);
                    $this->clearSimulationReference($payment);
                }
            }
        }
    }

    protected function clearSimulationReference(InfoInterface $payment)
    {
        $additionalInformation = $payment->getAdditionalInformation();

        if (is_array($additionalInformation) && isset($additionalInformation['sandbox_simulation_reference'])) {
            unset($additionalInformation['sandbox_simulation_reference']);
            $payment->setAdditionalInformation($additionalInformation);
        }

        $quoteLink = $this->getQuoteLink($payment);

        if ($quoteLink->getSandboxSimulationReference()) {
            $quoteLink->setSandboxSimulationReference(null)->save();
        }
    }

    /**
     * @param $payment
     * @return string
     */
    protected function getSimulationReference($payment)
    {
        $simulationReference = $this->getSimulationReferenceFromPayment($payment);
        $quoteLink = $this->getQuoteLink($payment);

        if ($simulationReference) {
            $quoteLink->setSandboxSimulationReference($simulationReference)->save();
        } else {
            $simulationReference = $quoteLink->getSandboxSimulationReference();
        }

        return $simulationReference;
    }

    /**
     * @param $payment
     * @return string
     */
    protected function getSimulationReferenceFromPayment($payment)
    {
        $simulationReference = null;

        $additionalInformation = $payment->getAdditionalInformation();
        if (is_array($additionalInformation) &&
            array_key_exists('sandbox_simulation_reference', $additionalInformation)
        ) {
            $simulationReference = $additionalInformation['sandbox_simulation_reference'];
        }

        return $simulationReference;
    }

    /**
     * @param $payment
     * @return \Amazon\Payment\Api\Data\QuoteLinkInterface
     */
    protected function getQuoteLink($payment)
    {
        $quoteId = $payment->getOrder()->getQuoteId();
        $quoteLink = $this->quoteLinkFactory->create();
        $quoteLink->load($quoteId, 'quote_id');

        return $quoteLink;
    }

    /**
     * @return array
     */
    protected function getRequestParameters()
    {
        $requestParameters = [
            'authorization' => 'seller_authorization_note',
            'authorization_capture' => 'seller_authorization_note',
            'capture' => 'seller_capture_note',
            'refund' => 'seller_refund_note'
        ];

        return $requestParameters;
    }

    /**
     * @param string $context
     * @return string
     */
    protected function getRequestParameter($context)
    {
        $requestParameter = null;

        $requestParameters = $this->getRequestParameters();
        if (array_key_exists($context, $requestParameters)) {
            $requestParameter = $requestParameters[$context];
        }

        return $requestParameter;
    }

    /**
     * @param string $simulationReference
     * @param string|null $context
     * @return string
     */
    protected function getSimulationString($simulationReference, $context = null)
    {
        $simulationString = null;

        $simulationStrings = $this->coreHelper->getSandboxSimulationStrings($context);
        if (array_key_exists($simulationReference, $simulationStrings)) {
            $simulationString = $simulationStrings[$simulationReference];
        }

        return $simulationString;
    }
}