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