mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 09:08:09 +08:00
WIP payment capture
This commit is contained in:
parent
7d0e3d2e38
commit
c7f437b2b6
3 changed files with 26 additions and 19 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -74,7 +74,7 @@ class WcGatewayModule implements ModuleInterface
|
|||
* @var WcGateway $gateway
|
||||
*/
|
||||
$gateway = $container->get('wcgateway.gateway');
|
||||
$gateway->authorizeOrder($wcOrder);
|
||||
$gateway->captureAuthorizedPayment($wcOrder);
|
||||
}
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue