| 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\Business\Payment\Handler\Transaction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Generated\Shared\Transfer\AmazonpayCallTransfer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Generated\Shared\Transfer\AmazonpayStatusTransfer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SprykerEco\Shared\Amazonpay\AmazonpayConfigInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SprykerEco\Shared\Amazonpay\AmazonpayConstants; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SprykerEco\Zed\Amazonpay\Business\Api\Adapter\CallAdapterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SprykerEco\Zed\Amazonpay\Business\Converter\AmazonpayTransferToEntityConverterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use SprykerEco\Zed\Amazonpay\Business\Order\RefundOrderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Transaction\Logger\TransactionLoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use SprykerEco\Zed\Amazonpay\Persistence\AmazonpayQueryContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class UpdateOrderRefundStatusTransaction extends AbstractAmazonpayTransaction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var \SprykerEco\Zed\Amazonpay\Business\Order\RefundOrderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $refundOrderModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param \SprykerEco\Zed\Amazonpay\Business\Api\Adapter\CallAdapterInterface $executionAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param \SprykerEco\Shared\Amazonpay\AmazonpayConfigInterface $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param \SprykerEco\Zed\Amazonpay\Business\Payment\Handler\Transaction\Logger\TransactionLoggerInterface $transactionLogger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param \SprykerEco\Zed\Amazonpay\Persistence\AmazonpayQueryContainerInterface $amazonpayQueryContainer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param \SprykerEco\Zed\Amazonpay\Business\Converter\AmazonpayTransferToEntityConverterInterface $converter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param \SprykerEco\Zed\Amazonpay\Business\Order\RefundOrderInterface $refundOrderModel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         CallAdapterInterface $executionAdapter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         AmazonpayConfigInterface $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         TransactionLoggerInterface $transactionLogger, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         AmazonpayQueryContainerInterface $amazonpayQueryContainer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         AmazonpayTransferToEntityConverterInterface $converter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         RefundOrderInterface $refundOrderModel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         parent::__construct($executionAdapter, $config, $transactionLogger, $amazonpayQueryContainer, $converter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->refundOrderModel = $refundOrderModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return \Generated\Shared\Transfer\AmazonpayCallTransfer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function execute(AmazonpayCallTransfer $amazonpayCallTransfer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if (!$amazonpayCallTransfer->getAmazonpayPayment()->getRefundDetails()->getAmazonRefundId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             return $amazonpayCallTransfer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $amazonpayCallTransfer = parent::execute($amazonpayCallTransfer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (!$this->apiResponse->getHeader()->getIsSuccess()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             return $amazonpayCallTransfer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $isPartialProcessing = $this->isPartialProcessing($this->paymentEntity, $amazonpayCallTransfer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if ($isPartialProcessing) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->paymentEntity = $this->duplicatePaymentEntity($this->paymentEntity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $status = $this->getPaymentStatus($this->apiResponse->getRefundDetails()->getRefundStatus()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $refundIsRequired = ($status === AmazonpayConstants::OMS_STATUS_REFUND_COMPLETED | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             && $this->paymentEntity->getStatus() !== $status); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->paymentEntity->setStatus($status); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->paymentEntity->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if ($isPartialProcessing) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->assignAmazonpayPaymentToItemsIfNew($this->paymentEntity, $amazonpayCallTransfer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if ($refundIsRequired) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $this->refundOrderModel->refundPayment($this->paymentEntity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return $amazonpayCallTransfer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param \Generated\Shared\Transfer\AmazonpayStatusTransfer $status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 97 |  | View Code Duplication |     protected function getPaymentStatus(AmazonpayStatusTransfer $status) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         if ($status->getIsPending()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             return AmazonpayConstants::OMS_STATUS_REFUND_PENDING; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         if ($status->getIsDeclined()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             return AmazonpayConstants::OMS_STATUS_REFUND_DECLINED; | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         if ($status->getIsCompleted()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             return AmazonpayConstants::OMS_STATUS_REFUND_COMPLETED; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         return AmazonpayConstants::OMS_STATUS_CANCELLED; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.