diff --git a/modules.local/ppcp-api-client/src/Endpoint/PaymentsEndpoint.php b/modules.local/ppcp-api-client/src/Endpoint/PaymentsEndpoint.php index 63aaddfa7..4bcdc72bd 100644 --- a/modules.local/ppcp-api-client/src/Endpoint/PaymentsEndpoint.php +++ b/modules.local/ppcp-api-client/src/Endpoint/PaymentsEndpoint.php @@ -5,26 +5,26 @@ declare(strict_types=1); namespace Inpsyde\PayPalCommerce\ApiClient\Endpoint; use Inpsyde\PayPalCommerce\ApiClient\Authentication\Bearer; -use Inpsyde\PayPalCommerce\ApiClient\Factory\AuthorizationsFactory; +use Inpsyde\PayPalCommerce\ApiClient\Factory\AuthorizationFactory; use Inpsyde\PayPalCommerce\ApiClient\Factory\ErrorResponseCollectionFactory; class PaymentsEndpoint { private $host; private $bearer; - private $authorizationsFactory; + private $authorizationFactory; private $errorResponseFactory; public function __construct( string $host, Bearer $bearer, - AuthorizationsFactory $authorizationsFactory, + AuthorizationFactory $authorizationsFactory, ErrorResponseCollectionFactory $errorResponseFactory ) { $this->host = $host; $this->bearer = $bearer; - $this->authorizationsFactory = $authorizationsFactory; + $this->authorizationFactory = $authorizationsFactory; $this->errorResponseFactory = $errorResponseFactory; } @@ -61,11 +61,11 @@ class PaymentsEndpoint } $json = json_decode($response['body']); - $authorization = $this->authorizationsFactory->fromPayPalRequest($json); + $authorization = $this->authorizationFactory->fromPayPalRequest($json); return $authorization; } - public function captureAuthorization($authorizationId) + public function capture($authorizationId) { $bearer = $this->bearer->bearer(); $url = trailingslashit($this->host) . 'v2/payments/authorizations/' . $authorizationId . '/capture'; @@ -98,6 +98,7 @@ class PaymentsEndpoint } $json = json_decode($response['body']); - return $json; + $authorization = $this->authorizationFactory->fromPayPalRequest($json); + return $authorization; } } diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php index 8127d9cab..277d4ce95 100644 --- a/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php +++ b/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php @@ -6,6 +6,8 @@ namespace Inpsyde\PayPalCommerce\WcGateway\Gateway; use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use Inpsyde\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint; +use Inpsyde\PayPalCommerce\ApiClient\Entity\Authorization; +use Inpsyde\PayPalCommerce\ApiClient\Entity\AuthorizationStatus; use Inpsyde\PayPalCommerce\ApiClient\Entity\Order; use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus; use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException; @@ -116,7 +118,7 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface ]; } - public function authorizeOrder(\WC_Order $wcOrder) + public function captureAuthorizedPayment(\WC_Order $wcOrder) { $payPalOrderId = get_post_meta($wcOrder->get_id(), '_paypal_order_id', true); @@ -127,16 +129,20 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface return; } - if ($order->status()->is(OrderStatus::COMPLETED)) { - AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::ALREADY_AUTHORIZED); - return; - } - - try { - $this->orderEndpoint->authorize($payPalOrderId); - } catch (RuntimeException $exception) { - AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::FAILED); - return; + foreach ($order->purchaseUnits() as $purchaseUnit) { + foreach ($purchaseUnit->payments()->authorizations() as $authorization) { + /** + * @var Authorization $authorization; + */ + if ($authorization->status()->name() === AuthorizationStatus::CREATED) { + try { + $result = $this->paymentsEndpoint->capture($authorization->id()); + } catch (RuntimeException $exception) { + AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::FAILED); + return; + } + } + } } AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::SUCCESS); diff --git a/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php b/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php index 4c862f461..e61014a11 100644 --- a/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php +++ b/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php @@ -74,7 +74,7 @@ class WcGatewayModule implements ModuleInterface * @var WcGateway $gateway */ $gateway = $container->get('wcgateway.gateway'); - $gateway->authorizeOrder($wcOrder); + $gateway->captureAuthorizedPayment($wcOrder); } );