Exceptions
2 years ago
Formatters
2 years ago
Payments
2 years ago
Routes
2 years ago
Schemas
2 years ago
Utilities
2 years ago
Authentication.php
2 years ago
Formatters.php
2 years ago
Legacy.php
2 years ago
RoutesController.php
2 years ago
SchemaController.php
2 years ago
SessionHandler.php
2 years ago
StoreApi.php
2 years ago
deprecated.php
2 years ago
functions.php
2 years ago
Legacy.php
71 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 | |
| 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 | public function process_legacy_payment( PaymentContext $context, PaymentResult &$result ) { |
| 28 | if ( $result->status ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | // phpcs:ignore WordPress.Security.NonceVerification |
| 33 | $post_data = $_POST; |
| 34 | |
| 35 | // Set constants. |
| 36 | wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true ); |
| 37 | |
| 38 | // Add the payment data from the API to the POST global. |
| 39 | $_POST = $context->payment_data; |
| 40 | |
| 41 | // Call the process payment method of the chosen gateway. |
| 42 | $payment_method_object = $context->get_payment_method_instance(); |
| 43 | |
| 44 | if ( ! $payment_method_object instanceof \WC_Payment_Gateway ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | $payment_method_object->validate_fields(); |
| 49 | |
| 50 | // If errors were thrown, we need to abort. |
| 51 | NoticeHandler::convert_notices_to_exceptions( 'woocommerce_rest_payment_error' ); |
| 52 | |
| 53 | // Process Payment. |
| 54 | $gateway_result = $payment_method_object->process_payment( $context->order->get_id() ); |
| 55 | |
| 56 | // Restore $_POST data. |
| 57 | $_POST = $post_data; |
| 58 | |
| 59 | // If `process_payment` added notices, clear them. Notices are not displayed from the API -- payment should fail, |
| 60 | // and a generic notice will be shown instead if payment failed. |
| 61 | wc_clear_notices(); |
| 62 | |
| 63 | // Handle result. |
| 64 | $result->set_status( isset( $gateway_result['result'] ) && 'success' === $gateway_result['result'] ? 'success' : 'failure' ); |
| 65 | |
| 66 | // set payment_details from result. |
| 67 | $result->set_payment_details( array_merge( $result->payment_details, $gateway_result ) ); |
| 68 | $result->set_redirect_url( $gateway_result['redirect'] ); |
| 69 | } |
| 70 | } |
| 71 |