Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/StoreApi
File: Legacy.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\StoreApi;
[1] Fix | Delete
[2] Fix | Delete
use Automattic\WooCommerce\StoreApi\Payments\PaymentContext;
[3] Fix | Delete
use Automattic\WooCommerce\StoreApi\Payments\PaymentResult;
[4] Fix | Delete
use Automattic\WooCommerce\StoreApi\Utilities\NoticeHandler;
[5] Fix | Delete
use Automattic\WooCommerce\Blocks\Package;
[6] Fix | Delete
use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
[7] Fix | Delete
/**
[8] Fix | Delete
* Legacy class.
[9] Fix | Delete
*/
[10] Fix | Delete
class Legacy {
[11] Fix | Delete
/**
[12] Fix | Delete
* Hook into WP lifecycle events.
[13] Fix | Delete
*/
[14] Fix | Delete
public function init() {
[15] Fix | Delete
add_action( 'woocommerce_rest_checkout_process_payment_with_context', array( $this, 'process_legacy_payment' ), 999, 2 );
[16] Fix | Delete
}
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Attempt to process a payment for the checkout API if no payment methods support the
[20] Fix | Delete
* woocommerce_rest_checkout_process_payment_with_context action.
[21] Fix | Delete
*
[22] Fix | Delete
* @param PaymentContext $context Holds context for the payment.
[23] Fix | Delete
* @param PaymentResult $result Result of the payment.
[24] Fix | Delete
*
[25] Fix | Delete
* @throws RouteException If the gateway returns an explicit error message.
[26] Fix | Delete
*/
[27] Fix | Delete
public function process_legacy_payment( PaymentContext $context, PaymentResult &$result ) {
[28] Fix | Delete
if ( $result->status ) {
[29] Fix | Delete
return;
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification
[33] Fix | Delete
$post_data = $_POST;
[34] Fix | Delete
[35] Fix | Delete
// Set constants.
[36] Fix | Delete
wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
[37] Fix | Delete
[38] Fix | Delete
// Add the payment data from the API to the POST global.
[39] Fix | Delete
$_POST = $context->payment_data;
[40] Fix | Delete
[41] Fix | Delete
// Call the process payment method of the chosen gateway.
[42] Fix | Delete
$payment_method_object = $context->get_payment_method_instance();
[43] Fix | Delete
[44] Fix | Delete
if ( ! $payment_method_object instanceof \WC_Payment_Gateway ) {
[45] Fix | Delete
return;
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
$payment_method_object->validate_fields();
[49] Fix | Delete
[50] Fix | Delete
// If errors were thrown, we need to abort.
[51] Fix | Delete
NoticeHandler::convert_notices_to_exceptions( 'woocommerce_rest_payment_error' );
[52] Fix | Delete
[53] Fix | Delete
// Process Payment.
[54] Fix | Delete
$gateway_result = $payment_method_object->process_payment( $context->order->get_id() );
[55] Fix | Delete
[56] Fix | Delete
// Restore $_POST data.
[57] Fix | Delete
$_POST = $post_data;
[58] Fix | Delete
[59] Fix | Delete
// If the payment failed with a message, throw an exception.
[60] Fix | Delete
if ( isset( $gateway_result['result'] ) && 'failure' === $gateway_result['result'] ) {
[61] Fix | Delete
if ( isset( $gateway_result['message'] ) ) {
[62] Fix | Delete
throw new RouteException( 'woocommerce_rest_payment_error', esc_html( wp_strip_all_tags( $gateway_result['message'] ) ), 400 );
[63] Fix | Delete
} else {
[64] Fix | Delete
NoticeHandler::convert_notices_to_exceptions( 'woocommerce_rest_payment_error' );
[65] Fix | Delete
}
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
// Handle result. If status was not returned we consider this invalid and return failure.
[69] Fix | Delete
$result_status = $gateway_result['result'] ?? 'failure';
[70] Fix | Delete
// These are the same statuses supported by the API and indicate processing status. This is not the same as order status.
[71] Fix | Delete
$valid_status = array( 'success', 'failure', 'pending', 'error' );
[72] Fix | Delete
$result->set_status( in_array( $result_status, $valid_status, true ) ? $result_status : 'failure' );
[73] Fix | Delete
[74] Fix | Delete
// If `process_payment` added notices but didn't set the status to failure, clear them. Notices are not displayed from the API unless status is failure.
[75] Fix | Delete
wc_clear_notices();
[76] Fix | Delete
[77] Fix | Delete
// set payment_details from result.
[78] Fix | Delete
$result->set_payment_details( array_merge( $result->payment_details, $gateway_result ) );
[79] Fix | Delete
$result->set_redirect_url( $gateway_result['redirect'] ?? '' );
[80] Fix | Delete
}
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function