|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright since 2007 PrestaShop SA and Contributors |
| 4 | + * PrestaShop is an International Registered Trademark & Property of PrestaShop SA |
| 5 | + * |
| 6 | + * NOTICE OF LICENSE |
| 7 | + * |
| 8 | + * This source file is subject to the Academic Free License version 3.0 |
| 9 | + * that is bundled with this package in the file LICENSE.md. |
| 10 | + * It is also available through the world-wide-web at this URL: |
| 11 | + * https://opensource.org/licenses/AFL-3.0 |
| 12 | + * If you did not receive a copy of the license and are unable to |
| 13 | + * obtain it through the world-wide-web, please send an email |
| 14 | + * to [email protected] so we can send you a copy immediately. |
| 15 | + * |
| 16 | + * @author PrestaShop SA and Contributors <[email protected]> |
| 17 | + * @copyright Since 2007 PrestaShop SA and Contributors |
| 18 | + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 |
| 19 | + */ |
| 20 | + |
| 21 | +namespace PsCheckout\Core\PayPal\Order\Action; |
| 22 | + |
| 23 | +use PsCheckout\Api\Http\PaymentHttpClientInterface; |
| 24 | +use PsCheckout\Api\ValueObject\PayPalOrderResponse; |
| 25 | +use PsCheckout\Core\Exception\PsCheckoutException; |
| 26 | +use PsCheckout\Core\PayPal\Order\Configuration\PayPalAuthorizationStatus; |
| 27 | +use PsCheckout\Core\PayPal\Order\Configuration\PayPalOrderStatus; |
| 28 | +use PsCheckout\Core\PayPal\Order\Entity\PayPalOrderAuthorization; |
| 29 | +use PsCheckout\Core\PayPal\Order\Repository\PayPalOrderAuthorizationRepositoryInterface; |
| 30 | + |
| 31 | +class CaptureAuthorizationAction implements CaptureAuthorizationActionInterface |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var PaymentHttpClientInterface |
| 35 | + */ |
| 36 | + private $paymentHttpClient; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var PayPalOrderAuthorizationRepositoryInterface |
| 40 | + */ |
| 41 | + private $authorizationRepository; |
| 42 | + |
| 43 | + public function __construct( |
| 44 | + PaymentHttpClientInterface $paymentHttpClient, |
| 45 | + PayPalOrderAuthorizationRepositoryInterface $authorizationRepository |
| 46 | + ) { |
| 47 | + $this->paymentHttpClient = $paymentHttpClient; |
| 48 | + $this->authorizationRepository = $authorizationRepository; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritDoc} |
| 53 | + */ |
| 54 | + public function execute(PayPalOrderResponse $payPalOrder): PayPalOrderAuthorization |
| 55 | + { |
| 56 | + // Check PayPal order status must be APPROVED |
| 57 | + if ($payPalOrder->getStatus() !== PayPalOrderStatus::APPROVED) { |
| 58 | + throw new PsCheckoutException( |
| 59 | + sprintf('PayPal Order %s status must be APPROVED, current status: %s', $payPalOrder->getId(), $payPalOrder->getStatus()), |
| 60 | + PsCheckoutException::PAYPAL_ORDER_STATUS_INVALID |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + // Check intent must be AUTHORIZE |
| 65 | + if ($payPalOrder->getIntent() !== 'AUTHORIZE') { |
| 66 | + throw new PsCheckoutException( |
| 67 | + sprintf('PayPal Order %s intent must be AUTHORIZE, current intent: %s', $payPalOrder->getId(), $payPalOrder->getIntent()), |
| 68 | + PsCheckoutException::PAYPAL_ORDER_INTENT_INVALID |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + // Fetch payment authorization from order |
| 73 | + $authorization = $payPalOrder->getAuthorization(); |
| 74 | + |
| 75 | + if (!$authorization) { |
| 76 | + throw new PsCheckoutException( |
| 77 | + sprintf('PayPal Order %s does not have a valid authorization', $payPalOrder->getId()), |
| 78 | + PsCheckoutException::PAYPAL_AUTHORIZATION_NOT_FOUND |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + $authorizationId = $authorization['id']; |
| 83 | + $authorizationStatus = $authorization['status']; |
| 84 | + |
| 85 | + // Check if status is VOIDED |
| 86 | + if ($authorizationStatus === PayPalAuthorizationStatus::VOIDED) { |
| 87 | + throw new PsCheckoutException( |
| 88 | + "Authorization $authorizationId is voided and cannot be captured", |
| 89 | + PsCheckoutException::PAYPAL_AUTHORIZATION_VOIDED |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + // Validate authorization status must be CREATED or PARTIALLY_CAPTURED |
| 94 | + if (!in_array($authorizationStatus, [PayPalAuthorizationStatus::CREATED, PayPalAuthorizationStatus::PARTIALLY_CAPTURED], true)) { |
| 95 | + throw new PsCheckoutException( |
| 96 | + "Authorization $authorizationId status must be CREATED or PARTIALLY_CAPTURED, current status: $authorizationStatus", |
| 97 | + PsCheckoutException::PAYPAL_AUTHORIZATION_STATUS_INVALID |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + if (isset($authorization['expiration_time'])) { |
| 102 | + $expirationTime = new \DateTime((string) $authorization['expiration_time']); |
| 103 | + } else { |
| 104 | + $expirationTime = new \DateTime((string) $authorization['create_time']); |
| 105 | + $expirationTime->modify('+30 days'); |
| 106 | + } |
| 107 | + |
| 108 | + $currentTime = new \DateTime('now', new \DateTimeZone('UTC')); |
| 109 | + |
| 110 | + if ($expirationTime < $currentTime) { |
| 111 | + throw new PsCheckoutException( |
| 112 | + "Authorization $authorizationId has expired", |
| 113 | + PsCheckoutException::PAYPAL_AUTHORIZATION_EXPIRED |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + $captureResponse = $this->paymentHttpClient->captureAuthorization($authorizationId); |
| 118 | + |
| 119 | + /** |
| 120 | + * @var array{ |
| 121 | + * id: string, |
| 122 | + * status: string, |
| 123 | + * create_time: string, |
| 124 | + * update_time: string |
| 125 | + * } $capturedAuthorization |
| 126 | + */ |
| 127 | + $capturedAuthorization = json_decode($captureResponse->getBody(), true); |
| 128 | + |
| 129 | + $authorizationEntity = $this->authorizationRepository->getById($authorizationId); |
| 130 | + |
| 131 | + if ($authorizationEntity) { |
| 132 | + $authorizationEntity->setStatus($capturedAuthorization['status']); |
| 133 | + } else { |
| 134 | + $authorizationEntity = new PayPalOrderAuthorization( |
| 135 | + $authorizationId, |
| 136 | + $payPalOrder->getId(), |
| 137 | + $capturedAuthorization['status'], |
| 138 | + '', |
| 139 | + $capturedAuthorization['create_time'], |
| 140 | + $capturedAuthorization['update_time'], |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + $this->authorizationRepository->save($authorizationEntity); |
| 145 | + |
| 146 | + return $authorizationEntity; |
| 147 | + } |
| 148 | +} |
0 commit comments