diff --git a/modules.local/ppcp-api-client/src/Endpoint/PaymentsEndpoint.php b/modules.local/ppcp-api-client/src/Endpoint/PaymentsEndpoint.php new file mode 100644 index 000000000..63aaddfa7 --- /dev/null +++ b/modules.local/ppcp-api-client/src/Endpoint/PaymentsEndpoint.php @@ -0,0 +1,103 @@ +host = $host; + $this->bearer = $bearer; + $this->authorizationsFactory = $authorizationsFactory; + $this->errorResponseFactory = $errorResponseFactory; + } + + public function authorization($authorizationId) + { + $bearer = $this->bearer->bearer(); + $url = trailingslashit($this->host) . 'v2/payments/authorizations/' . $authorizationId; + $args = [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $bearer, + 'Content-Type' => 'application/json', + 'Prefer' => 'return=representation', + ], + ]; + + $response = wp_remote_get($url, $args); + + if (is_wp_error($response)) { + throw new RuntimeException( + __( + 'Could not get authorized payment info.', + 'woocommerce-paypal-commerce-gateway' + ) + ); + } + + if (wp_remote_retrieve_response_code($response) !== 200) { + throw new RuntimeException( + __( + 'Could not get authorized payment info.', + 'woocommerce-paypal-commerce-gateway' + ) + ); + } + + $json = json_decode($response['body']); + $authorization = $this->authorizationsFactory->fromPayPalRequest($json); + return $authorization; + } + + public function captureAuthorization($authorizationId) + { + $bearer = $this->bearer->bearer(); + $url = trailingslashit($this->host) . 'v2/payments/authorizations/' . $authorizationId . '/capture'; + $args = [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $bearer, + 'Content-Type' => 'application/json', + 'Prefer' => 'return=representation', + ], + ]; + + $response = wp_remote_post($url, $args); + + if (is_wp_error($response)) { + throw new RuntimeException( + __( + 'Could not capture authorized payment.', + 'woocommerce-paypal-commerce-gateway' + ) + ); + } + + if (wp_remote_retrieve_response_code($response) !== 201) { + throw new RuntimeException( + __( + 'Could not capture authorized payment.', + 'woocommerce-paypal-commerce-gateway' + ) + ); + } + + $json = json_decode($response['body']); + return $json; + } +} diff --git a/modules.local/ppcp-wc-gateway/services.php b/modules.local/ppcp-wc-gateway/services.php index aa858ac04..c9dc551f0 100644 --- a/modules.local/ppcp-wc-gateway/services.php +++ b/modules.local/ppcp-wc-gateway/services.php @@ -19,10 +19,11 @@ return [ 'wcgateway.gateway' => function (ContainerInterface $container) : WcGateway { $sessionHandler = $container->get('session.handler'); $cartRepository = $container->get('api.repository.cart'); - $endpoint = $container->get('api.endpoint.order'); + $orderEndpoint = $container->get('api.endpoint.order'); + $paymentsEndpoint = $container->get('api.endpoint.payments'); $orderFactory = $container->get('api.factory.order'); $settingsFields = $container->get('wcgateway.settings.fields'); - return new WcGateway($sessionHandler, $cartRepository, $endpoint, $orderFactory, $settingsFields); + return new WcGateway($sessionHandler, $cartRepository, $orderEndpoint, $paymentsEndpoint, $orderFactory, $settingsFields); }, 'wcgateway.disabler' => function (ContainerInterface $container) : DisableGateways { $sessionHandler = $container->get('session.handler'); diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php index 121fa688f..12e9e4e82 100644 --- a/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php +++ b/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Inpsyde\PayPalCommerce\WcGateway\Gateway; use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; +use Inpsyde\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint; use Inpsyde\PayPalCommerce\ApiClient\Entity\Order; use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus; use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException; @@ -20,22 +21,28 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface { private $isSandbox = true; private $sessionHandler; - private $endpoint; + private $orderEndpoint; private $cartRepository; private $orderFactory; private $settingsFields; + /** + * @var PaymentsEndpoint + */ + private $paymentsEndpoint; public function __construct( SessionHandler $sessionHandler, CartRepository $cartRepository, - OrderEndpoint $endpoint, + OrderEndpoint $orderEndpoint, + PaymentsEndpoint $paymentsEndpoint, OrderFactory $orderFactory, SettingsFields $settingsFields ) { $this->sessionHandler = $sessionHandler; $this->cartRepository = $cartRepository; - $this->endpoint = $endpoint; + $this->orderEndpoint = $orderEndpoint; + $this->paymentsEndpoint = $paymentsEndpoint; $this->orderFactory = $orderFactory; $this->settingsFields = $settingsFields; @@ -89,11 +96,11 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface $order = $this->patchOrder($wcOrder, $order); if ($order->intent() === 'CAPTURE') { - $order = $this->endpoint->capture($order); + $order = $this->orderEndpoint->capture($order); } if ($order->intent() === 'AUTHORIZE') { - $order = $this->endpoint->authorize($order); + $order = $this->orderEndpoint->authorize($order); } $wcOrder->update_status('on-hold', __('Awaiting payment.', 'woocommerce-paypal-gateway')); @@ -114,7 +121,7 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface $payPalOrderId = get_post_meta($wcOrder->get_id(), '_paypal_order_id', true); try { - $order = $this->endpoint->order($payPalOrderId); + $order = $this->orderEndpoint->order($payPalOrderId); } catch (RuntimeException $exception) { AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::NO_INFO); return; @@ -126,7 +133,7 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface } try { - $this->endpoint->authorize($payPalOrderId); + $this->orderEndpoint->authorize($payPalOrderId); } catch (RuntimeException $exception) { AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::FAILED); return; @@ -147,7 +154,7 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface private function patchOrder(\WC_Order $wcOrder, Order $order): Order { $updatedOrder = $this->orderFactory->fromWcOrder($wcOrder, $order); - $order = $this->endpoint->patchOrderWith($order, $updatedOrder); + $order = $this->orderEndpoint->patchOrderWith($order, $updatedOrder); return $order; } }