Merge pull request #886 from woocommerce/PCP-922-update-order-with-order-note-if-payment-failed-after-billing-agreement-canceled-at-pay-pal

update order with order note if payment failed after billing agreement canceled at PayPal (922)
This commit is contained in:
Emili Castells 2022-11-03 14:25:34 +01:00 committed by GitHub
commit aa7a7adaf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 29 deletions

View file

@ -119,4 +119,26 @@ class PayPalApiException extends RuntimeException {
public function status_code(): int { public function status_code(): int {
return $this->status_code; return $this->status_code;
} }
/**
* Return exception details if exists.
*
* @param string $error The error to return in case no details found.
* @return string
*/
public function get_details( string $error ): string {
if ( empty( $this->details() ) ) {
return $error;
}
$details = '';
foreach ( $this->details() as $detail ) {
$issue = $detail->issue ?? '';
$field = $detail->field ?? '';
$description = $detail->description ?? '';
$details .= $issue . ' ' . $field . ' ' . $description . '<br>';
}
return $details;
}
} }

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Subscription;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
@ -141,17 +142,27 @@ class RenewalHandler {
public function renew( \WC_Order $wc_order ) { public function renew( \WC_Order $wc_order ) {
try { try {
$this->process_order( $wc_order ); $this->process_order( $wc_order );
} catch ( \Exception $error ) { } catch ( \Exception $exception ) {
$this->logger->error( $error = $exception->getMessage();
sprintf( if ( is_a( $exception, PayPalApiException::class ) ) {
'An error occurred while trying to renew the subscription for order %1$d: %2$s', $error = $exception->get_details( $error );
$wc_order->get_id(), }
$error->getMessage()
) $wc_order->update_status(
'failed',
$error
); );
$error_message = sprintf(
'An error occurred while trying to renew the subscription for order %1$d: %2$s',
$wc_order->get_id(),
$error
);
$this->logger->error( $error_message );
return; return;
} }
$this->logger->info( $this->logger->info(
sprintf( sprintf(
'Renewal for order %d is completed.', 'Renewal for order %d is completed.',

View file

@ -167,17 +167,8 @@ class OXXOGateway extends WC_Payment_Gateway {
} }
} catch ( RuntimeException $exception ) { } catch ( RuntimeException $exception ) {
$error = $exception->getMessage(); $error = $exception->getMessage();
if ( is_a( $exception, PayPalApiException::class ) ) {
if ( is_a( $exception, PayPalApiException::class ) && is_array( $exception->details() ) ) { $error = $exception->get_details( $error );
$details = '';
foreach ( $exception->details() as $detail ) {
$issue = $detail->issue ?? '';
$field = $detail->field ?? '';
$description = $detail->description ?? '';
$details .= $issue . ' ' . $field . ' ' . $description . '<br>';
}
$error = $details;
} }
$this->logger->error( $error ); $this->logger->error( $error );

View file

@ -274,17 +274,8 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
); );
} catch ( RuntimeException $exception ) { } catch ( RuntimeException $exception ) {
$error = $exception->getMessage(); $error = $exception->getMessage();
if ( is_a( $exception, PayPalApiException::class ) ) {
if ( is_a( $exception, PayPalApiException::class ) && is_array( $exception->details() ) ) { $error = $exception->get_details( $error );
$details = '';
foreach ( $exception->details() as $detail ) {
$issue = $detail->issue ?? '';
$field = $detail->field ?? '';
$description = $detail->description ?? '';
$details .= $issue . ' ' . $field . ' ' . $description . '<br>';
}
$error = $details;
} }
$this->logger->error( $error ); $this->logger->error( $error );