mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Pass current amount when capturing authorization
This commit is contained in:
parent
7cf3e6c842
commit
8c20c3ba28
2 changed files with 31 additions and 14 deletions
|
@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Refund;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
@ -146,22 +147,30 @@ class PaymentsEndpoint {
|
|||
/**
|
||||
* Capture an authorization by a given ID.
|
||||
*
|
||||
* @param string $authorization_id The id.
|
||||
* @param string $authorization_id The id.
|
||||
* @param Money|null $amount The amount to capture. If not specified, the whole authorized amount is captured.
|
||||
*
|
||||
* @return Capture
|
||||
* @throws RuntimeException If the request fails.
|
||||
* @throws PayPalApiException If the request fails.
|
||||
*/
|
||||
public function capture( string $authorization_id ): Capture {
|
||||
public function capture( string $authorization_id, ?Money $amount = null ): Capture {
|
||||
$bearer = $this->bearer->bearer();
|
||||
$url = trailingslashit( $this->host ) . 'v2/payments/authorizations/' . $authorization_id . '/capture';
|
||||
$args = array(
|
||||
|
||||
$data = array();
|
||||
if ( $amount ) {
|
||||
$data['amount'] = $amount->to_array();
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation',
|
||||
),
|
||||
'body' => wp_json_encode( $data, JSON_FORCE_OBJECT ),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
|
|
|
@ -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() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue