|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * (c) shopware AG <info@shopware.com> |
| 4 | + * |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace SwagPaymentPayPalUnified\Components\Services; |
| 10 | + |
| 11 | +use DateTime; |
| 12 | +use Doctrine\DBAL\Connection; |
| 13 | +use PDO; |
| 14 | +use Shopware\Bundle\StoreFrontBundle\Service\Core\ContextService; |
| 15 | +use SwagPaymentPayPalUnified\Components\Exception\TimeoutInfoException; |
| 16 | +use SwagPaymentPayPalUnified\PayPalBundle\V2\Api\Order\PurchaseUnit\Payments\Refund; |
| 17 | +use SwagPaymentPayPalUnified\PayPalBundle\V2\Api\Order\PurchaseUnit\Payments\Refund\Amount; |
| 18 | +use SwagPaymentPayPalUnified\PayPalBundle\V2\Resource\CaptureResource; |
| 19 | + |
| 20 | +class TimeoutRefundService |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var Connection |
| 24 | + */ |
| 25 | + private $connection; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ContextService |
| 29 | + */ |
| 30 | + private $contextService; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var CaptureResource |
| 34 | + */ |
| 35 | + private $captureResource; |
| 36 | + |
| 37 | + public function __construct( |
| 38 | + Connection $connection, |
| 39 | + ContextService $contextService, |
| 40 | + CaptureResource $captureResource |
| 41 | + ) { |
| 42 | + $this->connection = $connection; |
| 43 | + $this->contextService = $contextService; |
| 44 | + $this->captureResource = $captureResource; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param string $payPalOrderId |
| 49 | + * @param float $orderAmount |
| 50 | + * |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function saveInfo($payPalOrderId, $orderAmount) |
| 54 | + { |
| 55 | + $this->connection->insert('swag_payment_paypal_unified_order_refund_info', [ |
| 56 | + 'paypal_order_id' => (string) $payPalOrderId, |
| 57 | + 'order_amount' => (string) $orderAmount, |
| 58 | + 'currency' => $this->contextService->getShopContext()->getCurrency()->getCurrency(), |
| 59 | + 'created_at' => (string) (new DateTime())->getTimestamp(), |
| 60 | + ]); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param string $payPalOrderId |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function deleteInfo($payPalOrderId) |
| 69 | + { |
| 70 | + $this->connection->delete('swag_payment_paypal_unified_order_refund_info', [ |
| 71 | + 'paypal_order_id' => $payPalOrderId, |
| 72 | + ]); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param string $payPalOrderId |
| 77 | + * @param string $captureId |
| 78 | + * |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + public function refund($payPalOrderId, $captureId) |
| 82 | + { |
| 83 | + $info = $this->getInfo($payPalOrderId); |
| 84 | + if (!\is_array($info)) { |
| 85 | + throw new TimeoutInfoException($payPalOrderId); |
| 86 | + } |
| 87 | + |
| 88 | + $amount = new Amount(); |
| 89 | + $amount->setCurrencyCode($info['currency']); |
| 90 | + $amount->setValue($info['order_amount']); |
| 91 | + |
| 92 | + $refund = new Refund(); |
| 93 | + $refund->setAmount($amount); |
| 94 | + $refund->setNoteToPayer('User timeout'); |
| 95 | + |
| 96 | + $this->captureResource->refund($captureId, $refund); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param string $payPalOrderId |
| 101 | + * |
| 102 | + * @return array<string, string>|null |
| 103 | + */ |
| 104 | + private function getInfo($payPalOrderId) |
| 105 | + { |
| 106 | + $result = $this->connection->createQueryBuilder() |
| 107 | + ->select(['*']) |
| 108 | + ->from('swag_payment_paypal_unified_order_refund_info') |
| 109 | + ->where('paypal_order_id = :paypalOrderId') |
| 110 | + ->setParameter('paypalOrderId', $payPalOrderId) |
| 111 | + ->execute() |
| 112 | + ->fetch(PDO::FETCH_ASSOC); |
| 113 | + |
| 114 | + if (!\is_array($result)) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + return $result; |
| 119 | + } |
| 120 | +} |
0 commit comments