Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ public function handle_request(): void {
exit();
}

if ( ! in_array( $wc_order->get_status(), array( 'pending', 'on-hold' ), true ) ) {
$payment_gateway = $this->get_payment_gateway( $wc_order->get_payment_method() );
if ( $payment_gateway ) {
$this->session_handler->destroy_session_data();
wp_safe_redirect( $payment_gateway->get_return_url( $wc_order ) );
exit();
}
}

$payment_gateway = $this->get_payment_gateway( $wc_order->get_payment_method() );
if ( ! $payment_gateway ) {
wc_add_notice( __( 'Payment gateway is unavailable. Please try again or contact support.', 'woocommerce-paypal-payments' ), 'error' );
Expand Down
210 changes: 134 additions & 76 deletions modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,84 +212,96 @@ public function __construct(
* @throws Exception If processing fails.
*/
public function process( WC_Order $wc_order ): void {
$order = $this->session_handler->order();
if ( ! $order ) {
// phpcs:ignore WordPress.Security.NonceVerification
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ) ?: wc_clean( wp_unslash( $_POST['paypal_order_id'] ?? '' ) );
if ( is_string( $order_id ) && $order_id ) {
try {
$order = $this->order_endpoint->order( $order_id );
} catch ( RuntimeException $exception ) {
throw new Exception( __( 'Could not retrieve PayPal order.', 'woocommerce-paypal-payments' ) );
}
} else {
$is_paypal_return = isset( $_GET['wc-ajax'] ) && wc_clean( wp_unslash( $_GET['wc-ajax'] ) ) === 'ppc-return-url'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( $is_paypal_return ) {
$this->logger->warning(
sprintf(
'No PayPal order ID found for WooCommerce order #%d.',
$wc_order->get_id()
if ( ! $this->verify_order_can_be_processed( $wc_order ) ) {
return;
}

$wc_order->update_meta_data( '_ppcp_processing', 'yes' );
$wc_order->save();

try {
$order = $this->session_handler->order();
if ( ! $order ) {
// phpcs:ignore WordPress.Security.NonceVerification
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ) ?: wc_clean( wp_unslash( $_POST['paypal_order_id'] ?? '' ) );
if ( is_string( $order_id ) && $order_id ) {
try {
$order = $this->order_endpoint->order( $order_id );
} catch ( RuntimeException $exception ) {
throw new Exception( __( 'Could not retrieve PayPal order.', 'woocommerce-paypal-payments' ) );
}
} else {
$is_paypal_return = isset( $_GET['wc-ajax'] ) && wc_clean( wp_unslash( $_GET['wc-ajax'] ) ) === 'ppc-return-url'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( $is_paypal_return ) {
$this->logger->warning(
sprintf(
'No PayPal order ID found for WooCommerce order #%d.',
$wc_order->get_id()
)
);
}

throw new PayPalOrderMissingException(
esc_attr__(
'There was an error processing your order. Please check for any charges in your payment method and review your order history before placing the order again.',
'woocommerce-paypal-payments'
)
);
}
}

// Do not continue if PayPal order status is completed.
$order = $this->order_endpoint->order( $order->id() );
if ( $order->status()->is( OrderStatus::COMPLETED ) ) {
$this->logger->warning( 'Could not process PayPal completed order #' . $order->id() . ', Status: ' . $order->status()->name() );
return;
}

throw new PayPalOrderMissingException(
esc_attr__(
'There was an error processing your order. Please check for any charges in your payment method and review your order history before placing the order again.',
$this->add_paypal_meta( $wc_order, $order, $this->environment );

if ( $this->order_helper->contains_physical_goods( $order ) && ! $this->order_is_ready_for_process( $order ) ) {
throw new Exception(
__(
'The payment is not ready for processing yet.',
'woocommerce-paypal-payments'
)
);
}
}

// Do not continue if PayPal order status is completed.
$order = $this->order_endpoint->order( $order->id() );
if ( $order->status()->is( OrderStatus::COMPLETED ) ) {
$this->logger->warning( 'Could not process PayPal completed order #' . $order->id() . ', Status: ' . $order->status()->name() );
return;
}
$order = $this->patch_order( $wc_order, $order );

$this->add_paypal_meta( $wc_order, $order, $this->environment );

if ( $this->order_helper->contains_physical_goods( $order ) && ! $this->order_is_ready_for_process( $order ) ) {
throw new Exception(
__(
'The payment is not ready for processing yet.',
'woocommerce-paypal-payments'
)
);
}
if ( $order->intent() === 'CAPTURE' ) {
$order = $this->order_endpoint->capture( $order );
}

$order = $this->patch_order( $wc_order, $order );
if ( $order->intent() === 'AUTHORIZE' ) {
$order = $this->order_endpoint->authorize( $order );

if ( $order->intent() === 'CAPTURE' ) {
$order = $this->order_endpoint->capture( $order );
}
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );

if ( $order->intent() === 'AUTHORIZE' ) {
$order = $this->order_endpoint->authorize( $order );
if ( $this->subscription_helper->has_subscription( $wc_order->get_id() ) ) {
$wc_order->update_meta_data( '_ppcp_captured_vault_webhook', 'false' );
}
}

$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );
$transaction_id = $this->get_paypal_order_transaction_id( $order );

if ( $this->subscription_helper->has_subscription( $wc_order->get_id() ) ) {
$wc_order->update_meta_data( '_ppcp_captured_vault_webhook', 'false' );
if ( $transaction_id ) {
$this->update_transaction_id( $transaction_id, $wc_order );
}
}

$transaction_id = $this->get_paypal_order_transaction_id( $order );

if ( $transaction_id ) {
$this->update_transaction_id( $transaction_id, $wc_order );
}
$this->handle_new_order_status( $order, $wc_order );

$this->handle_new_order_status( $order, $wc_order );
if ( $this->capture_authorized_downloads( $order ) ) {
$this->authorized_payments_processor->capture_authorized_payment( $wc_order );
}

if ( $this->capture_authorized_downloads( $order ) ) {
$this->authorized_payments_processor->capture_authorized_payment( $wc_order );
do_action( 'woocommerce_paypal_payments_after_order_processor', $wc_order, $order );
} finally {
$wc_order->delete_meta_data( '_ppcp_processing' );
$wc_order->save();
}

do_action( 'woocommerce_paypal_payments_after_order_processor', $wc_order, $order );
}

/**
Expand Down Expand Up @@ -355,6 +367,70 @@ public function create_order( WC_Order $wc_order ): Order {
return $order;
}

/**
* Patches a given PayPal order with a WooCommerce order.
*
* @param WC_Order $wc_order The WooCommerce order.
* @param Order $order The PayPal order.
*
* @return Order
*/
public function patch_order( WC_Order $wc_order, Order $order ): Order {
$this->apply_outbound_order_filters( $wc_order );
$updated_order = $this->order_factory->from_wc_order( $wc_order, $order );
$this->restore_order_from_filters( $wc_order );

$order = $this->order_endpoint->patch_order_with( $order, $updated_order );

return $order;
}

/**
* Verifies whether the order can be processed.
*
* @param WC_Order $wc_order The WooCommerce order.
* @return bool
*/
private function verify_order_can_be_processed( WC_Order $wc_order ): bool {
if ( ! in_array( $wc_order->get_status(), array( 'pending', 'on-hold' ), true ) ) {
$this->logger->info(
sprintf(
'Order #%d has status "%s", skipping payment processing.',
$wc_order->get_id(),
$wc_order->get_status()
)
);

return false;
}

if ( $wc_order->get_transaction_id() ) {
$this->logger->info(
sprintf(
'Order #%d already has transaction ID "%s", skipping payment processing.',
$wc_order->get_id(),
$wc_order->get_transaction_id()
)
);

return false;
}

$processing_lock = $wc_order->get_meta( '_ppcp_processing', true );
if ( $processing_lock === 'yes' ) {
$this->logger->warning(
sprintf(
'Order #%d is already being processed (lock active), skipping payment processing.',
$wc_order->get_id()
)
);

return false;
}

return true;
}

/**
* Returns if an order should be captured immediately.
*
Expand Down Expand Up @@ -389,24 +465,6 @@ private function capture_authorized_downloads( Order $order ): bool {
return true;
}

/**
* Patches a given PayPal order with a WooCommerce order.
*
* @param WC_Order $wc_order The WooCommerce order.
* @param Order $order The PayPal order.
*
* @return Order
*/
public function patch_order( WC_Order $wc_order, Order $order ): Order {
$this->apply_outbound_order_filters( $wc_order );
$updated_order = $this->order_factory->from_wc_order( $wc_order, $order );
$this->restore_order_from_filters( $wc_order );

$order = $this->order_endpoint->patch_order_with( $order, $updated_order );

return $order;
}

/**
* Whether a given order is ready for processing.
*
Expand Down
31 changes: 19 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/PHPUnit/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
<directory suffix=".php">./modules/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">./tests/PHPUnit</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/PHPUnit/bootstrap.php" backupGlobals="false" colors="true"
convertErrorsToExceptions="true" convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
<directory suffix=".php">./modules/</directory>
</include>
<exclude>
<directory suffix=".php">./modules/*/node_modules</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">./tests/PHPUnit</directory>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,6 @@ public function setUp(): void
);
}

public function testProcessPayment()
{
$this->markTestSkipped('must be revisited.');
list($order, $purchase_unit, $payment_source) = $this->setTestStubs();

$this->order_endpoint->shouldReceive('create')->with(
[$purchase_unit],
$payment_source
)->andReturn($order);

define( 'MINUTE_IN_SECONDS', 60 );
when('as_schedule_single_action')->justReturn();

$result = $this->testee->process_payment(1);
$this->assertEquals('success', $result['result']);
}

public function testProcessPaymentError()
{
list($order, $purchase_unit, $payment_source) = $this->setTestStubs();
Expand Down
Loading
Loading