WIP payment capture

This commit is contained in:
Mészáros Róbert 2020-04-14 19:19:29 +03:00
parent 7d0e3d2e38
commit c7f437b2b6
3 changed files with 26 additions and 19 deletions

View file

@ -5,26 +5,26 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\ApiClient\Endpoint; namespace Inpsyde\PayPalCommerce\ApiClient\Endpoint;
use Inpsyde\PayPalCommerce\ApiClient\Authentication\Bearer; use Inpsyde\PayPalCommerce\ApiClient\Authentication\Bearer;
use Inpsyde\PayPalCommerce\ApiClient\Factory\AuthorizationsFactory; use Inpsyde\PayPalCommerce\ApiClient\Factory\AuthorizationFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\ErrorResponseCollectionFactory; use Inpsyde\PayPalCommerce\ApiClient\Factory\ErrorResponseCollectionFactory;
class PaymentsEndpoint class PaymentsEndpoint
{ {
private $host; private $host;
private $bearer; private $bearer;
private $authorizationsFactory; private $authorizationFactory;
private $errorResponseFactory; private $errorResponseFactory;
public function __construct( public function __construct(
string $host, string $host,
Bearer $bearer, Bearer $bearer,
AuthorizationsFactory $authorizationsFactory, AuthorizationFactory $authorizationsFactory,
ErrorResponseCollectionFactory $errorResponseFactory ErrorResponseCollectionFactory $errorResponseFactory
) { ) {
$this->host = $host; $this->host = $host;
$this->bearer = $bearer; $this->bearer = $bearer;
$this->authorizationsFactory = $authorizationsFactory; $this->authorizationFactory = $authorizationsFactory;
$this->errorResponseFactory = $errorResponseFactory; $this->errorResponseFactory = $errorResponseFactory;
} }
@ -61,11 +61,11 @@ class PaymentsEndpoint
} }
$json = json_decode($response['body']); $json = json_decode($response['body']);
$authorization = $this->authorizationsFactory->fromPayPalRequest($json); $authorization = $this->authorizationFactory->fromPayPalRequest($json);
return $authorization; return $authorization;
} }
public function captureAuthorization($authorizationId) public function capture($authorizationId)
{ {
$bearer = $this->bearer->bearer(); $bearer = $this->bearer->bearer();
$url = trailingslashit($this->host) . 'v2/payments/authorizations/' . $authorizationId . '/capture'; $url = trailingslashit($this->host) . 'v2/payments/authorizations/' . $authorizationId . '/capture';
@ -98,6 +98,7 @@ class PaymentsEndpoint
} }
$json = json_decode($response['body']); $json = json_decode($response['body']);
return $json; $authorization = $this->authorizationFactory->fromPayPalRequest($json);
return $authorization;
} }
} }

View file

@ -6,6 +6,8 @@ namespace Inpsyde\PayPalCommerce\WcGateway\Gateway;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint; 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\Order;
use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus; use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus;
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException; 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); $payPalOrderId = get_post_meta($wcOrder->get_id(), '_paypal_order_id', true);
@ -127,16 +129,20 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
return; return;
} }
if ($order->status()->is(OrderStatus::COMPLETED)) { foreach ($order->purchaseUnits() as $purchaseUnit) {
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::ALREADY_AUTHORIZED); foreach ($purchaseUnit->payments()->authorizations() as $authorization) {
return; /**
} * @var Authorization $authorization;
*/
try { if ($authorization->status()->name() === AuthorizationStatus::CREATED) {
$this->orderEndpoint->authorize($payPalOrderId); try {
} catch (RuntimeException $exception) { $result = $this->paymentsEndpoint->capture($authorization->id());
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::FAILED); } catch (RuntimeException $exception) {
return; AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::FAILED);
return;
}
}
}
} }
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::SUCCESS); AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::SUCCESS);

View file

@ -74,7 +74,7 @@ class WcGatewayModule implements ModuleInterface
* @var WcGateway $gateway * @var WcGateway $gateway
*/ */
$gateway = $container->get('wcgateway.gateway'); $gateway = $container->get('wcgateway.gateway');
$gateway->authorizeOrder($wcOrder); $gateway->captureAuthorizedPayment($wcOrder);
} }
); );