Pass current amount when capturing authorization

This commit is contained in:
Alex P 2022-02-09 16:23:42 +02:00
parent 7cf3e6c842
commit 8c20c3ba28
2 changed files with 31 additions and 14 deletions

View file

@ -11,12 +11,14 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\AuthorizationStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
@ -96,11 +98,11 @@ class AuthorizedPaymentsProcessor {
/**
* Process a WooCommerce order.
*
* @param \WC_Order $wc_order The WooCommerce order.
* @param WC_Order $wc_order The WooCommerce order.
*
* @return string One of the AuthorizedPaymentsProcessor status constants.
*/
public function process( \WC_Order $wc_order ): string {
public function process( WC_Order $wc_order ): string {
$this->captures = array();
try {
@ -123,7 +125,7 @@ class AuthorizedPaymentsProcessor {
}
try {
$this->capture_authorizations( ...$authorizations );
$this->captures[] = $this->capture_authorization( $wc_order, ...$authorizations );
} catch ( Exception $exception ) {
$this->logger->error( 'Failed to capture authorization: ' . $exception->getMessage() );
return self::FAILED;
@ -144,11 +146,11 @@ class AuthorizedPaymentsProcessor {
/**
* Captures an authorized payment for an WooCommerce order.
*
* @param \WC_Order $wc_order The WooCommerce order.
* @param WC_Order $wc_order The WooCommerce order.
*
* @return bool
*/
public function capture_authorized_payment( \WC_Order $wc_order ): bool {
public function capture_authorized_payment( WC_Order $wc_order ): bool {
$result_status = $this->process( $wc_order );
$this->render_authorization_message_for_status( $result_status );
@ -211,11 +213,11 @@ class AuthorizedPaymentsProcessor {
/**
* Returns the PayPal order from a given WooCommerce order.
*
* @param \WC_Order $wc_order The WooCommerce order.
* @param WC_Order $wc_order The WooCommerce order.
*
* @return Order
*/
private function paypal_order_from_wc_order( \WC_Order $wc_order ): Order {
private function paypal_order_from_wc_order( WC_Order $wc_order ): Order {
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
return $this->order_endpoint->order( $order_id );
}
@ -239,15 +241,21 @@ class AuthorizedPaymentsProcessor {
}
/**
* Captures the authorizations.
* Captures the authorization.
*
* @param WC_Order $order The order.
* @param Authorization ...$authorizations All authorizations.
* @throws Exception If capture failed.
*/
private function capture_authorizations( Authorization ...$authorizations ) {
private function capture_authorization( WC_Order $order, Authorization ...$authorizations ): Capture {
$uncaptured_authorizations = $this->authorizations_to_capture( ...$authorizations );
foreach ( $uncaptured_authorizations as $authorization ) {
$this->captures[] = $this->payments_endpoint->capture( $authorization->id() );
if ( ! $uncaptured_authorizations ) {
throw new Exception( 'No authorizations to capture.' );
}
$authorization = end( $uncaptured_authorizations );
return $this->payments_endpoint->capture( $authorization->id(), new Money( (float) $order->get_total(), $order->get_currency() ) );
}
/**